Example #1
0
    def new_task(data):
        json_data = request.get_json(force=True)
        task_name = json_data['task_name']
        task_status = json_data['done']

        if is_empty(task_name):
            return {
                'data': {
                    'message': 'task name is necessary'
                },
                'code': 'none'
            }
        elif is_empty(task_status):
            return {
                'data': {
                    'message': 'task status is necessary'
                },
                'code': 'none'
            }
        elif Task.query.filter_by(task_name=task_name).first() is not None:
            return {
                'data': {
                    'message': 'task is already exist'
                },
                'code': 'exist'
            }
        else:
            db.session.add(Task(task_name, task_status))
            db.session.commit()
            return {'data': {'message': 'create success'}, 'code': 'success'}
Example #2
0
    def post(self):
        json_data = request.get_json(force=True)
        username = json_data.get('username')
        password = json_data.get('password')

        # Username is empty
        if is_empty(username):
            return request_return('username is null', 'none')
        # Password is empty
        if is_empty(password):
            return request_return('password is null', 'none')
        # Post data format is right
        else:
            user = User.query.filter_by(username=username).first()
            # User is exist
            if user is not None:
                # Argument must be hash password
                user_password = User(password_hash=user.password_hash)
                # Check login password
                if not user_password.verify_password(password):
                    return request_return('wrong password', 'none')
                # Login success, return token auth
                else:
                    return request_return(
                        {
                            'status': 'login success',
                            'data': Token(user.username).generate_auth_token()
                        }, 'success')
            # User is not exist in the database
            else:
                return request_return(
                    'user is not exist, please register first', 'none')
Example #3
0
    def new_task(cls, data, token):
        json_data = request.get_json(force=True)
        task_name = json_data['task_name']
        task_status = json_data['done']

        if is_empty(task_name):
            return {
                'data': {
                    'message': 'task name is necessary'
                },
                'code': 'none'
            }
        elif is_empty(task_status):
            return {
                'data': {
                    'message': 'task status is necessary'
                },
                'code': 'none'
            }
        elif Task.query.filter_by(task_name=task_name).first() is not None:
            return {
                'data': {
                    'message': 'task is already exist'
                },
                'code': 'exist'
            }
        else:
            task_user = get_userinfo(token)
            Task(task_name, task_status, task_user)
            save_to_db(task_user)
            return {'data': {'message': 'create success'}, 'code': 'success'}
Example #4
0
    def post(self):
        json_data = request.get_json(force=True)
        username = json_data.get('username')
        password = json_data.get('password')

        # Username is empty
        if is_empty(username):
            return request_return('username is null', 'none')
        # Password is empty
        if is_empty(password):
            return request_return('password is null', 'none')
        # User is already exist
        elif User.query.filter_by(username=username).first() is not None:
            return request_return('user is already exist', 'exist')
        # Register new user
        else:
            user = User(username=username)
            user.hash_password(password)
            user.save_to_db()
            return request_return('register success', 'success')
Example #5
0
 def verify_auth_token(token):
     s = Serializer('SECRET_KEY')
     try:
         data = s.loads(token)
         return util.is_empty(
             Auth.query.filter(Auth.username == data['username']).first())
     # Valid token, but expired
     except SignatureExpired:
         return 'expired'
     # Invalid token
     except BadSignature:
         return 'invalid'