def get_object(self): # Makes sure the space ins't already in the cache before hitting # the database space_url = self.kwargs['space_url'] space_object = get_or_insert_object_in_cache(Space, space_url, url=space_url) if space_object.public or has_all_permissions(self.request.user): if self.request.user.is_anonymous(): messages.info(self.request, _("Hello anonymous user. Remember \ that this space is public to view, but you must \ <a href=\"/accounts/register\">register</a> or \ <a href=\"/accounts/login\">login</a> to participate.")) return space_object # Check if the user is in the admitted user groups of the space if has_space_permission(self.request.user, space_object, allow=['admins', 'mods', 'users']): return space_object # If the user does not meet any of the conditions, it's not allowed to # enter the space if self.request.user.is_anonymous(): messages.info(self.request, _("You're an anonymous user. You must \ <a href=\"/accounts/register\">register</a> or \ <a href=\"/accounts/login\">login</a> to access here.")) else: messages.warning(self.request, _("You're not registered to this \ space.")) self.template_name = 'not_allowed.html' return space_object
def vote_poll(request, poll_id, space_url): """ Vote on a choice inside the polls. .. versionadded:: 0.1.5 """ space = get_object_or_404(Space, url=space_url) poll = get_object_or_404(Poll, pk=poll_id) try: choice = get_object_or_404(Choice, pk=request.POST['choice']) except KeyError: return render_to_response('voting/poll_detail.html', { 'poll': poll, 'get_place': space, 'error_message': "You didn't select a choice.", }, context_instance=RequestContext(request)) if request.method == 'POST' and has_space_permission(request.user, space, allow=['admins', 'mods', 'users']): poll.participants.add(request.user) choice.votes.add(request.user) return render_to_response('voting/poll_results.html', {'poll': poll, 'get_place': space, 'error_message': "You didn't \ select a choice."}, context_instance=RequestContext(request)) else: return HttpResponse("Error P02: Couldn't emit the vote. You're not \ allowed.")
def vote_poll(request, poll_id, space_url): """ Vote on a choice inside the polls. .. versionadded:: 0.1.5 """ space = get_object_or_404(Space, url=space_url) choice = get_object_or_404(Choice, pk=request.POST['choice']) poll = get_object_or_404(Poll, pk=poll_id) if request.method == 'POST' and has_space_permission( request.user, space, allow=['admins', 'mods', 'users']): poll.participants.add(request.user) choice.votes.add(request.user) return render_to_response( 'voting/poll_results.html', { 'poll': poll, 'get_place': space, 'error_message': "You didn't \ select a choice." }, context_instance=RequestContext(request)) else: return HttpResponse("Error P02: Couldn't emit the vote. You're not \ allowed.")
def vote_voting(request, space_url, voting_id): """ View to control the votes during a votation process. Do not confuse with proposals support_votes. """ place = get_object_or_404(Space, url=space_url) v = get_object_or_404(Voting, pk=voting_id) proposal = get_object_or_404(Proposal, pk=request.POST["propid"]) if has_space_permission(request.user, space, allow=["admins", "mods", "users"]): try: prop.votes.add(request.user) return HttpResponse(" Support vote emmited.") except: return HttpResponse( "Error P01: Couldn't emit the vote. Couldn't \ add the user to the count. Contact support and tell them the \ error code." ) else: return HttpResponse( "Error P02: Couldn't emit the vote. You're not \ allowed." )
def get_context_data(self, **kwargs): context = super(EditDocument, self).get_context_data(**kwargs) space = get_object_or_404(Space, url=self.kwargs['space_url']) context['get_place'] = space context['user_is_admin'] = (has_space_permission( self.request.user, space, allow=['admins', 'mods']) or has_all_permissions(self.request.user)) return context
def get_context_data(self, **kwargs): context = super(EditDocument, self).get_context_data(**kwargs) space = get_object_or_404(Space, url=self.kwargs['space_url']) context['get_place'] = space context['user_is_admin'] = (has_space_permission(self.request.user, space, allow=['admins', 'mods']) or has_all_permissions( self.request.user)) return context
def get_queryset(self): place = get_object_or_404(Space, url=self.kwargs['space_url']) objects = Proposal.objects.annotate(Count('support_votes')).filter(space=place.id).order_by('pub_date') if place.public or has_space_permission(self.request.user, place, allow=['admins', 'mods', 'users']): return objects else: return render_to_response('not_allowed.html', context_instance=RequestContext(self.request))
def get_queryset(self): place = get_object_or_404(Space, url=self.kwargs['space_url']) objects = Proposal.objects.annotate( Count('support_votes')).filter(space=place.id).order_by('pub_date') if place.public or has_space_permission( self.request.user, place, allow=['admins', 'mods', 'users']): return objects else: return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def form_valid(self, form): space = get_object_or_404(Space, url=self.kwargs['space_url']) if has_space_permission(self.request.user, space, allow=['admins', 'mods','users']): form_uncommited = form.save(commit=False) form_uncommited.space = space form_uncommited.author = self.request.user form_uncommited.save() else: return render_to_response('not_allowed.html', context_instance=RequestContext(request)) return super(AddProposal, self).form_valid(form)
def get_object(self): prop_id = self.kwargs['prop_id'] space_url = self.kwargs['space_url'] proposal = get_object_or_404(Proposal, pk = prop_id) space = get_object_or_404(Space, url = space_url) if has_space_permission(self.request.user, space, allow=['admins', 'mods']) or proposal.author.id == self.request.user.id: return proposal else: return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def edit_debate(request, space_url, pk): place = get_object_or_404(Space, url=space_url) if has_space_permission(request.user, place, allow=['admins']) \ or has_all_permissions(request.user): RowFormSet = inlineformset_factory(Debate, Row, extra=1) ColumnFormSet = inlineformset_factory(Debate, Column, extra=1) instance = Debate.objects.get(pk=pk) debate_form = DebateForm(request.POST or None, instance=instance) row_formset = RowFormSet(request.POST or None, instance=instance, prefix="rowform") column_formset = ColumnFormSet(request.POST or None, instance=instance, prefix="colform") if request.user.has_perm('debate.debate_edit') \ or has_all_permissions(request.user): if request.method == 'POST': if debate_form.is_valid() and row_formset.is_valid() \ and column_formset.is_valid(): debate_form_uncommited = debate_form.save(commit=False) debate_form_uncommited.space = place debate_form_uncommited.author = request.user saved_debate = debate_form_uncommited.save() debate_instance = get_object_or_404(Debate, pk=pk) row = row_formset.save(commit=False) for form in row: form.debate = instance form.save() column = column_formset.save(commit=False) for form in column: form.debate = instance form.save() return HttpResponseRedirect(reverse(urln.DEBATE_VIEW, kwargs={'space_url': space_url, 'debate_id': str(debate_form_uncommited.id)})) return render_to_response('debate/debate_add.html', {'form': debate_form, 'rowform': row_formset, 'colform': column_formset, 'get_place': place, 'debateid': pk}, context_instance=RequestContext(request)) return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def edit_space(request, space_url): """ Returns a form filled with the current space data to edit. Access to this view is restricted only to site and space administrators. The filter for space administrators is given by the change_space permission and their belonging to that space. :attributes: - place: current space intance. - form: SpaceForm instance. - form_uncommited: form instance before commiting to the DB, so we can modify the data. :param space_url: Space URL :rtype: HTML Form :context: form, get_place """ place = get_object_or_404(Space, url=space_url) if has_space_permission(request.user, place, allow=['admins']): form = SpaceForm(request.POST or None, request.FILES or None, instance=place) entity_forms = EntityFormSet( request.POST or None, request.FILES or None, queryset=Entity.objects.all().filter(space=place)) if request.method == 'POST': if form.is_valid() and entity_forms.is_valid(): form_uncommited = form.save(commit=False) form_uncommited.author = request.user new_space = form_uncommited.save() space = get_object_or_404(Space, name=form_uncommited.name) ef_uncommited = entity_forms.save(commit=False) for ef in ef_uncommited: ef.space = space ef.save() form.save_m2m() messages.success(request, _('Space edited successfully')) return redirect('/spaces/' + space.url + '/') return render_to_response('spaces/space_form.html', { 'form': form, 'get_place': place, 'entityformset': entity_forms }, context_instance=RequestContext(request)) return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def form_valid(self, form): space = get_object_or_404(Space, url=self.kwargs['space_url']) if has_space_permission(self.request.user, space, allow=['admins', 'mods', 'users']): form_uncommited = form.save(commit=False) form_uncommited.space = space form_uncommited.author = self.request.user form_uncommited.save() else: return render_to_response('not_allowed.html', context_instance=RequestContext(request)) return super(AddProposal, self).form_valid(form)
def get_object(self): prop_id = self.kwargs['prop_id'] space_url = self.kwargs['space_url'] proposal = get_object_or_404(Proposal, pk=prop_id) space = get_object_or_404(Space, url=space_url) if has_space_permission( self.request.user, space, allow=[ 'admins', 'mods' ]) or proposal.author.id == self.request.user.id: return proposal else: return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def get_object(self): prop_id = self.kwargs['prop_id'] space_url = self.kwargs['space_url'] proposal = get_object_or_404(Proposal, pk=prop_id) place = get_object_or_404(Space, url=space_url) if place.public: return proposal elif self.request.user.is_authenticated and \ has_space_permission(self.request.user, place, allow=['admins', 'mods', 'users']): return proposal else: self.template_name = 'not_allowed.html' return Proposal.objects.none()
def get_object(self): prop_id = self.kwargs['prop_id'] space_url = self.kwargs['space_url'] proposal = get_object_or_404(Proposal, pk = prop_id) place = get_object_or_404(Space, url = space_url) if place.public: return proposal elif self.request.user.is_authenticated and \ has_space_permission(self.request.user, place, allow=['admins', 'mods', 'users']): return proposal else: self.template_name = 'not_allowed.html' return Proposal.objects.none()
def support_proposal(request, space_url): """ Increment support votes for the proposal in 1. We porform some permission checks, for example, the user has to be inside any of the user groups of the space. """ prop = get_object_or_404(Proposal, pk=request.POST['propid']) space = get_object_or_404(Space, url=space_url) if has_space_permission(request.user, space, allow=['admins', 'mods', 'users']): try: prop.support_votes.add(request.user) return HttpResponse(" Support vote emmited.") except: return HttpResponse("Error P01: Couldn't emit the vote. Couldn't \ add the user to the count. Contact support and tell them the \ error code.") else: return HttpResponse("Error P02: Couldn't emit the vote. You're not \ allowed.") # @require_POST # def vote_proposal(request, space_url): # """ # Send email to user to validate vote before is calculated. # :attributes: - prop: current proposal # :rtype: multiple entity objects. # """ # prop = get_object_or_404(Proposal, pk=request.POST['propid']) # try: # intent = ConfirmVote.objects.get(user=request.user, proposal=prop) # except ConfirmVote.DoesNotExist: # token = hashlib.md5("%s%s%s" % (request.user, prop, # datetime.datetime.now())).hexdigest() # intent = ConfirmVote(user=request.user, proposal=prop, token=token) # intent.save() # subject = _("New vote validation request") # body = _("Hello {0}, \n \ # You are getting this email because you wanted to support proposal {1}.\n\ # Please click on the link below to vefiry your vote.\n {2} \n \ # Thank you for your vote." # .format(request.user.username, prop.title, # intent.get_approve_url())) # send_mail(subject=subject, message=body, # from_email="*****@*****.**", # recipient_list=[request.user.email])
def get_queryset(self): place = get_object_or_404(Space, url=self.kwargs['space_url']) objects = Document.objects.all().filter(space=place.id) \ .order_by('pub_date') cur_user = self.request.user if has_space_permission(cur_user, place, allow=['admins', 'mods', 'users']): return objects if self.request.user.is_anonymous(): self.template_name = 'not_allowed.html' return objects self.template_name = 'not_allowed.html' return objects
def edit_space(request, space_url): """ Returns a form filled with the current space data to edit. Access to this view is restricted only to site and space administrators. The filter for space administrators is given by the change_space permission and their belonging to that space. :attributes: - place: current space intance. - form: SpaceForm instance. - form_uncommited: form instance before commiting to the DB, so we can modify the data. :param space_url: Space URL :rtype: HTML Form :context: form, get_place """ place = get_object_or_404(Space, url=space_url) if has_space_permission(request.user, place, allow=["admins"]): form = SpaceForm(request.POST or None, request.FILES or None, instance=place) entity_forms = EntityFormSet( request.POST or None, request.FILES or None, queryset=Entity.objects.all().filter(space=place) ) if request.method == "POST": if form.is_valid() and entity_forms.is_valid(): form_uncommited = form.save(commit=False) form_uncommited.author = request.user new_space = form_uncommited.save() space = get_object_or_404(Space, name=form_uncommited.name) ef_uncommited = entity_forms.save(commit=False) for ef in ef_uncommited: ef.space = space ef.save() form.save_m2m() return HttpResponseRedirect(reverse(urln.SPACE_INDEX, kwargs={"space_url": space.url})) return render_to_response( "spaces/space_form.html", {"form": form, "get_place": place, "entityformset": entity_forms}, context_instance=RequestContext(request), ) return render_to_response("not_allowed.html", context_instance=RequestContext(request))
def edit_space(request, space_url): """ Returns a form filled with the current space data to edit. Access to this view is restricted only to site and space administrators. The filter for space administrators is given by the change_space permission and their belonging to that space. :attributes: - place: current space intance. - form: SpaceForm instance. - form_uncommited: form instance before commiting to the DB, so we can modify the data. :param space_url: Space URL :rtype: HTML Form :context: form, get_place """ place = get_object_or_404(Space, url=space_url) if has_space_permission(request.user, place, allow=['admins']): form = SpaceForm(request.POST or None, request.FILES or None, instance=place) entity_forms = EntityFormSet(request.POST or None, request.FILES or None, queryset=Entity.objects.all().filter(space=place)) if request.method == 'POST': if form.is_valid() and entity_forms.is_valid(): form_uncommited = form.save(commit=False) form_uncommited.author = request.user new_space = form_uncommited.save() space = get_object_or_404(Space, name=form_uncommited.name) ef_uncommited = entity_forms.save(commit=False) for ef in ef_uncommited: ef.space = space ef.save() form.save_m2m() messages.success(request, _('Space edited successfully')) return redirect('/spaces/' + space.url + '/') return render_to_response('spaces/space_form.html', {'form': form, 'get_place': place, 'entityformset': entity_forms}, context_instance=RequestContext(request)) return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def merge_proposal(request, space_url, set_id): """ Create a new merged proposal. This proposal can be linked to many other proposals which are in the same proposal set. Only admin and moderator can create merged proposals. .. versionadded:: 0.1.5 :arguments: space_url, p_set :context:form, get_place, form_field """ get_place = get_object_or_404(Space, url=space_url) field = ProposalField.objects.filter(proposalset=set_id) form_field = [f_name.field_name for f_name in field] if request.method == 'POST' and has_space_permission( request.user, get_place, allow=['admins', 'mods', 'users']): merged_form = ProposalForm(request.POST) if merged_form.is_valid(): form_data = merged_form.save(commit=False) form_data.proposalset = get_object_or_404(ProposalSet, pk=set_id) form_data.space = get_object_or_404(Space, url=space_url) form_data.author = request.user form_data.merged = True field = ProposalField.objects.filter(proposalset=set_id) form_field = [f_name.field_name for f_name in field] form_data.save() merged_form.save_m2m() return reverse(urln_space.SPACE_INDEX, kwargs={'space_url': space_url}) else: print "id: " + set_id merged_form = ProposalMergeForm(initial={'set_id': set_id}) return render_to_response("proposals/proposal_merged.html", { 'form': merged_form, 'get_place': get_place, 'form_field': form_field }, context_instance=RequestContext(request))
def get_queryset(self): # I think I should explain this mess. What we want to obtain here is: # a list of public spaces in case the user is anonymous, or a list of # the public spaces plus the spaces the user is registered to if the # user is logged in. # To do the second, we create a set of PK objects, and outside of the # 'for' loop we make a queryset for those PK objects, after that we # combine the data of the user spaces and public ones with the '|' # operand. current_user = self.request.user user_spaces = set() if not current_user.is_anonymous(): for space in self.all_spaces: if has_space_permission(current_user, space, allow=["users", "admins", "mods"]): user_spaces.add(space.pk) user_spaces = Space.objects.filter(pk__in=user_spaces) return self.public_spaces | user_spaces return self.public_spaces
def get_object(self): # Makes sure the space ins't already in the cache before hitting the # databass space_url = self.kwargs['space_url'] space_object = get_or_insert_object_in_cache(Space, space_url, url=space_url) if has_space_permission(self.request.user, space_object, allow=['admins','mods']) \ or has_all_permissions(self.request.user): try: intent = Intent.objects.get(token=self.kwargs['token']) intent.space.users.add(intent.user) self.status = _("The user has been authorized to participate \ in space \"%s\"." % space_object.name) messages.success(self.request, _("Authorization successful")) except Intent.DoesNotExist: self.status = _("The requested intent does not exist!") return space_object
def get_object(self): # Makes sure the space ins't already in the cache before hitting # the database space_url = self.kwargs['space_url'] space_object = get_or_insert_object_in_cache(Space, space_url, url=space_url) if space_object.public or has_all_permissions(self.request.user): if self.request.user.is_anonymous(): messages.info( self.request, _("Hello anonymous user. Remember \ that this space is public to view, but you must \ <a href=\"/accounts/register\">register</a> or \ <a href=\"/accounts/login\">login</a> to participate.")) return space_object # Check if the user is in the admitted user groups of the space if has_space_permission(self.request.user, space_object, allow=['admins', 'mods', 'users']): return space_object # If the user does not meet any of the conditions, it's not allowed to # enter the space if self.request.user.is_anonymous(): messages.info( self.request, _("You're an anonymous user. You must \ <a href=\"/accounts/register\">register</a> or \ <a href=\"/accounts/login\">login</a> to access here.")) else: messages.warning( self.request, _("You're not registered to this \ space.")) self.template_name = 'not_allowed.html' return space_object
def merge_proposal(request, space_url, set_id): """ Create a new merged proposal. This proposal can be linked to many other proposals which are in the same proposal set. Only admin and moderator can create merged proposals. .. versionadded:: 0.1.5 :arguments: space_url, p_set :context:form, get_place, form_field """ get_place = get_object_or_404(Space, url=space_url) field = ProposalField.objects.filter(proposalset=set_id) form_field = [f_name.field_name for f_name in field] if request.method == 'POST' and has_space_permission(request.user, get_place, allow=['admins', 'mods', 'users']): merged_form = ProposalForm(request.POST) if merged_form.is_valid(): form_data = merged_form.save(commit=False) form_data.proposalset = get_object_or_404(ProposalSet, pk=set_id) form_data.space = get_object_or_404(Space, url=space_url) form_data.author = request.user form_data.merged = True field = ProposalField.objects.filter(proposalset=set_id) form_field = [f_name.field_name for f_name in field] form_data.save() merged_form.save_m2m() return reverse(urln_space.SPACE_INDEX, kwargs={'space_url':space_url}) else: print "id: " + set_id merged_form = ProposalMergeForm(initial={'set_id':set_id}) return render_to_response("proposals/proposal_merged.html", {'form':merged_form, 'get_place':get_place, 'form_field':form_field}, context_instance = RequestContext(request))
def get_queryset(self): # I think I should explain this mess. What we want to obtain here is: # a list of public spaces in case the user is anonymous, or a list of # the public spaces plus the spaces the user is registered to if the # user is logged in. # To do the second, we create a set of PK objects, and outside of the # 'for' loop we make a queryset for those PK objects, after that we # combine the data of the user spaces and public ones with the '|' # operand. current_user = self.request.user user_spaces = set() if not current_user.is_anonymous(): for space in self.all_spaces: if has_space_permission(current_user, space, allow=['users', 'admins', 'mods']): user_spaces.add(space.pk) user_spaces = Space.objects.filter(pk__in=user_spaces) return self.public_spaces | user_spaces return self.public_spaces
def add_new_debate(request, space_url): """ Create a new debate. This function returns two forms to create a complete debate, debate form and phases formset. .. versionadded:: 0.1.5 :attributes: debate_form, row_formset, column_formset :context: form, rowform, colform, get_place, debateid """ place = get_object_or_404(Space, url=space_url) if has_space_permission(request.user, place, allow=['admins']) \ or has_all_permissions(request.user): RowFormSet = inlineformset_factory(Debate, Row, extra=1) ColumnFormSet = inlineformset_factory(Debate, Column, extra=1) debate_form = DebateForm(request.POST or None) row_formset = RowFormSet(request.POST or None, prefix="rowform") column_formset = ColumnFormSet(request.POST or None, prefix="colform") # Get the last PK and add 1 to get the current PK try: last_debate_id = Debate.objects.latest('id') current_debate_id = last_debate_id.pk + 1 except ObjectDoesNotExist: current_debate_id = 1 if request.user.has_perm('debate.debate_add') \ or has_all_permissions(request.user): if request.method == 'POST': if debate_form.is_valid() and row_formset.is_valid() \ and column_formset.is_valid(): debate_form_uncommited = debate_form.save(commit=False) debate_form_uncommited.space = place debate_form_uncommited.author = request.user saved_debate = debate_form_uncommited.save() debate_instance = get_object_or_404(Debate, pk=current_debate_id) row = row_formset.save(commit=False) for form in row: form.debate = debate_instance form.save() column = column_formset.save(commit=False) for form in column: form.debate = debate_instance form.save() return HttpResponseRedirect(reverse(urln.DEBATE_VIEW, kwargs={'space_url': space_url, 'debate_id': str(debate_form_uncommited.id)})) return render_to_response('debate/debate_add.html', {'form': debate_form, 'rowform': row_formset, 'colform': column_formset, 'get_place': place, 'debateid': current_debate_id}, context_instance=RequestContext(request)) return render_to_response('not_allowed.html', context_instance=RequestContext(request))
def add_new_debate(request, space_url): """ Create a new debate. This function returns two forms to create a complete debate, debate form and phases formset. .. versionadded:: 0.1.5 :attributes: debate_form, row_formset, column_formset :context: form, rowform, colform, get_place, debateid """ place = get_object_or_404(Space, url=space_url) if has_space_permission(request.user, place, allow=['admins']) \ or has_all_permissions(request.user): # Define FormSets # This class is used to make empty formset forms required # See http://stackoverflow.com/questions/2406537/django-formsets-make # -first-required/4951032#4951032 class RequiredFormSet(BaseFormSet): """ """ def __init__(self, *args, **kwargs): super(RequiredFormSet, self).__init__(*args, **kwargs) for form in self.forms: form.empty_permitted = False RowFormSet = formset_factory(RowForm, max_num=10, formset=RequiredFormSet, can_delete=True) ColumnFormSet = formset_factory(ColumnForm, max_num=10, formset=RequiredFormSet, can_delete=True) debate_form = DebateForm(request.POST or None) row_formset = RowFormSet(request.POST or None, prefix="rowform") column_formset = ColumnFormSet(request.POST or None, prefix="colform") # Get the last PK and add 1 to get the current PK try: last_debate_id = Debate.objects.latest('id') current_debate_id = last_debate_id.pk + 1 except ObjectDoesNotExist: current_debate_id = 1 if request.user.has_perm('debate.debate_add') or has_all_permissions(): if request.method == 'POST': if debate_form.is_valid() and row_formset.is_valid() \ and column_formset.is_valid(): debate_form_uncommited = debate_form.save(commit=False) debate_form_uncommited.space = place debate_form_uncommited.author = request.user saved_debate = debate_form_uncommited.save() debate_instance = get_object_or_404(Debate, pk=current_debate_id) for form in row_formset.forms: row = form.save(commit=False) row.debate = debate_instance row.save() for form in column_formset.forms: column = form.save(commit=False) column.debate = debate_instance column.save() return HttpResponseRedirect(reverse(urln.DEBATE_VIEW, kwargs={'space_url': space_url, 'debate_id': str(debate_form_uncommited.id)})) return render_to_response('debate/debate_add.html', {'form': debate_form, 'rowform': row_formset, 'colform': column_formset, 'get_place': place, 'debateid': current_debate_id}, context_instance=RequestContext(request)) return render_to_response('not_allowed.html', context_instance=RequestContext(request))