def test_redirect_uniqueness_validation(self): # Validation should stop someone changing a place slug to one # that's redirecting via SlugRedirect. pkind = models.PlaceKind.objects.create( name="Example Places", slug="example-places", ) place_redirected_to = models.Place.objects.create( name='Echo Beach', slug='echo-beach', kind=pkind, ) existing_redirect = SlugRedirect.objects.create( new_object=place_redirected_to, old_object_slug='eb', ) other_place = models.Place( name='Ethereal Boulevard', slug='eb', kind=pkind, ) with self.assertRaises(ValidationError): other_place.clean_fields() existing_redirect.delete() place_redirected_to.delete() pkind.delete()
def test_place_is_required(self): """ Some job titles (like an MP) are meaningless if there is no place associated with them. """ # create position with no place position = models.Position( person=self.person, title=self.title, ) position._set_sorting_dates() position.full_clean() # Change the title to require a place self.title.requires_place = True with self.assertRaises(exceptions.ValidationError): position.full_clean() # give the pos a place and check that it now validates position.place = models.Place(name='Test Place', slug='test-place') position.full_clean() # put it back self.title.requires_place = False
from pprint import pprint from django.utils.text import slugify from pombola.core import models constituency_kind = models.PlaceKind.objects.get(slug="constituency") objects = simplejson.loads(sys.stdin.read()) for obj in objects: pprint(obj) # {'Code': '001', # 'ConstituencyID': '1', # 'DistrictID': '86', # 'MemberID': '19', # 'Name': 'Makadara', # 'Population': '0', # 'RegisteredVoters': '132630'} try: db_obj = models.Place.objects.get(slug=slugify(obj['Name'])) except models.Place.DoesNotExist: db_obj = models.Place(slug=slugify(obj['Name'])) db_obj.kind = constituency_kind db_obj.original_id = obj['ConstituencyID'] db_obj.name = obj['Name'] db_obj.save()