def form_valid(self, form): post_id = form.cleaned_data["post_id"] with transaction.atomic(): post = get_object_or_404(Post, slug=post_id) raise_if_locked(self.request, post, self.election_data) change_metadata = get_change_metadata(self.request, form.cleaned_data["source"]) person = get_object_or_404(Person, id=form.cleaned_data["person_id"]) LoggedAction.objects.create( user=self.request.user, action_type="candidacy-delete", ip_address=get_client_ip(self.request), popit_person_new_version=change_metadata["version_id"], person=person, source=change_metadata["information_source"], ) memberships_to_delete = person.memberships.filter( post=post, role=self.election_data.candidate_membership_role, post_election__election=self.election_data, ) for m in memberships_to_delete: raise_if_unsafe_to_delete(m) m.delete() check_no_candidancy_for_election(person, self.election_data) person.not_standing.add(self.election_data) person.record_version(change_metadata) person.save() if self.request.is_ajax(): return JsonResponse({"success": True}) return get_redirect_to_post(self.election, post)
def form_valid(self, form): post_id = form.cleaned_data['post_id'] with transaction.atomic(): post = get_object_or_404(Post, extra__slug=post_id) raise_if_locked(self.request, post) change_metadata = get_change_metadata(self.request, form.cleaned_data['source']) person = get_object_or_404(Person, id=form.cleaned_data['person_id']) LoggedAction.objects.create( user=self.request.user, action_type='candidacy-delete', ip_address=get_client_ip(self.request), popit_person_new_version=change_metadata['version_id'], person=person, source=change_metadata['information_source'], ) person.memberships.filter( post=post, role=self.election_data.candidate_membership_role, extra__election=self.election_data, ).delete() check_no_candidancy_for_election(person, self.election_data) person.extra.not_standing.add(self.election_data) person.extra.record_version(change_metadata) person.extra.save() return get_redirect_to_post(self.election, post)
def mark_as_not_standing(person, election_data, post): # Remove any existing candidacy: for membership in Membership.objects.filter( ballot__election=election_data, person=person, # n.b. we are planning to make "not standing" post # specific in the future, in which case we would also want # this line: # post__slug=post_slug, ): raise_if_unsafe_to_delete(membership) membership.delete() from candidates.models.constraints import check_no_candidancy_for_election check_no_candidancy_for_election(person, election_data) person.not_standing.add(election_data)
def test_raise_if_candidacy_exists(self): new_candidate = PersonFactory.create(name="John Doe") post = Post.objects.get(slug="14419") # Create a new candidacy: MembershipFactory.create( person=new_candidate, post=post, ballot=self.election.ballot_set.get(post=post), ) with self.assertRaisesRegex( Exception, (r"There was an existing candidacy for John Doe " r'\({person_id}\) in the election "2015 General ' r'Election"').format(person_id=new_candidate.id), ): check_no_candidancy_for_election(new_candidate, self.election)
def test_raise_if_candidacy_exists(self): new_candidate = PersonExtraFactory.create(base__name='John Doe') post_extra = PostExtra.objects.get(slug='14419') # Create a new candidacy: MembershipFactory.create( person=new_candidate.base, post=post_extra.base, role=self.election.candidate_membership_role, post_election=self.election.postextraelection_set.get( postextra=post_extra)) with self.assertRaisesRegexp( Exception, (r'There was an existing candidacy for John Doe ' \ r'\({person_id}\) in the election "2015 General ' \ r'Election"').format(person_id=new_candidate.base.id)): check_no_candidancy_for_election(new_candidate.base, self.election)