Support votes modification
[e_cidadania.git] / src / apps / ecidadania / proposals / views / common.py
blob81f590553e72d3be0ee480ddddb4a251381bd206
1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2010-2012 Cidadania S. Coop. Galega
5 # This file is part of e-cidadania.
7 # e-cidadania is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # e-cidadania is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with e-cidadania. If not, see <http://www.gnu.org/licenses/>.
20 """
21 Common functions and classes for proposals and proposal sets.
22 """
24 from django.views.generic.detail import DetailView
25 from django.views.decorators.http import require_POST
26 from django.db.models import Count
27 from django.template import RequestContext
28 from django.utils.translation import ugettext_lazy as _
29 from django.utils.decorators import method_decorator
30 from django.http import HttpResponse
31 from django.shortcuts import get_object_or_404
33 from apps.ecidadania.proposals import url_names as urln_prop
34 from core.spaces import url_names as urln_space
35 from core.spaces.models import Space
36 from core.permissions import has_space_permission, has_all_permissions
37 from apps.ecidadania.proposals.models import Proposal
39 class ViewProposal(DetailView):
41 """
42 Detail view of a proposal. Inherits from django :class:`DetailView` generic
43 view.
45 **Permissions:** Everyone can read if the space is public. If it is private
46 only logged in users that belong to any of the space groups can read. In
47 other case just return an empty object and a not_allowed template.
49 :rtype: object
50 :context: proposal
51 """
52 context_object_name = 'proposal'
53 template_name = 'proposals/proposal_detail.html'
55 def get_object(self):
56 prop_id = self.kwargs['prop_id']
57 space_url = self.kwargs['space_url']
58 proposal = get_object_or_404(Proposal, pk = prop_id)
59 place = get_object_or_404(Space, url = space_url)
61 if place.public:
62 return proposal
63 elif has_space_permission(self.request.user, place,
64 allow=['admins', 'mods', 'users']) \
65 or has_all_permissions(request.user):
66 return proposal
67 else:
68 self.template_name = 'not_allowed.html'
69 return Proposal.objects.none()
71 def get_context_data(self, **kwargs):
72 context = super(ViewProposal, self).get_context_data(**kwargs)
73 current_space = get_object_or_404(Space, url=self.kwargs['space_url'])
74 # We are going to get the proposal position in the list
75 self.get_position = 0
76 proposal = get_object_or_404(Proposal, pk=self.kwargs['prop_id'])
77 if proposal.merged == True:
78 context['merged_proposal'] = proposal.merged_proposals.all()
80 support_votes_count = Proposal.objects.filter(space=current_space)\
81 .annotate(Count('support_votes'))
82 for i,x in enumerate(support_votes_count):
83 if x.id == int(self.kwargs['prop_id']):
84 self.get_position = i
85 context['support_votes_count'] = support_votes_count[int(self.get_position)].support_votes__count
86 context['get_place'] = current_space
87 return context
90 @require_POST
91 def support_proposal(request, space_url):
93 """
94 Increment support votes for the proposal in 1. We porform some permission
95 checks, for example, the user has to be inside any of the user groups of
96 the space.
97 """
98 prop = get_object_or_404(Proposal, pk=request.POST['propid'])
99 space = get_object_or_404(Space, url=space_url)
100 if has_space_permission(request.user, space,
101 allow=['admins', 'mods', 'users']):
102 try:
103 prop.support_votes.add(request.user)
104 return HttpResponse(" Support vote emmited.")
105 except:
106 return HttpResponse("Error P01: Couldn't emit the vote. Couldn't \
107 add the user to the count. Contact support and tell them the \
108 error code.")
109 else:
110 return HttpResponse("Error P02: Couldn't emit the vote. You're not \
111 allowed.")
113 # @require_POST
114 # def vote_proposal(request, space_url):
116 # """
117 # Send email to user to validate vote before is calculated.
118 # :attributes: - prop: current proposal
119 # :rtype: multiple entity objects.
120 # """
121 # prop = get_object_or_404(Proposal, pk=request.POST['propid'])
122 # try:
123 # intent = ConfirmVote.objects.get(user=request.user, proposal=prop)
124 # except ConfirmVote.DoesNotExist:
125 # token = hashlib.md5("%s%s%s" % (request.user, prop,
126 # datetime.datetime.now())).hexdigest()
127 # intent = ConfirmVote(user=request.user, proposal=prop, token=token)
128 # intent.save()
129 # subject = _("New vote validation request")
130 # body = _("Hello {0}, \n \
131 # You are getting this email because you wanted to support proposal {1}.\n\
132 # Please click on the link below to vefiry your vote.\n {2} \n \
133 # Thank you for your vote."
134 # .format(request.user.username, prop.title,
135 # intent.get_approve_url()))
136 # send_mail(subject=subject, message=body,
137 # from_email="noreply@ecidadania.org",
138 # recipient_list=[request.user.email])