def invoice_item_image(request, ack_id=None): if request.method.lower() == "post": try: credentials = request.user.aws_credentials key = credentials.access_key_id secret = credentials.secret_access_key except AttributeError as e: logger.error(e) key = '' secret = '' filename = save_upload(request) if ack_id: key = u"invoice/{0}/item/image/{1}".format(ack_id, filename.split('/')[-1]) else: key = u"invoice/item/image/{0}".format(filename.split('/')[-1]) obj = S3Object.create(filename, key, 'media.dellarobbiathailand.com', key, secret) serializer = S3ObjectSerializer(obj) response = HttpResponse(JSONRenderer().render(serializer.data), content_type="application/json") response.status_code = 201 return response # If any method other than POST else: response = HttpResponse('{"message": "Not Allowed"}', content_type='application/json; charset=utf-8') response.status_code = 405 return response
def po_file(request, po_id=None): if request.method == "POST": try: credentials = request.user.aws_credentials key = credentials.access_key_id secret = credentials.secret_access_key except AttributeError as e: logger.error(e) key = '' secret = '' filename = save_upload(request) if po_id: key = u"purchase_order/{0}/files/{1}".format( po_id, filename.split('/')[-1]) else: key = u"purchase_order/files/{0}".format(filename.split('/')[-1]) obj = S3Object.create(filename, key, u"document.dellarobbiathailand.com", key, secret) serializer = S3ObjectSerializer(obj) response = HttpResponse(JSONRenderer().render(serializer.data), content_type="application/json") response.status_code = 201 return response # If any method other than POST else: response = HttpResponse('{"message": "Not Allowed"}', content_type='application/json; charset=utf-8') response.status_code = 405 return response
class SupplySerializer(serializers.ModelSerializer): quantity = serializers.DecimalField(decimal_places=2, max_digits=12, required=False) description_th = serializers.CharField(required=False, allow_null=True, allow_blank=True) notes = serializers.CharField(required=False, allow_null=True, allow_blank=True) type = serializers.CharField(required=False, allow_null=True, allow_blank=True) suppliers = ProductSerializer(source="products", required=False, many=True, allow_null=True) employee = EmployeeFieldSerializer(write_only=True, required=False, allow_null=True) acknowledgement = AcknowledgementFieldSerializer(write_only=True, required=False, allow_null=True) quantity = serializers.DecimalField(decimal_places=2, max_digits=12, required=False, allow_null=True) id = serializers.IntegerField(required=False) status = serializers.CharField(required=False, allow_null=True, allow_blank=True) image = S3ObjectSerializer(required=False, allow_null=True) sticker = S3ObjectSerializer(read_only=True, required=False, allow_null=True) # Method Fields #supplier = serializers.SerializerMethodField() supplier = None unit_cost = serializers.SerializerMethodField() cost = serializers.SerializerMethodField() reference = serializers.SerializerMethodField() class Meta: model = Supply list_serializer_class = SupplyListSerializer #read_only_fields = ['suppliers'] exclude = ['quantity_th', 'quantity_kh', 'shelf'] def __init__(self, *args, **kwargs): try: # Set the supplier first so the other fields can be set if 'supplier_id' in kwargs['context']['request'].query_params: supplier_id = kwargs['context']['request'].query_params['supplier_id'] self.supplier = Supplier.objects.get(pk=supplier_id) except Exception as e: logger.warn(e) # Call parent init return super(SupplySerializer, self).__init__(*args, **kwargs) def to_internal_value(self, data): ret = super(SupplySerializer, self).to_internal_value(data) # Serializing associated data library = {'employee': Employee, 'acknowledgement': Acknowledgement, 'image': S3Object} for key in library: try: ret[key] = library[key].objects.get(pk=data[key]['id']) except (library[key].DoesNotExist, KeyError, TypeError) as e: logger.warn(e) return ret def to_representation(self, instance): """ Override the 'to_representation' method to allow integration of products into output data """ ret = super(SupplySerializer, self).to_representation(instance) bulk_qp = bool(self.context['request'].query_params.get('bulk', False)) bulk_flag = self.context.get('bulk', False) pk = self.context['view'].kwargs.get('pk', False) request_m = True if self.context['request'].method.lower() == 'get' else False if (pk and request_m) or bulk_qp or bulk_flag: del ret['suppliers'] #product_serializer = ProductSerializer(instance.products.all(), many=True) #logger.debug(product_serializer.data) """ products = instance.products.all() products = products.prefetch_related('supplier') ret['suppliers'] = [{'id': product.id, 'supplier': {'id': product.supplier.id, 'name': product.supplier.name}, 'cost': product.cost, 'reference': product.reference, 'purchasing_units': product.purchasing_units, 'quantity_per_purchasing_unit': product.quantity_per_purchasing_unit, 'upc': product.upc} for product in products] if len(ret['suppliers']) == 1: ret['cost'] = ret['suppliers'][0]['cost'] ret['unit_cost'] = ret['suppliers'][0]['cost'] """ return ret def create(self, validated_data): """ Override the 'create' method in order to customize creation of products """ if 'supplier' in validated_data: suppliers_data = [validated_data.pop('supplier')] suppliers_data = [self.initial_data['supplier']] elif 'suppliers' in validated_data: suppliers_data = validated_data.pop('suppliers') logger.debug(self.initial_data) suppliers_data = self.initial_data.get('suppliers', suppliers_data) elif 'products' in validated_data: suppliers_data = validated_data.pop('products') suppliers_data = self.initial_data.get('suppliers', suppliers_data) else: data = {} for field in ['cost', 'reference', 'purchasing_units', 'quantity_per_purchasing_units', 'upc']: try: logger.debug(field) logger.debug(self.context['request']) data[field] = self.context['request'].data[field] except KeyError: pass except TypeError: try: data[field] = self.context['request'].data[self.context['index']][field] except KeyError: pass try: data['supplier'] = self.context['request'].data['supplier'] except KeyError: try: data['supplier'] = self.context['request'].data['suppliers'][0] except KeyError as e: logger.warn(e) except TypeError as e: try: data['supplier'] = self.context['request'].data[self.context['index']]['supplier'] except KeyError: try: data['supplier'] = self.context['request'].data[self.context['index']]['suppliers'][0] except KeyError: pass suppliers_data = [data] instance = self.Meta.model.objects.create(**validated_data) #instance.create_stickers(key, secret) product_serializer = ProductSerializer(data=suppliers_data, context={'supply': instance}, many=True) if product_serializer.is_valid(raise_exception=True): product_serializer.save() return instance def update(self, instance, validated_data): """ Override the 'update' method in order to customize create, update and delete of products """ try: products_data = validated_data.pop('suppliers') except KeyError: products_data = validated_data.pop('products', None) # Extract other data that is part of the product info old_quantity = instance.quantity new_quantity = validated_data.get('quantity', instance.quantity) # Associated data for the logs employee = validated_data.pop('employee', None) acknowledgement = validated_data.pop('acknowledgement', None) for field in validated_data.keys(): setattr(instance, field, validated_data[field]) if products_data: product_serializer = ProductSerializer(data=products_data, context={'supply': instance}, many=True) if product_serializer.is_valid(raise_exception=True): product_serializer.save() instance.save() assert instance.quantity == new_quantity self._log_quantity(instance, old_quantity, new_quantity, employee, acknowledgement) assert instance.quantity == new_quantity return instance def get_supplier(self, obj): """Get Supplier Returns a supplier if the supplier_id is in the query params """ if 'supplier_id' in self.context['request'].query_params: supplier_id = self.context['request'].query_params['supplier_id'] return Supplier.objects.get(pk=supplier_id) else: return None def get_unit_cost(self, obj): """Get Unit Cost Returns a unit cost if the supplier is set """ if self.supplier: obj.supplier = self.supplier return obj.cost else: return Decimal('0') def get_cost(self, obj): """Get Cost Returns a cost if the supplier is set """ if self.supplier: obj.supplier = self.supplier return obj.cost else: return Decimal('0') def get_reference(self, obj): """Get Reference Returns a reference if the supplier is set """ if self.supplier: obj.supplier = self.supplier return obj.reference else: return u'' def _log_quantity(self, obj, old_quantity, new_quantity, employee=None, acknowledgement=None): """ Internal method to apply the new quantity to the obj and create a log of the quantity change """ new_quantity = Decimal(str(new_quantity)) #Type change to ensure that calculations are only between Decimals old_quantity = Decimal(str(old_quantity)) if new_quantity < 0: raise ValueError('Quantity cannot be negative') if new_quantity != old_quantity: if new_quantity > old_quantity: action = 'ADD' diff = new_quantity - old_quantity elif new_quantity < old_quantity: action = 'SUBTRACT' diff = old_quantity - new_quantity #Create log to track quantity changes log = Log(supply=obj, action=action, quantity=diff, employee=employee, acknowledgement=acknowledgement, message=u"{0}ed {1}{2} {3} {4}".format(action.capitalize(), diff, obj.units, "to" if action == "ADD" else "from", obj.description)) #Save log log.save()