def list(): if request.method == 'GET': return render_template('proposals/list.html', proposals=get_sanitized_proposals()) else: # POST proposal = Proposal(created_by=request.form['created_by'], number=request.form['number']) proposal.save() return render_template('proposals/list.html', proposals=get_sanitized_proposals())
def proposals(request): was_limited = getattr(request, 'limited', False) if was_limited: return JsonResponse( { 'status': 'you hit the rate limit!' } ) else: token = get_token(request) user_id_login = User.verify_auth_token(token) if user_id_login: if request.method == 'GET': all_request = Request.objects.filter(user_id=user_id_login) request_id = [] for r in all_request: request_id.append(r.id) all_proposal = Proposal.objects.filter(request_id in request_id) data = [] for proposal in all_proposal: item = { 'id': proposal.id, 'user_proposed_to': proposal.user_proposed_to, 'user-proposed_from': proposal.user_proposed_from, 'request_id': proposal.request_id.id, 'filled': proposal.filled } data.append(item) return JsonResponse( { 'objects': data } ) elif request.method == 'POST': request_id = request.POST.get('request_id') user_from = Request.objects.filter(id=request_id).first().user_id user_to = user_id_login if user_from == user_to: return HttpResponseForbidden('the two users are same') else: proposal = Proposal(user_proposed_to=user_to, user_proposed_from=user_from, request_id=request_id) proposal.save() return JsonResponse( { 'status': 'proposal created successfully', 'object': { 'id': proposal.id, 'user_proposed_to': proposal.user_proposed_to, 'user-proposed_from': proposal.user_proposed_from, 'request_id': proposal.request_id.id, 'filled': proposal.filled } } ) else: return HttpResponseForbidden('invalid token')
def post_submit(user=None, community=None): entry = Proposal(community='', title='', content='') if community is None: community = request.form.get('community') or None if community and community[0:2] == "c/": community = community[2:] if request.method == 'POST': try: entry.title = request.form.get('title') or '' entry.content = request.form.get('content') or '' entry.published = request.form.get('published') or True entry.vote_options = request.form.get('internalvote') or None entry.usepositions = (True if request.form.get('use-positions') == '' else False) entry.author = User.get(User.id == current_user.id) entry.community = Community.get(Community.name == community) if not (entry.title and entry.community and entry.author): flash('Community and Title are required.', 'error') else: entry.update_search_index() # Wrap the call to save in a transaction so we can roll it back # cleanly in the event of an integrity error. try: with database.atomic(): entry.save() except peewee.IntegrityError: flash('This title is already in use.', 'error') else: if entry.published: return redirect( url_for('post_details', community=entry.community.name, slug=entry.slug)) else: abort(404) except peewee.DoesNotExist: flash('Community and Title are required.', 'error') if community is not None: community = Community.get_or_none(Community.name == community) return render_template('submit.html', entry=entry, community=community)
def proposals(request, action=None, slug=None): """ Handle proposals. """ ctx = { 'title': 'Proposals » PizzaHackers - Doers of NIT Jamshedpur', 'description': 'PizzaHackers is the doer community of NIT Jamshedpur.' } if request.method == 'POST': fields = [ 'title', 'type', 'description', 'tags', 'completed', 'url', 'repo_url' ] data = request.POST.dict() if not data.has_key('completed'): data['completed'] = False if data['completed'] == 'on': data['completed'] = True else: data['completed'] = False if data.has_key('slug'): proposal = get_object_or_404(Proposal, slug=data['slug']) else: proposal = Proposal(proposer=request.user.hacker) for field in fields: setattr(proposal, field, data[field]) try: proposal.save() except Exception, e: ctx['error'] = e return render(request, 'proposals_edit.html', ctx) return redirect(request.META['HTTP_REFERER'])
def proposal_downvote(slug): prop = Proposal.get(Proposal.slug == slug) result = {"diff": '0', "votes": str(prop.votes)} if current_user.id == prop.author_id or current_user.karma <= 0: return jsonify(result) if current_user.has_downvoted(prop): # current_user.karma += 1 prop.author.karma += 1 prop.downvotes -= 1 PostVote.delete().where((PostVote.post == prop) & ( PostVote.user_id == current_user.id)).execute() result["diff"] = '+1' else: # current_user.karma -= 1 prop.author.karma -= 1 score_mod = 1 if current_user.has_upvoted(prop): score_mod = 2 result["diff"] = str(-score_mod) prop.downvotes += score_mod (PostVote.insert(post=prop, user=current_user.id, vote=-1).on_conflict( conflict_target=[PostVote.user, PostVote.post], preserve=[PostVote.vote, PostVote.timestamp]).execute()) Proposal.save(prop) # FIXME move karma to another table User.save(current_user) User.save(prop.author) result["votes"] = str(prop.votes) return jsonify(result)