2016-04-20 14:45:42 +03:00
|
|
|
"""Example business services module."""
|
2016-04-20 14:19:54 +03:00
|
|
|
|
|
|
|
|
2016-11-12 00:08:47 +03:00
|
|
|
class BaseService(object):
|
|
|
|
"""Service base class."""
|
|
|
|
|
|
|
|
|
|
|
|
class UsersService(BaseService):
|
2016-04-20 14:19:54 +03:00
|
|
|
"""Users service."""
|
|
|
|
|
2016-10-06 22:48:43 +03:00
|
|
|
def __init__(self, logger, db):
|
2016-11-12 00:08:47 +03:00
|
|
|
"""Initializer.
|
|
|
|
|
|
|
|
:param logger: Logger instance.
|
|
|
|
:type logger: logging.Logger
|
|
|
|
|
|
|
|
:param db: Database connection.
|
|
|
|
:type db: sqlite3.Connection
|
|
|
|
"""
|
2016-10-06 22:48:43 +03:00
|
|
|
self.logger = logger
|
2016-04-20 14:19:54 +03:00
|
|
|
self.db = db
|
|
|
|
|
2016-10-06 22:48:43 +03:00
|
|
|
def get_user_by_id(self, uid):
|
2016-11-12 00:08:47 +03:00
|
|
|
"""Return user's data by identifier.
|
|
|
|
|
|
|
|
:param uid: User identifier.
|
|
|
|
:type uid: int
|
|
|
|
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
2016-10-06 22:48:43 +03:00
|
|
|
self.logger.debug('User %s has been found in database', uid)
|
2016-11-12 00:08:47 +03:00
|
|
|
return dict(uid=uid, password_hash='secret_hash')
|
2016-04-20 14:19:54 +03:00
|
|
|
|
|
|
|
|
2016-11-12 00:08:47 +03:00
|
|
|
class AuthService(BaseService):
|
|
|
|
"""Authentication service."""
|
2016-04-20 14:19:54 +03:00
|
|
|
|
2016-10-06 22:48:43 +03:00
|
|
|
def __init__(self, logger, db, token_ttl):
|
2016-11-12 00:08:47 +03:00
|
|
|
"""Initializer.
|
|
|
|
|
|
|
|
:param logger: Logger instance.
|
|
|
|
:type logger: logging.Logger
|
|
|
|
|
|
|
|
:param db: Database connection.
|
|
|
|
:type db: sqlite3.Connection
|
|
|
|
|
|
|
|
:param token_ttl: Token lifetime in seconds.
|
|
|
|
:type token_ttl: int
|
|
|
|
"""
|
2016-10-06 22:48:43 +03:00
|
|
|
self.logger = logger
|
2016-04-20 14:19:54 +03:00
|
|
|
self.db = db
|
|
|
|
self.token_ttl = token_ttl
|
|
|
|
|
|
|
|
def authenticate(self, user, password):
|
2016-11-12 00:08:47 +03:00
|
|
|
"""Authenticate user.
|
|
|
|
|
|
|
|
:param user: User's data.
|
|
|
|
:type user: dict
|
|
|
|
|
|
|
|
:param password: User's password for verification.
|
|
|
|
:type password: str
|
|
|
|
|
|
|
|
:raises: AssertionError when password is wrong
|
|
|
|
|
|
|
|
:rtype: None
|
|
|
|
"""
|
2016-04-20 14:19:54 +03:00
|
|
|
assert user['password_hash'] == '_'.join((password, 'hash'))
|
2016-10-06 22:48:43 +03:00
|
|
|
self.logger.debug('User %s has been successfully authenticated',
|
|
|
|
user['uid'])
|
2016-04-20 14:19:54 +03:00
|
|
|
|
|
|
|
|
2016-11-12 00:08:47 +03:00
|
|
|
class PhotosService(BaseService):
|
2016-04-20 14:19:54 +03:00
|
|
|
"""Photos service."""
|
|
|
|
|
2016-10-06 22:48:43 +03:00
|
|
|
def __init__(self, logger, db, s3):
|
2016-11-12 00:08:47 +03:00
|
|
|
"""Initializer.
|
|
|
|
|
|
|
|
:param logger: Logger instance.
|
|
|
|
:type logger: logging.Logger
|
|
|
|
|
|
|
|
:param db: Database connection.
|
|
|
|
:type db: sqlite3.Connection
|
|
|
|
|
|
|
|
:param s3: AWS S3 client.
|
2017-03-15 18:26:59 +03:00
|
|
|
:type s3: botocore.client.S3
|
2016-11-12 00:08:47 +03:00
|
|
|
"""
|
2016-10-06 22:48:43 +03:00
|
|
|
self.logger = logger
|
2016-04-20 14:19:54 +03:00
|
|
|
self.db = db
|
|
|
|
self.s3 = s3
|
|
|
|
|
2016-10-06 22:48:43 +03:00
|
|
|
def upload_photo(self, uid, photo_path):
|
2016-11-12 00:08:47 +03:00
|
|
|
"""Upload user photo.
|
|
|
|
|
|
|
|
:param uid: User identifier.
|
|
|
|
:type uid: int
|
|
|
|
|
|
|
|
:param photo_path: Path to photo for uploading.
|
|
|
|
:type photo_path: str
|
|
|
|
|
|
|
|
:rtpe: None
|
|
|
|
"""
|
2016-10-06 22:48:43 +03:00
|
|
|
self.logger.debug('Photo %s has been successfully uploaded by user %s',
|
|
|
|
photo_path, uid)
|