Coverage for model/announcement.py: 100%
65 statements
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-05 04:22 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-05 04:22 +0000
1from datetime import datetime
2from flask import Blueprint, request
4from mongo import *
5from mongo.utils import *
6from .auth import *
7from .utils import *
8from .course import *
10__all__ = ['ann_api']
12ann_api = Blueprint('ann_api', __name__)
15@ann_api.route('/', methods=['GET'])
16@ann_api.route('/<ann_id>', methods=['GET'])
17def get_sys_ann(ann_id=None):
18 public_name = Course.get_public().course_name
19 anns = Announcement.ann_list(None, public_name)
20 data = [{
21 'annId': str(an.id),
22 'title': an.title,
23 'createTime': int(an.create_time.timestamp()),
24 'updateTime': int(an.update_time.timestamp()),
25 'creator': an.creator.info,
26 'updater': an.updater.info,
27 'markdown': an.markdown,
28 'pinned': an.pinned
29 } for an in anns if ann_id == None or str(an.id) == ann_id]
30 return HTTPResponse('Sys Ann bro', data=data)
33@ann_api.route('/', methods=['POST', 'PUT', 'DELETE'])
34@course_api.route('/<course_name>/ann', methods=['GET'])
35@ann_api.route('/<course_name>/<ann_id>', methods=['GET'])
36@login_required
37def anncmnt(user, course_name=None, ann_id=None):
39 def get_anns():
40 # Get an announcement list
41 try:
42 anns = Announcement.ann_list(user.obj, course_name or 'Public')
43 except (DoesNotExist, ValidationError):
44 return HTTPError('Cannot Access a Announcement', 403)
45 if anns is None:
46 return HTTPError('Announcement Not Found', 404)
47 data = [{
48 'annId': str(an.id),
49 'title': an.title,
50 'createTime': int(an.create_time.timestamp()),
51 'updateTime': int(an.update_time.timestamp()),
52 'creator': an.creator.info,
53 'updater': an.updater.info,
54 'markdown': an.markdown,
55 'pinned': an.pinned
56 } for an in anns if ann_id == None or str(an.id) == ann_id]
57 return HTTPResponse('Announcement List', data=data)
59 @Request.json('title', 'markdown', 'course_name', 'pinned')
60 def create(title, markdown, course_name, pinned):
61 # Create a new announcement
62 try:
63 ann = Announcement.new_ann(
64 title=title,
65 creator=user.obj,
66 markdown=markdown,
67 pinned=pinned,
68 course=course_name or 'Public',
69 )
70 except ValidationError as ve:
71 return HTTPError('Failed to Create Announcement',
72 400,
73 data=ve.to_dict())
74 if ann is None:
75 return HTTPError('Failed to Create Announcement', 403)
76 data = {
77 'annId': str(ann.id),
78 'createTime': int(ann.create_time.timestamp())
79 }
80 return HTTPResponse('Announcement Created', data=data)
82 @Request.json('ann_id', 'title', 'markdown', 'pinned')
83 def update(ann_id, title, markdown, pinned):
84 # Update an announcement
85 ann = Announcement(ann_id)
86 if not ann:
87 return HTTPError('Announcement Not Found', 404)
89 course = Course(ann.course)
90 if not course.permission(user, Course.Permission.GRADE):
91 return HTTPError('Failed to Update Announcement', 403)
92 try:
93 ann.update(
94 title=title,
95 markdown=markdown,
96 update_time=datetime.now(),
97 updater=user.obj,
98 pinned=pinned,
99 )
100 except ValidationError as ve:
101 return HTTPError(
102 'Failed to Update Announcement',
103 400,
104 data=ve.to_dict(),
105 )
106 return HTTPResponse('Updated')
108 @Request.json('ann_id')
109 def delete(ann_id):
110 # Delete an announcement
111 ann = Announcement(ann_id)
112 if not ann:
113 return HTTPError('Announcement Not Found', 404)
115 course = Course(ann.course)
116 if not course.permission(user, Course.Permission.GRADE):
117 return HTTPError('Failed to Delete Announcement', 403)
118 ann.update(status=1)
119 return HTTPResponse('Deleted')
121 methods = {
122 'GET': get_anns,
123 'POST': create,
124 'PUT': update,
125 'DELETE': delete
126 }
128 return methods[request.method]()