<commit_before># -*- coding: utf-8 -*-
from flask import render_template, flash, redirect, url_for, request, jsonify
from flask.ext.login import current_user, login_required
from . import msg
from .. import db
from .forms import MessageForm
from ..models import Item, Message


@msg.route('/msg/create/<int:id>', methods=['GET', 'POST'])
@login_required
def create(id): # pylint: disable=W0622
  item = Item.query.get_or_404(id)
  form = MessageForm()

  if form.validate_on_submit():
    msg = Message(subject=form.subject.data, description=form.description.data,
                  sender_id=current_user.id, receiver_id=item.user_id,
                  item_id=item.id)
    db.session.add(msg)
    flash('Your message has been sent.')
    return redirect(url_for('main.item', id=item.id))
  return render_template('msg/create.html', form=form, item=item)


@msg.route('/notifications', methods=['GET', 'POST'])
@login_required
def notifications():
  if request.method == 'POST':
    if request.form.get('type') == 'unread':
      msgs = current_user.msgs_unread
    elif request.form.get('type') == 'sent':
      msgs = current_user.msgs_sent.all()
    else:
      msgs = current_user.msgs_received.all()
    return jsonify(msgs=[msg.serialize for msg in msgs])

  else: #GET request
    msgs = current_user.msgs_unread
    return render_template('msg/notifications.html', msgs=msgs)


@msg.route('/msg/<int:id>', methods=['GET', 'POST'])
@login_required
def message(id): # pylint: disable=W0622
  msg = Message.query.get_or_404(id)
  if not (current_user == msg.receiver or current_user == msg.sender):
    return redirect(url_for('main.index'))

  #mark message as read if user is the receiver
  if current_user == msg.receiver and msg.unread:
    msg.unread = False

  form = MessageForm()
  #do not reply messages to deleted user accounts, to same user or
  #regarding a product that has been deleted
  if msg.item and msg.sender and current_user != msg.sender:
    if form.validate_on_submit():
      reply = Message(subject=form.subject.data,
                      description=form.description.data,
                      sender_id=current_user.id, receiver_id=msg.sender_id,
                      item_id=msg.item_id)
      db.session.add(reply)
      flash('Your message has been sent.')
      return redirect(url_for('msg.notifications'))
  form.subject.data = "Re: " + msg.subject
  return render_template('msg/message.html', msg=msg, form=form)

<commit_msg>add default msg text #27<commit_after># -*- coding: utf-8 -*-
from flask import render_template, flash, redirect, url_for, request, jsonify
from flask.ext.login import current_user, login_required
from . import msg
from .. import db
from .forms import MessageForm
from ..models import Item, Message


@msg.route('/msg/create/<int:id>', methods=['GET', 'POST'])
@login_required
def create(id): # pylint: disable=W0622
  item = Item.query.get_or_404(id)
  form = MessageForm()

  if form.validate_on_submit():
    msg = Message(subject=form.subject.data, description=form.description.data,
                  sender_id=current_user.id, receiver_id=item.user_id,
                  item_id=item.id)
    db.session.add(msg)
    flash('Your message has been sent.')
    return redirect(url_for('main.item', id=item.id))
  form.subject.data = "Hi! I'm interested on your " + item.name
  return render_template('msg/create.html', form=form, item=item)


@msg.route('/notifications', methods=['GET', 'POST'])
@login_required
def notifications():
  if request.method == 'POST':
    if request.form.get('type') == 'unread':
      msgs = current_user.msgs_unread
    elif request.form.get('type') == 'sent':
      msgs = current_user.msgs_sent.all()
    else:
      msgs = current_user.msgs_received.all()
    return jsonify(msgs=[msg.serialize for msg in msgs])

  else: #GET request
    msgs = current_user.msgs_unread
    return render_template('msg/notifications.html', msgs=msgs)


@msg.route('/msg/<int:id>', methods=['GET', 'POST'])
@login_required
def message(id): # pylint: disable=W0622
  msg = Message.query.get_or_404(id)
  if not (current_user == msg.receiver or current_user == msg.sender):
    return redirect(url_for('main.index'))

  #mark message as read if user is the receiver
  if current_user == msg.receiver and msg.unread:
    msg.unread = False

  form = MessageForm()
  #do not reply messages to deleted user accounts, to same user or
  #regarding a product that has been deleted
  if msg.item and msg.sender and current_user != msg.sender:
    if form.validate_on_submit():
      reply = Message(subject=form.subject.data,
                      description=form.description.data,
                      sender_id=current_user.id, receiver_id=msg.sender_id,
                      item_id=msg.item_id)
      db.session.add(reply)
      flash('Your message has been sent.')
      return redirect(url_for('msg.notifications'))
  form.subject.data = "Re: " + msg.subject
  return render_template('msg/message.html', msg=msg, form=form)


{'fraction_non_alphanumeric': 0.1292123629112662, 'fraction_numerical': 0.006380857427716849, 'mean_word_length': 3.610294117647059, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '107430', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1573, 'n_words': 385}