11from mongoengine import *
2- from mongoengine import signals
32import mongoengine
43import os
5- import html
64from enum import IntEnum
75from datetime import datetime
86from zipfile import ZipFile , BadZipFile
2422 connect ('normal-oj' , host = MONGO_HOST )
2523
2624
27- def handler (event ):
28- '''
29- Signal decorator to allow use of callback functions as class decorators.
30- reference: http://docs.mongoengine.org/guide/signals.html
31- '''
32-
33- def decorator (fn ):
34-
35- def apply (cls ):
36- event .connect (fn , sender = cls )
37- return cls
38-
39- fn .apply = apply
40- return fn
41-
42- return decorator
43-
44-
45- @handler (signals .pre_save )
46- def escape_markdown (sender , document ):
47- document .markdown = html .escape (document .markdown )
48-
49-
5025class ZipField (FileField ):
5126
5227 def __init__ (self , max_size = 0 , ** ks ):
@@ -172,7 +147,6 @@ def info(self):
172147 }
173148
174149
175- @escape_markdown .apply
176150class Homework (Document ):
177151
178152 homework_name = StringField (
@@ -263,26 +237,7 @@ class ProblemDescription(EmbeddedDocument):
263237 db_field = 'sampleOutput' ,
264238 )
265239
266- def escape (self ):
267- self .description , self .input , self .output , self .hint = (html .escape (
268- v or '' ) for v in (
269- self .description ,
270- self .input ,
271- self .output ,
272- self .hint ,
273- ))
274- _io = zip (self .sample_input , self .sample_output )
275- for i , (ip , op ) in enumerate (_io ):
276- self .sample_input [i ] = ip or html .escape (ip )
277- self .sample_output [i ] = op or html .escape (op )
278-
279-
280- @handler (signals .pre_save )
281- def problem_desc_escape (sender , document ):
282- document .description .escape ()
283-
284240
285- @problem_desc_escape .apply
286241class Problem (Document ):
287242
288243 class Visibility :
@@ -419,7 +374,6 @@ class Submission(Document):
419374 ip_addr = StringField (default = None , null = True )
420375
421376
422- @escape_markdown .apply
423377class Message (Document ):
424378 timestamp = DateTimeField (default = datetime .now )
425379 sender = StringField (max_length = 16 , required = True )
@@ -429,7 +383,6 @@ class Message(Document):
429383 markdown = StringField (max_length = 100000 , required = True )
430384
431385
432- @escape_markdown .apply
433386class Announcement (Document ):
434387 status = IntField (default = 0 , choices = [0 , 1 ]) # not delete / delete
435388 title = StringField (max_length = 64 , required = True )
@@ -442,7 +395,6 @@ class Announcement(Document):
442395 pinned = BooleanField (default = False )
443396
444397
445- @escape_markdown .apply
446398class PostThread (Document ):
447399 markdown = StringField (default = '' , required = True , max_length = 100000 )
448400 author = ReferenceField ('User' , db_field = 'author' )
0 commit comments