aboutsummaryrefslogtreecommitdiffstats
path: root/src/pmod/pmod_test/py/pmod_test.py
blob: baa51511bc00a639cf3874adf36205c4db13003a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
from tojauth import TOJAuth
from asyncdb import AsyncDB
import mod
import com
import imc.async
from imc.proxy import Proxy
import config
from problem import Problem

class pmod_test(Problem):
    _pmod_name = 'pmod_test'

    def __init__(self, mod_idendesc, get_link_fn, proid):
        self._proid = proid
        self._idendesc = mod_idendesc
        self.get_link = get_link_fn

        self._proinfo = mod.ProblemMg.get_problem_info_by_proid(self._proid)
        self._accessid = mod.ProblemMg.get_accessid_by_proid(self._proid)

        self.db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, 
                          config.MOD_DBPASSWORD)

        self._reg_path = 'pro/' + str(self._proid) + '/'

        Proxy.instance.register_call(
            self._reg_path, 'add_mode', self.add_mode)
        Proxy.instance.register_call(
            self._reg_path, 'del_mode', self.del_mode)
        Proxy.instance.register_call(
            self._reg_path, 'set_mode', self.set_mode)
        Proxy.instance.register_call(
            self._reg_path, 'get_mode', self.get_mode)
        Proxy.instance.register_call(
            self._reg_path, 'list_mode', self.list_mode)
        Proxy.instance.register_call(
            self._reg_path, 'add_testmode', self.add_testmode)
        Proxy.instance.register_call(
            self._reg_path, 'del_testmode', self.del_testmode)
        Proxy.instance.register_call(
            self._reg_path, 'set_testmode', self.set_testmode)
        Proxy.instance.register_call(
            self._reg_path, 'get_testmode', self.get_testmode)
        Proxy.instance.register_call(
            self._reg_path, 'list_testmode', self.list_testmode)
        Proxy.instance.register_call(
            self._reg_path, 'set_testdata', self.set_testdata)
        Proxy.instance.register_call(
            self._reg_path, 'get_testdata', self.get_testdata)

    def unload(self, force):
        Proxy.instance.unregister_call(
            self._reg_path, 'add_mode')
        Proxy.instance.unregister_call(
            self._reg_path, 'del_mode')
        Proxy.instance.unregister_call(
            self._reg_path, 'set_mode')
        Proxy.instance.unregister_call(
            self._reg_path, 'get_mode')
        Proxy.instance.unregister_call(
            self._reg_path, 'list_mode')
        Proxy.instance.unregister_call(
            self._reg_path, 'add_testmode')
        Proxy.instance.unregister_call(
            self._reg_path, 'del_testmode')
        Proxy.instance.unregister_call(
            self._reg_path, 'set_testmode')
        Proxy.instance.unregister_call(
            self._reg_path, 'get_testmode')
        Proxy.instance.unregister_call(
            self._reg_path, 'set_testdata')
        Proxy.instance.unregister_call(
            self._reg_path, 'get_testdata')

    @staticmethod
    @TOJAuth.check_access(mod.ProblemMg._accessid, TOJAuth.ACCESS_CREATE)
    def create_problem_data(proid):
        db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, 
                     config.MOD_DBPASSWORD)

        cur = db.cursor()
        sqlstr = ('INSERT INTO "PMOD_TEST_MODE" ("proid", "modeid", '
                  '"content", "testmodeid") VALUES (%s, %s, %s, %s);')
        sqlarr = (proid, 1, '', None)
        cur.execute(sqlstr, sqlarr)        

    @staticmethod
    @TOJAuth.check_access(mod.ProblemMg._accessid, TOJAuth.ACCESS_DELETE)
    def delete_problem_data(proid):
        db = AsyncDB(config.MOD_DBNAME, config.MOD_DBUSER, 
                     config.MOD_DBPASSWORD)

        cur = db.cursor()
        sqlstr = ('DELETE FROM "PMOD_TEST_MODE" WHERE "proid" = %s;')
        sqlarr = (proid, )
        cur.execute(sqlstr, sqlarr)

        sqlstr = ('DELETE FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s;')
        sqlarr = (proid, )
        cur.execute(sqlstr, sqlarr)

        sqlstr = ('DELETE FROM "PMOD_TEST_TESTDATA" WHERE "proid" = %s;')
        sqlarr = (proid, )
        cur.execute(sqlstr, sqlarr)

    @imc.async.caller
    def add_mode(self, content, testmodeid):
        if(
            (content != None and type(content) != str) or
            (testmodeid != None and type(testmodeid) != int)
        ):
            return 'Eparameter'

        if testmodeid != None and not self._does_testmodeid_exist(testmodeid):
            return 'Etestmodeid'

        self._add_mode(None, content, testmodeid)

        return 'Success'

    def _add_mode(self, modeid, content, testmodeid):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqltab = ('INSERT INTO "PMOD_TEST_MODE" (')
        sqlcol = ('"proid", "content", "testmodeid"')
        sqlval = (') VALUES (%s, %s, %s')
        sqlend = (');')
        sqlarr = [self._proid, content, testmodeid]

        if modeid != None:
            sqlcol = sqlcol + ', "modeid"'
            sqlval = sqlval + ', %s'
            sqlarr.append(modeid)

        sqlstr = sqltab + sqlcol + sqlval + sqlend
        cur.execute(sqlstr, sqlarr)

    @imc.async.caller
    def del_mode(self, modeid):
        if(
            type(modeid) != int
        ):
            return 'Eparameter'

        if modeid == 1 or not self._does_modeid_exist(modeid):
            return 'Emodeid'

        self._del_mode(modeid)

        return 'Success'

    def _del_mode(self, modeid):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('DELETE FROM "PMOD_TEST_MODE" WHERE "proid" = %s AND '
                  '"modeid" = %s;')
        sqlarr = (self._proid, modeid)
        cur.execute(sqlstr, sqlarr)

    @imc.async.caller
    def set_mode(self, modeid, content, testmodeid):
        if(
            type(modeid) != int or
            (content != None and type(content) != str) or
            (testmodeid != None and type(testmodeid) != int)
        ):
            return 'Eparameter'

        if not self._does_modeid_exist(modeid):
            return 'Emodeid'

        if testmodeid != None and not self._does_testmodeid_exist(testmodeid):
            return 'Etestmodeid'

        self._set_mode(modeid, content, testmodeid)

        return 'Success'

    def _set_mode(self, modeid, content, testmodeid):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('UPDATE "PMOD_TEST_MODE" SET "content" = %s, '
                  '"testmodeid" = %s WHERE "proid" = %s AND "modeid" = %s;')
        sqlarr = (content, testmodeid, self._proid, modeid)
        cur.execute(sqlstr, sqlarr)

    @imc.async.caller
    def get_mode(self, modeid):
        if(
            type(modeid) != int
        ):
            return 'Eparameter'

        mode = self._get_mode_by_modeid(modeid)

        if mode == None:
            return 'Emodeid'

        return mode

    @imc.async.caller
    def list_mode(self):
        mode_list = self._list_mode()

        return mode_list        

    def _list_mode(self):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('SELECT "modeid", "testmodeid" FROM "PMOD_TEST_MODE" '
                  'WHERE "proid" = %s ORDER BY "modeid" ASC;')
        sqlarr = (self._proid, )
        cur.execute(sqlstr, sqlarr)

        mode_list = []
        for data in cur:
            obj = {}
            obj['modeid'] = data[0]
            obj['testmodeid'] = data[1]

            mode_list.append(obj)

        return mode_list
    
    @imc.async.caller
    def add_testmode(self, testmodename, timelimit, memlimit):
        if(
            type(testmodename) != str or
            (timelimit != None and type(timelimit) != int) or
            (memlimit != None and type(memlimit) != int)
        ):
            return 'Eparameter'

        if timelimit != None and timelimit < 0:
            return 'Etimelimit'
        if memlimit != None and memlimit < 0:
            return 'Ememlimit'

        self._add_testmode(testmodename, timelimit, memlimit)

        return 'Success'

    def _add_testmode(self, testmodename, timelimit, memlimit):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('INSERT INTO "PMOD_TEST_TESTMODE" ("proid", "testmodename", '
                  '"timelimit", "memlimit") VALUES (%s, %s, %s, %s);')
        sqlarr = (self._proid, testmodename, timelimit, memlimit)
        cur.execute(sqlstr, sqlarr)

    @imc.async.caller
    def del_testmode(self, testmodeid):
        if(
            type(testmodeid) != int
        ):
            return 'Eparameter'

        if not self._does_testmodeid_exist(testmodeid):
            return 'Etestmodeid'

        self._del_testmode(testmodeid)

        return 'Success'

    def _del_testmode(self, testmodeid):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('DELETE FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s AND '
                  '"testmodeid" = %s;')
        sqlarr = (self._proid, testmodeid)
        cur.execute(sqlstr, sqlarr)

        sqlstr = ('DELETE FROM "PMOD_TEST_TESTDATA" WHERE "proid" = %s AND '
                  '"testmodeid" = %s;')
        sqlarr = (self._proid, testmodeid)
        cur.execute(sqlstr, sqlarr)

    @imc.async.caller
    def set_testmode(self, testmodeid, testmodename, timelimit, memlimit):
        if(
            type(testmodeid) != int or
            type(testmodename) != str or
            (timelimit != None and type(timelimit) != int) or
            (memlimit != None and type(memlimit) != int)
        ):
            return 'Eparameter'

        if not self._does_testmodeid_exist(testmodeid):
            return 'Etestmodeid'

        if timelimit != None and timelimit < 0:
            return 'Etimelimit'
        if memlimit != None and memlimit < 0:
            return 'Ememlimit'

        self._set_testmode(testmodeid, testmodename, timelimit, memlimit)

        return 'Success'

    def _set_testmode(self, testmodeid, testmodename, timelimit, memlimit):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('UPDATE "PMOD_TEST_TESTMODE" SET "testmodename" = %s, '
                  '"timelimit" = %s, "memlimit" = %s WHERE "proid" = %s AND '
                  '"testmodeid" = %s;')
        sqlarr = (testmodename, timelimit, memlimit, self._proid, testmodeid)
        cur.execute(sqlstr, sqlarr)

    @imc.async.caller
    def get_testmode(self, testmodeid):
        if(
            type(testmodeid) != int
        ):
            return 'Eparameter'

        if not self._does_testmodeid_exist(testmodeid):
            return 'Etestmodeid'

        testmode = self._get_testmode(testmodeid)

        return testmode

    def _get_testmode(self, testmodeid):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('SELECT "testmodeid", "testmodename", "timelimit", '
                  '"memlimit" FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s AND '
                  '"testmodeid" = %s;')
        sqlarr = (self._proid, testmodeid)
        cur.execute(sqlstr, sqlarr)

        testmode = None
        for data in cur:
            testmode = {}
            testmode['testmodeid'] = data[0]
            testmode['testmodename'] = data[1]
            testmode['timelimit'] = data[2]
            testmode['memlimit'] = data[3]

        return testmode

    @imc.async.caller
    def list_testmode(self):
        testmode_list = self._list_testmode()

        return testmode_list

    def _list_testmode(self):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('SELECT "testmodeid", "testmodename", "timelimit", '
                  '"memlimit" FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s '
                  'ORDER BY "testmodeid" ASC;')
        sqlarr = (self._proid, )
        cur.execute(sqlstr, sqlarr)

        testmode_list = []
        for data in cur:
            obj = {}
            obj['testmodeid'] = data[0]
            obj['testmodename'] = data[1]
            obj['timelimit'] = data[2]
            obj['memlimit'] = data[3]

            testmode_list.append(obj)

        return testmode_list

    @imc.async.caller
    def set_testdata(self, testmodeid, testdata):
        if(
            type(testmodeid) != int or
            type(testdata) != list
        ):
            return 'Eparameter'

        for test in testdata:
            if type(test) != list:
                return 'Eparameter'
            if(
                'testid' not in test or
                type(test['testid']) != int or
                'timelimit' not in test or
                type(test['timelimit']) != int or
                'memlimit' not in test or
                type(test['memlimit']) != int or
                'subtask' not in test or
                type(test['subtask']) != int
            ):
                return 'Eparameter'

            if test['timelimit'] != None and test['timelimit'] < 0:
                return 'Etimelimit'
            if test['memlimit'] != None and test['memlimit'] < 0:
                return 'Ememlimit'

        if not self._does_testmodeid_exist(testmodeid):
            return 'Etestmodeid'

        self._set_testdata(testmodeid, testdata)

        return 'Success'

    def _set_testdata(self, testmodeid, testdata):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('DELETE FROM "PMOD_TEST_TESTDATA" WHERE "proid" = %s AND '
                  '"testmodeid" = %s;')
        sqlarr = (self._proid, testmodeid)
        cur.execute(sqlstr, sqlarr)

        if len(testdata) == 0:
            return

        sqltab = ('INSERT INTO "PMOD_TEST_TESTDATA" ("proid", "testmodeid", '
                  '"order", "testid", "timelimit", "memlimit", "subtask") '
                  'VALUES')
        sqlval = ()
        sqlend = (';')
        sqlarr = []

        cnt = 0
        for test in testdata:
            if cnt == 0:
                sqlval = sqlval + ' '
            else:
                sqlval = sqlval + ', '

            cnt = cnt + 1
            sqlval = sqlval + '(%s, %s, %s, %s, %s, %s, %s)'
            sqlarr.append(self._proid)
            sqlarr.append(testmodeid)
            sqlarr.append(cnt)
            sqlarr.append(test['testid'])
            sqlarr.append(test['timelimit'])
            sqlarr.append(test['memlimit'])
            sqlarr.append(test['subtask'])

        sqlstr = sqltab + sqlval + sqlend
        cur.execute(sqlstr, sqlarr)

    @imc.async.caller
    def get_testdata(self, testmodeid):
        if(
            type(testmodeid) != int
        ):
            return 'Eparameter'

        if not self._does_testmodeid_exist(testmodeid):
            return 'Etestmodeid'

        testdata = self._get_testdata(testmodeid)

        return testdata

    def _get_testdata(self, testmodeid):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('SELECT "order", "testid", "timelimit", "memlimit", '
                  '"subtask" FROM "PMOD_TEST_TESTDATA" WHERE "proid" = %s AND '
                  '"testmodeid" = %s ORDER BY "order" ASC;')
        sqlarr = (self._proid, testmodeid)
        cur.execute(sqlstr, sqlarr)

        testdata = []
        for data in cur:
            obj = {}
            obj['order'] = data[0]
            obj['testid'] = data[1]
            obj['timelimit'] = data[2]
            obj['memlimit'] = data[3]
            obj['subtask'] = data[4]

            testdata.append(obj)

        return testdata

    def _does_modeid_exist(self, modeid):
        mode = self._get_mode_by_modeid(modeid)

        return mode != None

    def _get_mode_by_modeid(self, modeid):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('SELECT "proid", "modeid", "content", "testmodeid" FROM '
                  '"PMOD_TEST_MODE" WHERE "proid" = %s AND "modeid" = %s;')
        sqlarr = (self._proid, modeid)
        cur.execute(sqlstr, sqlarr)

        mode = None
        for data in cur:
            mode = {}
            mode['proid'] = data[0]
            mode['modeid'] = data[1]
            mode['content'] = data[2]
            mode['testmodeid'] = data[3]

        return mode

    def _does_testmodeid_exist(self, testmodeid):
        testmode_info = self._get_testmode_info(testmodeid)

        return testmode_info != None

    def _get_testmode_info(self, testmodeid):
        TOJAuth.check_access_func(self._accessid, TOJAuth.ACCESS_WRITE)

        cur = self.db.cursor()
        sqlstr = ('SELECT "proid", "testmodeid", "testmodename", "timelimit",'
                  ' "memlimit" FROM "PMOD_TEST_TESTMODE" WHERE "proid" = %s '
                  'AND "testmodeid" = %s;')
        sqlarr = (self._proid, testmodeid)
        cur.execute(sqlstr, sqlarr)

        testmode_info = None
        for data in cur:
            testmode_info = {}
            testmode_info['proid'] = data[0]
            testmode_info['testmodeid'] = data[1]
            testmode_info['testmodename'] = data[2]
            testmode_info['timelimit'] = data[3]
            testmode_info['memlimit'] = data[4]

        return testmode_info