def register(): form = {k: request.form[k].strip() for k in request.form} # form = {'username': '******', 'password': '******', 'email_addr': '*****@*****.**'} print(form['username'], form['password']) if utils.validate_username(form['username']): code = None if MAIL_USERNAME: code = utils.get_check_code() content = 'Hello! your checking code is:' + code thread = Thread(target=send_mail, args=[app, content, form['email_addr']]) thread.start() user = User(username=form['username'], password=form['password'], code=code) db.session.add(user) db.session.commit() # user = User.query.filter_by(username=form['username']).first() # success = user.update(code=code) # print(success) # db.session.commit() # login_user(user, remember=True) print('valid') return Validity(True).get_resp() # 'register succeeds' else: print('invalid') return Validity(False, 'Register fails: Invalid username or password.' ).get_resp() # 'register fails'
def update_group_task(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_taskid(int(form['task_id'])): task = Task.query.filter_by(id=int(form['task_id'])).first() if utils.validate_ownership(int(current_user.id), int(form['group_id'])): task.update( owner_id=None, title=(None if 'title' not in form else form['title']), finish_time=(None if 'finish_time' not in form else utils.trans_to_date(form['finish_time'])), status=(None if 'status' not in form else form['status']), publicity=(None if 'publicity' not in form else int( form['publicity'])), group_id=(None if 'group_id' not in form else int( form['group_id'])), info=(None if 'info' not in form else form['info'])) db.session.commit() return Validity(True).get_resp() elif utils.validate_membership(int(current_user.id), int(form['group_id'])): # members can only edit the progress task.update( status=(None if 'status' not in form else int(form['status']))) db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'No access').get_resp() else: return Validity(False, 'Invalid task id').get_resp()
def get_task(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_taskid(int(form['task_id'])): task = Task.query.filter_by(id=int(form['task_id'])).first() return Validity(True, task.get_info_map()).get_resp() else: return Validity(False, 'Invalid task id').get_resp()
def verify_user(): form = {k: request.form[k].strip() for k in request.form} success = current_user.update(code=form['code']) if success: db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'Wrong code').get_resp()
def delete_task(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_taskid(task_id=int(form['task_id'])): task = Task.query.filter_by(id=int(form['task_id'])).first() db.session.delete(task) db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'Invalid task id').get_resp()
def get_group(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_groupid(int(form['group_id'])): # if utils.validate_membership(current_user.id, int(form['group_id'])): group = Group.query.filter_by(id=int(form['group_id'])).first() return Validity(True, group.get_info_map()).get_resp() # else: # return Validity(False, 'No access').get_resp() else: return Validity(False, 'Invalid group id').get_resp()
def delete_group_task(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_ownership(int(current_user.id), int(form['group_id'])): if Task.query.filter_by(id=int(form['task_id'])).first(): task = Task.query.filter_by(id=int(form['task_id'])).first() db.session.delete(task) db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'Invalid task id').get_resp() else: return Validity(False, 'No access').get_resp()
def login(): form = {k: request.form[k].strip() for k in request.form} print(form['username'], form['password']) user = User.query.filter_by(username=form['username'], password=form['password']).first() if user: login_user(user, remember=True) return Validity(True).get_resp( ) #redirect(url_for('get_tasklist')) #json.dumps({'valid': True, 'task': ret}) #'login succeeds' else: return Validity(False, 'Login fails: Invalid username or password.').get_resp( ) #'login fails'
def deny_groupReq(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_groupid(group_id=int(form['group_id'])): if not utils.validate_groupreqs(int(current_user.id), int(form['group_id'])): return Validity(False, 'Invitation does not exist.').get_resp() else: group = Group.query.filter_by(id=int(form['group_id'])).first() group.deny_groupReq(int(current_user.id)) db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'Invalid group id').get_resp()
def get_group_member(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_groupid(int(form['group_id'])): if utils.validate_membership(int(current_user.id), int(form['group_id'])): group = Group.query.filter_by(id=int(form['group_id'])).first() ret = sorted([user for user in group.get_members()], key=lambda v: v.name) ret = [user.get_info_map() for user in ret] return Validity(True, {'member list': ret}).get_resp() else: return Validity(False, 'No access').get_resp() else: return Validity(False, 'Invalid group id').get_resp()
def create_group_task(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_ownership(int(current_user.id), int(form['group_id'])): task = Task(owner_id=int(current_user.id), title=form['title'], finish_time=utils.trans_to_date(form['deadline']), status=(0 if 'status' not in form else form['status']), publicity=2, group_id=(None if 'group_id' not in form else int( form['group_id'])), info=('' if 'info' not in form else form['info'])) db.session.add(task) db.session.commit() return Validity(True, task.get_info_map()).get_resp() else: return Validity(False, 'No access').get_resp()
def deny_friendReqs(): form = {k: request.form[k].strip() for k in request.form} if 'friend_id' not in form: assert 'friend_username' in form if utils.validate_username(form['friend_username']): return Validity( False, 'User ' + form['friend_username'] + ' does not exist.').get_resp() form['friend_id'] = utils.get_userid(form['friend_username']) if not utils.validate_friendreqs(int(form['friend_id']), int(current_user.id)): return Validity(False, 'Request does not exist.').get_resp() friend = User.query.filter_by(id=int(form['friend_id'])).first() friend.deny_friendReq(int(current_user.id)) db.session.commit() return Validity(True).get_resp()
def get_group_tasklist(): ret = [] for group in current_user.get_groups(): ret.extend([task for task in group.get_tasks()]) ret = sorted(ret, key=lambda v: v.finish_time) ret = [task.get_info_map() for task in ret] return Validity(True, {'group task list': ret}).get_resp()
def update_user(): form = {k: request.form[k].strip() for k in request.form} current_user.update( username=(None if 'username' not in form else form['username']), password=(None if 'password' not in form else form['password']), name=(None if 'name' not in form else form['name']), info=(None if 'info' not in form else form['info'])) db.session.commit() return Validity(True).get_resp()
def create_group(): form = {k: request.form[k].strip() for k in request.form} group = Group(name=form['name'], owner_id=int(current_user.id), info=('' if 'info' not in form else form['info'])) db.session.add(group) db.session.commit() print('group created') return Validity(True, group.get_info_map()).get_resp()
def get_friend_tasklist(): if request.form is None or 'friend_username' not in request.form: ret = [] for friend in current_user.get_friends(): ret.extend([task for task in friend.get_public_tasks()]) ret = sorted(ret, key=lambda v: v.finish_time) ret = [task.get_info_map() for task in ret] return Validity(True, {'friend task list': ret}).get_resp() else: form = {k: request.form[k].strip() for k in request.form} if utils.validate_username(form['friend_username']): return Validity( False, 'User ' + form['friend_username'] + ' does not exist.').get_resp() friend = User.query.filter_by(username=form['friend_username']).first() ret = sorted([task for task in friend.get_public_tasks()], key=lambda v: v.finish_time) ret = [task.get_info_map() for task in ret] return Validity(True, {'friend task list': ret}).get_resp()
def delete_member(): form = {k: request.form[k].strip() for k in request.form} if 'user_id' not in form: assert 'user_username' in form if utils.validate_username(form['user_username']): return Validity( False, 'User ' + form['user_username'] + ' does not exist.').get_resp() form['user_id'] = utils.get_userid(form['user_username']) if utils.validate_groupid(group_id=int(form['group_id'])): if utils.validate_membership(int(form['user_id']), int( form['group_id'])) and not utils.validate_ownership( int(form['user_id']), int(form['group_id'])): group = Group.query.filter_by(id=int(form['group_id'])).first() group.delete_member(int(form['user_id'])) db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'Can not quit the group').get_resp() else: return Validity(False, 'Invalid group id').get_resp()
def add_member(): form = {k: request.form[k].strip() for k in request.form} if 'user_id' not in form: assert 'user_username' in form if utils.validate_username(form['user_username']): return Validity( False, 'User ' + form['user_username'] + ' does not exist.').get_resp() form['user_id'] = utils.get_userid(form['user_username']) if not utils.validate_userid(int(form['user_id'])): return Validity(False, 'Invalid user.').get_resp() if utils.validate_groupid(group_id=int(form['group_id'])): if utils.validate_groupreqs(int(form['user_id']), int(form['group_id'])): return Validity(False, 'Invitation already sent.').get_resp() if not utils.validate_membership(int(form['user_id']), int(form['group_id'])): group = Group.query.filter_by(id=int(form['group_id'])).first() group.add_memberReq(int(form['user_id'])) db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'Already in the group').get_resp() else: return Validity(False, 'Invalid group id').get_resp()
def add_friend(): form = {k: request.form[k].strip() for k in request.form} if 'friend_id' not in form: assert 'friend_username' in form if utils.validate_username(form['friend_username']): return Validity( False, 'User ' + form['friend_username'] + ' does not exist.').get_resp() form['friend_id'] = utils.get_userid(form['friend_username']) if utils.validate_friendship(int(current_user.id), int(form['friend_id'])): return Validity( False, 'User ' + form['friend_username'] + ' has already been your friend.').get_resp() if utils.validate_friendreqs(int(current_user.id), int(form['friend_id'])): return Validity(False, 'Request already sent.').get_resp() # friend = User.query.filter_by(id = int(form['friend_id'])).first() current_user.add_friendReq(int(form['friend_id'])) # friend.add_friend(int(current_user.id)) db.session.commit() return Validity(True).get_resp()
def update_task(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_taskid(int(form['task_id'])): if utils.validate_task_ownership(int(current_user.id), int(form['task_id'])): task = Task.query.filter_by(id=int(form['task_id'])).first() task.update( # owner_id=None, # I don't think any user have the authority to change task's owner title=(None if 'title' not in form else form['title']), finish_time=(None if 'finish_time' not in form else utils.trans_to_date(form['finish_time'])), status=(None if 'status' not in form else form['status']), publicity=(None if 'publicity' not in form else int( form['publicity'])), group_id=(None if 'group_id' not in form else int( form['group_id'])), info=(None if 'info' not in form else form['info'])) db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'No access').get_resp() else: return Validity(False, 'Invalid task id').get_resp()
def update_group(): form = {k: request.form[k].strip() for k in request.form} if 'owner_id' not in form and 'owner_username' in form: #assert 'owner_username' in form if utils.validate_username(form['owner_username']): return Validity( False, 'User ' + form['owner_username'] + ' does not exist.').get_resp() form['owner_id'] = utils.get_userid(form['owner_username']) if utils.validate_groupid(int(form['group_id'])): if utils.validate_ownership(int(current_user.id), int(form['group_id'])): group = Group.query.filter_by(id=int(form['group_id'])).first() group.update(name=(None if 'name' not in form else form['name']), owner_id=(None if 'owner_id' not in form else int( form['owner_id'])), info=(None if 'info' not in form else form['info'])) db.session.commit() return Validity(True, group.get_info_map()).get_resp() else: return Validity(False, 'No access').get_resp() else: return Validity(False, 'Invalid group id').get_resp()
def create_task(): form = {k: request.form[k].strip() for k in request.form} print(current_user.id) print(form) task = Task( owner_id=int(current_user.id), title=form['title'], finish_time=(datetime.datetime.now() if 'finish_time' not in form else utils.trans_to_date(form['finish_time'])), status=(0 if 'status' not in form else form['status']), publicity=(0 if 'publicity' not in form else form['publicity']), group_id=(None if 'group_id' not in form else form['group_id']), info=('' if 'info' not in form else form['info'])) db.session.add(task) db.session.commit() return Validity(True, task.get_info_map()).get_resp()
def logout(): logout_user() return Validity(True).get_resp()
def get_tasklist(): ret = sorted([task for task in current_user.get_tasks()], key=lambda v: v.finish_time) ret = [task.get_info_map() for task in ret] return Validity(True, {'task list': ret}).get_resp()
def check_ownership(): form = {k: request.form[k].strip() for k in request.form} if utils.validate_ownership(int(current_user.id), int(form['group_id'])): return Validity(True).get_resp() else: return Validity(False).get_resp()
def get_groupreq(): ret = sorted([groupReq for groupReq in current_user.get_groupsReqs()], key=lambda v: v.name) ret = [group.get_info_map() for group in ret] return Validity(True, {'group invitations': ret}).get_resp()
def get_friendreq(): ret = sorted([friendreq for friendreq in current_user.get_friendreqs()], key=lambda v: v.name) ret = [friend.get_info_map() for friend in ret] return Validity(True, {'friend requests': ret}).get_resp()
def get_grouplist(): ret = sorted([group for group in current_user.get_groups()], key=lambda v: v.name) ret = [group.get_info_map() for group in ret] return Validity(True, {'group list': ret}).get_resp()