def async_create_link(request, candidate_pk): link_name = request.POST.get('link_name', False) link_url = request.POST.get('link_url', False) election_slug = request.POST.get('election_slug', False) slug = request.POST.get('candidate_slug', False) cand_name = request.POST.get('candidate_name', False) election = get_object_or_404(Election, slug=election_slug, owner=request.user) candidate = get_object_or_404(Candidate, slug=slug, election=election, name=cand_name) link = Link(name = link_name, url= link_url, candidate = candidate) try: link.full_clean() link.save() json_dictionary = {"result": "OK", 'name':link_name, 'url':link.http_prefix, 'pk':link.pk} except ValidationError as e: json_dictionary = {"result":"NOK", "errors":e.message_dict} return HttpResponse(json.dumps(json_dictionary),content_type='application/json')
def test_does_not_create_an_empty_link(self): link = Link(name="", url="", candidate=self.candidate) try: link.full_clean() self.fail("The link name can not be empty") except ValidationError as e: expected_error = {"name": [u"This field cannot be blank."], "url": [u"This field cannot be blank."]} self.assertEqual(e.message_dict, expected_error) link = Link(name="", url="http://www.google.com", candidate=self.candidate) try: link.full_clean() self.fail("The link name can not be empty") except ValidationError as e: expected_error = {"name": [u"This field cannot be blank."]} self.assertEqual(e.message_dict, expected_error) link = Link(name="Twitter", url="", candidate=self.candidate) try: link.full_clean() self.fail("The url not can be empty") except ValidationError as e: expected_error = {"url": [u"This field cannot be blank."]} self.assertEqual(e.message_dict, expected_error)
def test_does_not_create_an_empty_link(self): link = Link(name="", url="", candidate=self.candidate) try: link.full_clean() self.fail('The link name can not be empty') except ValidationError as e: expected_error = { 'name': [u'This field cannot be blank.'], 'url': [u'This field cannot be blank.'] } self.assertEqual(e.message_dict, expected_error) link = Link(name="", url="http://www.google.com", candidate=self.candidate) try: link.full_clean() self.fail('The link name can not be empty') except ValidationError as e: expected_error = {'name': [u'This field cannot be blank.']} self.assertEqual(e.message_dict, expected_error) link = Link(name="Twitter", url="", candidate=self.candidate) try: link.full_clean() self.fail('The url not can be empty') except ValidationError as e: expected_error = {'url': [u'This field cannot be blank.']} self.assertEqual(e.message_dict, expected_error)