def get(self, request, link_type, *args, **kwargs): instance = DownloadLinkModel.objects.get(pk=1) if link_type == 'link-patch': res_data = instance.link_patch else: res_data = instance.link_full return res_by_gzip(gen_res('success', 200, res_data, link_type))
def post(self, request, *args, **kwargs): payload = request.data.copy() # VALIDATE birthdate try: payload['birthdate'] = datetime.fromisoformat( payload['birthdate'].replace('Z', '+00:00')).replace( tzinfo=timezone.utc).astimezone(tz=None).date() except KeyError: return Response(gen_res(self.status_ref[1], 400, detail=self.error_ref[1]), status=400) serializer = RegisterAccountSerializer(data=payload) if serializer.is_valid(): # VALIDATE userid try: existed_instance = Login.objects.get(userid=payload['userid']) return Response(gen_res(self.status_ref[2], 400, detail=self.error_ref[2]), status=400) except Login.DoesNotExist: pass # VALIDATE email try: existed_instance = Login.objects.get(email=payload['email']) return Response(gen_res(self.status_ref[2], 400, detail=self.error_ref[3]), status=400) except Login.DoesNotExist: pass # CREATE instance instance = Login(**serializer.validated_data) instance.save() return res_by_gzip(gen_res(self.status_ref[0], 200)) return Response(gen_res(self.status_ref[1], 400, detail=self.error_ref[4]), status=400)
def patch(self, request, link_type, *args, **kwargs): instance = DownloadLinkModel.objects.get(pk=1) payload = request.data.copy() if link_type == 'link-full': try: DownloadLinkModel.objects.filter(pk=1).update( link_full=payload['link_full']) except KeyError: return Response( gen_res('fail', 400, {}, 'update link_full fail')) elif link_type == 'link-patch': try: DownloadLinkModel.objects.filter(pk=1).update( link_patch=payload['link_patch']) except KeyError: return Response( gen_res('fail', 400, {}, 'update link_patch fail')) response_data = { 'link_full': instance.link_full, 'link_patch': instance.link_patch } return res_by_gzip( gen_res('success', 200, response_data, 'update success'))
def post(self, request, *args, **kwargs): payload = request.data.copy() serializer = LoginSerializer(data=payload) print('aaaaaaaaaaaa') if serializer.is_valid(): user = get_object_or_404( Login, userid=serializer.validated_data['userid']) if user and user.user_pass == serializer.validated_data[ 'user_pass']: created_at = dj_timezone.now() expired_at = dj_timezone.now() + timedelta(minutes=15) token = jwt.encode( { 'id': user.account_id, 'exp_iso': expired_at.isoformat() }, private_key, algorithm='RS256') try: _token = Token.objects.get(user=user) _token.token = token.decode('utf-8') _token.created_at = created_at _token.expired_at = expired_at except Token.DoesNotExist: _token = Token(user=user, token=token.decode('utf-8')) _token.save() return res_by_gzip( gen_res(self.status_ref[0], 200, token.decode('utf-8'))) return Response(gen_res(self.status_ref[1], 400, detail=self.error_ref[4]), status=400) return Response(gen_res(self.status_ref[1], 400, detail=self.error_ref[4]), status=400)
def get(self, request, *args, **kwargs): serializer = AccountInfoSerializer(request.user) return res_by_gzip(gen_res('success', 200, dict(serializer.data)))
def create(self, request): response = { 'name': 'Hằng', 'properties': 'Quí' } return res_by_gzip(response)
def dataView(request): response = { 'data': 'Hằng cute', 'status': 'active' } return res_by_gzip(response)
def defaultView(request): response = { 'name': 'Hằng', 'properties': 'Quí' } return res_by_gzip(response)