Exemplo n.º 1
0
    def test_add_person(self):

        request = self.factory.get('/')
        request.user = self.user

        person_data = {'name': 'Foo', 'source': 'example.com'}

        with self.assertNumQueries(9):
            helpers.add_person(request, person_data)
    def test_add_person(self):

        request = self.factory.get("/")
        request.user = self.user

        person_data = {"name": "Foo", "source": "example.com"}

        with self.assertNumQueries(7):
            helpers.add_person(request, person_data)
Exemplo n.º 3
0
    def form_valid(self, formsets):
        source = self.request.session['bulk_add_by_party_data'].get('source')
        assert len(formsets) >= 1

        with transaction.atomic():
            for formset in formsets:
                for person_form in formset:
                    data = person_form.cleaned_data

                    # Ignore blank extra forms with no name
                    # This happens when we have fewer names
                    # than the winner_count for this pee
                    if not data.get('select_person'):
                        continue

                    data['source'] = source
                    if data.get('select_person') == "_new":
                        # Add a new person
                        person_extra = helpers.add_person(self.request, data)
                    else:
                        person_extra = PersonExtra.objects.get(
                            base__pk=int(data['select_person']))

                    # Update the person's candacies
                    helpers.update_person(self.request, person_extra,
                                          self.get_party(), formset.pee,
                                          source)

        url = self.get_election().get_absolute_url()
        return HttpResponseRedirect(url)
Exemplo n.º 4
0
    def form_valid(self, formsets):

        source = self.request.session["bulk_add_by_party_data"].get("source")
        assert len(formsets) >= 1

        with transaction.atomic():
            for formset in formsets:
                for person_form in formset:
                    data = person_form.cleaned_data

                    # Ignore blank extra forms with no name
                    # This happens when we have fewer names
                    # than the winner_count for this pee
                    if not data.get("select_person"):
                        continue

                    data["source"] = source
                    if data.get("select_person") == "_new":
                        # Add a new person
                        person = helpers.add_person(self.request, data)
                    else:
                        person = Person.objects.get(
                            pk=int(data["select_person"]))
                    # Update the person's candacies
                    helpers.update_person(
                        self.request,
                        person,
                        self.get_party(),
                        formset.ballot,
                        source,
                        list_position=data.get("party_list_position"),
                    )

        url = self.get_election().get_absolute_url()
        return HttpResponseRedirect(url)
Exemplo n.º 5
0
    def form_valid(self, context):

        with transaction.atomic():
            for person_form in context["formset"]:
                data = person_form.cleaned_data
                if data.get("select_person") == "_new":
                    # Add a new person
                    person = helpers.add_person(self.request, data)
                else:
                    person = Person.objects.get(pk=int(data["select_person"]))
                helpers.update_person(
                    self.request,
                    person,
                    Party.objects.get(
                        ec_id=person_form.cleaned_data["party"].split("__")[0]
                    ),
                    context["post_election"],
                    data["source"],
                )

            if self.request.POST.get("suggest_locking") == "on":
                pee = PostExtraElection.objects.get(
                    post=context["post"],
                    election=Election.objects.get(slug=context["election"]),
                )
                SuggestedPostLock.objects.create(
                    user=self.request.user, postextraelection=pee
                )

                LoggedAction.objects.create(
                    user=self.request.user,
                    action_type="suggest-ballot-lock",
                    ip_address=get_client_ip(self.request),
                    post_election=pee,
                    source="Suggested after bulk adding",
                )

                if hasattr(pee, "rawpeople"):
                    # Delete the raw import, as it's no longer useful
                    pee.rawpeople.delete()

        messages.add_message(
            self.request,
            messages.SUCCESS,
            get_add_from_document_cta_flash_message(
                self.official_document, self.remaining_posts_for_sopn()
            ),
            extra_tags="safe do-something-else",
        )

        remaining_qs = self.remaining_posts_for_sopn().exclude(
            pk=self.official_document.pk
        )
        if remaining_qs.exists():
            url = reverse(
                "posts_for_document", kwargs={"pk": self.official_document.pk}
            )
        else:
            url = context["post_election"].get_absolute_url()
        return HttpResponseRedirect(url)
    def form_valid(self, context):

        with transaction.atomic():
            for person_form in context['formset']:
                data = person_form.cleaned_data
                if data.get('select_person') == "_new":
                    # Add a new person
                    person_extra = helpers.add_person(self.request, data)
                else:
                    person_extra = PersonExtra.objects.get(
                        base__pk=int(data['select_person']))

                helpers.update_person(
                    self.request, person_extra,
                    Organization.objects.get(
                        pk=person_form.cleaned_data['party'].split('__')[0]),
                    context['post_election'], data['source'])

            if self.request.POST.get('suggest_locking') == 'on':
                pee = PostExtraElection.objects.get(
                    postextra=context['post_extra'],
                    election=Election.objects.get(slug=context['election']),
                )
                SuggestedPostLock.objects.create(
                    user=self.request.user,
                    postextraelection=pee,
                )

        messages.add_message(self.request,
                             messages.SUCCESS,
                             get_add_from_document_cta_flash_message(
                                 self.official_document,
                                 self.remaining_posts_for_sopn()),
                             extra_tags='safe do-something-else')

        if self.remaining_posts_for_sopn().exists():
            url = reverse('posts_for_document',
                          kwargs={'pk': self.official_document.pk})
        else:
            url = reverse('constituency',
                          kwargs={
                              'election':
                              context['election'],
                              'post_id':
                              context['post_extra'].slug,
                              'ignored_slug':
                              slugify(context['post_extra'].base.label),
                          })
        return HttpResponseRedirect(url)
Exemplo n.º 7
0
    def form_valid(self, context):

        with transaction.atomic():
            for person_form in context["formset"]:
                data = person_form.cleaned_data
                if data.get("select_person") == "_new":
                    # Add a new person
                    person = helpers.add_person(self.request, data)
                else:
                    person = Person.objects.get(pk=int(data["select_person"]))

                helpers.update_person(
                    self.request,
                    person,
                    Party.objects.get(ec_id=person_form.cleaned_data["party"].
                                      split("__")[0]),
                    context["post_election"],
                    data["source"],
                )

            if self.request.POST.get("suggest_locking") == "on":
                pee = PostExtraElection.objects.get(
                    post=context["post"],
                    election=Election.objects.get(slug=context["election"]),
                )
                SuggestedPostLock.objects.create(user=self.request.user,
                                                 postextraelection=pee)

        messages.add_message(
            self.request,
            messages.SUCCESS,
            get_add_from_document_cta_flash_message(
                self.official_document, self.remaining_posts_for_sopn()),
            extra_tags="safe do-something-else",
        )

        if self.remaining_posts_for_sopn().exists():
            url = reverse("posts_for_document",
                          kwargs={"pk": self.official_document.pk})
        else:
            url = reverse(
                "constituency",
                kwargs={
                    "election": context["election"],
                    "post_id": context["post"].slug,
                    "ignored_slug": slugify(context["post"].label),
                },
            )
        return HttpResponseRedirect(url)