class _BaseIdeaTest(APITestCase): def setUp(self): self.user = User.objects.create_user('bob', password='******') self.project = Project(owner=self.user, title='testing', description='Super test', status='IDEA') self.project.save() self.client.login(username='******', password='******') self.IDEA_DATA = {'owner': self.user, 'title' : 'really descriptive title', 'project' : self.project, 'description': 'test idea', 'votes': 1} self.idea = Idea(**self.IDEA_DATA) self.idea.save() def tearDown(self): self.user.delete() self.idea.delete() self.project.delete()
def add_additional_create_idea_params(idea: Idea, data: dict, url_slug: str) -> None: idea.url_slug = url_slug try: hub = Hub.objects.get(url_slug=data['hub']) hub_shared_in = Hub.objects.get(url_slug=data['hub_shared_in']) except Hub.DoesNotExist: idea.delete() raise ValidationError('Hub does not exist: ' + data['hub']) idea.hub = hub idea.hub_shared_in = hub_shared_in if 'location' in data: idea.location = get_location(data['location']) if 'image' in data and 'thumbnail_image' in data: idea.image = get_image_from_data_url(data['image'])[0] idea.thumbnail_image = get_image_from_data_url( data['thumbnail_image'])[0] if 'parent_organization' in data: try: organization = Organization.objects.get( id=data['parent_organization']) except Organization.DoesNotExist: idea.delete() raise ValidationError('Organization does not exist!') idea.organization = organization try: idea.save() except IntegrityError: if url_slug.endswith(str(idea.id)): # It seems like we alread called this function and added the id to the url slug, but there is still an integrity error! # Seems like it is unrelated to the url_slug and there is some bigger problem idea.delete() raise ValidationError("Internal Server Error") else: # The url slug is already taken! We'll append the project id in the end to make it unique again add_additional_create_idea_params(idea, data, url_slug + str(idea.id))