def flag_deal(deal_id): ''' This function is used by a user to flag a deal as inappropriate or spam ''' msg = {} user = get_current_user() if str(deal_id) in user.deals_flagged: msg['status'] = 'error' msg['message'] = 'you cannot flag the same deal twice' else: user.deals_flagged.append(deal_id) user.save() celery_tasks.flag.delay(deal_id, user.id) # noting that current user has flagged this deal set_user_action_as_completed('flag', deal_id, user.sequence_num) msg['status'] = 'success' return jsonify(msg)
def save_deal(deal_id): ''' This allows a user to bookmark a deal, so the user can refer to the deal at a later time ''' msg = {} user = get_current_user() if str(deal_id) in user.deals_saved: msg['status'] = 'error' msg['message'] = 'you cannot save the same deal twice' else: user.deals_saved.append(deal_id) user.save() # noting that current user has bookmarked this deal set_user_action_as_completed('save', deal_id, user.sequence_num) # updating our redis cache for sort in sorts: r.delete("".join([user.name, '_', 'bookmarked', '_', sort])) msg['status'] = 'success' return jsonify(msg)
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)
def post_deal(): form = Deal_Form() # if current_user is None: # msg = {"status": "error", "message": "user not logged in"} # return msg if request.method == 'GET': return render_template('post_deal.html', form=form) elif request.method == 'POST': if form.validate_on_submit(): title = request.form.get('title') title = string.strip(title) short_title = title[0:short_title_length - 1] short_title = string_to_url_fix(short_title) category = request.form.get('categories') category = string.lower(category) location = request.form.get('location', None) if location == "": location = None if location and is_amazon_url(location): location = gen_amazon_affiliate_url(location) description = request.form.get('description', None) user = get_current_user() ip = request.remote_addr new_deal = Deal(title=title, short_title=short_title, location=location, category=category, description=description, author_id=str(user.id), num_votes=1, ip=ip) new_deal.save() new_deal_id = new_deal.id # updating redis cache store_deal(new_deal) insert_new_deal_into_list(category, new_deal.sequence_num) set_user_action_as_completed('vote', new_deal_id, user.sequence_num) for sort in sorts: r.delete("".join([user.name, '_', 'shared', '_', sort])) # updating mongodb or datastore user.deals_voted.append(str(new_deal_id)) user.deals_submitted.append(str(new_deal_id)) user.save() celery_tasks.upvote.delay(new_deal_id, user.id, request.remote_addr) #building this deal's url so to redirect the user next = Href('/') next = next('deals', new_deal.sequence_num, new_deal.short_title) msg = {'status': 'success', 'redirect': next} return jsonify(msg) else: #if form returns errors, return the errors to the users via js msg = {"status": "error"} if form.title.errors: msg["title_error"] = form.title.errors[0] if form.location.errors: msg["location_error"] = form.location.errors[0] if form.categories.errors: msg["category_error"] = form.categories.errors[0] if form.description.errors: msg["description_error"] = form.description.errors[0] return jsonify(msg) else: abort(404)