UserService.UserAPI package

Submodules

UserService.UserAPI.app module

UserService.UserAPI.auth module

UserService.UserAPI.config module

UserService.UserAPI.service module

class UserService.UserAPI.service.Service(*args, **kwargs)

Bases: object

init_model(tables)
static init_orm(model_config)
query_api(api_name, request_method, request_params={}, headers=None, body=None)

method used for api queries — api_name: registered api name in api_config request_method: “get”, “post”, “put”, “delete” request_params: request parameters headers: request headers body: request body — USAGE: api_config = { “api_name1”: “http:api1:5000/path/to/api”, “api_name2”: “http:api2:5000/path/to/api”, }

query_db(query, args=(), retval=False)

method used for sql database queries — query: sql query args: sql query arguments retval: True for SELECT | FALSE for INSERT, DELETE, etc

query_model(model_name)

Method used for orm database queries — USAGE: with self.query_model(<TABLE NAME>) as (conn, <TABLE>):

res = conn.execute(…)

— model_name: table name

UserService.UserAPI.service.default_validator(col_type, val)
UserService.UserAPI.service.insert_into_dict(dest, keys, vals)
UserService.UserAPI.service.validate(self, data, optional=False, drop=False, exclude=[])

Validator method for request body. Injected into sqlalchemy Model. — USAGE: with self.query_model(<TABLE NAME>) as (conn, <TABLE>):

verified_json_body, status = <TABLE>.validate(<REQUEST BODY>) if status:

# validation success

else:

# validation fail

— data: json dict — Returns dict of validated values mapped to model.

UserService.UserAPI.service.validator(*args)

Decorator which decorates validation functions. — USAGE:

# single value validator @validator(“Reservation.user”) def reservation_date_validator(user): # example validator

if user is cond:

return True

return False

# multiple value validator @validator(“Reservation.start_date”, “Reservation.end_date”) def reservation_date_validator(start_date, end_date): # example validator

if start_date is cond and end_date is cond:

return True

return False

UserService.UserAPI.users module

UserService.UserAPI.utils module

UserService.UserAPI.utils.admin_only()

This is a Python decorator function that protects endpoints requiring authorization by checking the identity type and returning an error message if unauthorized. :return: The admin_only function returns a decorator function wrapper, which in turn returns a decorator function decorator. The decorator function checks if the user identity retrieved from retrieve_jwt() has a type of 1 (which presumably means an admin user), and if so, it calls the original function fn with the given arguments and returns its result. If the user identity

UserService.UserAPI.utils.create_register_confirmation_email(user, sender, title='[회의실 예약 시스템] 회원가입이 완료되었습니다.', template_name='template.txt')

Generates an alert email for when new user is registered. sender: email address that sends out alert emails title: email title template_name: email body template file name

Returns a dict for POST alertservice/alert body

UserService.UserAPI.utils.protected()

This is a Python decorator function that protects endpoints requiring authorization by checking the identity of the user. :return: The protected() function returns a decorator function wrapper(), which in turn returns a decorated function decorator(). The decorated function decorator() checks if the user is authorized to access the endpoint by retrieving the JWT token and checking the identity type and ID. If the user is authorized, it calls the original function fn(*args, **kwargs) and returns its result. If

UserService.UserAPI.utils.retrieve_jwt()

This function attempts to retrieve a JSON Web Token (JWT) from a request and returns the “sub” claim if successful, otherwise it returns None and prints the error message. :return: the value of the “sub” claim in the JSON Web Token (JWT) retrieved from the request, if the token is verified successfully. If there is an exception, the function prints the error message and returns None.

UserService.UserAPI.utils.serialize(data, include=[], exclude=[])

The function “serialize” takes in data and returns a dictionary with specified keys included or excluded.

Parameters:
  • data – The data parameter is a dictionary containing the data that needs to be serialized

  • include – A list of keys that should be included in the serialized output. If this parameter

is not empty, only the keys specified in the list will be included in the output :param exclude: The exclude parameter is a list of keys that should be excluded from the serialized output. If a key in the exclude list is found in the data dictionary, it will not be included in the returned retval :return: The function serialize returns a dictionary containing a subset of the input data dictionary, based on the include and exclude parameters. If include is not empty, only the keys specified in include are included in the output dictionary. If exclude is not empty, the keys specified in exclude are excluded from the output dictionary. If both `include

UserService.UserAPI.validators module

Module contents