Skip to content
This repository has been archived by the owner on Sep 30, 2019. It is now read-only.

캐시 및 코드 스타일 수정 #7

Merged
merged 3 commits into from Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 10 additions & 20 deletions hermes/cache.py
Expand Up @@ -26,32 +26,22 @@ async def destroy(cls):

cls.redis = None

@classmethod
async def set(cls, directory, key, value, expire=None):
await cls.redis.set(f"user:{directory}:{key}", value, expire=expire)

@classmethod
async def get(cls, directory, key):
return await cls.redis.get(f"user:{directory}:{key}")
class UserCache:
_key_template = "hermes:info:{0}"

@classmethod
async def delete(cls, directory, key):
await cls.redis.delete(f"user:{directory}:{key}")


class UserCache:
@staticmethod
async def set(user_id, user_info):
async def set(cls, user_id, user_info):
user_info = json.dumps(user_info)
await Cache.set("info", user_id, user_info)
await Cache.redis.set(cls._key_template.format(user_id), user_info)

@staticmethod
async def get(user_id):
user_info = await Cache.get("info", user_id)
@classmethod
async def get(cls, user_id):
user_info = await Cache.redis.get(cls._key_template.format(user_id))
user_info = json.loads(user_info) if user_info else None

return user_info

@staticmethod
async def delete(user_id):
await Cache.delete("info", user_id)
@classmethod
async def delete(cls, user_id):
await Cache.redis.delete(cls._key_template.format(user_id))
1 change: 1 addition & 0 deletions hermes/exceptions.py
Expand Up @@ -5,6 +5,7 @@
class Conflict(SanicException):
pass


@add_status_code(400)
class BadRequest(SanicException):
pass
17 changes: 9 additions & 8 deletions hermes/manager.py
Expand Up @@ -8,21 +8,23 @@ async def register_admin(self, admin_id, admin_name, admin_email, admin_password
if await Admin.query_by_id(admin_id):
raise Conflict("Admin already exists")

new_admin = Admin(admin_id=admin_id,
admin_name=admin_name,
admin_email=admin_email,
admin_password=admin_password,
admin_type=admin_type.name)
new_admin = Admin(
admin_id=admin_id,
admin_name=admin_name,
admin_email=admin_email,
admin_password=admin_password,
admin_type=admin_type.name
)

await new_admin.save()


class AdminBatchManager:
async def search_admins(self, **kwargs):
admins = None

try:
admins = await Admin.query(**kwargs)
except Exception as e:
except Exception:
raise BadRequest("Invalid query")

if not admins:
Expand All @@ -33,7 +35,6 @@ async def search_admins(self, **kwargs):

class AdminInfoManager:
async def get_admin_info(self, admin_id):
admin = None

admin = await UserCache.get(admin_id)
if not admin:
Expand Down
71 changes: 58 additions & 13 deletions hermes/model.py
Expand Up @@ -87,9 +87,15 @@ async def save(self):
created_at,
updated_at
) VALUES (%s, %s, %s, %s, %s, %s, %s)"""
await MySQLConnection.execute(query, self.admin_id, generate_password_hash(self.admin_password),
self.admin_type, self.admin_email, self.admin_name,
self.created_at, self.updated_at)
await MySQLConnection.execute(
query, self.admin_id,
generate_password_hash(self.admin_password),
self.admin_type,
self.admin_email,
self.admin_name,
self.created_at,
self.updated_at
)

async def update_info(self):
query = f"""
Expand All @@ -102,8 +108,14 @@ async def update_info(self):
admin_id = %s
"""

await MySQLConnection.execute(query, self.admin_email, self.admin_type,
self.admin_name, datetime.datetime.now(), self.admin_id)
await MySQLConnection.execute(
query,
self.admin_email,
self.admin_type,
self.admin_name,
datetime.datetime.now(),
self.admin_id
)

async def update_password(self):
query = f"""
Expand All @@ -113,7 +125,11 @@ async def update_password(self):
WHERE
admin_id = %s
"""
await MySQLConnection.execute(query, self.admin_password, datetime.datetime.now())
await MySQLConnection.execute(
query,
self.admin_password,
datetime.datetime.now()
)

async def delete(self):
query = f"""
Expand Down Expand Up @@ -263,9 +279,22 @@ async def save(self):
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""

await MySQLConnection.execute(query, self.email, generate_password_hash(self.password), self.applicant_name,
self.sex, self.birth_date, self.parent_name, self.parent_tel, self.applicant_tel,
self.address, self.post_code, self.image_path, self.created_at, self.updated_at)
await MySQLConnection.execute(
query,
self.email,
generate_password_hash(self.password),
self.applicant_name,
self.sex,
self.birth_date,
self.parent_name,
self.parent_tel,
self.applicant_tel,
self.address,
self.post_code,
self.image_path,
self.created_at,
self.updated_at
)

async def update_info(self):
query = f"""UPDATE {self.table_name}
Expand All @@ -281,13 +310,29 @@ async def update_info(self):
updated_at = %s
WHERE email = %s
"""
await MySQLConnection.execute(query, self.applicant_name, self.sex, self.birth_date, self.parent_name,
self.parent_tel, self.applicant_tel, self.address, self.post_code,
self.image_path, datetime.datetime.now(), self.email)
await MySQLConnection.execute(
query,
self.applicant_name,
self.sex,
self.birth_date,
self.parent_name,
self.parent_tel,
self.applicant_tel,
self.address,
self.post_code,
self.image_path,
datetime.datetime.now(),
self.email
)

async def change_password(self, new_password: str):
query = f"UPDATE {self.table_name} SET password = %s, updated_at = %s WHERE email = %s"
await MySQLConnection.execute(query, generate_password_hash(new_password), datetime.datetime.now(), self.email)
await MySQLConnection.execute(
query,
generate_password_hash(new_password),
datetime.datetime.now(),
self.email
)

@classmethod
async def query_by_email(cls, email: str) -> "Applicant":
Expand Down
2 changes: 1 addition & 1 deletion hermes/view.py
Expand Up @@ -23,7 +23,7 @@ async def post(self, request: Request):
admin_password = admin_info["password"]
admin_type = self.AdminType(int(admin_info["type"]))
admin_email = admin_info["email"]
except KeyError as e:
except KeyError:
raise BadRequest(f"Invalid Parameter")

await self.manager.register_admin(admin_id, admin_name, admin_email, admin_password, admin_type)
Expand Down