Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. from django.contrib.auth.models import User
  2. from django.shortcuts import render
  3. from rest_framework.authentication import TokenAuthentication, SessionAuthentication, BasicAuthentication
  4. from rest_framework.parsers import FileUploadParser, MultiPartParser
  5. from rest_framework.permissions import IsAuthenticated
  6. from ims.custom_authentication import CustomAuthentication
  7. # Create your views here.
  8. from rest_framework import viewsets
  9. from contacts.models import Contacs, ContactGroup
  10. from contacts.serializers import *
  11. from django.http import HttpResponse
  12. from django.views.decorators.csrf import csrf_exempt
  13. from rest_framework.views import APIView
  14. from rest_framework.response import Response
  15. from user.serializers import *
  16.  
  17.  
  18. import json
  19. import logging
  20.  
  21.  
  22.  
  23. class ContacsViewSet(viewsets.ModelViewSet):
  24. authentication_classes = [
  25. TokenAuthentication, SessionAuthentication, BasicAuthentication, CustomAuthentication]
  26. permission_classes = [IsAuthenticated]
  27.  
  28. """
  29. API endpoint that allows users to be viewed or edited.
  30. """
  31. ContacsSerializer.Meta.fields = ['id', 'fullname', 'user_id']
  32. queryset = Contacs.objects.all()
  33. serializer_class = ContacsSerializer
  34.  
  35.  
  36. class ContactGroupViewSet(viewsets.ModelViewSet):
  37. authentication_classes = [
  38. TokenAuthentication, SessionAuthentication, BasicAuthentication, CustomAuthentication]
  39. permission_classes = [IsAuthenticated]
  40.  
  41. """
  42. API endpoint that allows users to be viewed or edited.
  43. """
  44. queryset = ContactGroup.objects.all()
  45. serializer_class = ContactGroupSerializer
  46.  
  47.  
  48. class List(APIView):
  49. authentication_classes = [
  50. TokenAuthentication, SessionAuthentication, BasicAuthentication, CustomAuthentication]
  51. permission_classes = [IsAuthenticated]
  52.  
  53. def get(self, format=None, Many=True):
  54. data = Contacs.objects.all()
  55. serializer = ContacsSerializer(data, many=True)
  56. return Response(serializer.data)
  57.  
  58.  
  59. class ListContactName(APIView):
  60. authentication_classes = [
  61. TokenAuthentication, SessionAuthentication, BasicAuthentication, CustomAuthentication]
  62. permission_classes = [IsAuthenticated]
  63.  
  64. def get(self, format=None, Many=True):
  65. data = Contacs.objects.values('fullname')
  66. serializer = ContacsSerializer(data, many=True)
  67. return Response(serializer.data)
  68.  
  69.  
  70. class ListContacsByCreated(APIView):
  71. authentication_classes = [
  72. TokenAuthentication, SessionAuthentication, BasicAuthentication, CustomAuthentication]
  73. permission_classes = [IsAuthenticated]
  74.  
  75. def get(self, format=None):
  76. tanggal = self.request.data.get('tanggal', False)
  77. data = Contacs.objects.filter(dtm_created=tanggal)
  78. serializer = ContacsSerializer(data, many=True)
  79. return Response(serializer.data)
  80.  
  81.  
  82. class Register(APIView):
  83. parser_classes = (MultiPartParser,)
  84. def post(self, request, format=None):
  85.  
  86. user = User()
  87. user.first_name = request.POST.get('first_name')
  88. user.last_name = request.POST.get('last_name')
  89. user.username = request.POST.get('username')
  90. user.email = request.POST.get('email')
  91. user.password = request.POST.get('password')
  92. user.is_superuser = True
  93. user.is_staff = True
  94. user.is_active = True
  95.  
  96. serializer = UserSerializer(data=request.data)
  97. if serializer.is_valid():
  98. serializer.save()
  99.  
  100. ud = UserDetail()
  101.  
  102. users = User.objects.last()
  103. ud.user_id = users.id
  104. ud.business_type = BusinessType.objects.get(id=request.POST.get('business_type'))
  105. ud.business_name = request.POST.get('business_name')
  106. ud.business_category = BusinessCategory.objects.get(id=request.POST.get('business_category'))
  107. ud.mobile_number = request.POST.get('mobile_number')
  108. ud.email_address = request.POST.get('email_address')
  109. ud.document = request.FILES['document']
  110.  
  111. serializers = UserDetailSerializer(data=request.data)
  112. if serializers.is_valid():
  113. serializers.save()
  114. return Response({
  115. 'rc': '00',
  116. 'rm': 'Success'
  117. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement