Esempio n. 1
0
def create_message_specific_user(user_name):
    now = datetime.utcnow()

    form = CreateAMsgUser()
    the_user = User.query.filter(User.user_name == user_name).first()

    theposts = db.session.query(Messages)
    theposts = theposts.filter(
        or_(Messages.rec_user_id == current_user.id,
            Messages.sender_user_id == current_user.id))
    theposts = theposts.order_by(Messages.last_message.desc())
    posts = theposts.limit(100)

    seeifpostexists = db.session.query(Messages)
    seeifpostexists = seeifpostexists.filter(
        Messages.rec_user_id == current_user.id)
    seeifpostexists = seeifpostexists.filter(
        Messages.sender_user_id == the_user.id)
    seeifpostexists = seeifpostexists.first()

    seeifotherpostexists = db.session.query(Messages)
    seeifotherpostexists = seeifotherpostexists.filter(
        Messages.sender_user_id == the_user.id)
    seeifotherpostexists = seeifotherpostexists.filter(
        Messages.rec_user_id == current_user.id)
    seeifotherpostexists = seeifotherpostexists.first()

    if the_user is None:
        flash("User is Not Found.  Did you spell it correctly?",
              category="success")
        return redirect((url_for('message.create_message_specific_user',
                                 user_name=user_name)))

    if request.method == 'GET':
        return render_template(
            '/msg/create_msg_user.html',
            form=form,
            the_user=the_user,
            posts=posts,
        )

    if request.method == 'POST':
        # see if blocked
        isuserblocked = BlockedUser.query.filter(
            BlockedUser.user_id == the_user.id,
            BlockedUser.blocked_user == current_user.id).first()

        isuserbeingblocked = BlockedUser.query.filter(
            BlockedUser.user_id == current_user.id,
            BlockedUser.blocked_user == the_user.id).first()
        if isuserblocked is not None or isuserbeingblocked is not None:
            flash("User isnt accepting messages.", category="danger")
            return redirect(url_for('index'))

        if the_user.id == current_user.id:
            flash("Can not message yourself", category="success")
            return redirect((url_for('message.create_message')))

        if seeifpostexists is None and seeifotherpostexists is None:

            newmsg = Messages(created=now,
                              read_rec=1,
                              read_send=0,
                              msg_type=1,
                              sender_user_id=current_user.id,
                              sender_user_user_name=current_user.user_name,
                              rec_user_id=the_user.id,
                              rec_user_user_name=the_user.user_name,
                              body='',
                              biz_id=None,
                              biz_name='',
                              last_message=now)

            db.session.add(newmsg)
            db.session.commit()

            newreply = Reply(
                created=now,
                message_id=newmsg.id,
                sender_user_id=current_user.id,
                sender_user_user_name=current_user.user_name,
                rec_user_id=the_user.id,
                rec_user_user_name=the_user.user_name,
                body=form.postmessage.data,
                biz_id=None,
                biz_name='',
            )

            # add notification for poster about new comment
            add_new_notification(
                user_id=the_user.id,
                subid=0,
                subname='',
                postid=newmsg.id,
                commentid=0,
                msg=50,
            )

            db.session.add(newreply)
            db.session.commit()
            flash("Message Sent", category="success")
            return redirect((url_for('message.view_message', msgid=newmsg.id)))

        else:

            if seeifpostexists.rec_user_id == current_user.id:
                otheruserid = seeifpostexists.sender_user_id
                otherusername = seeifpostexists.sender_user_user_name

            elif seeifpostexists.sender_user_id == current_user.id:
                otheruserid = seeifpostexists.rec_user_id
                otherusername = seeifpostexists.rec_user_user_name
            else:
                otheruserid = current_user.id
                otherusername = current_user.id

            if seeifpostexists is not None:
                seeifpostexists.last_message = now

                if seeifpostexists.sender_user_id == current_user.id:
                    if seeifpostexists.read_send == 0:
                        seeifpostexists.read_send = 1

                        db.session.add(seeifpostexists)

                if seeifpostexists.rec_user_id == current_user.id:
                    if seeifpostexists.read_rec == 0:
                        seeifpostexists.read_rec = 1

                        db.session.add(seeifpostexists)

            if seeifotherpostexists is not None:

                seeifotherpostexists.last_message = now

                if seeifotherpostexists.sender_user_id == current_user.id:
                    if seeifpostexists.read_send == 0:
                        seeifpostexists.read_send = 1

                        db.session.add(seeifpostexists)

                if seeifpostexists.rec_user_id == current_user.id:
                    if seeifpostexists.read_rec == 0:
                        seeifpostexists.read_rec = 1

                        db.session.add(seeifpostexists)

            seeifpostexists.last_message = now
            newreply = Reply(
                created=now,
                message_id=seeifpostexists.id,
                sender_user_id=current_user.id,
                sender_user_user_name=current_user.user_name,
                rec_user_id=otheruserid,
                rec_user_user_name=otherusername,
                body=form.postmessage.data,
                biz_id=seeifpostexists.biz_id,
                biz_name=seeifpostexists.biz_name,
            )

            db.session.add(newreply)
            db.session.add(seeifpostexists)
            # add notification for poster about new comment
            add_new_notification(
                user_id=otheruserid,
                subid=0,
                subname='',
                postid=seeifpostexists.id,
                commentid=0,
                msg=51,
            )

            db.session.commit()

            flash("Reply Sent", category="success")
            return redirect((url_for('message.view_message',
                                     msgid=seeifpostexists.id)))
Esempio n. 2
0
def create_message_business(bizid):
    now = datetime.utcnow()

    form = CreateAMsgUser()
    the_biz = Business.query.filter(Business.id == bizid).first()

    if the_biz is None:
        flash(
            "Business is Not Found. Try Back Later or post this issue on /a/bugs",
            category="success")
        return redirect((url_for('index')))

    theposts = db.session.query(Messages)
    theposts = theposts.filter(
        or_(Messages.rec_user_id == current_user.id,
            Messages.sender_user_id == current_user.id))
    theposts = theposts.order_by(Messages.last_message.desc())
    posts = theposts.limit(100)

    seeifpostexists = db.session.query(Messages)
    seeifpostexists = seeifpostexists.filter(
        Messages.rec_user_id == current_user.id)
    seeifpostexists = seeifpostexists.filter(
        Messages.sender_user_id == the_biz.user_id)
    seeifpostexists = seeifpostexists.first()

    seeifotherpostexists = db.session.query(Messages)
    seeifotherpostexists = seeifotherpostexists.filter(
        Messages.sender_user_id == the_biz.user_id)
    seeifotherpostexists = seeifotherpostexists.filter(
        Messages.rec_user_id == current_user.id)
    seeifotherpostexists = seeifotherpostexists.first()

    if request.method == 'GET':
        return render_template('/msg/create_msg_biz.html',
                               form=form,
                               the_biz=the_biz,
                               posts=posts)

    if request.method == 'POST':

        if the_biz.user_id == current_user.id:
            flash("Can not message yourself", category="success")
            return redirect((url_for('message.create_message')))

        if seeifpostexists is None and seeifotherpostexists is None:

            newmsg = Messages(created=now,
                              read=0,
                              msg_type=2,
                              sender_user_id=current_user.id,
                              sender_user_user_name=current_user.user_name,
                              rec_user_id=the_biz.user_id,
                              rec_user_user_name=the_biz.user_name,
                              body='',
                              biz_id=the_biz.id,
                              biz_name=the_biz.business_name,
                              last_message=now)

            db.session.add(newmsg)
            db.session.commit()

            newreply = Reply(
                created=now,
                message_id=newmsg.id,
                sender_user_id=current_user.id,
                sender_user_user_name=current_user.user_name,
                rec_user_id=the_biz.user_id,
                rec_user_user_name=the_biz.user_name,
                body=form.postmessage.data,
                biz_id=the_biz.id,
                biz_name=the_biz.business_name,
            )

            # add notification for poster about new comment
            add_new_notification(
                user_id=the_biz.user_id,
                subid=0,
                subname='',
                postid=newmsg.id,
                commentid=0,
                msg=52,
            )

            db.session.add(newreply)
            db.session.commit()
            flash("Message Sent", category="success")
            return redirect((url_for('message.view_message', msgid=newmsg.id)))

        else:

            if seeifpostexists.rec_user_id == current_user.id:
                otheruserid = seeifpostexists.sender_user_id
                otherusername = seeifpostexists.sender_user_user_name

            elif seeifpostexists.sender_user_id == current_user.id:
                otheruserid = seeifpostexists.rec_user_id
                otherusername = seeifpostexists.rec_user_user_name
            else:
                otheruserid = current_user.id
                otherusername = current_user.id

            if seeifpostexists is not None:
                seeifpostexists.last_message = now

                if seeifpostexists.sender_user_id == current_user.id:
                    if seeifpostexists.read_send == 0:
                        seeifpostexists.read_send = 1

                        db.session.add(seeifpostexists)

                if seeifpostexists.rec_user_id == current_user.id:
                    if seeifpostexists.read_rec == 0:
                        seeifpostexists.read_rec = 1

                        db.session.add(seeifpostexists)

            if seeifotherpostexists is not None:

                seeifotherpostexists.last_message = now

                if seeifotherpostexists.sender_user_id == current_user.id:
                    if seeifpostexists.read_send == 0:
                        seeifpostexists.read_send = 1

                        db.session.add(seeifpostexists)

                if seeifpostexists.rec_user_id == current_user.id:
                    if seeifpostexists.read_rec == 0:
                        seeifpostexists.read_rec = 1

                        db.session.add(seeifpostexists)

            seeifpostexists.last_message = now
            newreply = Reply(
                created=now,
                message_id=seeifpostexists.id,
                sender_user_id=current_user.id,
                sender_user_user_name=current_user.user_name,
                rec_user_id=otheruserid,
                rec_user_user_name=otherusername,
                body=form.postmessage.data,
                biz_id=seeifpostexists.biz_id,
                biz_name=seeifpostexists.biz_name,
            )

            db.session.add(newreply)
            db.session.add(seeifpostexists)
            # add notification for poster about new comment
            add_new_notification(
                user_id=otheruserid,
                subid=0,
                subname='',
                postid=seeifpostexists.id,
                commentid=0,
                msg=51,
            )

            db.session.commit()

            flash("Reply Sent", category="success")
            return redirect((url_for('message.view_message',
                                     msgid=seeifpostexists.id)))
Esempio n. 3
0
def reply_message(msgid):
    now = datetime.utcnow()

    form = ReplyToMsg()
    if request.method == 'POST':
        thepost = Messages.query.filter(Messages.id == msgid).first_or_404()

        if (thepost.rec_user_id == current_user.id) or (thepost.sender_user_id
                                                        == current_user.id):
            if thepost.rec_user_id == current_user.id:
                otheruserid = thepost.sender_user_id
                otherusername = thepost.sender_user_user_name

            elif thepost.sender_user_id == current_user.id:
                otheruserid = thepost.rec_user_id
                otherusername = thepost.rec_user_user_name
            else:
                otheruserid = current_user.id
                otherusername = current_user.id

            if request.method == 'POST':
                if thepost.rec_user_id == current_user.id:
                    if thepost.read_send == 0:
                        thepost.read_send = 1

                        db.session.add(thepost)

                if thepost.sender_user_id == current_user.id:
                    if thepost.read_rec == 0:
                        thepost.read_rec = 1

                        db.session.add(thepost)

                thepost.last_message = now
                newreply = Reply(
                    created=now,
                    message_id=msgid,
                    sender_user_id=current_user.id,
                    sender_user_user_name=current_user.user_name,
                    rec_user_id=otheruserid,
                    rec_user_user_name=otherusername,
                    body=form.postmessage.data,
                    biz_id=thepost.biz_id,
                    biz_name=thepost.biz_name,
                )

                db.session.add(newreply)
                db.session.add(thepost)
                # add notification for poster about new comment
                add_new_notification(
                    user_id=otheruserid,
                    subid=0,
                    subname='',
                    postid=msgid,
                    commentid=0,
                    msg=51,
                )

                db.session.commit()

                flash("Reply Sent", category="success")
                return redirect((url_for('message.view_message',
                                         msgid=thepost.id)))
        else:
            flash("Message not found", category="danger")
            return redirect((url_for('index')))
    if request.method == 'GET':
        return redirect((url_for('index')))