def create(self, validated_data): if validated_data.get('gallery'): gallery = validated_data.pop('gallery') else: gallery = [] if len(Company.objects.filter(name=validated_data['name'])) > 0: raise CustomException( detail=ugettext('Company with this name exists.')) try: validated_data['city'] = City.objects.get( city_slug=validated_data['city']['city_slug']) except City.DoesNotExist as e: raise CustomException( detail=ugettext('City with this name does not exist.')) try: validated_data['industry'] = Industry.objects.get( industry_slug=validated_data['industry']['industry_slug']) except Industry.DoesNotExist as e: raise CustomException( detail=ugettext('Industry with this slug does not exist.')) validated_data['user'] = self.context['request'].user if validated_data.get('site'): validated_data['site'] = self.validate_site(validated_data['site']) validated_data['logo'] = self.validate_logo(validated_data['logo']) validated_data['approved'] = False validated_data['user_generated'] = True company = Company(**validated_data) company.save() for item in gallery: item['company'] = company gallery_item = Gallery(**item) gallery_item.save() cache.delete(settings.COMPANY_NAME_LIST) return company
def create(self, validated_data): validated_data['industry'] = Industry.objects.get( industry_slug=validated_data['industry']['industry_slug']) if validated_data.get('gallery'): gallery = validated_data.pop('gallery') else: gallery = [] if validated_data.get('benefit'): benefit = validated_data.pop('benefit') else: benefit = [] validated_data['user'] = self.context['request'].user validated_data['city'] = City.objects.get( city_slug=validated_data['city']['city_slug']) validated_data['approved'] = True if validated_data.get('location_point'): validated_data['location_point'] = Point( validated_data.pop('location')) company = Company(**validated_data) company.save() for item in benefit: benefit_item = Benefit.objects.get(name=item['name']) company.benefit.add(benefit_item) for item in gallery: item['company'] = company gallery_item = Gallery(**item) gallery_item.save() cache.delete(settings.COMPANY_NAME_LIST) return company
def update(self, instance, validated_data): instance.logo = validated_data.get('logo', instance.logo) instance.cover = validated_data.get('cover', instance.cover) instance.size = validated_data.get('size', instance.size) if validated_data.get('industry'): new_industry = Industry.objects.get( industry_slug=validated_data['industry']['industry_slug']) instance.industry = new_industry if instance.industry != new_industry else instance.industry instance.founded = validated_data.get('founded', instance.founded) if validated_data.get('benefit') is not None: instance.benefit.clear() for item in validated_data.get('benefit'): benefit_item = Benefit.objects.get(name=item['name']) instance.benefit.add(benefit_item) if validated_data.get('city') and validated_data['city'][ 'city_slug'] != instance.city.city_slug: instance.city = City.objects.get( id=validated_data['city']['city_slug']) instance.tell = validated_data.get('tell', instance.tell) instance.site = validated_data.get('site', instance.site) instance.description = validated_data.get('description', instance.description) instance.working_hours_start = validated_data.get( 'working_hours_start', instance.working_hours_start) instance.working_hours_stop = validated_data.get( 'working_hours_stop', instance.working_hours_stop) if validated_data.get('gallery'): instance.gallery_set.update(is_deleted=True) for gallery_data in validated_data['gallery']: if gallery_data.get('id'): # Update gallery gallery = Gallery.objects.get(id=gallery_data['id']) gallery.path = gallery_data.get('path', gallery.path) gallery.description = gallery_data.get( 'description', gallery.description) gallery.save() else: gallery_data['company'] = instance gallery = Gallery(**gallery_data) gallery.save() if validated_data.get('location'): instance.location_point = Point(validated_data.pop('location')) if validated_data.get('approved'): instance.approved = validated_data.pop('approved') instance.save() cache.delete(settings.COMPANY_NAME_LIST) return instance