class ContactSerializer(serializers.ModelSerializer): company = serviceSerializer(many=False, read_only=True) class Meta: model = Contact fields = ('pk', 'user', 'name', 'created', 'updated', 'company', 'email', 'emailSecondary', 'mobile', 'mobileSecondary', 'designation', 'notes', 'linkedin', 'facebook', 'dp', 'male') read_only_fields = ('user', ) def create(self, validated_data): c = Contact(**validated_data) c.user = self.context['request'].user if 'company' in self.context['request'].data: c.company_id = int(self.context['request'].data['company']) c.save() return c def update(self, instance, validated_data): for key in [ 'name', 'email', 'emailSecondary', 'mobile', 'mobileSecondary', 'designation', 'notes', 'linkedin', 'facebook', 'dp', 'male' ]: try: setattr(instance, key, validated_data[key]) except: pass if 'company' in self.context['request'].data: instance.company_id = int(self.context['request'].data['company']) instance.save() return instance
class VendorServicesLiteSerializer(serializers.ModelSerializer): service = serviceSerializer(many=False, read_only=True) vendor = VendorProfileSerializer(many=False, read_only=True) class Meta: model = VendorServices fields = ('pk', 'vendor', 'product', 'rate', 'fulfilmentTime', 'logistics', 'service')
class VendorProfileSerializer(serializers.ModelSerializer): service = serviceSerializer(many=False, read_only=True) class Meta: model = VendorProfile fields = ('pk', 'created', 'updated', 'service', 'contactDoc', 'paymentTerm', 'contactPersonName', 'contactPersonNumber', 'contactPersonEmail') def create(self, validated_data): p = VendorProfile(**validated_data) p.service = service.objects.get( pk=self.context['request'].data['service']) try: p.save() except: raise ValidationError( detail={'PARAMS': 'Service Profile already exists'}) return p
class InvoiceSerializer(serializers.ModelSerializer): service = serviceSerializer(many=False, read_only=True) class Meta: model = Invoice fields = ('pk', 'user', 'created', 'service', 'code', 'amount', 'currency', 'dated', 'attachment', 'sheet', 'description', 'approved') read_only_fields = ('user', ) def create(self, validated_data): u = self.context['request'].user print 'came to create an Invoce', u inv = Invoice(**validated_data) inv.user = u inv.service = service.objects.get( pk=self.context['request'].data['service']) inv.approved = False inv.save() return inv def update(self, instance, validated_data): # if the user is manager or something then he can update the approved flag reqData = self.context['request'].data if 'approved' in reqData: instance.approved = validated_data.pop('approved') instance.save() return instance if 'service' in reqData: instance.service = service.objects.get(pk=reqData['service']) if 'dated': dateStr = reqData['dated'] instance.dated = datetime.strptime(dateStr, '%Y-%m-%d').date() for f in ['amount', 'currency', 'sheet', 'description']: setattr(instance, f, validated_data.pop(f)) if 'attachment' in reqData: instance.attachment = validated_data.pop('attachment') instance.save() # print instance.service return instance