def test_candidate_form(self): form_class = get_candidate_profile_form_class() form = form_class(candidate=self.candidate, data=self.data) self.assertTrue(form.is_valid()) self.assertIn('age', form.cleaned_data.keys()) self.assertEquals(form.cleaned_data['age'], self.data['age']) self.assertEquals(form.cleaned_data['party'], self.data['party']) form.save() personal_datas = PersonalData.objects.filter(candidate=self.candidate) self.assertEquals(len(personal_datas), 3) age = personal_datas.get(value=self.data['age']) self.assertEquals(age.label, 'age') party = personal_datas.get(value=self.data['party']) self.assertEquals(party.label, 'party') program = personal_datas.get(label='program_link') self.assertTrue(program.value) facebook = self.candidate.contact_details.get(contact_type='FACEBOOK') self.assertEquals(facebook.value, self.data['facebook']) facebook = self.candidate.contact_details.get(contact_type='TWITTER') self.assertEquals(facebook.value, self.data['twitter']) facebook = self.candidate.contact_details.get(contact_type='URL') self.assertEquals(facebook.value, self.data['url']) form.save() personal_datas = PersonalData.objects.filter(candidate=self.candidate) self.assertEquals(personal_datas.count(), 3) self.assertEquals(self.candidate.contact_details.count(), 3) self.candidate.add_contact_detail(label='perrito', contact_type="FACEBOOK", value="http://facebook.com") form = form_class(candidate=self.candidate, data=self.data) self.assertEquals(form.initial['facebook'], self.data['facebook'])
def test_get_12_naranja_brings_the_form(self): election = Election.objects.get(id=2) url = reverse('backend_candidate:complete_profile', kwargs={ 'slug': election.slug, 'candidate_slug': self.candidate.slug }) self.client.login(username=self.feli, password='******') response = self.client.get(url) self.assertIn('form', response.context) form_class = get_candidate_profile_form_class() for field in form_class.base_fields: self.assertIn(field, response.context['form'].fields) form = form_class(data=self.data, candidate=self.candidate) self.assertTrue(form.is_valid()) form.save() response = self.client.get(url) form = response.context['form'] self.assertEquals(form.initial['age'], str(self.data['age'])) self.assertEquals(form.initial['party'], self.data['party']) self.assertEquals(form.initial['facebook'], self.data['facebook']) self.assertEquals(form.initial['twitter'], self.data['twitter']) self.assertEquals(form.initial['url'], self.data['url'])
def test_get_12_naranja_brings_the_form(self): election = Election.objects.get(id=2) url = reverse('backend_candidate:complete_profile', kwargs={'slug': election.slug, 'candidate_slug': self.candidate.slug}) self.client.login(username=self.feli, password='******') response = self.client.get(url) self.assertIn('form', response.context) form_class = get_candidate_profile_form_class() for field in form_class.base_fields: self.assertIn(field, response.context['form'].fields) form = form_class(data=self.data, candidate=self.candidate) self.assertTrue(form.is_valid()) form.save() response = self.client.get(url) form = response.context['form'] self.assertEquals(form.initial['age'], str(self.data['age'])) self.assertEquals(form.initial['party'], self.data['party']) self.assertEquals(form.initial['facebook'], self.data['facebook']) self.assertEquals(form.initial['twitter'], self.data['twitter']) self.assertEquals(form.initial['url'], self.data['url'])
def test_candidate_form(self): form_class = get_candidate_profile_form_class() form = form_class(candidate=self.candidate, data=self.data) self.assertTrue(form.is_valid()) self.assertIn('age', form.cleaned_data.keys()) self.assertEquals(form.cleaned_data['age'], self.data['age']) self.assertEquals(form.cleaned_data['party'], self.data['party']) form.save() personal_datas = PersonalData.objects.filter(candidate=self.candidate) self.assertEquals(len(personal_datas), 3) age = personal_datas.get(value=self.data['age']) self.assertEquals(age.label, 'age') party = personal_datas.get(value=self.data['party']) self.assertEquals(party.label, 'party') program = personal_datas.get(label='program_link') self.assertTrue(program.value) facebook = self.candidate.contact_details.get(contact_type='FACEBOOK') self.assertEquals(facebook.value, self.data['facebook']) facebook = self.candidate.contact_details.get(contact_type='TWITTER') self.assertEquals(facebook.value, self.data['twitter']) facebook = self.candidate.contact_details.get(contact_type='URL') self.assertEquals(facebook.value, self.data['url']) form.save() personal_datas = PersonalData.objects.filter(candidate=self.candidate) self.assertEquals(personal_datas.count(), 3) self.assertEquals(self.candidate.contact_details.count(), 3) self.candidate.add_contact_detail(label='perrito', contact_type="FACEBOOK", value="http://facebook.com") form = form_class(candidate=self.candidate, data=self.data) self.assertEquals(form.initial['facebook'], self.data['facebook'])
def get_form_class(self): return get_candidate_profile_form_class()
def personal_data_label(personal_data): _class = get_candidate_profile_form_class() field = _class.base_fields.get(personal_data.label, None) if field is not None: return getattr(field, 'label', '') return personal_data.label
def get_redirect_url(self, *args, **kwargs): candidacy, created = Candidacy.objects.get_or_create( candidate=self.contact.candidate, user=self.request.user) self.contact.candidacy = candidacy self.contact.used_by_candidate = True self.contact.save() candidacy = self.request.user.candidacies.first() profile_url = reverse('backend_candidate:complete_profile', kwargs={ 'slug': candidacy.candidate.election.slug, 'candidate_id': candidacy.candidate.id }) return profile_url form_class = get_candidate_profile_form_class() class ProfileView(FormView): form_class = form_class template_name = 'backend_candidate/complete_profile.html' @method_decorator(login_required) def dispatch(self, request, *args, **kwargs): if not is_candidate(request.user): raise Http404 self.user = request.user self.election = get_object_or_404(Election, slug=self.kwargs['slug']) self.candidate = get_object_or_404(Candidate, id=self.kwargs['candidate_id']) if not Candidacy.objects.filter(user=self.request.user,
def dispatch(self, *args, **kwargs): self.contact = get_object_or_404(CandidacyContact, identifier=self.kwargs['identifier']) return super(CandidacyJoinView, self).dispatch(*args, **kwargs) def get_redirect_url(self, *args, **kwargs): candidacy, created = Candidacy.objects.get_or_create(candidate=self.contact.candidate, user=self.request.user ) self.contact.candidacy = candidacy self.contact.used_by_candidate = True self.contact.save() return reverse('backend_candidate:home') form_class = get_candidate_profile_form_class() class ProfileView(FormView): form_class = form_class template_name = 'backend_candidate/complete_profile.html' @method_decorator(login_required) def dispatch(self, request, *args, **kwargs): if not is_candidate(request.user): raise Http404 self.user = request.user self.election = get_object_or_404(Election, slug=self.kwargs['slug']) self.candidate = get_object_or_404(Candidate, id=self.kwargs['candidate_id']) return super(ProfileView, self).dispatch(request, *args, **kwargs)
def personal_data_label(personal_data): _class = get_candidate_profile_form_class() field = _class.base_fields.get(personal_data.label, None) if field is not None: return getattr(field, 'label', '') return personal_data.label