Beispiel #1
0
def messages():
    if not session.get("logged_in"):
        return login()

    conversation = Conversation.get_by_id(int(request.form["conversation"]))
    conversation.put()
    recipient = conversation.user1
    if session["user_id"] == recipient:
        recipient = conversation.user2
    print "popopo"
    notification = Notification(user=conversation.user1,
                                body="New Message",
                                ntype="new-message",
                                item=conversation.item,
                                item_category="",
                                noticed=False,
                                link="/view_conversations/" +
                                str(conversation.key.id()))
    notification.put()
    print "pop-" + notification.body + "-pop"

    message = Message(sender=session["user_id"],
                      recipient=recipient,
                      body=request.form["message_body"],
                      conversation=conversation.key.id())
    message.put()

    return "success"
Beispiel #2
0
def delete_item(item_id):
    if not session.get("logged_in"):
        return login()

    item_id = int(item_id)

    item = Item.get_by_id(item_id)

    if item.seller_id != session["user_id"]:
        return "oops", 500

    previous_notifications = Notification.query(Notification.item == item_id)

    notification_body = item.name + " removed"
    for prev_not in previous_notifications:

        notification = Notification(user=prev_not.user,
                                    body=notification_body,
                                    ntype="item-removed",
                                    item=item.key.id(),
                                    item_category=item.category,
                                    noticed=False,
                                    link="/browse/" + item.category)
        notification.put()
        prev_not.key.delete()

    offers = Offer.query(Offer.item == item.key.id())

    conversation = Conversation.query(Conversation.item == item_id).get()
    if conversation:
        messages = Message.query(Message.conversation == conversation.key.id())
        for message in messages:
            message.key.delete()
        conversation.key.delete()

    for offer in offers:
        offer.key.delete()

    item_tags = Item_Tag.query(Item_Tag.item == item.key.id())

    for item_tag in item_tags:
        item_tag.key.delete()

    item.key.delete()

    return "success"
Beispiel #3
0
def browse_item(item_id):
    item_id = int(item_id)
    if not session.get("logged_in"):
        return login()

    if request.method == "GET":
        item = Item.get_by_id(item_id)
        category_id = item.category
        print item.name
        seller = User.get_by_id(item.seller_id)

        previous_offer = Offer.query(Offer.bidder == session["user_id"],
                                     Offer.item == item_id).get()

        was_previous_offer = False
        if previous_offer:
            was_previous_offer = True

        fields = []
        fields.append(
            Field(
                name="message",
                title="Message For Seller",
                the_type='text',
                identifier='message',
                placeholder=
                "A short message for the seller. Perhaps, where you can meet or payment options.",
                tag="textarea"))
        if item.biddable:

            fields.append(
                Field(name='amount',
                      title="Offer Amount",
                      the_type="number",
                      identifier="amount",
                      placeholder="10.95",
                      step=True))

        title = "Make Offer"
        form = Form(fields=fields, title=title)

        tags = Item_Tag.query(Item_Tag.item == item_id)
        notifications = Notification.query(
            Notification.user == session["user_id"]).order(-Notification.time)
        return render_template("browse_item.html",
                               item=item,
                               category_id=category_id,
                               bid_form=form,
                               previous_offer=previous_offer,
                               was_previous_offer=was_previous_offer,
                               offer=previous_offer,
                               notifications=notifications,
                               tags=tags)

    if request.method == "POST":

        item = Item.get_by_id(item_id)

        if not item or item.sold:
            return page_was_not_found(
                "Sorry but the item you tried to bid on has been removed by the seller"
            )

        category_id = item.category
        seller = User.get_by_id(item.seller_id)

        previous_offer = Offer.query(Offer.bidder == session["user_id"],
                                     Offer.item == item_id).get()

        if previous_offer:
            previous_offer.key.delete()

        amount = item.price
        if item.biddable:
            amount = float(request.form["amount"])

        offer = Offer(bidder=session["user_id"],
                      item=item_id,
                      message=request.form["message"],
                      amount=amount,
                      bidder_name=session["first_name"] + " " +
                      session["last_name"],
                      accepted=False,
                      confirmed=False,
                      item_name=item.name)
        offer.put()

        if item.biddable:
            item.update_best_offer(amount)

        notification_body = "Offer made on " + item.name + "for $" + str(
            offer.amount)
        notification = Notification(user=item.seller_id,
                                    body=notification_body,
                                    ntype="item-offer",
                                    item=item.key.id(),
                                    item_category=item.category,
                                    noticed=False,
                                    link="/my_items/" + str(item.key.id()))
        notification.put()

        fields = []
        fields.append(
            Field(
                name="message",
                title="Message For Seller",
                the_type='text',
                identifier='message',
                placeholder=
                "A short message for the seller. Perhaps, where you can meet or payment options.",
                tag="textarea"))
        if item.biddable:

            fields.append(
                Field(name='amount',
                      title="Offer Amount",
                      the_type="number",
                      identifier="amount",
                      placeholder="10.95",
                      step=True))

        title = "Make Offer"
        form = Form(fields=fields, title=title, submit="Make Offer")

        tags = Item_Tag.query(Item_Tag.item == item_id)
        notifications = Notification.query(
            Notification.user == session["user_id"]).order(-Notification.time)
        return render_template("browse_item.html",
                               item=item,
                               category_id=category_id,
                               bid_form=form,
                               offer=offer,
                               was_previous_offer=True,
                               notifications=notifications,
                               tags=tags)
Beispiel #4
0
def offers(offer_id):
    if not session.get("logged_in"):
        return login()

    if request.method == "GET":
        return "oops", 404
    offer_id = int(offer_id)
    offer = Offer.get_by_id(offer_id)
    if not offer:
        return page_was_not_found("Offer has been removed")
    item = Item.get_by_id(offer.item)

    if request.form["reason"] == "accept":
        if item.seller_id != session["user_id"]:
            return "uninformative error", 404

        offer.accepted = True
        offer.put()

        previous_notification = Notification.query(
            Notification.user == offer.bidder,
            Notification.item == item.key.id(),
            Notification.ntype == "accepted-offer").get()
        if previous_notification:
            previous_notification.key.delete()
        notification_body = session["first_name"] + " " + session[
            "last_name"] + " has accepted your offer for their " + item.name + " posting."
        notification = Notification(user=offer.bidder,
                                    body=notification_body,
                                    ntype="accepted-offer",
                                    item=item.key.id(),
                                    item_category=item.category,
                                    noticed=False,
                                    link="/browse_item/" + str(item.key.id()))
        notification.put()

        conversation = Conversation(user1=session["user_id"],
                                    user2=offer.bidder,
                                    subject="Arranging Sale",
                                    item=item.key.id(),
                                    item_name=item.name,
                                    read1=True,
                                    read2=True)

        conversation.put()

        return "success"

    if request.form["reason"] == "reject":
        if item.seller_id != session["user_id"]:
            return "uninformative error", 404

        offer.accepted = True
        offer.put()
        previous_notification = Notification.query(
            Notification.user == offer.bidder,
            Notification.item == item.key.id(),
            Notification.ntype == "accepted-offer").get()
        if previous_notification:
            previous_notification.key.delete()
        notification_body = "Offer Rejected:" + item.name
        notification = Notification(user=offer.bidder,
                                    body=notification_body,
                                    ntype="rejected-offer",
                                    item=item.key.id(),
                                    item_category=item.category,
                                    noticed=False,
                                    link="/browse_item/" + str(item.key.id()))
        notification.put()
        return "success"

    if request.form["reason"] == "confirm":
        if item.seller_id != session["user_id"]:
            return "uninformative error", 404

        item.sold = True
        item.put()
        offer.confirmed = True
        offer.put()
        offers = Offer.query(Offer.item == item.key.id(),
                             Offer.bidder != offer.bidder)
        for temp_offer in offers:
            notification_body = item.name + " sold"
            notification = Notification(user=offer.bidder,
                                        body=notification_body,
                                        ntype="item-sold",
                                        item=item.key.id(),
                                        item_category=item.category,
                                        noticed=False,
                                        link="/browse/" + str(item.category))
            notification.put()
            temp_offer.key.delete()
        conversation = Conversation.query(
            Conversation.item == item.key.id()).get()
        if conversation:
            conversation.key.delete()
        return "Offer confirmed"
    if request.form["reason"] == "remove":
        offer.key.delete()
        return "Offer removed"
    return "uninformative error", 404