예제 #1
0
파일: views.py 프로젝트: sbabashahi/saipa
 def post(self, request):
     try:
         serialized_data = self.serializer_class(data=request.data)
         if serialized_data.is_valid(raise_exception=True):
             username = serialized_data.data['username']
             password = serialized_data.data['password']
             try:
                 user = User.objects.get(username=username)
             except User.DoesNotExist as e:
                 raise util_exception.CustomException(
                     detail=ugettext('User credentials are incorrect.'),
                     code=404)
             if user.check_password(password):
                 payload = jwt_payload_handler(user)  # todo: Is deprecated
                 jwt_token = utils.jwt_response_payload_handler(
                     jwt_encode_handler(payload))
                 return responses.SuccessResponse(jwt_token).send()
             else:
                 raise util_exception.CustomException(
                     detail=ugettext('User credentials are incorrect.'),
                     code=404)
     except util_exception.CustomException as e:
         return responses.ErrorResponse(message=e.detail,
                                        status=e.status_code).send()
     except exceptions.ValidationError as e:
         return responses.ErrorResponse(message=e.detail,
                                        status=e.status_code).send()
예제 #2
0
파일: views.py 프로젝트: sbabashahi/saipa
 def post(self, request):
     try:
         serialized_data = self.serializer_class(data=request.data)
         if serialized_data.is_valid(raise_exception=True):
             username = serialized_data.data['username']
             password = serialized_data.data['password']
             try:
                 user = User.objects.get(username=username)
             except User.DoesNotExist as e:
                 user = None
             if user:
                 raise util_exception.CustomException(detail=ugettext(
                     'User with this credentials is registered before.'),
                                                      code=400)
             user = transactions.register_user(username, password)
             payload = jwt_payload_handler(user)  # todo: Is deprecated
             jwt_token = utils.jwt_response_payload_handler(
                 jwt_encode_handler(payload))
             return responses.SuccessResponse(jwt_token, status=201).send()
     except util_exception.CustomException as e:
         return responses.ErrorResponse(message=e.detail,
                                        status=e.status_code).send()
     except exceptions.ValidationError as e:
         return responses.ErrorResponse(message=e.detail,
                                        status=e.status_code).send()
예제 #3
0
파일: views.py 프로젝트: sbabashahi/feedio
    def post(self, request):
        try:
            serialized_data = self.serializer_class(data=request.data)
            if serialized_data.is_valid(raise_exception=True):
                email = serialized_data.data['email']
                password = serialized_data.data['password']
                user_list = User.objects.filter(email=email,
                                                email_confirmed=True)
                if user_list:
                    raise authnz_exceptions.CustomException(
                        detail=ugettext('You registered before.'))

                permissions.check_send_email_permission(email)

                try:  # check for previous used email
                    user = User.objects.get(email=email, email_confirmed=False)
                except User.DoesNotExist as e:
                    user = None

                if user:  # registered before but not confirmed
                    user = user.update_password(password)
                else:
                    user = User.register_user(email, password)
                user.send_email_confirm(request)
                return responses.SuccessResponse({}, status=201).send()
        except (authnz_exceptions.CustomException,
                exceptions.ValidationError) as e:
            return responses.ErrorResponse(message=e.detail,
                                           status=e.status_code).send()
예제 #4
0
def handle_jwt_decode(req):
    auth = req.headers.get('Authorization', None)
    if auth is None or auth == '':
        raise exceptions.CustomException(detail='You have no Authorization',
                                         code=403)
    if auth.startswith('JWT '):
        token = auth.split()
        if len(token) == 1:
            raise exceptions.CustomException(
                detail='Invalid Authorization header. No credentials provided.'
            )
        if len(token) > 2:
            raise exceptions.CustomException(
                detail=
                'Invalid Authorization header. should not contain spaces.')
        try:
            user_id = jwt.decode(token[1], options={"verify_signature":
                                                    False})['sub']
        except jwt.exceptions.PyJWTError as e:
            raise exceptions.CustomException(detail='Token not verified.',
                                             code=403)
        user = User.query.get(user_id)
        if user:
            try:
                jwt.decode(token[1], user.secret, algorithms='HS256')
            except jwt.exceptions.PyJWKError as e:
                raise exceptions.CustomException(detail='Token not verified.',
                                                 code=403)
            return user
        else:
            raise exceptions.CustomException(detail='Invalid token.', code=403)
    else:
        raise exceptions.CustomException(
            detail='Invalid Authorization header.')
예제 #5
0
def pagination_util(request):
    arguments = parser.parse(request.GET.urlencode())
    try:
        size = int(arguments.pop('size', 20))
        index = int(arguments.pop('index', 0))
    except ValueError:
        raise exceptions.CustomException(detail=ugettext('Size and index query param for pagination must be integer.'))
    size = index + size
    return index, size, arguments
예제 #6
0
 def create(self, validated_data):
     try:
         validated_data['feed'] = Feed.objects.get(
             id=validated_data['feed']['id'], is_deleted=False)
     except Rss.DoesNotExist as e:
         raise exceptions.CustomException(
             detail=ugettext('Feed does not exist.'))
     validated_data['user'] = self.context['request'].user
     comment = Comment(**validated_data)
     comment.save()
     return comment
예제 #7
0
파일: views.py 프로젝트: sbabashahi/feedio
 def post(self, request):
     try:
         serialized_data = self.serializer_class(data=request.data)
         if serialized_data.is_valid(raise_exception=True):
             email = serialized_data.data['email']
             password = serialized_data.data['password']
             try:
                 user = User.objects.get(email=email)
             except User.DoesNotExist as e:
                 raise authnz_exceptions.CustomException(
                     detail=ugettext('You did not registered before.'))
             if user.email_confirmed and user.is_active and user.check_password(
                     password):
                 user.save_last_login()
                 jwt_token = utils.jwt_response_payload_handler(
                     user.generate_token())
                 return responses.SuccessResponse(jwt_token).send()
             elif not user.email_confirmed:
                 try:
                     permissions.check_send_email_permission(email)
                 except authnz_exceptions.CustomException as e:
                     raise authnz_exceptions.CustomException(
                         detail=ugettext(
                             'You did not confirm your email and you'
                             ' reached max email sent, try later.'))
                 user.send_email_confirm(request)
                 raise authnz_exceptions.CustomException(
                     detail=ugettext('You did not confirm your email,'
                                     ' We sent you a confirmation email'))
             elif not user.is_active:
                 raise authnz_exceptions.CustomException(
                     detail=ugettext('Your account is not active,'
                                     ' please contact support.'))
             else:
                 raise authnz_exceptions.CustomException(
                     detail=ugettext('Wrong login credentials.'))
     except (authnz_exceptions.CustomException,
             exceptions.ValidationError) as e:
         return responses.ErrorResponse(message=e.detail,
                                        status=e.status_code).send()
예제 #8
0
파일: update.py 프로젝트: sbabashahi/feedio
 def get_instance(self, request, id):
     if id:
         if hasattr(self.model, 'is_deleted'):
             instance = self.model.objects.get(id=id, is_deleted=False)
         else:
             instance = self.model.objects.get(id=id)
         permissions.check_perm_owner_update(request, instance)
     elif self.model == User:
         instance = request.user
     else:
         raise exceptions.CustomException(
             detail=ugettext('Not implemented'), code=500)
     return instance
예제 #9
0
파일: views.py 프로젝트: sbabashahi/feedio
    def get(self, request):
        try:
            if request.user.is_active:
                jwt_token = utils.jwt_response_payload_handler(
                    request.user.generate_token())
                return responses.SuccessResponse(jwt_token).send()
            else:
                raise authnz_exceptions.CustomException(
                    detail=ugettext('This user is inactive, contact us.'))

        except authnz_exceptions.CustomException as e:
            return responses.ErrorResponse(message=e.detail,
                                           status=e.status_code).send()
예제 #10
0
 def get(self, request, id):
     try:
         rss = self.model.objects.get(id=id, is_active=True)
         fs = FollowRss.objects.filter(rss=rss, user=request.user)
         if fs:
             count = Feed.objects.filter(
                 rss=rss, published__gte=fs[0].last_seen).count()
             data = {'unread': count if count else 0}
             return responses.SuccessResponse(data).send()
         else:
             raise exceptions.CustomException(
                 detail=ugettext('You did not follow this Rss.'))
     except (exceptions.CustomException, drf_exceptions.ValidationError,
             Rss.DoesNotExist) as e:
         return responses.ErrorResponse(message=e.detail,
                                        status=e.status_code).send()