def post(self, request): serializer = RegisterSerializers(data=request.data) if serializer.is_valid(): serializer.save() response = CustomResponse(success=True) return Response(response.get_response, status=status.HTTP_200_OK) response = CustomResponse(success=False, error=serializer.errors) return Response(response.get_response, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): decode = JwtDecode.decode(request) username = decode.get("username") user = User.objects.get(username=username) reason = None try: reason = request.data["reason"] except: reason = None finally: user_appointment = Appointment.objects.filter(user=user) appointment_count_false = user_appointment.filter( appointed=False).count() appointment_count_true = user_appointment.filter( appointed=True).count() if appointment_count_false > 0: response = CustomResponse( success=False, error={ "appointment": "Already has one appointment request" }) return Response(response.get_response, status=status.HTTP_400_BAD_REQUEST) else: if appointment_count_true == 0: appointment = Appointment(user=user, reason=reason) appointment.save() response = Response( CustomResponse(success=True).get_response, status=status.HTTP_200_OK) response['HTTP_AUTHORIZATION'] = JwtDecode.encode(username) return response else: appointment_date_passed_count = Appointment.objects.filter( user=user).filter(appointed=True).filter( day_of_appointment__gte=datetime.now()).count() if appointment_date_passed_count == 0: appointment = Appointment(user=user, reason=reason) appointment.save() response = Response( CustomResponse(success=True).get_response, status=status.HTTP_200_OK) response['HTTP_AUTHORIZATION'] = JwtDecode.encode( username) return response else: response = CustomResponse( success=False, error={ "appointment": "Already has one appointment" }) return Response(response.get_response, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): username = request.token_decode.get("username") user = User.objects.get(username=username) serializer = QuerySerializers(data=request.data) if serializer.is_valid(): serializer.save(user) response = Response(CustomResponse(success=True).get_response, status=status.HTTP_200_OK) response['HTTP_AUTHORIZATION'] = JwtDecode.encode(username) return response else: response = CustomResponse(success=False, error=serializer.errors) return Response(response.get_response, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = LoginSerializers(data=request.data) if serializer.is_valid(): username = serializer.data['username'] password = serializer.data['password'] user = auth.authenticate(username=username, password=password) if user is not None and not user.is_staff: token = JwtDecode.encode(username=username) response = CustomResponse(success=True, data={"token": token}) return Response(response.get_response, status=status.HTTP_200_OK) else: response = CustomResponse( success=False, error={"error": "Username or password not valid"}) return Response(response.get_response, status=status.HTTP_400_BAD_REQUEST) response = CustomResponse(success=False, error=serializer.errors) return Response(response.get_response, status=status.HTTP_400_BAD_REQUEST)
def save(self, user): answer = Answer( reply=self.validated_data['reply'], replied_to=self.validated_data['replied_to'], replied_by=user, ) query = Query.objects.get(id=self.data['replied_to']) if not query.closed: answer.save() else: error = CustomResponse(success=False, error={'thread': 'thread is closed'}) raise serializers.ValidationError(error.get_response)
def get(self, request): username = request.token_decode.get("username") serializer = IntSerializer(data=request.data) if serializer.is_valid(): page_num = serializer.data['page_no'] user = User.objects.get(username=username) objects = None try: replied = request.data['replied'] objects = Query.objects.filter(asked_by=user).filter( replied=replied).order_by('-asked_On') except: objects = Query.objects.filter( asked_by=user).order_by('-asked_On') finally: queryset = Paginator(objects, PAGE_SIZE) last_page = queryset.page_range[-1] if last_page >= page_num: serializer = QuestionSerializer( queryset.page(page_num).object_list, many=True) response = Response(CustomResponse( success=True, data=serializer.data, last_page=last_page).get_response, status=status.HTTP_200_OK) response['HTTP_AUTHORIZATION'] = JwtDecode.encode(username) return response else: response = CustomResponse( success=False, error={"page_no": "invalid page number"}) return Response(response.get_response, status=status.HTTP_400_BAD_REQUEST) else: response = CustomResponse(success=False, error=serializer.errors) return Response(response.get_response, status=status.HTTP_400_BAD_REQUEST)
def save(self): user = User(username=self.validated_data['username'], email=self.validated_data['email'], first_name=self.validated_data['first_name'], last_name=self.validated_data['last_name']) password = self.validated_data['password'] if password != "": user.set_password(password) user.save() return user else: error = CustomResponse( success=False, error={'password': '******'}) raise serializers.ValidationError(error.get_response)