Пример #1
0
    def put(self, guid):

        if request.form is None:
            return resp_data_invalid_err('Users', [])

        try:
            img_file = request.files['imageFile']
            if img_file:
                file_ext = img_file.filename.rsplit('.', 1)[1].lower()
                filename = '{}.{}'.format(guid, file_ext)
                img_file.save(os.path.join(config.UPLOAD_DIR, filename))

            user = User.objects.get(guid=guid)
            User.objects.get(guid=guid).update(
                set__profile__name=request.form['profile.name'],
                set__profile__avatar=filename,
                set__profile__cellPhone=request.form['profile.cellPhone'],
                set__roles__admin=True
                if request.form['role'] == 'administrator' else False,
                set__roles__superuser=True
                if request.form['role'] == 'superuser' else False,
                set__roles__collaborator=True
                if request.form['role'] == 'collaborator' else False)

            return resp_user_updated('Users', user.username)

        except DoesNotExist:
            return resp_does_not_exist_err('Users', guid)

        except Exception as ex:  # pylint: disable=broad-except
            return resp_exception_err('Users', ex.__str__())
Пример #2
0
    def post(self):
        if request.form is None:
            return resp_data_invalid_err('Users', [])

        try:
            unique_guid = str(uuid.uuid4())
            initial_pwd = 'Rckb1000!'
            img_file = request.files['imageFile']
            if img_file:
                file_ext = img_file.filename.rsplit('.', 1)[1].lower()
                filename = '{}.{}'.format(unique_guid, file_ext)
                img_file.save(os.path.join(config.UPLOAD_DIR, filename))

            User(guid=unique_guid,
                 username=request.form['username'],
                 active=True if request.form['active'] == 'true' else False,
                 password=bcrypt.hashpw(initial_pwd.encode('utf-8'),
                                        bcrypt.gensalt()),
                 profile=Profile(name=request.form['profile.name'],
                                 cellPhone=request.form['profile.cellPhone'],
                                 avatar=filename),
                 roles=Roles(admin=True if request.form['role']
                             == 'administrator' else False,
                             superuser=True
                             if request.form['role'] == 'superuser' else False,
                             collaborator=True if request.form['role']
                             == 'collaborator' else False)).save()

            return resp_user_created('Users', request.form['username'])

        except NotUniqueError:
            return resp_not_unique_err('Users', 'usuário')

        except Exception as ex:  # pylint: disable=broad-except
            return resp_exception_err('Users', ex.__str__())
Пример #3
0
    def get(self):

        try:
            return [self.__to_json(user) for user in User.objects], 200

        except DoesNotExist:
            return resp_does_not_exist_err('Profiles', 'no_data')

        except Exception as e:
            return resp_exception_err('Profiles', e.__str__())
Пример #4
0
    def get(self, guid):

        try:
            user = User.objects.get(guid=guid)

            return self.__to_json(user)

        except DoesNotExist:
            return resp_does_not_exist_err('Profiles', guid)

        except Exception as e:
            return resp_exception_err('Profiles', e.__str__())
Пример #5
0
    def delete(self, guid):

        try:
            user = User.objects.get(guid=guid)
            User.objects.get(guid=guid).delete()

            if (os.path.exists(
                    os.path.join(config.UPLOAD_DIR, user.profile.avatar))):
                os.remove(os.path.join(config.UPLOAD_DIR, user.profile.avatar))

            return resp_user_deleted('Users', user.username)

        except DoesNotExist:
            return resp_does_not_exist_err('Users', guid)

        except Exception as ex:  # pylint: disable=broad-except
            return resp_exception_err('Users', ex.__str__())
Пример #6
0
    def put(self, guid):

        req_data = request.get_json() or None

        if req_data is None:
            return resp_data_invalid_err('Users', [])

        try:
            user = User.objects.get(guid=guid)
            User.objects.get(guid=guid).update(active=req_data['active'], )

            return resp_user_updated('Users', user.username)

        except DoesNotExist:
            return resp_does_not_exist_err('Users', req_data['guid'])

        except Exception as ex:  # pylint: disable=broad-except
            return resp_exception_err('Users', ex.__str__())
Пример #7
0
    def post(self):
        # return "success", 200
        req_data = request.get_json() or None

        print(req_data)
        if req_data is None:
            return resp_data_invalid_err('Users', [])

        try:
            user = User.objects.get(username=req_data['username'])

            if user:
                return resp_user_already_exists_err('User', user)
                
        except DoesNotExist:
            return {
                       'message': 'Usuário disponível'
                   }, 200

        except Exception as ex: # pylint: disable=broad-except
            return resp_exception_err('Users', ex.__str__())
Пример #8
0
    def post(self):
        req_data = request.get_json() or None

        if req_data is None:
            return resp_data_invalid_err('Users', [])

        try:
            user = User.objects.get(username=req_data['username'])
            if checkpw(req_data['password'].encode('utf-8'),
                       user.password.encode('utf-8')) is False:
                return resp_invalid_credentials_err('Users')
            if not user.active:
                return resp_not_active_err('Users', user.username)
            if user and checkpw(req_data['password'].encode('utf-8'),
                                user.password.encode('utf-8')):
                expires = datetime.timedelta(seconds=3600)
                access_token = create_access_token(identity=user.guid,
                                                   fresh=True,
                                                   expires_delta=expires)
                refresh_token = create_refresh_token(user.guid)
                return {
                    'access_token': access_token,
                    'refresh_token': refresh_token
                }, 200
            return resp_invalid_credentials_err('Users')

        except DoesNotExist:
            return resp_does_not_exist_err('Users', req_data['username'])

        except Exception as e:
            return resp_exception_err('Users', e.__str__())


# class TokenRefresh(Resource):
#     @jwt_refresh_token_required
#     def post (self):
#         current_user = get_jwt_identity()
#         new_token = create_access_token(identity=current_user, fresh=False)
#         return {'access_token': new_token}, 200