def teamGet(): username = request.args.get('username', default='*', type=str) currentID = request.args.get('currentID', default='*', type=str) teamName = request.args.get('teamName', default='*', type=str) if username == '*' or currentID == '*' or teamName == '*': return err.return_json_error(err.error.JSON_STRUCTURE_WRONG) user = chk.checkUser(s, username, currentID=currentID) if not user['error']: user = user['user'] team = s.query(db.Team).filter(db.Team.name == teamName).all() if team: team = team[0] isPartOfTeam = s.query(db.TeamUser).filter( db.and_(db.TeamUser.team == team, db.TeamUser.user == user)).all() if isPartOfTeam: members = list() for teamuser in team.teamUser: toAdd = { # 'id': friend.id, 'name': teamuser.user.name, 'enrollTime': teamuser.enrollTime, 'role': teamuser.role } members.append(toAdd) return jsonify({"name": team.name, "members": members}) else: return err.return_json_error(err.error.TEAM_ROLE_WAS_TOO_LOW) else: return err.return_json_error(err.error.TEAM_NOT_FOUND) else: return user['return'] return err.return_json_error(err.error.JSON_STRUCTURE_WRONG)
def messageNew(): data = request.get_json(silent=False) if ('currentID' not in data) or ('username' not in data) or ( 'message' not in data) or ('receiver' not in data): return err.return_json_error(err.error.JSON_STRUCTURE_WRONG) else: user = chk.checkUser(s, username=data['username'], currentID=data['currentID']) receiver = s.query( db.User).filter(db.User.name == data['receiver']).all() if not user['error']: user = user['user'] if receiver: receiver = receiver[0] mess = db.Message(receiver=receiver, sender=user, message=data['message']) s.add(mess) s.commit() return jsonify({ 'id': mess.id, 'sender': mess.sender.name, 'receiver': mess.receiver.name, 'message': mess.message, 'sendTime': mess.sentTime, 'receiverRead': mess.receiverRead }) else: return err.return_json_error( err.error.MESSAGE_RECEIVER_NOT_FOUND) else: return user['return']
def teamRemove(): data = request.get_json(silent=False) if ('currentID' not in data) or ('username' not in data) or ('teamName' not in data): return err.return_json_error(err.error.JSON_STRUCTURE_WRONG) else: user = chk.checkUser(s, username=data['username'], currentID=data['currentID']) if not user['error']: user = user['user'] if user.rights > 100: team = s.query( db.Team).filter(db.Team.name == data['teamName']).all() if team: team = team[0] teamuser = s.query(db.TeamUser).filter( db.and_(db.TeamUser.team == team, db.TeamUser.user == user)).all() if teamuser: teamuser = teamuser[0] if teamuser.role >= 1000: s.delete(team) s.commit() return jsonify(done=True) return err.return_json_error( err.error.TEAM_ROLE_WAS_TOO_LOW) else: return err.return_json_error(err.error.TEAM_NOT_FOUND) else: return err.return_json_error(err.error.USER_NOT_PRIVILEGED) else: return user['return']
def teamCreate(): data = request.get_json(silent=False) if ('currentID' not in data) or ('username' not in data) or ('teamName' not in data): return err.return_json_error(err.error.JSON_STRUCTURE_WRONG) else: user = chk.checkUser(s, username=data['username'], currentID=data['currentID']) if not user['error']: user = user['user'] if user.rights > 100: teamExist = s.query( db.Team).filter(db.Team.name == data['teamName']).all() if teamExist: return err.return_json_error(err.error.TEAM_ALREADY_EXISTS) team = db.Team(name=data['teamName']) teammember = db.TeamUser(user=user, team=team, role=1000) s.add(team) s.add(teammember) s.commit() return jsonify({ "name": team.name, "members": [{ "name": user.name }] }) else: return err.return_json_error(err.error.USER_NOT_PRIVILEGED) else: return user['return']
def friendAccept(): data = request.get_json(silent=False) if ('currentID' not in data) or ('username' not in data) or ('friendsname' not in data): return err.return_json_error(err.error.JSON_STRUCTURE_WRONG) else: user = chk.checkUser(s, username=data['username'], currentID=data['currentID']) friend = s.query( db.User).filter(db.User.name == data['friendsname']).all() if not user['error']: user = user['user'] if friend: friend = friend[0] friendship = s.query(db.UserFriend).filter( db.and_(db.UserFriend.user1 == friend, db.UserFriend.user2 == user)).all() if friendship: friendship[0].accepted = True s.commit() return jsonify(done=True) return err.return_json_error(err.error.FRIEND_NOT_FOUND) else: return err.return_json_error(err.error.FRIEND_NOT_FOUND) else: return user['return']
def checkUser(session, username="******", password="******", currentID="_"): user = 0 if currentID == "_": user = session.query(db.User).filter( db.and_(db.User.name == username, db.User.password == password)).one() if user: if user.confirmed: currentID = ''.join( random.choice(ALPHABET) for i in range(128)) timingID = datetime.now() + timedelta(days=1) user.currentID = currentID user.validUntilID = timingID session.commit() else: return { 'error': True, 'return': err.return_json_error(err.error.USER_NOT_CONFIRMED) } else: return { 'error': True, 'return': err.return_json_error(err.error.USER_NOT_LOGGED_IN) } else: user = session.query(db.User).filter( db.and_(db.User.name == username, db.User.currentID == currentID)).all() if user: user = user[0] if user.validUntilID <= datetime.now(): return { 'error': True, 'return': err.return_json_error(err.error.USER_LOGIN_EXPIRED) } if not user.confirmed: return { 'error': True, 'return': err.return_json_error(err.error.USER_NOT_CONFIRMED) } else: return { 'error': True, 'return': err.return_json_error(err.error.USER_NOT_LOGGED_IN) } return {'error': False, 'user': user}
def register(): data = request.get_json(silent=False) if ('salt' not in data) or \ ('username' not in data) or \ ('password' not in data) or \ ('email' not in data): userID = ''.join(random.choice(ALPHABET) for i in range(128)) return jsonify(salt=userID) else: ret = s.query(db.User).filter(db.User.name == data['username']).all() if ret: return err.return_json_error(err.error.USER_NAME_ALREADY_USED) else: newUser = db.User(name=data['username'], email=data['email'], password=data['password'], salt=data['salt'], confirmed=False) userID = ''.join(random.choice(ALPHABET) for i in range(128)) timingID = datetime.now() + timedelta(days=7) newUser.currentID = userID newUser.validUntilID = timingID s.add(newUser) s.commit() email_handlr.sendConfirmation(userID, data['username'], data['email']) return jsonify(validUntilID=newUser.validUntilID)
def userGet(): username = request.args.get('username', default='*', type=str) user = s.query(db.User).filter(db.User.name == username).all() if user: user = user[0] return jsonify({'userID': user.id, 'rights': user.rights}) return err.return_json_error(err.error.JSON_STRUCTURE_WRONG)
def login(): data = request.get_json(silent=False) if 'username' not in data: return err.return_json_error(err.error.JSON_STRUCTURE_WRONG) elif 'password' not in data: ret = s.query(db.User).filter(db.User.name == data['username']).one() if ret: return jsonify(salt=ret.salt) return err.return_json_error(err.error.USER_NAME_NOT_FOUND) else: ret = chk.checkUser(s, username=data['username'], password=data['password']) if not ret['error']: user = ret['user'] return jsonify( #id=user.id, currentID=user.currentID, validUntilID=user.validUntilID, rights=user.rights) return ret['return']
def registerConfirm(): username = request.args.get('username', default='*', type=str) confirmID = request.args.get('confirmID', default='*', type=str) if ('confirmID' == '*') or ('username' == '*'): return err.return_json_error(err.error.JSON_STRUCTURE_WRONG) else: user = s.query(db.User).filter(db.User.name == username).all() if user: user = user[0] if not user.confirmed: if user.currentID == confirmID: user.confirmed = True user.validUntilID = datetime.now() s.commit() return "Successful" else: return "Identification ID wrong." else: return "Already confirmed." else: return err.return_json_error(err.error.USER_NAME_NOT_FOUND)
def teamMemberAdd(): data = request.get_json(silent=False) if ('currentID' not in data) or ('username' not in data) or ( 'teamName' not in data) or ('userNameToAdd' not in data) or ('userRoleToAdd' not in data): return err.return_json_error(err.error.JSON_STRUCTURE_WRONG) else: user = chk.checkUser(s, username=data['username'], currentID=data['currentID']) if not user['error']: user = user['user'] team = s.query( db.Team).filter(db.Team.name == data['teamName']).all() if team: team = team[0] teamuser = s.query(db.TeamUser).filter( db.and_(db.TeamUser.team == team, db.TeamUser.user == user)).all() if teamuser: teamuser = teamuser[0] if teamuser.role >= 100: newuser = s.query(db.User).filter( db.User.name == data['userNameToAdd']) if newuser: newuser = newuser[0] teamnewuser = s.query(db.TeamUser).filter( db.and_(db.TeamUser.team == team, db.TeamUser.user == newuser)).all() if teamnewuser: return err.return_json_error( err.error.TEAM_USER_EXISTS) else: teamnewuser = db.TeamUser( user=newuser, team=team, role=data['userRoleToAdd']) s.add(teamnewuser) s.commit() return jsonify(done=True) else: return err.return_json_error( err.error.USER_NAME_NOT_FOUND) else: return err.return_json_error( err.error.TEAM_ROLE_WAS_TOO_LOW) else: return err.return_json_error( err.error.TEAM_ROLE_WAS_TOO_LOW) else: return err.return_json_error(err.error.TEAM_NOT_FOUND) else: return user['return']
def messageRemove(): data = request.get_json(silent=False) if ('currentID' not in data) or ('username' not in data) or ('messageID' not in data): return err.return_json_error(err.error.JSON_STRUCTURE_WRONG) else: user = chk.checkUser(s, username=data['username'], currentID=data['currentID']) if not user['error']: user = user['user'] mess = s.query(db.Message).filter( db.and_(db.Message.id == data['messageID'], db.Message.receiver == user)).all() if mess: s.delete(mess[0]) s.commit() return jsonify(done=True) return jsonify(done=False) else: return user['return']