def update(self, instance, validated_data): if 'file' in validated_data: file = validated_data.pop('file') # delete old one media = AbMedias.objects.filter(mediable_type='App\\Category', mediable_id=instance.id) if media.exists(): media.first()._delete_file() media.first().delete() AbMedias(mediable_type='App\\Category', mediable_id=instance.id, path='Category', original_file_name=file, is_active=1, accessibility=1, is_featured=0).save() else: media = AbMedias.objects.filter(mediable_type='App\\Category', mediable_id=instance.id) if media.exists(): media.first()._delete_file() media.first().delete() instance.description = validated_data.get('description', instance.description) instance.name = validated_data.get('name', instance.name) instance.is_active = validated_data.get('is_active', instance.is_active) instance.save() return instance
def update(self, instance, validated_data): if 'file' in validated_data: file = validated_data.pop('file') media = AbMedias.objects.filter( mediable_type='App\\Advertisements', mediable_id=instance.id) if media.exists(): media.first()._delete_file() media.first().delete() AbMedias(mediable_type='App\\Advertisements', mediable_id=instance.id, path='Advertisements', original_file_name=file, is_active=1, accessibility=1, is_featured=0).save() else: media = AbMedias.objects.filter( mediable_type='App\\Advertisements', mediable_id=instance.id) if media.exists(): media.first()._delete_file() media.first().delete() return super(AdvertisementsSerializer, self).update(instance, validated_data)
def create(self, validated_data): if 'file' in validated_data: file = validated_data.pop('file') company = super(CompanySerializer, self).create(validated_data) AbMedias(mediable_type='App\\Company', mediable_id=company.id, path='Company', original_file_name=file, is_active=1, accessibility=1, is_featured=0).save() else: company = super(CompanySerializer, self).create(validated_data) return company
def create(self, validated_data): if 'file' in validated_data: file = validated_data.pop('file') seos = super(SeoSerializer, self).create(validated_data) AbMedias(mediable_type='App\\Seo', mediable_id=seos.id, path='Seo', original_file_name=file, is_active=1, accessibility=1, is_featured=0).save() else: seos = super(SeoSerializer, self).create(validated_data) return seos
def retrieve(self, request: Request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) data = serializer.data data['media'] = AbMedias.get_media(instance, 'App\\Company') related_models = ['country','city','state'] for model in related_models: try: data[model] = to_dict(getattr(instance, model)) except: data[model] = None return Response(data)
def create(self, validated_data): if 'file' in validated_data: file = validated_data.pop('file') advertisements = super(AdvertisementsSerializer, self).create(validated_data) AbMedias(mediable_type='App\\Advertisements', mediable_id=advertisements.id, path='Advertisements', original_file_name=file, is_active=1, accessibility=1, is_featured=0).save() else: advertisements = super(AdvertisementsSerializer, self).create(validated_data) return advertisements
def create(self, validated_data): if 'file' in validated_data: file = validated_data.pop('file') category = AbCategories.objects.create(**validated_data) AbMedias(mediable_type='App\\Category', mediable_id=category.id, path='Category', original_file_name=file, is_active=1, accessibility=1, is_featured=0).save() else: category = AbCategories.objects.create(**validated_data) return category
def create(self, validated_data): if 'file' in validated_data: file = validated_data.pop('file') type = super(TypesSerializer, self).create(validated_data) AbMedias( mediable_type='App\\Type', mediable_id=type.id, path='Type', original_file_name=file, is_active=1, accessibility=1, is_featured=0 ).save() else: type = super(TypesSerializer, self).create(validated_data) return type
def create(self, validated_data): if 'file' in validated_data: file = validated_data.pop('file') manufacturer = super(ManufacturersSerializer, self).create(validated_data) AbMedias( mediable_type='App\\Manufacturer', mediable_id=manufacturer.id, path='Manufacturer', original_file_name=file, is_active=1, accessibility=1, is_featured=0 ).save() else: manufacturer = super(ManufacturersSerializer, self).create(validated_data) return manufacturer
def create(self, validated_data): validated_data['creator'] = self.context['request'].user if 'file' in validated_data: file = validated_data.pop('file') contact = super(ContactSerializer, self).create(validated_data) AbMedias(mediable_type='App\\Contact', mediable_id=contact.id, path='Contact', original_file_name=file, is_active=1, accessibility=1, is_featured=0).save() else: contact = super(ContactSerializer, self).create(validated_data) return contact
def to_representation(self, instance): representation = super(PlansSerializer, self).to_representation(instance) try: representation['media'] = AbMedias.get_media( instance, 'App\\Plans') except: representation['media'] = None try: if instance.id == 2: print(self.context['request'].user.abcredits_set) representation['credits'] = CreditsSerializer( self.context['request'].user.abcredits_set.filter( plan=instance, is_active=1).order_by('-id')[:3], many=True).data except: representation['credits'] = None return representation
def create(self, validated_data): customs_datas = validated_data.pop('custom') file = None if 'file' in validated_data: file = validated_data.pop('file') plans = AbPlans.objects.create(**validated_data) for customs_data in customs_datas: AbCustom.objects.create(plan=plans, **customs_data) if file is not None: # plans = super(PlansSerializer, self).create(validated_data) AbMedias(mediable_type='App\\Plans', mediable_id=plans.id, path='Plans', original_file_name=file, is_active=1, accessibility=1, is_featured=0).save() return plans
def retrieve(self, request: Request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) data = serializer.data # serializer['companies'] = AbCompanies.available_companies(instance) data['media'] = AbMedias.get_media(instance, 'App\\Contact') try: data['user'] = to_dict(instance.user) try: data['user']['contact'] = to_dict(instance.user.contact) except: data['user']['contact'] = None except: data['user'] = None related_models = ['department','country','city','state'] for model in related_models: try: data[model] = to_dict(getattr(instance, model)) except: data[model] = None return Response(data)
def update(self, instance, validated_data): customs_data = validated_data.pop('custom') customs_dict = dict((i.id, i) for i in instance.custom.all()) for custom in customs_data: if 'id' in custom: custom_obj = customs_dict.pop(custom['id']) custom_obj.label = custom.get('label', custom_obj.label) custom_obj.value = custom.get('value', custom_obj.value) custom_obj.save() else: AbCustom.objects.create(plan=instance, **custom) # delete remaining elements because they're not present in my update call if len(customs_dict) > 0: for custom in customs_dict.values(): custom.delete() if 'file' in validated_data: file = validated_data.pop('file') media = AbMedias.objects.filter(mediable_type='App\\Plans', mediable_id=instance.id) if media.exists(): media.first()._delete_file() media.first().delete() AbMedias(mediable_type='App\\Plans', mediable_id=instance.id, path='Plans', original_file_name=file, is_active=1, accessibility=1, is_featured=0).save() instance.title = validated_data.get('title', instance.title) instance.sub_title = validated_data.get('sub_title', instance.sub_title) instance.price_label = validated_data.get('price_label', instance.price_label) instance.price = validated_data.get('price', instance.price) instance.discount = validated_data.get('discount', instance.discount) instance.button_label = validated_data.get('button_label', instance.button_label) instance.aircraft_label = validated_data.get('aircraft_label', instance.aircraft_label) instance.aircraft_value = validated_data.get('aircraft_value', instance.aircraft_value) instance.engine_label = validated_data.get('engine_label', instance.engine_label) instance.engine_value = validated_data.get('engine_value', instance.engine_value) instance.apu_label = validated_data.get('apu_label', instance.apu_label) instance.apu_value = validated_data.get('apu_value', instance.apu_value) instance.wanted_label = validated_data.get('wanted_label', instance.wanted_label) instance.wanted_value = validated_data.get('wanted_value', instance.wanted_value) instance.parts_label = validated_data.get('parts_label', instance.parts_label) instance.parts_value = validated_data.get('parts_value', instance.parts_value) instance.is_active = validated_data.get('is_active', instance.is_active) instance.save() return instance
def retrieve(self, request: Request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) data = serializer.data data['media'] = AbMedias.get_media(instance, 'App\\Country') return Response(data)