Пример #1
0
def delete_deal(deal_id):
    '''
    This function is used by a user to delete a deal. The user trying to
    delete this deal must be the author of the deal.

    We are not sending this off to celery as a async task b'c we want to
    ensure that this a deal is deleted immediately, rather at time delta
    later
    '''
    msg = {}
    user = User.objects(id=current_user.id).first()
    if str(deal_id) not in user.deals_submitted:
        msg['status'] = 'error'
        msg['message'] = 'you cannot delete this deal b\'c you are not the author'
        return jsonify(msg)
    try:
        deal = Deal.objects(id=deal_id).first()
        deal.deleted = True
        deal.save()
        remove_deal(deal.sequence_num)
        remove_deal_num_from_lists(deal.sequence_num)
    except Exception as e:
        print e
        msg['status'] = 'error'
        msg['message'] = 'error occured while deleting user object'
        return jsonify(msg)

    msg['status'] = 'success'
    return jsonify(msg)
Пример #2
0
def vote_deal(deal_id):
    '''
    This function updates the number of votes of the deal by:
        1) increasing num_votes by 1
        2) add a new vote object into a deal's 'votes' list
    '''
    msg = {}
    user = get_current_user()
    if str(deal_id) in user.votes:
        msg['status'] = 'error'
        msg['message'] = 'you cannot vote for the same deal twice'
        return jsonify(msg)

    # we want to make sure that the user see's that his or her vote was counted
    # right away w/o any delay. Hence, the following line is part of
    # celery_tasks.upvote
    else:
        try:
            user.deals_voted.append(str(deal_id))
            user.save()
            deal_queryset = Deal.objects(id=deal_id)
            deal_queryset.update_one(inc__num_votes=1)
            deal = deal_queryset.first()
            # flushing redis cache to reflect the new vote count
            remove_deal(deal.sequence_num)
            # update redis cache: noting that current user has voted this deal in redis
            set_user_action_as_completed('vote', deal_id, user.sequence_num)
            for sort in sorts:
                r.delete("".join([user.name, '_', 'liked', '_', sort]))
            # update mongodb
            celery_tasks.upvote.delay(deal_id, user.id, request.remote_addr)
            msg['status'] = 'success'
            return jsonify(msg)
        except Exception as e:
            print e
            abort(404)