def removeInsertUserFromContactList(user_id, contact_id): if request.method == 'DELETE': return UserHandler().removeUserFromContacts(user_id, contact_id) elif request.method == 'POST': return UserHandler().insertUserToContacts(user_id, contact_id) else: return jsonify(Error="Method not allowed."), 405
class Contacts(Resource): def __init__(self): self.handler = UserHandler() @jwt_required def post(self): parser = reqparse.RequestParser() parser.add_argument('first_name', help=HELP_TEXT, required=True) parser.add_argument('last_name', help=HELP_TEXT, required=True) parser.add_argument('email', help=HELP_TEXT) parser.add_argument('phone_number', help=HELP_TEXT) data = parser.parse_args() return self.handler.insert_contact(data) # @jwt_required def put(self): parser = reqparse.RequestParser() parser.add_argument('contact_id', help=HELP_TEXT, required=True) data = parser.parse_args() contact = self.handler.update_contact(1) return jsonify(contact=contact) @jwt_required def delete(self): parser = reqparse.RequestParser() parser.add_argument('contact_id', help=HELP_TEXT, required=True) data = parser.parse_args() return self.handler.remove_contact(data)
def manage_users(): if request.method == 'GET': if not request.data: return UserHandler().getAllUsers() else: row = request.get_json() return UserHandler().getUserByUsername(row['username'])
def getAllUsers(): if request.method == 'POST': print("REQUEST: ", request.json) return UserHandler().createUser(request.json) else: if not request.args: return UserHandler().getAllUsers()
def getMyContacts(pid): if request.method == 'GET': if not request.args: return UserHandler().getUserContacts(pid) else: return UserHandler().searchContacts(pid, request.args) elif request.method == 'POST': return ContactHandler().insertContact(pid, request.json)
def getUserContactList(user_id): if request.method == 'GET': return UserHandler().getUserContactList(user_id) elif request.method == 'PUT': return UserHandler().updateUser(user_id, request.json) elif request.method == 'DELETE': return UserHandler().removeUserFromContacts(user_id) else: return jsonify(Error="Method not allowed."), 405
def getUserById(user_id): if request.method == 'GET': return UserHandler().getUserById(user_id) elif request.method == 'PUT': return UserHandler().updateUser(user_id, request.json) elif request.method == 'DELETE': return UserHandler().deleteUser(user_id) else: return jsonify(Error="Method not allowed."), 405
class User(Resource): def __init__(self): self.handler = UserHandler() def get(self, user): if user.isdigit(): _user = self.handler.get_user_by_id(user) else: _user = self.handler.get_user_by_username(user) return jsonify(user=user)
def num_of_mess_per_day(uid): """ Gets numbers of posted messages by user per day :param uid: int :return: JSON """ return UserHandler().get_num_messages_user(uid)
def removeInsertUserFromToChat(chat_id, user_id): if request.method == 'POST': return ChatHandler().insertUserToChat(chat_id, user_id) elif request.method == 'DELETE': return UserHandler().removeUserFromChat(user_id, chat_id) else: return jsonify(Error="Method not allowed."), 405
class Contact(Resource): def __init__(self): self.handler = UserHandler() def get(self, uid): contacts = self.handler.get_contacts(uid) return jsonify(contacts=contacts)
class UserRegistration(Resource): def __init__(self): self.handler = UserHandler() def post(self): """ Registers user to application. :return: JSON """ # Arguments to be extracted from request parser = reqparse.RequestParser() parser.add_argument('username', help=HELP_TEXT, required=True) parser.add_argument('email', help=HELP_TEXT, required=True) parser.add_argument('password', help=HELP_TEXT, required=True) parser.add_argument('first_name', help=HELP_TEXT, required=True) parser.add_argument('last_name', help=HELP_TEXT, required=True) parser.add_argument('phone_number', help=HELP_TEXT, required=True) # Verifies required arguments are in request and extracts data into a dictionary data = parser.parse_args() response, status = self.handler.insert_user(data) return app.response_class(response=response, status=status, mimetype='application/json')
class Users(Resource): def __init__(self): self.handler = UserHandler() # @jwt_required def get(self): users = self.handler.get_users() return jsonify(users=users) # @jwt_required def put(self): parser = reqparse.RequestParser() parser.add_argument('new_username') data = parser.parse_args() if 'new_username' in data and data['new_username']: user = self.handler.update_user_username(username='******', new_username='******') return jsonify(user=user) else: return jsonify(msg='Bad request')
def getChatUsers(self, chat_id): dao = ChatsDAO() chat = dao.getChatById(chat_id) user_list = dao.getChatUsers(chat_id) if not user_list: return jsonify(Chat=user_list), 404 elif not chat: return jsonify(Error="Chat not found"), 404 else: result_list = [] for row in user_list: user = UserHandler.build_user_dict(UserHandler, row) result_list.append(user) return jsonify(Chat=result_list)
def post(self): """ Logs in existing user :return: JSON """ parser = reqparse.RequestParser() parser.add_argument('username', help=HELP_TEXT, required=True) parser.add_argument('password', help=HELP_TEXT, required=True) # Extracts data from request and verifies required data is available data = parser.parse_args() response, status = UserHandler().verify_password(data) return app.response_class(response=response, status=status, mimetype='application/json')
def active_users(): """ Gets daily active users from database :return: JSON """ return UserHandler().get_daily_active_users(), 200
def get_most_active(): if request.method == 'GET': return UserHandler().get_most_active_users_per_day() else: return jsonify(Error="Method not allowed"), 405
def manage_user_username(username): if request.method == 'GET': return UserHandler().getUserByUsername(username) return jsonify(Error="Method Not Allowed")
def manage_user(uid): if request.method == 'GET': return UserHandler().getUsersById(uid) elif request.method == 'PUT': return UserHandler().updateUser(request.json) return jsonify(Error="Method Not Allowed")
def register(): if request.method == 'POST': return UserHandler().insertUser(request.json) else: return "Register"
def manage_account(): if request.method == 'POST': print('Created New User: ', jsonify(request.json)) return UserHandler().createUser(request.json)
def status(): global isLoggedIn if isLoggedIn: return UserHandler().getUsersById(loggedUID) else: return jsonify("Logged in")
def __init__(self): self.handler = UserHandler()
def login(): if request.method == 'POST': return UserHandler().UserLogin(request.json) else: return "Login"
def users(): if request.method == 'GET': if not request.args: return UserHandler().getAllUsers() else: return UserHandler().searchUser(request.args)
def getUserById(pid): if request.method == 'GET': return UserHandler().getUserById(pid)
def getUserGroups(pid): if request.method == 'GET': return UserHandler().getUserGroups(pid) elif request.method == 'POST': return GroupHandler().createGroup(pid, request.json)
def getUsersThatReactToPostX(post_id, react_type): if request.method == 'GET': return UserHandler().getUsersThatReact(post_id, react_type) else: return jsonify(Error="Method not allowed."), 405
def login(): if request.method == 'POST': return UserHandler().login(request.json) else: return jsonify(Error="Method not allowed."), 405
def insertPhoneNumber(): if request.method == 'POST': return UserHandler().insertPhone(request.json)