def friendRemove(): 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] isFriend = s.query(db.UserFriend).filter( db.or_( db.and_(db.UserFriend.user1 == user, db.UserFriend.user2 == friend), db.and_(db.UserFriend.user1 == friend, db.UserFriend.user2 == user))).all() if not isFriend: return jsonify(done=False) s.delete(isFriend[0]) s.commit() return jsonify(done=True) else: return err.return_json_error(err.error.FRIEND_NOT_FOUND) else: return user['return']
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 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 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 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 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']