def _update_new_agency(self, request, foia): """Update the new agency""" form = AgencyForm(request.POST, instance=foia.agency) if foia.has_perm(request.user, 'change'): if form.is_valid(): form.save() messages.success(request, 'Agency info saved. Thanks for your help!') else: messages.success(request, 'The data was invalid! Try again.') else: messages.error(request, 'You cannot do that, stop it.') return redirect(foia)
def update_new_agency(request, foia): """Update the new agency""" form = AgencyForm( request.POST, instance=foia.agency, prefix=request.POST.get("task", "") ) if foia.has_perm(request.user, "change"): if form.is_valid(): form.save() messages.success(request, "Agency info saved. Thanks for your help!") else: messages.error(request, "The data was invalid! Try again.") else: messages.error(request, "You cannot do that, stop it.") return _get_redirect(request, foia)
def test_post_accept(self): contact_data = { 'name': 'Test Agency', 'address_street': '1234 Whatever Street', 'email': '*****@*****.**', 'portal_type': 'other', 'phone': '', 'fax': '', 'jurisdiction': self.task.agency.jurisdiction.pk, } form = AgencyForm(contact_data, instance=self.task.agency) ok_(form.is_valid()) contact_data.update({'approve': True, 'task': self.task.pk}) self.client.post(self.url, contact_data) updated_task = NewAgencyTask.objects.get(pk=self.task.pk) eq_(updated_task.agency.status, 'approved') ok_(updated_task.resolved)
def task_post_helper(self, request, task): """Special post handlers exclusive to NewAgencyTasks""" if request.POST.get('approve'): new_agency_form = AgencyForm(request.POST, instance=task.agency) if new_agency_form.is_valid(): new_agency_form.save() else: messages.error(request, 'The agency info form is invalid.') return task.approve() task.resolve(request.user) if request.POST.get('reject'): replacement_agency_id = request.POST.get('replacement') replacement_agency = get_object_or_404(Agency, id=replacement_agency_id) task.reject(replacement_agency) task.resolve(request.user) return super(NewAgencyTaskList, self).task_post_helper(request, task)
def task_post_helper(self, request, task, form_data=None): """Special post handlers exclusive to NewAgencyTasks""" if request.POST.get("approve"): new_agency_form = AgencyForm(request.POST, instance=task.agency, prefix=request.POST.get("task", "")) if new_agency_form.is_valid(): new_agency_form.save() else: raise ValueError("The agency info form is invalid.") task.approve() form_data = new_agency_form.cleaned_data # phone numbers must be strings not phone number objects to serialize if form_data.get("phone"): form_data["phone"] = str(form_data["phone"]) if form_data.get("fax"): form_data["fax"] = str(form_data["fax"]) if form_data.get("jurisdiction"): form_data["jurisdiction"] = form_data["jurisdiction"].pk form_data.update({"approve": True}) task.resolve(request.user, form_data) elif request.POST.get("replace"): form = ReplaceNewAgencyForm(request.POST, prefix=request.POST.get("task", "")) if form.is_valid(): replace_agency = form.cleaned_data["replace_agency"] task.reject(replace_agency) form_data = { "replace": True, "replace_agency": replace_agency.pk } task.resolve(request.user, form_data) else: messages.error(request, "Bad form data") return None elif request.POST.get("reject"): task.reject() form_data = {"reject": True} task.resolve(request.user, form_data) elif request.POST.get("spam"): task.spam(request.user) form_data = {"spam": True} task.resolve(request.user, form_data) return super(NewAgencyTaskList, self).task_post_helper(request, task)
def test_post_accept(self): contact_data = { f"{self.task.pk}-name": "Test Agency", f"{self.task.pk}-address_street": "1234 Whatever Street", f"{self.task.pk}-email": "*****@*****.**", f"{self.task.pk}-portal_type": "other", f"{self.task.pk}-phone": "", f"{self.task.pk}-fax": "", f"{self.task.pk}-jurisdiction": self.task.agency.jurisdiction.pk, } form = AgencyForm( contact_data, instance=self.task.agency, prefix=str(self.task.pk) ) ok_(form.is_valid()) contact_data.update({"approve": True, "task": self.task.pk}) self.client.post(self.url, contact_data) updated_task = NewAgencyTask.objects.get(pk=self.task.pk) eq_(updated_task.agency.status, "approved") ok_(updated_task.resolved)
def task_post_helper(self, request, task, form_data=None): """Special post handlers exclusive to NewAgencyTasks""" if request.POST.get('approve'): new_agency_form = AgencyForm(request.POST, instance=task.agency) if new_agency_form.is_valid(): new_agency_form.save() else: raise ValueError('The agency info form is invalid.') task.approve() form_data = new_agency_form.cleaned_data # phone numbers must be strings not phone number objects to serialize if form_data.get('phone'): form_data['phone'] = unicode(form_data['phone']) if form_data.get('fax'): form_data['fax'] = unicode(form_data['fax']) if form_data.get('jurisdiction'): form_data['jurisdiction'] = form_data['jurisdiction'].pk form_data.update({'approve': True}) task.resolve(request.user, form_data) elif request.POST.get('replace'): form = ReplaceNewAgencyForm(request.POST) if form.is_valid(): replace_agency = form.cleaned_data['replace_agency'] task.reject(replace_agency) form_data = { 'replace': True, 'replace_agency': replace_agency.pk } task.resolve(request.user, form_data) else: messages.error(request, 'Bad form data') return elif request.POST.get('reject'): task.reject() form_data = {'reject': True} task.resolve(request.user, form_data) elif request.POST.get('spam'): task.spam() form_data = {'spam': True} task.resolve(request.user, form_data) return super(NewAgencyTaskList, self).task_post_helper(request, task)
class TestAgencyForm(TestCase): """Tests the AgencyForm""" def setUp(self): self.agency = AgencyFactory() self.form = AgencyForm( { 'name': self.agency.name, 'jurisdiction': self.agency.jurisdiction.pk, 'portal_type': 'other', }, instance=self.agency, ) def test_validate_empty_form(self): """The form should have a name, at least""" ok_(not AgencyForm().is_valid(), 'Empty AgencyForm should not validate.') def test_instance_form(self): """The form should validate given only instance data""" ok_(self.form.is_valid())