views.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import json
  2. from django.views.decorators.csrf import csrf_exempt
  3. from django.shortcuts import render
  4. from django.http import HttpResponse
  5. from django.utils.decorators import method_decorator
  6. from scanapp.serializers import *
  7. from rest_framework import viewsets, status
  8. from rest_framework.decorators import api_view
  9. from rest_framework.response import Response
  10. from rest_framework.permissions import IsAuthenticated, AllowAny
  11. # Create your views here.
  12. @api_view(['GET'])
  13. def get_units(request, language):
  14. units = {
  15. 'weight': [
  16. {'label': 'kg', 'value': 'kg'},
  17. {'label': 'lb', 'value': 'lb'}
  18. ],
  19. 'length': [
  20. {'label': 'm', 'value': 'm'},
  21. {'label': 'in', 'value': 'in'} ]
  22. }
  23. return Response(units)
  24. @api_view(['GET'])
  25. def get_product_types(request, pk, language):
  26. product_type = ProductType.objects.get(pk=pk)
  27. #print(product_type)
  28. product_type_hiers = ProductTypeHier.objects.filter(parent=product_type)
  29. product_children = []
  30. [product_children.append(temp.child) for temp in product_type_hiers]
  31. product_types = ProductTypeSerializer(product_children, many=True)
  32. response = Response(product_types.data)
  33. return response
  34. def download_product_picture(request, pk):
  35. image = ProductPicture.objects.get(pk=pk)
  36. response = HttpResponse()
  37. #print(image)
  38. response['X-Accel-Redirect'] = image.get_url()
  39. response['Content-Disposition'] = 'attachment; filename="{}"'.format(image.name)
  40. return response
  41. class MyViewSet(viewsets.ModelViewSet):
  42. model_class = None
  43. permission_classes = (IsAuthenticated,)
  44. def list(self, request):
  45. return super(viewsets.ModelViewSet, self).list(request)
  46. class TranslationViewSet(object):
  47. """docstring for TranslationViewSet"""
  48. def get_queryset(self):
  49. language = get_language(self.request)
  50. return self.model_class.getAll(language)
  51. def get_object(self):
  52. object = super(viewsets.ModelViewSet, self).get_object()
  53. language = get_language(self.request)
  54. return object.get_with_locale(language)
  55. class AutoSetUser(object):
  56. def perform_create(self, serializer):
  57. serializer.save(user=User.getUser(self.request.user))
  58. def create_viewset(base, name, queryset, serializer_class, model_class):
  59. return type(name, (base,), {'queryset': queryset,
  60. 'serializer_class': serializer_class, 'model_class':model_class})
  61. class ProductTypeViewSet(AutoSetUser, TranslationViewSet, MyViewSet):
  62. queryset = ProductType.objects.all()
  63. serializer_class = ProductTypeSerializer
  64. model_class = ProductType
  65. class EAN_CodeViewSet(AutoSetUser, MyViewSet):
  66. queryset = EAN_Code.objects.all()
  67. serializer_class = EAN_CodeSerializer
  68. model_class = EAN_Code
  69. lookup_field = 'barcode'
  70. class ProductViewSet(AutoSetUser, MyViewSet):
  71. queryset = Product.objects.all()
  72. write_serializer_class = ProductWriteSerializer
  73. read_serializer_class = ProductSerializer
  74. model_class = Product
  75. def get_serializer(self, *args, **kwargs):
  76. if(self.request.method == 'PUT' or self.request.method == 'PATCH' or self.request.method == 'POST'):
  77. self.serializer_class = self.write_serializer_class
  78. else:
  79. self.serializer_class = self.read_serializer_class
  80. return super(MyViewSet, self).get_serializer(*args, **kwargs)
  81. class ProductPictureViewSet(AutoSetUser, MyViewSet):
  82. queryset = ProductPicture.objects.all()
  83. serializer_class = ProductPictureSerializer
  84. model_class = ProductPicture
  85. class ProductTypeHierViewSet(AutoSetUser, MyViewSet):
  86. queryset = ProductTypeHier.objects.all()
  87. serializer_class = ProductTypeHierSerializer
  88. model_class = ProductTypeHier
  89. class ProductTypeHierMainViewSet(AutoSetUser, MyViewSet):
  90. queryset = ProductType.objects.all()
  91. serializer_class = ProductTypeHierMainSerializer
  92. model_class = ProductType
  93. class BOMViewSet(AutoSetUser, MyViewSet):
  94. queryset = BOM.objects.all()
  95. serializer_class = BOMSerializer
  96. model_class = BOM