async def list_users(username: str = Query(None), email: EmailStr = Query(None), birth_date: date = Query(None), active: bool = Query(None), user=Depends(AuthorizationController.scan_token)): filters = get_filters(locals(), ["user"]) user_controler = UserController(user) return user_controler.fetch(filters)
def change_password(uid: int): schema = { "type": "object", "additionalProperties": False, "required": ['password'], "properties": { "password": { "type": "string", "minLength": 8 } } } data = dict(**request.form) try: validate(data, schema) except ValidationError as e: err = {"error": "ValidationError", "msg": e.message} return jsonify(err), 400 modified = UserController.change_password(uid, data['password']) if not modified: error = {"error": "NotFound", "msg": f"The user \"{uid}\" not found."} return jsonify(error), 404 else: return jsonify('password modified'), 200
def delete(uid: int): deleted = UserController.delete(uid) if not deleted: error = {"error": "NotFound", "msg": f"The user \"{uid}\" not found."} return jsonify(error), 404 return jsonify('deleted'), 200
def get_one(uid: int): user = UserController.get_one(uid) if user is None: error = {"error": "NotFound", "msg": f"The user \"{uid}\" not found."} return jsonify(error), 404 return jsonify(user.to_json()), 200
def create(): schema = { "type": "object", "additionalProperties": False, "required": ['fName', 'fLastName', 'email', 'password'], "properties": { "fName": { "type": "string" }, "sName": { "type": "string" }, "fLastName": { "type": "string" }, "sLastName": { "type": "string" }, "email": { "type": "string", "pattern": r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" }, "password": { "type": "string", "minLength": 8 } } } data = dict(**request.form) try: validate(data, schema) except ValidationError as e: err = {"error": "ValidationError", "msg": e.message} return jsonify(err), 400 data['f_name'] = data.pop('fName') data['f_last_name'] = data.pop('fLastName') data['s_name'] = data.pop('sName', None) data['s_last_name'] = data.pop('sLastName', None) try: uid = UserController.create(**data) return jsonify(uid), 200 except UniqueViolation as e: err = {"error": "UniqueViolation", "msg": "The email already exists."} return jsonify(err), 500
def get_user(self, *, id: str = None, credentials: Tuple[str, str] = None) -> UserController.model: if id: return UserController().get(id, raw=True) else: if credentials: username, password = credentials user = self.get_user_by_username(username) self.verify_user(user, password) return user else: raise HTTPException(404, detail="User not found")
def get_login(): schema = { "type": "object", "additionalProperties": False, "required": ['email', 'password'], "properties": { "email": { "type": "string", "pattern": r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" }, "password": { "type": "string", "minLength": 8 } } } data = dict(**request.form) try: validate(data, schema) except ValidationError as e: err = {"error": "ValidationError", "msg": e.message} return jsonify(err), 400 user = UserController.get_login(data["email"], data["password"]) if user is None: err = { "error": "Unauthorized", "msg": f"The user \"{data['email']}\" doesn\'t exist or the password doesn\'t match." } return jsonify(err), 401 return jsonify(user.to_json()), 200
def get_all(): users = list(map(lambda u: u.to_json(), UserController.get_all())) return jsonify(users), 200
def modify(uid: int): schema = { "type": "object", "additionalProperties": False, "properties": { "fName": { "type": "string" }, "sName": { "type": "string" }, "fLastName": { "type": "string" }, "sLastName": { "type": "string" }, "email": { "type": "string", "pattern": r"^([a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*|)$" } } } form = dict(**request.form) data = {} try: validate(form, schema) except ValidationError as e: err = {"error": "ValidationError", "msg": e.message} return jsonify(err), 400 for index in form: if form[index] != '': data[index] = form.get(index) if 'fName' in data: data['f_name'] = data.pop('fName') if 'fLastName' in data: data['f_last_name'] = data.pop('fLastName') if 'sName' in data: if data.get('sName') == 'null': data['s_name'] = None data.pop('sName') else: data['s_name'] = data.pop('sName') if 'sLastName' in data: if data.get('sLastName') == 'null': data['s_last_name'] = None data.pop('sLastName') else: data['s_last_name'] = data.pop('sLastName') modified = UserController.modify(uid, **data) if not modified: error = {"error": "NotFound", "msg": f"The user \"{uid}\" not found."} return jsonify(error), 404 else: return jsonify('modified'), 200
async def delete_user(id: str): user_controler = UserController() user_controler.delete(id)
async def edit_user(id: str, user_data: RegisterSchema.EditSchema): user_controler = UserController() return user_controler.update(id, user_data.dict())
async def create_user(user_data: RegisterSchema): user_controler = UserController() return user_controler.create(user_data.dict())
async def get_user(id: str = Path(...)): user_controler = UserController() return user_controler.get(id)
import os, sys, logging log = logging.getLogger() from bottle import post, request, response, abort from utils import Struct import time, re from controllers.users import UserController uc = UserController() _digits = re.compile('[0-9]+') # TODO: caching @post('/fever/') def endpoint(): result = Struct({'api_version': 1, 'auth': 0}) api_key = request.forms.get('api_key', None) if not api_key or 'api' not in request.query.keys(): log.debug("<- %s" % result) return result u = uc.get_user_by_api_key(api_key) if not u: log.debug("<- %s" % result) return result result.auth = 1
def get_user_by_username(self, username): user_controller = UserController() user = user_controller.fetch({'username': username}, raw=True) if not user: raise HTTPException(404, detail="Invalid credentials") return user[0]