def register(request): data = JSONParser().parse(request) serializer = RegisterSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(getStandardResponse(200)) return Response(getStandardResponse(500, serializer.errors))
def activateuser(request): activate_code = request.data.get('activecode', '') if len(activate_code) > 0: activate_user_handle(activate_code.strip()) return Response(getStandardResponse(200, 'user activated successfully')) return Response(getStandardResponse(500, 'user activated failed!'))
def login(request): username = request.data.get("username", "") password = request.data.get("password", "") user = authenticate(username=username, password=password) if not user: return Response(getStandardResponse(400, 'username or password wrong!')) token, _ = Token.objects.get_or_create(user=user) update_last_login(None, user) return Response(getStandardResponse(200, '', {'token': token.key}))
def put(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if serializer.is_valid(): # Check old password if not self.get_object().check_password( serializer.data.get("oldPassword")): return Response(getStandardResponse(400, 'old password wrong')) # set_password also hashes the password that the user will get self.get_object().set_password(serializer.data.get("newPassword")) self.get_object().save() return Response(getStandardResponse(200, 'password changed')) return Response(getStandardResponse(500, serializer.errors))
def resetpassword(request): username = request.data.get("username", "") user = User.objects.filter(username=username).first() if user is None: return Response( getStandardResponse(400, "User " + username + " not found!")) new_pass = getRandomNo() user.set_password(new_pass) if not user.is_active: user.is_active = True user.save() sendNewPassw(username, new_pass) return Response(getStandardResponse(200, 'password changed'))
def whs_exception_handler(exc, context): # Call REST framework's default exception handler first, # to get the standard error response. if isinstance(exc, Http404): exc = exceptions.NotFound() elif isinstance(exc, PermissionDenied): exc = exceptions.PermissionDenied() logger.error(str(exc)) return Response(getStandardResponse(500, str(exc)))
def dashboard(request): data = {} # todo return Response(getStandardResponse(200, '', data))
def logout(request): request.user.auth_token.delete() return Response(getStandardResponse(200, 'logout sucess'))