2023-05-12 02:32:50 +08:00
|
|
|
import logging
|
|
|
|
from typing import List, Optional
|
|
|
|
|
|
|
|
import strawberry # noqa
|
2024-02-16 23:48:19 +08:00
|
|
|
|
2024-07-27 03:00:08 +08:00
|
|
|
from felicity.api.gql.auth import auth_from_info
|
2024-01-27 01:08:02 +08:00
|
|
|
from felicity.api.gql.noticeboard.types import NoticeType
|
2024-02-16 23:48:19 +08:00
|
|
|
from felicity.api.gql.permissions import IsAuthenticated
|
|
|
|
from felicity.api.gql.types import DeletedItem, DeleteResponse, OperationError
|
2024-07-27 05:29:01 +08:00
|
|
|
from felicity.apps.noticeboard import schemas
|
|
|
|
from felicity.apps.noticeboard.services import NoticeService
|
|
|
|
from felicity.apps.setup.services import DepartmentService
|
2024-07-28 03:52:31 +08:00
|
|
|
from felicity.apps.user.services import GroupService, UserService
|
2023-05-12 02:32:50 +08:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
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[str]]
|
|
|
|
departments: Optional[List[str]]
|
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class NoticeMutations:
|
2023-07-26 15:23:28 +08:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
2023-05-12 02:32:50 +08:00
|
|
|
async def create_notice(self, info, payload: NoticeInputType) -> NoticeResponse:
|
|
|
|
|
2024-07-27 03:00:08 +08:00
|
|
|
felicity_user = await auth_from_info(info)
|
2023-05-12 02:32:50 +08:00
|
|
|
|
|
|
|
if not payload.title or not payload.body or not payload.expiry:
|
|
|
|
return OperationError(
|
|
|
|
error="Some fields have missing required data",
|
|
|
|
suggestion="Make sure that the fields: [title, body, expiry] all have values",
|
|
|
|
)
|
|
|
|
|
2024-07-27 05:29:01 +08:00
|
|
|
exists = await NoticeService().get(title=payload.title)
|
2023-05-12 02:32:50 +08:00
|
|
|
if exists:
|
|
|
|
return OperationError(
|
|
|
|
error="Notice title Duplication not Allowed",
|
|
|
|
suggestion=f"Change the notice title",
|
|
|
|
)
|
|
|
|
|
|
|
|
incoming = {
|
|
|
|
"created_by_uid": felicity_user.uid,
|
|
|
|
"updated_by_uid": felicity_user.uid,
|
|
|
|
}
|
|
|
|
for k, v in payload.__dict__.items():
|
|
|
|
incoming[k] = v
|
|
|
|
|
|
|
|
if payload.groups:
|
|
|
|
incoming["groups"] = []
|
|
|
|
for g_uid in payload.groups:
|
2024-07-27 05:29:01 +08:00
|
|
|
_gr = GroupService().get(uid=g_uid)
|
2023-05-12 02:32:50 +08:00
|
|
|
if _gr:
|
|
|
|
incoming["groups"].append(_gr)
|
|
|
|
|
|
|
|
if payload.departments:
|
|
|
|
incoming["departments"] = []
|
|
|
|
for dept_uid in payload.departments:
|
2024-07-27 05:29:01 +08:00
|
|
|
_gr = DepartmentService().get(uid=dept_uid)
|
2023-05-12 02:32:50 +08:00
|
|
|
if _gr:
|
|
|
|
incoming["departments"].append(_gr)
|
|
|
|
|
|
|
|
obj_in = schemas.NoticeCreate(**incoming)
|
2024-07-27 05:29:01 +08:00
|
|
|
notice = await NoticeService().create(obj_in)
|
2023-05-12 02:32:50 +08:00
|
|
|
return NoticeType(**notice.marshal_simple())
|
|
|
|
|
2023-07-26 15:23:28 +08:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
2023-05-12 02:32:50 +08:00
|
|
|
async def update_notice(
|
|
|
|
self, info, uid: str, payload: NoticeInputType
|
|
|
|
) -> NoticeResponse:
|
|
|
|
|
2024-07-27 03:00:08 +08:00
|
|
|
felicity_user = await auth_from_info(info)
|
2023-05-12 02:32:50 +08:00
|
|
|
|
2024-07-27 05:29:01 +08:00
|
|
|
notice = await NoticeService().get(uid=uid)
|
2023-05-12 02:32:50 +08:00
|
|
|
if not notice:
|
|
|
|
raise OperationError(
|
|
|
|
error=f"notice with uid {uid} does not exist",
|
|
|
|
suggestion=f"Refresh page",
|
|
|
|
)
|
|
|
|
|
|
|
|
notice_data = notice.to_dict()
|
|
|
|
for field in notice_data:
|
|
|
|
if field in payload.__dict__:
|
|
|
|
try:
|
|
|
|
setattr(notice, field, payload.__dict__[field])
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning(e)
|
|
|
|
|
|
|
|
if payload.groups:
|
|
|
|
_groups = []
|
|
|
|
for g_uid in payload.groups:
|
2024-07-27 05:29:01 +08:00
|
|
|
_gr = GroupService().get(uid=g_uid)
|
2023-05-12 02:32:50 +08:00
|
|
|
if _gr:
|
|
|
|
_groups.append(_gr)
|
|
|
|
setattr(notice, "groups", _groups)
|
|
|
|
|
|
|
|
if payload.departments:
|
|
|
|
_departments = []
|
|
|
|
for dept_uid in payload.departments:
|
2024-07-27 05:29:01 +08:00
|
|
|
_gr = DepartmentService().get(uid=dept_uid)
|
2023-05-12 02:32:50 +08:00
|
|
|
if _gr:
|
|
|
|
_departments.append(_gr)
|
|
|
|
setattr(notice, "departments", _departments)
|
|
|
|
|
|
|
|
setattr(notice, "update_by_uid", felicity_user.uid)
|
|
|
|
|
|
|
|
notice_in = schemas.NoticeUpdate(**notice.to_dict())
|
2024-07-27 05:29:01 +08:00
|
|
|
notice = await NoticeService().update(notice.uid, notice_in)
|
2023-05-12 02:32:50 +08:00
|
|
|
return NoticeType(**notice.marshal_simple())
|
|
|
|
|
2023-07-26 15:23:28 +08:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
2023-09-11 13:02:05 +08:00
|
|
|
async def view_notice(self, info, uid: str, viewer: str) -> NoticeType:
|
2023-05-12 02:32:50 +08:00
|
|
|
|
2024-07-27 03:00:08 +08:00
|
|
|
felicity_user = await auth_from_info(info)
|
2023-05-12 02:32:50 +08:00
|
|
|
|
2024-07-27 05:29:01 +08:00
|
|
|
notice = await NoticeService().get(uid=uid)
|
2023-05-12 02:32:50 +08:00
|
|
|
if not notice:
|
|
|
|
raise Exception(f"Notice with uid {uid} does not exist")
|
|
|
|
|
2024-07-27 05:29:01 +08:00
|
|
|
_viewer = await UserService().get(uid=viewer)
|
2023-05-12 02:32:50 +08:00
|
|
|
if not _viewer:
|
|
|
|
raise Exception(f"User with uid {viewer} does not exist")
|
|
|
|
|
2024-07-27 05:29:01 +08:00
|
|
|
notice = await NoticeService().add_viewer(notice.uid, _viewer)
|
2023-05-12 02:32:50 +08:00
|
|
|
return NoticeType(**notice.marshal_simple())
|
|
|
|
|
2023-07-26 15:23:28 +08:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
2023-05-12 02:32:50 +08:00
|
|
|
async def delete_notice(self, info, uid: str) -> DeleteResponse:
|
|
|
|
|
2024-07-27 03:00:08 +08:00
|
|
|
felicity_user = await auth_from_info(info)
|
2023-05-12 02:32:50 +08:00
|
|
|
|
2024-07-27 05:29:01 +08:00
|
|
|
notice = await NoticeService().get(uid=uid)
|
2023-05-12 02:32:50 +08:00
|
|
|
if not notice:
|
|
|
|
raise Exception(f"Notice with uid {uid} does not exist")
|
|
|
|
|
|
|
|
await notice.delete()
|
|
|
|
return DeletedItem(uid=uid)
|