def login_in(): """ This API is used for login processing and returning token to an user responses: 401: description: incorrect login or password 200: description: return token to user returns: type: dict """ get_request = request.form jsonschema.validate(get_request, request_schemas.login_center_schema) login = get_request['login'] password = get_request['password'] if Center.is_center_exist(login, password): expiration_date = datetime.datetime.utcnow() + datetime.timedelta( seconds=60 * 60 * 24) id_center = str(Center.get_center_by_login(login).id) token = jwt.encode( { 'exp': expiration_date, 'login': login, 'id': id_center }, app.config['SECRET_KEY'], algorithm="HS256") AccessRequest.register_access_request(login, datetime.datetime.now()) db.session.commit() return {'token': token}, 200 else: return {'res': 'Incorrect login or password'}, 401
def get_all_centers(): """ This API is used for retrieving all centers responses: 200: description: - returns: type: dict """ return {"centers": Center.get_all_centers()}, 200
def register_center(): """ This API is used for creating center and save its instance to db responses: 200: description: Center was successfully registered returns: type: dict """ get_request = request.form jsonschema.validate(get_request, request_schemas.register_center_schema) login = get_request['login'] password = get_request['password'] address = get_request['address'] Center.create_center(login, password, address) db.session.commit() return {'res': "Center was successfully registered"}, 200
def register_access_request(cls, center_login, timestamp): """ Save AccessRequest record to DB :param center_login: The login of center, which requested an access to service :param timestamp: Time, when access have been requested :return: """ center_from_db_id = Center.get_center_by_login(center_login).id new_access_request = AccessRequest(center_id=center_from_db_id, timestamp=timestamp) db.session.add(new_access_request)
def get_certain_center(id): """ This API is used for retrieving certain center by its id parameters: - name: id type: string required: true description: the id of center responses: 200: description: - returns: type: dict """ return {"center": Center.get_certain_center(id)}, 200
def delete_animal(cls, id, center_login): """ Delete animal from DB :param id: :type int: The id of animal :param center_login: :type str The login of center """ on_delete = Animals.query.filter_by( id=id, center_id=Center.get_center_by_login(center_login).id).first() if not on_delete: raise NoAccessException( f"center {center_login} either is not owner of animal, or animal does not exists" ) db.session.delete(on_delete)
def create_animal(cls, name, center, species_name, age, price=None, description=None): """ Create animal instance to save it in db :param name: :type str Name of animal :param center: :type str Login of center :param species_name: :type str Name of specie :param age: :type str Age of animal :param price: :type str Price of animal :param description: :type str Description of animal :return: :type int The id of created animal """ center_from_db = Center.get_center_by_login(center).id species_from_db = Species.get_concrete_species_by_name(species_name).id new_animal = Animals(name=name, center_id=center_from_db, species_id=species_from_db, age=age, price=price, description=description) db.session.add(new_animal)