def put(self, request, username, format=None): user = helpers.get_user_by_username(self, request, username) serializer = UserSerializer(user, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): serializer = UserSerializer(data=request.data) if serializer.is_valid(): serializer.save() # Update the password so that its hashed. user = CustomUser.objects.get(username=request.data.get("username")) user.set_password(request.data.get("password")) user.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): serializer = UserSerializer(data=request.data) if serializer.is_valid(): serializer.save() # Update the password so that its hashed. user = CustomUser.objects.get( username=request.data.get('username')) user.set_password(request.data.get('password')) user.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): user_data = request.data.copy() if user_data.get('password'): hased_password = Helper.get_hashed_password( user_data.get('password')) user_data['password'] = hased_password serialized_user = UserSerializer(data=user_data) if serialized_user.is_valid(): serialized_user.save() return Response(serialized_user.data, status=status.HTTP_201_CREATED) return Response(serialized_user.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): user = self.get_object(pk) user_request_data = request.data.copy() if user_request_data.get('password'): hased_password = Helper.get_hashed_password( user_request_data.get('password')) user_request_data['password'] = hased_password serialized_user = UserSerializer(user, user_request_data) if serialized_user.is_valid(): serialized_user.save() return Response(serialized_user.data) return Response(serialized_user.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format='json'): serializer = UserSerializer(data=request.data) if serializer.is_valid(): user = authenticate(username=serializer.data['username'], password=serializer.data['password']) if user is not None: token = Token.objects.get(user=user) group = user.groups.all()[0].name json = serializer.data json['token'] = token.key json['type'] = group return Response(json, status=status.HTTP_200_OK) return Response(status=status.HTTP_401_UNAUTHORIZED) return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
def post(self, request, format='json'): return Response('hello') serializer = UserSerializer(data=request.data) if serializer.is_valid(): user = serializer.save() if user: "******" token = Token.objects.create(user=user) json = serializer.data json['token'] = token.key return Response(json, status=status.HTTP_201_CREATED) "creation of token after sucessiful registration" return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def my_jwt_response_handler(token, user=None, request=None): return { 'token': token, 'user': UserSerializer(user, context={ 'request': request }).data }
def paginated_posts(self, obj): request = self.context['request'] display_type = obj.display_type list = None if display_type == SideBar.DISPLAY_HTML: return None elif display_type == SideBar.DISPLAY_LATEST: post = Post.lastes_posts() serializer = PostSerializer(post, many=True, context={'request': request}) list = serializer.data elif display_type == SideBar.DISPLAY_HOT: post = Post.hot_posts() serializer = PostSerializer(post, many=True, context={'request': request}) list = serializer.data elif display_type == SideBar.DISPLAY_COMMENT: comment = Comment.objects.filter(status=Comment.STATUS_NORMAL) serializer = CommentSerializer(comment, many=True, context={'request': request}) list = serializer.data elif display_type == SideBar.DISPLAY_Author: if request.user is None: list = [] else: serializer = UserSerializer(request.user, ) list = [serializer.data] return list
def post(self, request): signup_serializer = UserSignUpSerializer(data = request.data) if not signup_serializer.is_valid(): return Response(signup_serializer.errors, status = HTTP_400_BAD_REQUEST) user = authenticate( username = signup_serializer.data['username'], password = signup_serializer.data['password'] ) print(signup_serializer.data) if not user: user = User.objects.create_user(username = signup_serializer.data['username'], password = signup_serializer.data['password'], first_name = signup_serializer.data['first_name'], last_name = signup_serializer.data['last_name'], email = signup_serializer.data['email'], ) token, _ = Token.objects.get_or_create(user = user) user_serialized = UserSerializer(user) return Response({ 'user': user_serialized.data, 'expires_in': expires_in(token), 'token': token.key }, status=HTTP_200_OK) return Response({'detail': 'Username exists'}, status=HTTP_404_NOT_FOUND)
def create(self, request, *args, **kwargs): user = request.user admin = Group.objects.get(name="admin") teacher = Group.objects.get(name="teacher") serializer = UserSerializer(data=request.data) serializer.is_valid(raise_exception=True) if admin in user.groups.all(): pass elif teacher in user.groups.all(): if request.data.get("type") != "student": return Response({"error": "Operation not allowed"}, status=status.HTTP_401_UNAUTHORIZED) else: return Response({"error": "Operation not allowed"}, status=status.HTTP_401_UNAUTHORIZED) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
class PostSerializer(serializers.ModelSerializer): owner = UserSerializer(many=False,read_only=True) category = CategorySerializer() tag = TagSerializer(many=True) create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S') def create(self, validated_data): return super(PostSerializer, self).create(self,validated_data) class Meta: model = Post fields = ['id','title','category','tag','owner','create_time','desc']
def list(self, request, *args, **kwargs): user = request.user queryset = User.objects.none() admin = Group.objects.get(name="admin") student = Group.objects.get(name="student") teacher = Group.objects.get(name="teacher") if admin in user.groups.all(): queryset = User.objects.all() elif teacher in user.groups.all(): student_group = Group.objects.get(name="student") queryset = student_group.user_set.all() elif student in user.groups.all(): queryset = User.objects.filter(username=user.username) serializer = UserSerializer(queryset, many=True) return Response(serializer.data)
def post(self, request): signin_serializer = UserSigninSerializer(data = request.data) if not signin_serializer.is_valid(): return Response(signin_serializer.errors, status = HTTP_400_BAD_REQUEST) user = authenticate( username = signin_serializer.data['username'], password = signin_serializer.data['password'] ) if not user: return Response({'detail': 'Invalid Credentials'}, status=HTTP_404_NOT_FOUND) token, _ = Token.objects.get_or_create(user = user) is_expired, token = token_expire_handler(token) user_serialized = UserSerializer(user) return Response({ 'user': user_serialized.data, 'expires_in': expires_in(token), 'token': token.key }, status=HTTP_200_OK)
def registereduser(request): """ List all snippets, or create a new snippet. """ if request.method == 'GET': #user = UserProfile.objects.all() user = User.objects.all() serializer = UserSerializer(user, many=True) return Response(serializer.data) #return Response('ione'); elif request.method == 'POST': serializer = RegisterSerializer(data=request.data) try: if serializer.is_valid(): user = Register.objects.get( phonenumber=request.data['phonenumber']) return Response('already register') except Register.DoesNotExist: serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): users = User.objects.all() serialized_user = UserSerializer(users, many=True) return Response(serialized_user.data)
def get(self, request, pk, format=None): user = self.get_object(pk) serialized_user = UserSerializer(user) return Response(serialized_user.data)
class PictureSerializer(ModelSerializer): user = UserSerializer() class Meta: model = Picture fields = '__all__'
def raw_sql_query(self, request): name = request.query_params.get('name', None) user = fun_raw_sql_query(name=name) serializer = UserSerializer(user, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
def get_current_user(request): serializer = UserSerializer(request.user) return Response(serializer.data)
def get(self, request, username, format=None): user = helpers.get_user_by_username(self, request, username) serializer = UserSerializer(user) return Response(serializer.data)