示例#1
0
def fix_order( u, d, order_id, item_id):
    ord = Order.objects.get(id=order_id)
    exp_id = ord.table.experiment.id 

    # log to events
    item = MenuItem.objects.get(id=int(item_id))
    o = Order.objects.get(id=order_id)
    try:
        r = o.items.get(item=item)
        logger.debug("Already have %s on the order"%item.name)
        # already had ordered the item
    except MenuItemReview.DoesNotExist:
        logger.debug("Ordering %s"%item.name)
        # create a review
        r = MenuItemReview(item=item, rating=0, comment="Comments: click to edit")
        r.save()
        o.items.add(r)
        o.save()
        o.updated = d
        o.save()

    # log mark/order event
    people_ordered = item.menuitemreview_set.all().exclude(legals_ordered__user=u)
    event = EventMenuItem(user=u, 
            order=o,
            experiment=Experiment.objects.get(id=exp_id),
            item=item, 
            action=EventMenuItem.MARK,
            num_people=people_ordered.count())
    event.save()
    event.timestamp = d
    event.save()
示例#2
0
def order(request):
    """
        Order items

        :param POST['items']: the item id's of ordered items (list in string format)
        :param POST['table']: the table number where customer sits
    """
    #if request.user.is_authenticated():
    u = request.user
    #u = User.objects.get(id=2)
    tc = TableCode.objects.filter(code=request.POST['table']).latest('date')
    o = Order(user=u, table=tc)    
    o.save()
    #logger.debug("posted items %s"%request.POST['items'])

    for i in eval(request.POST['items']):
        m = MenuItem.objects.get(id=i)
        r = MenuItemReview(item=m, rating=0, comment="Please let us know how you liked this dish.")
        r.save()
        o.items.add(r)
    o.save()
    o.last_update()

    return JSONHttpResponse({'result':'1'})
示例#3
0
import datetime

e = Experiment.objects.get(id=1)
t = TableCode(code='abcd', experiment = e, date=datetime.date.today())
t.save()

t = TableCode.objects.filter(code='abcd')[0]

michael = User.objects.get(id=2)
o = Order(user=michael, table=t)
o.save()

# these items have a price that is not fixed
for i in [59,52]:
    m = MenuItem.objects.get(id=i)
    r = MenuItemReview(item=m, rating=5, comment="This was soo delicious")
    r.save()
    o.items.add(r)
o.save()

o = Order(user=michael, table=t)
o.save()

for i in [25,15,45]:
    m = MenuItem.objects.get(id=i)
    r = MenuItemReview(item=m, rating=5, comment="This was soo delicious")
    r.save()
    o.items.add(r)
o.save()

o = Order(user=michael, table=t)
示例#4
0
def mark_item(request, item_id, order_id):
    """
        Mark an item to order

        :url: /legals/m/mark/(?P<item_id>\d+)/(?<order_id>\d+)/

        :param order_id: cannot be 0 since if 0 it should never show up
                        in the first place
    """
    header = "legals"
    restaurant = Store.objects.get(id=1)

    u = request.user
    ord = Order.objects.get(id=order_id)
    exp_id = ord.table.experiment.id 

    # log to events
    item = MenuItem.objects.get(id=int(item_id))
    o = Order.objects.get(id=order_id)
    try:
        r = o.items.get(item=item)
        logger.debug("Already have %s on the order"%item.name)
        # already had ordered the item
    except MenuItemReview.DoesNotExist:
        logger.debug("Ordering %s"%item.name)
        # create a review
        r = MenuItemReview(item=item, rating=0, comment="Comments: click to edit")
        r.save()
        o.items.add(r)
        o.save()
        o.last_update()

    # log mark/order event
    people_ordered = item.menuitemreview_set.all().exclude(legals_ordered__user=u)
    event = EventMenuItem(user=u, 
            order=o,
            experiment=Experiment.objects.get(id=exp_id),
            item=item, 
            action=EventMenuItem.MARK,
            num_people=people_ordered.count())
    event.save()

    
    i_data = {}
    # build menu items from my orders
    # with who else liked it
    item_reviews = o.items.all()
    for r in item_reviews:
        #: contains item and other social metadata
        i_data[r.item.id] = {'item':r.item.get_json()}
        if exp_id == 3:
            numordered = r.item.menuitemreview_set.all().exclude(legals_ordered__user=u).count()
            if numordered == 0:
                i_data[r.item.id]['anonymous'] = ''
            elif numordered == 1: 
                i_data[r.item.id]['anonymous'] = '1 person ordered'
            else:
                i_data[r.item.id]['anonymous'] = '%d people ordered'%numordered

            numliked = r.item.menuitemreview_set.all().exclude(legals_ordered__user=u).filter(rating__gte=4).count()
            if numliked == 0:
                i_data[r.item.id]['aliked'] = ''
            elif numliked == 1: 
                i_data[r.item.id]['aliked'] = '1 person like this'
            else:
                i_data[r.item.id]['aliked'] = '%d people like this'%numordered

        elif exp_id in [2,4,5]:
            i_data[r.item.id]['friends'] = set() 
            i_data[r.item.id]['fliked'] = set() 
        

    # Handle friends orders and reviews
    if exp_id in [2,4,5]:
        # get friends of user
        fb_id = request.user.facebook_profile.facebook_id
        friends = Friends.objects.get(facebook_id=fb_id).friends.values_list('facebook_id', flat=True)
        
        # get those items that friends have ordered 
        orders = Order.objects.filter(user__facebook_profile__facebook_id__in=friends)
        for o in orders:
            if o.num_items() > 0:
                for r in o.items.all():
                    # 2: friends
                    # 4: friends and popularity mixed
                    # 5: intervention
                    if r.item.id in i_data:
                        # friends that ordered the item
                        i_data[r.item.id]['friends'].add(o.user.first_name)
                        if r.rating >= MenuItemReview.GOOD:
                            i_data[r.item.id]['fliked'].add(o.user.first_name)
    
        for key,val in i_data.items(): 
            # arrange labels
            numfriends = len(val['friends'])
            numliked = len(val['fliked'])

            if exp_id == 4:
                # anonymous friends
                i_data[key]['friend_label']="%d friend(s) ordered"%numfriends
                if numliked > 0:
                    i_data[key]['fliked']="%d friend(s) like this"%numliked
            else:
                # friends names           
                i = 0
                label = ''
                for f in val['friends']: 
                    i += 1 
                    if i == 3 and numfriends > 3:
                        label += f + ', ...'
                        # if there are more than three people, just show
                        # that there are more
                        break
                    elif i == numfriends:
                        label += f 
                    else:
                        label += f + ', '

                i_data[key]['friend_label']=label+" ordered"


    logger.debug("Mark Order: Listing order")

    return render_to_response( "legals/i/order.html",
            {
            'header': header,
            'restaurant': restaurant,
            'order_data': i_data,
            'exp_id': int(exp_id),
            'experiments': [2,5],
            'order_id': int(order_id)
            },
            context_instance=RequestContext(request))