2021-12-06 00:37:33 +08:00
|
|
|
import logging
|
|
|
|
from typing import Optional, List
|
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
import strawberry # noqa
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
from felicity.apps.noticeboard import schemas, models
|
2021-12-30 02:33:08 +08:00
|
|
|
from felicity.gql import auth_from_info, verify_user_auth, OperationError, DeleteResponse, DeletedItem
|
|
|
|
from felicity.gql.noticeboard.types import NoticeType
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
|
|
|
|
NoticeResponse = strawberry.union("NoticeResponse", (NoticeType, OperationError), # noqa
|
|
|
|
description="Union of possible outcomes when adding a new notice"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@strawberry.input
|
|
|
|
class NoticeInputType:
|
|
|
|
title: str
|
|
|
|
body: str
|
|
|
|
expiry: str
|
|
|
|
groups: Optional[List[int]]
|
|
|
|
departments: Optional[List[int]]
|
2021-12-08 19:51:36 +08:00
|
|
|
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class NoticeMutations:
|
|
|
|
@strawberry.mutation
|
2021-12-30 02:33:08 +08:00
|
|
|
async def create_notice(self, info, payload: NoticeInputType) -> NoticeResponse:
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
is_authenticated, felicity_user = await auth_from_info(info)
|
|
|
|
verify_user_auth(is_authenticated, felicity_user, "Only Authenticated user can create notices")
|
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
if not payload.title or not payload.body or not payload.expiry:
|
|
|
|
return OperationError(
|
2021-12-08 19:51:36 +08:00
|
|
|
error="Some fields have missing required data",
|
|
|
|
suggestion="Make sure that the fields: [title, body, expiry] all have values"
|
|
|
|
)
|
2021-12-06 00:37:33 +08:00
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
exists = await models.Notice.get(title=payload.title)
|
2021-12-06 00:37:33 +08:00
|
|
|
if exists:
|
2021-12-30 02:33:08 +08:00
|
|
|
return OperationError(
|
2021-12-08 19:51:36 +08:00
|
|
|
error="Notice title Duplication not Allowed",
|
|
|
|
suggestion=f"Change the notice title"
|
|
|
|
)
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
incoming = {
|
|
|
|
"created_by_uid": felicity_user.uid,
|
|
|
|
"updated_by_uid": felicity_user.uid
|
|
|
|
}
|
2021-12-30 02:33:08 +08:00
|
|
|
for k, v in payload.__dict__.items():
|
2021-12-06 00:37:33 +08:00
|
|
|
incoming[k] = v
|
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
if payload.groups:
|
2021-12-06 00:37:33 +08:00
|
|
|
incoming['groups'] = []
|
2021-12-30 02:33:08 +08:00
|
|
|
for g_uid in payload.groups:
|
2021-12-06 00:37:33 +08:00
|
|
|
_gr = models.Group.get(uid=g_uid)
|
|
|
|
if _gr:
|
|
|
|
incoming['groups'].append(_gr)
|
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
if payload.departments:
|
2021-12-06 00:37:33 +08:00
|
|
|
incoming['departments'] = []
|
2021-12-30 02:33:08 +08:00
|
|
|
for dept_uid in payload.departments:
|
2021-12-06 00:37:33 +08:00
|
|
|
_gr = models.Department.get(uid=dept_uid)
|
|
|
|
if _gr:
|
|
|
|
incoming['departments'].append(_gr)
|
|
|
|
|
|
|
|
obj_in = schemas.NoticeCreate(**incoming)
|
|
|
|
notice: models.Notice = await models.Notice.create(obj_in)
|
2021-12-13 00:20:48 +08:00
|
|
|
return NoticeType(**notice.marshal_simple())
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
@strawberry.mutation
|
2021-12-30 02:33:08 +08:00
|
|
|
async def update_notice(self, info, uid: int, payload: NoticeInputType) -> NoticeResponse:
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
is_authenticated, felicity_user = await auth_from_info(info)
|
|
|
|
verify_user_auth(is_authenticated, felicity_user, "Only Authenticated user can update notices")
|
|
|
|
|
|
|
|
notice = await models.Notice.get(uid=uid)
|
|
|
|
if not notice:
|
|
|
|
raise Exception(f"notice with uid {uid} does not exist")
|
|
|
|
|
|
|
|
notice_data = notice.to_dict()
|
|
|
|
for field in notice_data:
|
2021-12-30 02:33:08 +08:00
|
|
|
if field in payload.__dict__:
|
2021-12-06 00:37:33 +08:00
|
|
|
try:
|
2021-12-30 02:33:08 +08:00
|
|
|
setattr(notice, field, payload.__dict__[field])
|
2021-12-06 00:37:33 +08:00
|
|
|
except Exception as e:
|
|
|
|
logger.warning(e)
|
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
if payload.groups:
|
2021-12-06 00:37:33 +08:00
|
|
|
_groups = []
|
2021-12-30 02:33:08 +08:00
|
|
|
for g_uid in payload.groups:
|
2021-12-06 00:37:33 +08:00
|
|
|
_gr = models.Group.get(uid=g_uid)
|
|
|
|
if _gr:
|
|
|
|
_groups.append(_gr)
|
|
|
|
setattr(notice, "groups", _groups)
|
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
if payload.departments:
|
2021-12-06 00:37:33 +08:00
|
|
|
_departments = []
|
2021-12-30 02:33:08 +08:00
|
|
|
for dept_uid in payload.departments:
|
2021-12-06 00:37:33 +08:00
|
|
|
_gr = models.Department.get(uid=dept_uid)
|
|
|
|
if _gr:
|
|
|
|
_departments.append(_gr)
|
|
|
|
setattr(notice, "departments", _departments)
|
|
|
|
|
|
|
|
setattr(notice, "update_by_uid", felicity_user.uid)
|
|
|
|
|
|
|
|
notice_in = schemas.NoticeUpdate(**notice.to_dict())
|
|
|
|
notice = await notice.update(notice_in)
|
2021-12-30 02:33:08 +08:00
|
|
|
return NoticeType(**notice.marshal_simple())
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
@strawberry.mutation
|
|
|
|
async def view_notice(self, info, uid: int, viewer: int) -> NoticeType:
|
|
|
|
|
|
|
|
is_authenticated, felicity_user = await auth_from_info(info)
|
|
|
|
verify_user_auth(is_authenticated, felicity_user, "Only Authenticated user can view notices")
|
|
|
|
|
|
|
|
notice: models.Notice = await models.Notice.get(uid=uid)
|
|
|
|
if not notice:
|
|
|
|
raise Exception(f"Notice with uid {uid} does not exist")
|
|
|
|
|
|
|
|
_viewer = await models.User.get(uid=viewer)
|
|
|
|
if not _viewer:
|
|
|
|
raise Exception(f"User with uid {viewer} does not exist")
|
|
|
|
|
|
|
|
notice = await notice.add_viewer(_viewer)
|
2021-12-30 02:33:08 +08:00
|
|
|
return NoticeType(**notice.marshal_simple())
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
@strawberry.mutation
|
2021-12-30 02:33:08 +08:00
|
|
|
async def delete_notice(self, info, uid: int) -> DeleteResponse:
|
2021-12-06 00:37:33 +08:00
|
|
|
|
|
|
|
is_authenticated, felicity_user = await auth_from_info(info)
|
|
|
|
verify_user_auth(is_authenticated, felicity_user, "Only Authenticated user can view notices")
|
|
|
|
|
|
|
|
notice: models.Notice = await models.Notice.get(uid=uid)
|
|
|
|
if not notice:
|
|
|
|
raise Exception(f"Notice with uid {uid} does not exist")
|
|
|
|
|
|
|
|
await notice.delete()
|
2021-12-30 02:33:08 +08:00
|
|
|
return DeletedItem(uid)
|