def questions(request): if request.method == 'GET': return redirect("/") form = QuestionForm(request.POST) if not form.is_valid(): # form = QuestionForm() messages.error(request, _('You have some errors in the form')) return { 'form': form, 'categories': Category.objects.all(), 'ideas': [], } if not request.user.is_authenticated(): request.session['opendebates.stashed_submission'] = { "category": request.POST['category'], "question": request.POST['question'], "citation": request.POST.get("citation"), } return redirect("registration_register") category = request.POST.get('category') form_data = form.cleaned_data voter, created = Voter.objects.get_or_create( email=request.user.email, defaults=dict( source=request.COOKIES.get('opendebates.source') ) ) idea = Submission.objects.create( voter=voter, category_id=category, headline=form_data['headline'], idea=form_data['question'], citation=form_data['citation'], created_at=timezone.now(), ip_address=get_ip_address_from_request(request), approved=True, votes=1, source=request.COOKIES.get('opendebates.source'), ) Vote.objects.create( submission=idea, voter=voter, source=idea.source, ip_address=get_ip_address_from_request(request), request_headers=get_headers_from_request(request), created_at=timezone.now()) send_email("submitted_new_idea", {"idea": idea}) url = reverse("vote", kwargs={'id': idea.id}) return redirect(url)
def remove(request): if not request.user.is_superuser: return HttpResponseNotFound() to_remove = get_object_or_404(Submission, pk=request.POST.get('to_remove'), approved=True) remove = request.POST.get('action').lower() == 'remove' if remove: to_remove.approved = False msg = _(u'The question has been removed.') else: msg = _(u'The question has been kept and removed from the moderation list.') to_remove.moderated_removal = True to_remove.removal_flags.all().update(reviewed=True) to_remove.save() if remove and request.POST.get("send_email") == "yes": send_email("idea_is_removed", {"idea": to_remove}) messages.info(request, msg) return redirect('moderation_home')
def merge(request): if not request.user.is_superuser: return HttpResponseNotFound() to_remove = get_object_or_404(Submission, pk=request.POST['to_remove'], approved=True) duplicate_of = get_object_or_404(Submission, pk=request.POST['duplicate_of'], approved=True) if request.POST.get("action").lower() == "reject": msg = _(u'No changes made and flag has been removed.') elif request.POST.get("action").lower() == "merge": votes_already_cast = list(Vote.objects.filter( submission=duplicate_of).values_list("voter_id", flat=True)) votes_to_merge = Vote.objects.filter(submission=to_remove).exclude( voter__in=votes_already_cast) duplicate_of.keywords = (duplicate_of.keywords or '') \ + " " + to_remove.idea \ + " " + (to_remove.keywords or '') duplicate_of.has_duplicates = True duplicate_of.votes += votes_to_merge.count() duplicate_of.save() votes_to_merge.update(original_merged_submission=to_remove, submission=duplicate_of) to_remove.duplicate_of = duplicate_of to_remove.save() msg = _(u'Question has been merged.') if request.POST.get("send_email") == "yes": send_email("your_idea_is_merged", {"idea": to_remove}) send_email("idea_merged_into_yours", {"idea": duplicate_of}) else: return HttpResponseBadRequest('Invalid value for "action".') # mark all flags of this merge as 'reviewed' Flag.objects.filter( to_remove=to_remove, duplicate_of=duplicate_of, reviewed=False ).update(reviewed=True) messages.info(request, msg) return redirect('moderation_home')
def merge(request): if not request.user.is_superuser: return HttpResponseNotFound() to_remove = get_object_or_404(Submission, pk=request.POST['to_remove'], approved=True) duplicate_of = get_object_or_404(Submission, pk=request.POST['duplicate_of'], approved=True) if request.POST.get("action").lower() == "reject": msg = _(u'No changes made and flag has been removed.') elif request.POST.get("action").lower() == "unmoderate": msg = _(u'Duplicate has been removed, and votes have not been merged.') to_remove.approved = False to_remove.duplicate_of = duplicate_of to_remove.save() if request.POST.get("send_email") == "yes": send_email("your_idea_is_duplicate", {"idea": to_remove}) elif request.POST.get("action").lower() == "merge": votes_already_cast = list( Vote.objects.filter(submission=duplicate_of).values_list( "voter_id", flat=True)) votes_to_merge = Vote.objects.filter(submission=to_remove).exclude( voter__in=votes_already_cast) local_state = get_local_votes_state() if local_state: local_votes_to_merge = votes_to_merge.filter( voter__state=local_state).count() else: local_votes_to_merge = 0 duplicate_of.keywords = (duplicate_of.keywords or '') \ + " " + to_remove.idea \ + " " + (to_remove.keywords or '') duplicate_of.has_duplicates = True duplicate_of.votes += votes_to_merge.count() duplicate_of.local_votes += local_votes_to_merge duplicate_of.save() votes_to_merge.update(original_merged_submission=to_remove, submission=duplicate_of) to_remove.duplicate_of = duplicate_of to_remove.save() msg = _(u'Issue has been merged.') if request.POST.get("send_email") == "yes": send_email("your_idea_is_merged", {"idea": to_remove}) send_email("idea_merged_into_yours", {"idea": duplicate_of}) else: return HttpResponseBadRequest('Invalid value for "action".') # mark all flags of this merge as 'reviewed' Flag.objects.filter(to_remove=to_remove, duplicate_of=duplicate_of, reviewed=False).update(reviewed=True) messages.info(request, msg) return redirect('moderation_home')
def remove(request): if not request.user.is_superuser: return HttpResponseNotFound() to_remove = get_object_or_404(Submission, pk=request.POST.get('to_remove'), approved=True) remove = request.POST.get('action').lower() == 'remove' if remove: to_remove.approved = False msg = _(u'The issue has been removed.') else: msg = _( u'The issue has been kept and removed from the moderation list.') to_remove.moderated_removal = True to_remove.removal_flags.all().update(reviewed=True) to_remove.save() if remove and request.POST.get("send_email") == "yes": send_email("idea_is_removed", {"idea": to_remove}) messages.info(request, msg) return redirect('moderation_home')
def questions(request): if request.method == 'GET': return redirect("/") if not allow_voting_and_submitting_questions(): raise Http404 form = QuestionForm(request.POST) if not form.is_valid(): # form = QuestionForm() messages.error(request, _('You have some errors in the form')) return { 'form': form, 'categories': Category.objects.all(), 'ideas': [], } followup = request.POST.get('followup', '') if not request.user.is_authenticated(): request.session['opendebates.stashed_submission'] = { "category": request.POST['category'], "headline": request.POST['headline'], "followup": followup, "citation": request.POST.get("citation"), "happened": request.POST.get("happened"), "is_positive": request.POST.get("is_positive"), } return redirect("registration_register") category = request.POST.get('category') form_data = form.cleaned_data voter, created = Voter.objects.get_or_create( email=request.user.email, defaults=dict(source=request.COOKIES.get('opendebates.source'))) idea = Submission.objects.create( voter=voter, category_id=category, headline=form_data['headline'], followup=followup or None, idea=(u'%s %s' % (form_data['headline'], followup)).strip(), citation=form_data['citation'], citation_verified= True, # For the moment, default citations to verified so they show in list happened=form_data.get('happened'), is_positive=form_data['is_positive'], created_at=timezone.now(), ip_address=get_ip_address_from_request(request), approved=True, votes=1, local_votes=1 if voter.state and voter.state == get_local_votes_state() else 0, source=request.COOKIES.get('opendebates.source'), ) Vote.objects.create(submission=idea, voter=voter, source=idea.source, ip_address=get_ip_address_from_request(request), request_headers=get_headers_from_request(request), created_at=timezone.now()) send_email("submitted_new_idea", {"idea": idea}) send_email("notify_moderators_submitted_new_idea", {"idea": idea}) url = reverse('list_ideas') return redirect(url + "#created=%s" % idea.id)
def test_no_such_template(self, mock_task): send_email("no_such", self.ctx) self.assertFalse(mock_task.called)
def test_ctx_not_submission(self, mock_task): send_email(self.type, {'idea': "String not Submission object"}) self.assertFalse(mock_task.called)
def test_ctx_no_idea(self, mock_task): send_email(self.type, {}) self.assertFalse(mock_task.called)
def test_sends_in_background(self, mock_task): send_email(self.type, self.ctx) mock_task.delay.assert_called_with(self.type, self.idea.pk)