Beispiel #1
0
 def post(request):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         data = dict(status="Success!")
         return Response(data=data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=400)
Beispiel #2
0
 def update(self, request, *args, **kwargs):
     auth_token = request.META.get(HTTP_AUTHORIZATION, '').replace('Token ', '')
     user = Token.objects.get(key=auth_token).user
     user = User.objects.filter(username=user, pk=kwargs['pk'])
     if user:
         user_serializer = UserSerializer(data=request.data)
         user_serializer.is_valid(raise_exception=True)
         user.update(username=request.data['username'],
                              email=request.data['email'],
                              password=request.data['password'])
         return Response({'username': user_serializer.validated_data["username"]}, status=status.HTTP_200_OK)
     else:
         return Response(status=status.HTTP_404_NOT_FOUND)
 def get(self):
     load_only = list()
     if not auth_service.has_admin_role():
         load_only.append('email')
     users = user_service.find()
     return jsonify(
         UserSerializer(many=True, load_only=load_only).dump(users)[0])
 def get(self):
     load_only = list()
     if not auth_service.has_admin_role():
         load_only.append('email')
     data, err = GetUserSerializer(load_only=load_only).load(
         request.args.to_dict())
     user = user_service.find_one(data)
     return jsonify(UserSerializer().load(model_to_dict(user))[0])
Beispiel #5
0
 def get(self, provider):
     if provider == 'github':
         data, err = GitHubCallbackSerializer().load(github.authorized_response())
         session['github_token'] = (data['access_token'], '')
         github_user = github.get('user')
         user_data = github_to_user(github_user.data)
         access_token, user = auth_service.oauth_register_or_login(user_data, 'github')
         return auth_service.resp_with_access_token(jsonify(UserSerializer().load(model_to_dict(user))[0]), access_token)
     raise ProviderInvalid(provider)
Beispiel #6
0
 def get(self, request):
     if request.user.is_authenticated:
         return Response({
             'user':
             UserSerializer(request.user, context={
                 'request': request
             }).data
         })
     return HttpResponseForbidden()
Beispiel #7
0
    def get(request):
        user_id = request.current_app.get('user_id')
        try:
            user = User.objects.get(user_id=user_id)
        except ObjectDoesNotExist:
            data = dict(error='User not found')
            return Response(data=data, status=status.HTTP_400_BAD_REQUEST)

        serializer = UserSerializer(user)
        return JsonResponse(serializer.data)
    def post(self, request, *args, **kwargs):
        """
        This function is used to handle the POST API's
        :param request: Request object
        :param args: arguments
        :param kwargs: URL parameters
        :return: HTTP Response
        """
        logger.info('Creating calendar...')
        try:
            user_id = get_user_id(request)

            with transaction.atomic():
                if User.objects.filter(chegg_uuid=user_id).exists():
                    user = User.objects.get(chegg_uuid=user_id)
                else:
                    timekit_user = TimekitUserSerializer(request.data)
                    data = json.dumps(timekit_user.data)
                    user_service = UserService()
                    user_data = user_service.create_user(data)
                    serializer = UserSerializer(user_data['data'])
                    user = serializer.create(serializer.data)
                    user.chegg_uuid = user_id
                    user.save()

                auth = user.dummy_email, user.token
                calendar_input = TimekitCalendarSerializer(request.data)
                calendar_input_data = json.dumps(calendar_input.data)
                calendar_service = CalendarService()
                calendar_data = calendar_service.create_calendar(
                    auth, calendar_input_data)
                UserCalendar.objects.create(
                    user=user, calendar_id=calendar_data['data']['id'])
                return Response(calendar_data, status=status.HTTP_201_CREATED)
        except AuthenticationFailed as error:
            logger.error(error)
            return get_error_response(error, status.HTTP_401_UNAUTHORIZED)
        except (ParseError, IntegrityError, ValueError) as error:
            logger.error(error)
            return get_error_response(error, status.HTTP_400_BAD_REQUEST)
Beispiel #9
0
 def get(self, request):
     """ validates if user is still logged in, in which case returns app token"""
     if request.user.is_authenticated:
         token = AuthToken.objects.create(request.user)
         return Response({
             'user':
             UserSerializer(request.user, context={
                 'request': request
             }).data,
             'token':
             token
         })
     return HttpResponseForbidden()
Beispiel #10
0
    def post(self, request, *args, **kwargs):
        """logs in the user"""
        data = request.data
        # m2m_token = data.get('m2m_token', None)
        # if not check_m2m_token(request.user, m2m_token):
        #     return Response({
        #         'user': '',
        #         'token': ''
        #     })

        serializer = self.serializer_class(data=data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        token = AuthToken.objects.create(user)
        login(request, user)  # this sets csrftoken and sessionid cookies

        prefixes = prefix_service.find(user=user).all()
        if len(prefixes) == 1:
            user.profile.product_active_prefix = prefixes[0]
            user.profile.save()

        return Response({'user': UserSerializer(user).data, 'token': token})
 def put(self):
     data, err = UpdateUserSerializer().load(request.json)
     user = user_service.update(data['id'], data)
     return jsonify(UserSerializer.load(model_to_dict(user))[0])
Beispiel #12
0
 def put(self):
     data, err = UpdateAuthedUserSerializer().load(request.json)
     user = auth_service.update_authed_user(data)
     return jsonify(UserSerializer().load(model_to_dict(user))[0])
Beispiel #13
0
 def get(self):
     user = auth_service.get_authed_user()
     return jsonify(UserSerializer().load(model_to_dict(user))[0])
Beispiel #14
0
 def get(self):
     access_token, user = auth_service.renew_access_token()
     return auth_service.resp_with_access_token(jsonify(UserSerializer().load(model_to_dict(user))[0]), access_token)
Beispiel #15
0
 def create(self, request, *args, **kwargs):
     group = Group.objects.filter(name='candidates')
     user_serializer = UserSerializer(data=request.data)
     user_serializer.is_valid(raise_exception=True)
     user = user_serializer.save(groups=group)
     return Response({'id': user.pk}, status=status.HTTP_201_CREATED)
Beispiel #16
0
 def get(self):
     data, err = GetUserSerializer().load(request.args.to_dict())
     user = user_service.find_one(data)
     return jsonify(UserSerializer().load(model_to_dict(user))[0])