Пример #1
0
def register():
    """
    注册接口
    :return:
    """
    username = request.form.get('username', None)
    password = request.form.get('password', None)
    email = request.form.get('email', None)
    error = {}
    if username:
        if len(username) > 16 or len(username) < 6:
            error['username'] = '******'
        if re.match(r'^[0-9]+$', username) or re.match(r'^_|_$', username):
            error['username'] = '******'

        userObj = UserInfo.query.filter_by(username=username).first()
        if userObj:
            error['username'] = '******'
    else:
        error['username'] = '******'

    if password:
        if len(password) < 6:
            error['password'] = '******'

        if re.match(r'^[0-9]+$', password):
            error['password'] = '******'

    else:
        error['password'] = '******'

    if email:
        if len(email) > 7:
            if re.match(
                    "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$",
                    email) == None:
                error['email'] = '请输入正确的邮箱格式'
        else:
            error['email'] = '请输入正确的邮箱格式'

        userObj = UserInfo.query.filter_by(email=email).first()
        if userObj:
            error['email'] = '邮箱重复了'
    else:
        error['email'] = '请填写邮箱'

    if not error:
        userInfo = UserInfo(username=username,
                            password=md5(password),
                            email=email,
                            registered_time=time_format())
        db = get_db()
        db.session.add(userInfo)
        db.session.commit()
        res = {
            'msg': '注册成功',
            'username': username,
        }
        return response(res)
    return response(error)
Пример #2
0
    def post(self):
        form = request.get_json()
        if not form:
            return response(message='请传入必须的数据',code=201)
        note_id = form.get('note_id')
        noteObj = Note.query.filter_by(note_id=note_id).first()
        if  not noteObj:
            return response(message='没有找到这个笔记',code=201)

        collectObj = Collect.query.filter_by(user_id=online.user.user_id,note_id=note_id).first()
        if collectObj:
            return response(message='该笔记已收藏',code=201)

        isself = True if noteObj.user_id==online.user.user_id else False

        collect = Collect(
            user=online.user,
            note=noteObj,
            myself=isself,
            add_time=time_format()
        )
        db = get_db()
        db.session.add(collect)
        db.session.commit()
        return response(message='success',code=200)
Пример #3
0
def login():
    """
    登陆接口,登陆成功后返回token
    :return: json
    """
    username = request.form.get('username', None)
    password = request.form.get('password', None)
    if not username or not password:
        return response(code=401, message='UserName or Password is NULL')
    userObj = UserInfo.query.filter_by(username=username,
                                       password=md5(password)).first()

    if userObj:
        ## 先将之前的删除
        db = get_db()
        remove_token = Token.query.filter_by(user_id=userObj.user_id).first()
        if remove_token:
            db.session.delete(remove_token)
            db.session.commit()
        # 登陆成功
        token = md5(md5(username) + md5(userObj.email) + md5(str(time.time())))
        tokenObj = Token(
            user_id=userObj.user_id,
            token=token,
            expire_date=time_format(offset=config('TOKEN_VALIDITY')))
        db.session.add(tokenObj)
        db.session.commit()
        return response({'token': token, 'login': True}, )

    return response({'msg': "登陆失败,账号或者密码错误", 'login': False})
Пример #4
0
    def post(self):
        folder_name = request.form.get('folder_name')
        parent_id = request.form.get('parent_id')
        if not folder_name:
            return response('The folder name is null',message='params error',code=400)
        if len(folder_name) > 10:
            return response('The folder name is too long',message='params error',code=400)
        if parent_id:
            parentObj = Folder.query.filter_by(folder_id=parent_id).first()
            if parentObj.parent_id:
                parentObj = Folder.query.filter_by(folder_id=parentObj.parent_id).first()
                if parentObj.parent_id:
                    return response(message='Sorry,Can only support up to three levels of folders',code=201)

        folderObj = Folder(
            folder_name=folder_name,
            parent_id=parent_id,
            user_id=online.user.user_id,
            add_time=time_format()
        )
        db = get_db()
        db.session.add(folderObj)
        db.session.commit()

        return response({
            'folder_name':folder_name,
            'parent_id':parent_id
        })
Пример #5
0
def login():
    """
    登陆接口,登陆成功后返回token
    :return: json
    """
    userinfo = request.get_json()
    if request.headers['Content-Type'] == 'application/json;charset=UTF-8':
        username = userinfo['username']
        password = userinfo['password']
    else:
        username = request.form.get('username', None)
        password = request.form.get('password', None)
    if not username or not password:
        return response(code=401, message='UserName or Password is NULL')
    userObj = UserInfo.query.filter_by(username=username,
                                       password=md5(password)).first()

    if userObj:
        ## 先将之前的删除
        db = get_db()
        remove_token = Token.query.filter_by(user_id=userObj.user_id).first()
        if remove_token:
            db.session.delete(remove_token)
            db.session.commit()
        # 登陆成功
        token = md5(md5(username) + md5(userObj.email) + md5(str(time.time())))
        tokenObj = Token(
            user_id=userObj.user_id,
            token=token,
            expire_date=time_format(offset=config('TOKEN_VALIDITY')))
        db.session.add(tokenObj)
        db.session.commit()
        return response({'token': token, 'login': True}, )

    return response(message='Incorrect account or password', code=401)
Пример #6
0
 def delete(self, pk):
     noteObj, flag = self.verify(pk)
     if not flag:
         return noteObj
     db = get_db()
     db.session.delete(noteObj)
     db.session.commit()
     return {'code': 200, 'message': 'success'}
Пример #7
0
 def delete(self,pk):
     folderObj = Folder.query.filter_by(folder_id=pk).first()
     if not folderObj:
         return response(message='source not found', code=404)
     db = get_db()
     db.session.delete(folderObj)
     db.session.commit()
     return response()
Пример #8
0
 def delete(self, pk):
     noteObj, flag = self.verify(pk)
     if not flag:
         return noteObj
     db = get_db()
     [db.session.delete(note) for note in noteObj.collect]  # 批量将外键删除
     db.session.delete(noteObj)
     db.session.commit()
     return {'code': 200, 'message': 'success'}
Пример #9
0
 def put(self,pk):
     folderObj = Folder.query.filter_by(folder_id=pk).first()
     if not folderObj:
         return response(message='source not found',code=404)
     folder_name = request.form.get('folder_name')
     print(folder_name)
     print(folderObj.folder_name)
     db = get_db()
     folderObj.folder_name = folder_name if folder_name else folderObj.folder_name
     db.session.commit()
     return response()
Пример #10
0
    def put(self, pk):
        noteObj, flag = self.verify(pk)
        if not flag:
            return noteObj

        title = request.form.get('title')
        content = request.form.get('content')
        noteObj.title = title if title else noteObj.title
        noteObj.content = content if content else noteObj.content
        noteObj.up_time = time_format()
        db = get_db()
        db.session.commit()

        return {'code': 200, 'message': 'success'}
Пример #11
0
 def post(self):
     title = request.form.get('title', '')
     content = request.form.get('content', '')
     folder_id = request.form.get('folder_id', None)
     noteObj = Note(user_id=online.user.user_id,
                    title=title,
                    content=content,
                    folder_id=folder_id,
                    status=1,
                    add_time=time_format())
     db = get_db()
     db.session.add(noteObj)
     db.session.commit()
     res = {'code': 200, 'message': 'success'}
     return res
Пример #12
0
    def delete(self):
        form = request.get_json()
        if not form:
            return response(message='请传入必须的数据', code=201)
        note_id = form.get('note_id')
        noteObj = Note.query.filter_by(note_id=note_id).first()

        if not noteObj:
            return response(message='没有找到这个笔记', code=201)
        collectObj = Collect.query.filter_by(
            user_id=online.user.user_id,
            note_id=note_id
        ).first()
        db = get_db()
        db.session.delete(collectObj)
        db.session.commit()
        return response()
Пример #13
0
    def put(self, pk):
        noteObj, flag = self.verify(pk)
        if not flag:
            return noteObj
        form = request.get_json()
        if request.headers['Content-Type'] == 'application/json;charset=UTF-8':
            title = form.get('title', '')
            content = form.get('content', ''),
        else:
            title = request.form.get('title')
            content = request.form.get('content')

        noteObj.title = title
        noteObj.content = content if content else noteObj.content
        noteObj.up_time = time_format()
        db = get_db()
        db.session.commit()

        return {'code': 200, 'message': 'success'}
Пример #14
0
 def post(self):
     form = request.get_json()
     if request.headers['Content-Type'] == 'application/json;charset=UTF-8':
         title = form.get('title', '')
         content = form.get('content', '')
         folder_id = form.get('folder_id', None)
     else:
         title = request.form.get('title', '')
         content = request.form.get('content', '')
         folder_id = request.form.get('folder_id', None)
     noteObj = Note(user_id=online.user.user_id,
                    title=title,
                    content=content,
                    folder_id=folder_id,
                    status=1,
                    add_time=time_format())
     db = get_db()
     db.session.add(noteObj)
     db.session.commit()
     print(noteObj.note_id)
     data = {'note_id': noteObj.note_id, 'title': title, 'content': content}
     res = {'code': 200, 'message': 'success', 'data': data}
     return res