Ejemplo n.º 1
0
def login():
    if session.get('email') is None:
        if request.method == 'GET':
            return render_template('login.jinja2')
        else:
            username_email = request.form['username_email']
            password = request.form['password']
            user = User.get_user_by_email(username_email, UserConstants.COLLECTION)
            if user is not None:
                if user.password == password:
                    session['email'] = user.email
                    admin = Admin.get_user_by_email(username_email, AdminConstants.COLLECTION)
                    if admin is not None:
                        session['admin'] = admin.email
                    unread_messages = Message.get_unread_messages_count(user._id)
                    session['msgs_count'] = unread_messages
                    return make_response(view_items())
                else:
                    return render_template("login.jinja2", message="Invalid login")

            else:
                return render_template("login.jinja2", message="User does not exist")
    else:
        if session['email'] is not None:
            return render_template("message_center.jinja2", message="You are already logged In.")
        else:
            return render_template("login.jinja2")
Ejemplo n.º 2
0
def get_message_details(message_id):
    # TODO: add verification mechanism to verify that user is part of the message (user_id, sender_id, recipient_id)
    if session.get('email') is None:
        return render_template("login.jinja2",
                               message="You must be logged in to add items")
    else:
        message = Message.get_message_by_id(message_id)
        if message is not None:
            user = User.get_user_by_email(collection=UserConstants.COLLECTION,
                                          email=session['email'])
            if (user._id == message.sender_id) or (user._id
                                                   == message.recipient_id):
                if (user._id == message.recipient_id):
                    message.mark_massage_read()
                item = Item.get_item_by_id(message.item_id)
                messages = Message.get_messages_by_item_id(message.item_id)
                for reply in messages:
                    if (user._id == reply.recipient_id):
                        reply.mark_massage_read()
                session['msgs_count'] = Message.get_unread_messages_count(
                    user._id)
                return render_template("user/messages/message_details.jinja2",
                                       user_id=user._id,
                                       msgs=messages,
                                       item=item)
            else:
                return render_template(
                    "message_center.jinja2",
                    message=
                    "you are not allowed to view details of this message")
        else:
            return render_template("message_center.jinja2",
                                   message="could not find message details")
Ejemplo n.º 3
0
def view_all_items():
    if session.get('email') is None:
        flash("sign up for FREE to post your items.")
    else:
        user = User.get_user_by_email(session['email'],
                                      UserConstants.COLLECTION)
        session['msgs_count'] = Message.get_unread_messages_count(user._id)
    items = Item.get_all_approved_items()
    return render_template("item/all_items.jinja2", items=items)
Ejemplo n.º 4
0
def view_items():
    # if session.get('email') is None:
    #     return render_template("login.jinja2", message="You must be logged in to view your items")
    # else:
    if session.get('email') is None:
        return render_template(
            "login.jinja2", message="You must be logged in to view your items")
    user = User.get_user_by_email(session['email'], UserConstants.COLLECTION)
    items = Item.get_items_by_user_id(user._id)
    session['msgs_count'] = Message.get_unread_messages_count(user._id)
    return render_template("item/items.jinja2", items=items)
Ejemplo n.º 5
0
def get_recieved_messages(user_email):
    if session.get('email') is None:
        return render_template("login.jinja2",
                               message="You must be logged in to add items")
    else:
        if user_email == session['email']:
            user = User.get_user_by_email(collection=UserConstants.COLLECTION,
                                          email=user_email)
            if user is not None:
                messages = Message.get_recieved_messages_by_user_id(user._id)
                unread_messages_count = Message.get_unread_recieved_messages_count(
                    user._id)
                sent_messages = Message.get_sent_messages_by_user_id(user._id)
                unread_sent_messages_count = 0
                for sent_message in sent_messages:
                    unread_sent_messages_count += Message.get_unread_sent_messages_count(
                        user_id=sent_message.sender_id,
                        item_id=sent_message.item_id)
                unread_replies_list = {}
                for message in messages:
                    unread_replies_list[
                        message.
                        _id] = message.get_unread_recieved_replies_count(
                            user_id=user._id, item_id=message.item_id)
                session['msgs_count'] = Message.get_unread_messages_count(
                    user._id)
                return render_template(
                    "user/messages/recieved_messages.jinja2",
                    msgs=messages,
                    user_id=user._id,
                    unread_messages_count=unread_messages_count,
                    unread_replies=unread_replies_list,
                    unread_sent_messages_count=unread_sent_messages_count)
            else:
                return render_template("message_center.jinja2",
                                       message="Could not locate user")
        else:
            return render_template(
                "message_center.jinja2",
                message=
                "Looks like you used an email that could not be authorized")
Ejemplo n.º 6
0
def item_details(item_id):
    item = Item.get_item_by_id(item_id)
    if item is not None:
        if session.get('email') is not None:
            user = User.get_user_by_email(email=session['email'],
                                          collection=UserConstants.COLLECTION)
            session['msgs_count'] = Message.get_unread_messages_count(user._id)
            if (user._id == item.user_id) or (session.get('admin')
                                              is not None):
                return render_template("item/item_details.jinja2",
                                       item=item,
                                       editable=True)
            else:
                return render_template("item/item_details.jinja2", item=item)
        else:
            return render_template("item/item_details.jinja2", item=item)
    else:
        return render_template(
            "message_center.jinja2",
            message=
            "Item {} does not have details. Contact Us if you need further information!"
            .format(item.title))