示例#1
0
def new_conversation():
    form = ConversationForm()
    to_user = request.args.get("to_user")

    message_count = Conversation.query.\
        filter(Conversation.user_id == current_user.id).\
        count()

    if message_count >= flaskbb_config["MESSAGE_QUOTA"]:
        flash(
            _("You cannot send any messages anymore because you have "
              "reached your message limit."), "danger")
        return redirect(url_for("message.inbox"))

    if request.method == "POST":
        if "save_message" in request.form and form.validate():
            to_user = User.query.filter_by(username=form.to_user.data).first()

            shared_id = uuid.uuid4()

            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=current_user.id,
                      unread=False,
                      as_draft=True,
                      shared_id=shared_id)

            flash(_("Message saved."), "success")
            return redirect(url_for("message.drafts"))

        if "send_message" in request.form and form.validate():
            to_user = User.query.filter_by(username=form.to_user.data).first()

            # this is the shared id between conversations because the messages
            # are saved on both ends
            shared_id = uuid.uuid4()

            # Save the message in the current users inbox
            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=current_user.id,
                      unread=False,
                      shared_id=shared_id)

            # Save the message in the recievers inbox
            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=to_user.id,
                      unread=True,
                      shared_id=shared_id)
            to_user.invalidate_cache(permissions=False)

            flash(_("Message sent."), "success")
            return redirect(url_for("message.sent"))
    else:
        form.to_user.data = to_user

    return render_template("message/message_form.html",
                           form=form,
                           title=_("Compose Message"))
示例#2
0
文件: views.py 项目: 0xsKu/flaskbb
def new_conversation():
    form = ConversationForm()
    to_user = request.args.get("to_user")

    message_count = Conversation.query.\
        filter(Conversation.user_id == current_user.id).\
        count()

    if message_count >= flaskbb_config["MESSAGE_QUOTA"]:
        flash(_("You cannot send any messages anymore because you have"
                "reached your message limit."), "danger")
        return redirect(url_for("message.inbox"))

    if request.method == "POST":
        if "save_message" in request.form and form.validate():
            to_user = User.query.filter_by(username=form.to_user.data).first()

            shared_id = uuid.uuid4()

            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=current_user.id,
                      unread=False,
                      as_draft=True,
                      shared_id=shared_id)

            flash(_("Message saved."), "success")
            return redirect(url_for("message.drafts"))

        if "send_message" in request.form and form.validate():
            to_user = User.query.filter_by(username=form.to_user.data).first()

            # this is the shared id between conversations because the messages
            # are saved on both ends
            shared_id = uuid.uuid4()

            # Save the message in the current users inbox
            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=current_user.id,
                      unread=False,
                      shared_id=shared_id)

            # Save the message in the recievers inbox
            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=to_user.id,
                      unread=True,
                      shared_id=shared_id)
            to_user.invalidate_cache(permissions=False)

            flash(_("Message sent."), "success")
            return redirect(url_for("message.sent"))
    else:
        form.to_user.data = to_user

    return render_template("message/message_form.html", form=form,
                           title=_("Compose Message"))
示例#3
0
文件: views.py 项目: 0xsKu/flaskbb
def edit_conversation(conversation_id):
    conversation = Conversation.query.filter_by(
        id=conversation_id,
        user_id=current_user.id
    ).first_or_404()

    if not conversation.draft:
        flash(_("You cannot edit a sent message."), "danger")
        return redirect(url_for("message.inbox"))

    form = ConversationForm()

    if request.method == "POST":
        if "save_message" in request.form:
            to_user = User.query.filter_by(username=form.to_user.data).first()

            conversation.draft = True
            conversation.to_user_id = to_user.id
            conversation.first_message.message = form.message.data
            conversation.save()

            flash(_("Message saved."), "success")
            return redirect(url_for("message.drafts"))

        if "send_message" in request.form and form.validate():
            to_user = User.query.filter_by(username=form.to_user.data).first()
            # Save the message in the recievers inbox
            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=to_user.id,
                      unread=True,
                      shared_id=conversation.shared_id)

            # Move the message from ``Drafts`` to ``Sent``.
            conversation.draft = False
            conversation.to_user = to_user
            conversation.date_created = datetime.utcnow()
            conversation.save()

            flash(_("Message sent."), "success")
            return redirect(url_for("message.sent"))
    else:
        form.to_user.data = conversation.to_user.username
        form.subject.data = conversation.subject
        form.message.data = conversation.first_message.message

    return render_template("message/message_form.html", form=form,
                           title=_("Edit Message"))
示例#4
0
def edit_conversation(conversation_id):
    conversation = Conversation.query.filter_by(
        id=conversation_id,
        user_id=current_user.id
    ).first_or_404()

    if not conversation.draft:
        flash(_("You cannot edit a sent message."), "danger")
        return redirect(url_for("message.inbox"))

    form = ConversationForm()

    if request.method == "POST":
        if "save_message" in request.form:
            to_user = User.query.filter_by(username=form.to_user.data).first()

            conversation.draft = True
            conversation.to_user_id = to_user.id
            conversation.first_message.message = form.message.data
            conversation.save()

            flash(_("Message saved."), "success")
            return redirect(url_for("message.drafts"))

        if "send_message" in request.form and form.validate():
            to_user = User.query.filter_by(username=form.to_user.data).first()
            # Save the message in the recievers inbox
            form.save(from_user=current_user.id,
                      to_user=to_user.id,
                      user_id=to_user.id,
                      unread=True,
                      shared_id=conversation.shared_id)

            # Move the message from ``Drafts`` to ``Sent``.
            conversation.draft = False
            conversation.to_user = to_user
            conversation.date_created = datetime.utcnow()
            conversation.save()

            flash(_("Message sent."), "success")
            return redirect(url_for("message.sent"))
    else:
        form.to_user.data = conversation.to_user.username
        form.subject.data = conversation.subject
        form.message.data = conversation.first_message.message

    return render_template("message/message_form.html", form=form,
                           title=_("Edit Message"))