def list(self, request, *args, **kwargs): try: queryset = super(ClienteViewSet, self).get_queryset() page = self.request.query_params.get('page', None) filter_data = self.request.query_params.get('filter_data', None) # -------------------------------------------------------------- nombre = self.request.query_params.get('nombre',None) apellido = self.request.query_params.get('apellido',None) qset=(~Q(id=0)) if (filter_data or nombre or apellido): if filter_data: qset = qset & (Q(nombre__icontains = filter_data) ) if nombre: qset = qset & (Q(nombre = nombre) ) if apellido: qset = qset & (Q(apellido = apellido) ) queryset = self.model.objects.filter(qset) if page: pagination = self.paginate_queryset(queryset) if pagination is not None: serializer = self.get_serializer(pagination, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset,many=True) return Structure.success_200('', serializer.data) except Exception as e: return Structure.error_500()
def create(self, request, *args, **kwargs): if request.method == 'POST': try: json_data = json.loads(request.body) serializer = self.serializer_class( data=json_data, context={'request': request}) if serializer.is_valid(): serializer.save(cliente_id=json_data["cliente_id"], estado_id=json_data["estado_id"]) bulk_mgr = BulkCreateManager(chunk_size=1) for row in json_data["articulos"]: bulk_mgr.add( PedidoArticulo(pedido_id=serializer.data['id'], articulo_id=row['id'], cantidad=row['cantidad'])) bulk_mgr.done() return Structure.success_201( 'El pedido #{} ha sido registrado exitosamente.'. format(serializer.data["id"]), serializer.data) else: print(serializer.errors) return Structure.fail('', serializer.errors) except Exception as e: print(e) return Structure.error_500()
def list(self, request, *args, **kwargs): try: queryset = super(PedidoViewSet, self).get_queryset() page = self.request.query_params.get('page', None) filter_data = self.request.query_params.get('filter_data', None) # -------------------------------------------------------------- cliente_id = self.request.query_params.get('cliente_id', None) is_active = self.request.query_params.get('is_active', True) qset = (~Q(id=0)) if (filter_data or cliente_id or is_active): if filter_data: qset = qset & (Q(name__icontains=filter_data)) if cliente_id: qset = qset & (Q(cliente__id=cliente_id)) if is_active: qset = qset & (Q(is_active=is_active)) queryset = self.model.objects.filter(qset) if page: pagination = self.paginate_queryset(queryset) if pagination is not None: serializer = self.get_serializer(pagination, many=True) return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) return Structure.success_200('', serializer.data) except Exception as e: print(e) return Structure.error_500()
def update(self, request, *args, **kwargs): if request.method == 'PUT': try: partial = kwargs.pop('partial', False) instance = self.get_object() json_data = json.loads(request.body) serializer = self.serializer_class( instance, data=json_data, context={'request': request}, partial=partial) if serializer.is_valid(): serializer.save(articulo_id=json_data['articulo_id'], cliente_id=json_data['cliente_id'], estado_id=json_data['estado_id']) return Structure.success_201( 'El pedido ha sido actualizado exitosamente.', serializer.data) else: return Structure.fail('', serializer.errors) except Exception as e: return Structure.error_500()
def retrieve(self,request,*args, **kwargs): try: instance = self.get_object() serializer = self.get_serializer(instance) return Structure.success_200('', serializer.data) except Exception as e: return Structure.error_400('No se encontraron registros')
def destroy(self, request, *args, **kwargs): if request.method == 'DELETE': try: instance = self.get_object() self.perform_destroy(instance) serializer = self.get_serializer(instance) return Structure.success_204( 'El articulo ha sido eliminado exitosamente.', serializer.data) except Exception as e: return Structure.error_500()
def delete(self, request, *args, **kwargs): if request.method == 'PUT': json_data = json.loads(request.body) try: index = json_data["index"] pedido = Pedido.objects.filter(pk__in=index).update( is_active=False) return Structure.success_201( 'Los pedidos han sido eliminado exitosamente.', '') except Exception as e: return Structure.error_500()
def create(self, request, *args, **kwargs): if request.method == 'POST': try: json_data = json.loads(request.body) serializer = self.serializer_class(data=json_data,context={'request': request}) if serializer.is_valid(): serializer.save() return Structure.success_201('El cliente ha sido registrado exitosamente.', serializer.data) else: return Structure.fail('', serializer.errors) except Exception as e: print(e) return Structure.error_500()
def create(self, request, *args, **kwargs): if request.method == 'POST': try: # Validate serializer = self.serializer_class( data=request.data, context={'request': request}) if serializer.is_valid(): serializer.save(marca_id=request.data['marca_id']) return Structure.success_201( 'El articulo ha sido registrado exitosamente.', serializer.data) else: return Structure.fail('', serializer.errors) except Exception as e: return Structure.error_500()