def edit_deal(deal_id): ''' This function is used to allow a user to edit the description of a deal that he or she has submitted ''' deal = Deal.objects(id=deal_id).first() if deal is None: return abort(404) user = get_current_user() if user is None or str(get_current_user().id) != deal.author_id: return abort(404) msg = {} form = Edit_Form(csrf_enabled=False) if request.method == 'POST' and form.validate_on_submit(): print request.form description = request.form.get('description', None) if description: deal.description = description deal.edited = datetime.now() deal.save() store_deal(deal, overwrite=True) next = Href('/') next = next('deals', deal.sequence_num, deal.short_title) msg['status'] = 'success' else: msg['status'] = 'error' msg['description_error'] = form.description.errors[0] return jsonify(msg)
def show_user_profile(name, filter_by, page, sort): ''' This method retrieve a list of deals shared or bookmarked by the user. This method is used to show the user's history on the site. A user's bookmark is private to the user's eye's only. Abort 404 will be thrown if a users try to access some other user's bookmark. ''' #sanity check our inputs if sort not in sorts: return abort(404) #are we trying to retrieve the profile of a non-existent user? page_owner = User.objects(name=name).first() if page_owner is None: return abort(404) # if we are trying filtering by the correct categories? if filter_by not in user_history_filters: return abort(404) #do not allow other users to see other user's bookmark current_user = get_current_user() if filter_by == 'bookmarked' and (current_user == None or current_user.name != name): return abort(404) key = [name, '_', filter_by, '_', sort] key = ''.join(key) deal_seq_nums = None if r.exists(key): deal_seq_nums = r.lrange(key, 0, -1) if deal_seq_nums == ['None']: deal_seq_nums = [] else: deal_queryset = query_for_deals(page_owner, filter_by, sort) deal_seq_nums = [deal.sequence_num for deal in deal_queryset] store_list_of_deals(key, deal_seq_nums) for deal in deal_queryset: store_deal(deal) start = (page - 1) * per_page end = page * per_page has_next = True if end < len(deal_seq_nums) else False has_previous = True if start > 0 else False deal_seq_nums = deal_seq_nums[start:end] return render_template('user_history.html', current_filter=filter_by, current_page=page, current_sort=sort, owner=page_owner, deal_seq_nums=deal_seq_nums, has_next=has_next, has_previous=has_previous)
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)