示例#1
0
def messages_form():
    if request.method == "GET":
        return render_template("messages/new.html",
                               form=MessageForm(),
                               categories=Category.query.all())

    form = MessageForm(request.form)

    # Validate the form
    if not form.validate():
        return render_template("messages/new.html",
                               form=form,
                               categories=Category.query.all())

    # Check if the dropdown menu selection is valid
    if request.form.get('category_select') == 'null':
        return render_template("messages/new.html",
                               form=form,
                               categories=Category.query.all(),
                               error="Please select a category")

    # Create a message object and add it to the database
    m = Message(form.name.data)
    m.content = form.content.data
    m.account_id = current_user.id
    m.category_id = request.form.get('category_select')

    db.session().add(m)
    db.session().commit()

    return redirect(url_for("messages_index"))
示例#2
0
def create_application_message(sender, receiver, message_text):
    message = Message(user=sender,
                      message_type='application',
                      message=message_text)
    message.save()
    conversation = Conversation.objects(to_user=receiver).first()
    if conversation is None:
        conversation = Conversation(from_user=sender, to_user=user)
    conversation.messages.append(message)
    conversation.save()
示例#3
0
def create_system_message(sender, receiver, message_text):
    message = Message(user=sender, message_type='system', message=message_text)
    message.save()
    receivers = User.objects(is_robot=False, is_activated=True)
    for receiver in receivers:
        conversation = Conversation.objects(to_user=receiver).first()
        if conversation is None:
            conversation = Conversation(from_user=sender, to_user=user)
        conversation.messages.append(message)
        conversation.save()
示例#4
0
    def deleteAllMessages(category_id):
        messages = Message.query.filter_by(category_id=category_id).all()
        for message in messages:
            Message.deleteReadMessage(message.id)
            Message.deleteAllReplies(message.id)

        stmt = text("DELETE FROM Message"
                        " WHERE Message.category_id = :category_id").params(category_id = category_id)
        db.engine.execute(stmt)
        db.engine.execute(stmt)
示例#5
0
 def put(self, conversation_id):
     args = request.get_json()
     message = Message(sender=current_user.id,
                       sort="private",
                       content=args['content'])
     message.save()
     Conversation.objects(id=conversation_id).update_one(
         push__messages=message, deleted=False)
     redis_store.rpush('conversation_' + str(conversation_id),
                       json.dumps(message.to_dict()))
     return jsonify(message=str(message.id))
示例#6
0
def users_show(account_id):

    if request.method == 'GET':
        account = User.query.get_or_404(account_id)
        photos = User.find_user_pictures(account_id)

        form = MessageForm()

        return render_template("users/show.html",
                               account=account,
                               form=form,
                               photos=photos)

    else:
        form = MessageForm(request.form)

        account_from = User.query.get(current_user.get_id())
        account_to = User.query.get(account_id)

        photos = User.find_user_pictures(account_id)

        if not form.validate():
            return render_template("users/show.html",
                                   account=account_to,
                                   form=form,
                                   photos=photos)

        m = Message(form.message.data)

        account_from.conversations.append(m)
        account_to.conversations.append(m)

        db.session().commit()

        return redirect(url_for("users_index"))
示例#7
0
def messages_index():

    if current_user.get_id() != None:
        account = User.query.get_or_404(current_user.get_id())
        conversations = account.conversations
        message_count = Message.count_user_messages(current_user.get_id())

        return render_template("messages/index.html",
                               conversations=conversations,
                               message_count=message_count)

    else:
        return redirect(url_for("users_index"))
示例#8
0
def view_user(user_id):
    u = User.query.get(user_id)
    messages = Message.findAllThreadsPostedBy(u.id)
    return render_template("auth/user.html", user=u, messages=messages)