This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.py
executable file
·705 lines (603 loc) · 16.4 KB
/
editor.py
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from mysql.connector import MySQLConnection, Error
from getpass import getpass
from datetime import date
import shlex
from .dbconfig import read_db_config
from .user import User
from .logging import Logging
class Editor(User):
def __init__(self, editor_id: int, conn: MySQLConnection):
self.editor_id = editor_id
self.conn = conn
self.is_valid = self.login()
def __bool__(self):
return self.is_valid
@property
def prompt(self):
"""
Print the user prompt in the terminal
"""
return f"Editor {self.editor_id}> "
# 2. editor status.
def status(self):
"""
Return a status of the current editor's manuscripts.
Parameters
----------
`editor_id`: int
Editor ID.
Returns
-------
String value representing the status of all manuscripts
assigned to the editor.
"""
query = f"""
SELECT manuscript_number, status
FROM Manuscript
ORDER BY status, manuscript_number
"""
cursor = self.conn.cursor()
cursor.execute(query)
results = ""
title = "Status"
title = f"| Manuscript #### | {title:>30} |"
delim = "-" * len(title)
for row in cursor:
manuscript_number, status = row
results += f"| Manuscript {manuscript_number:4d} | {status:>30} |\n{delim}\n"
cursor.close()
if len(results) == 0:
results = "Editor has no manuscripts."
else:
results = f"{delim}\n{title}\n{delim}\n{results}"
return Logging.info(results)
# 3. editor login
def login(self):
"""
Editor login.
Returns
-------
bool: True if the editor was logged in successfully, False otherwise.
"""
query = f"""
SELECT CONCAT('Hello, ', Editor.f_name, ' ', Editor.l_name)
FROM Editor WHERE Editor.editor_ID = {self.editor_id}"""
success = False
try:
cursor = self.conn.cursor()
cursor.execute(query)
result = cursor.fetchone()
cursor.close()
if result:
success = True
print(result[0], "\n")
print(f"Status:\n{self.status()}\n")
except Error as error:
print(error)
return success
def register_editor(self, fname, lname):
"""Register a editor
Arguments:
fname -- first name of the reviewer
lname -- last name of the reviewer
"""
success = False
editor_id = None
query = f"""
INSERT INTO `Editor` (`f_name`,`l_name`)
VALUES ('{fname}','{lname}')"""
password = getpass("Enter password: ")
password_query = f"""
UPDATE credentials
SET password = MD5('{password}')
WHERE
user_type = "Editor"
AND type_id = (SELECT editor_ID FROM Editor WHERE f_name = '{fname}' AND l_name = '{lname}')
"""
success = False
try:
cursor = self.conn.cursor()
cursor.execute(query)
self.conn.commit()
if password:
cursor.execute(password_query)
self.conn.commit()
success = True
editor_id = cursor.lastrowid
user_id_query = f"""
SELECT user_id from credentials
WHERE user_type = 'Editor'
AND type_id = {editor_id}"""
cursor.execute(user_id_query)
user_id = int(cursor.fetchone()[0]) # get the user_id from the credentials table
print(f"Registered User ID: {user_id}") # print the user_id for the author
except Exception as e:
print(e.msg)
finally:
cursor.close()
return success
# 4. editor assign manuscript
def assign_reviewer(self, manuscript_number: int, reviewer_id: int):
"""
Assign a manuscript to a reviewer.
Parameters
----------
`manuscript_number`: int
Manuscript number.
`reviewer_id`: int
Reviewer ID.
Returns
-------
bool: True if the manuscript was assigned successfully, False otherwise.
"""
query = f"""
INSERT INTO Reviewer_has_Manuscript
(Manuscript_manuscript_number, Reviewer_reviewer_id)
VALUES ({manuscript_number}, {reviewer_id})
"""
success = False
try:
cursor = self.conn.cursor()
cursor.execute(query)
self.conn.commit()
success = True
except Error as error:
print(error)
finally:
cursor.close()
return success
# 5. editor reject manuscript
def reject_manuscript(self, manuscript_number: int):
"""
Reject a manuscript.
Parameters
----------
`manuscript_number`: int
Manuscript number.
Returns
-------
bool: True if the manuscript was rejected successfully, False otherwise.
"""
query = f"""
UPDATE Manuscript
SET status = 'rejected'
WHERE manuscript_number = {manuscript_number}
"""
success = False
try:
cursor = self.conn.cursor()
cursor.execute(query)
self.conn.commit()
success = True
except Error as error:
print(error)
finally:
cursor.close()
return success
# 6. editor accept manuscript
def accept_manuscript(self, manuscript_number: int):
"""
Accept a manuscript.
Parameters
----------
`manuscript_number`: int
Manuscript number.
Returns
-------
bool: True if the manuscript was accepted successfully, False otherwise.
"""
query = f"""
UPDATE Manuscript
SET status = 'accepted'
WHERE manuscript_number = {manuscript_number}
"""
success = False
try:
cursor = self.conn.cursor()
cursor.execute(query)
self.conn.commit()
success = True
except Error as error:
print(error)
finally:
cursor.close()
return success
def schedule_manuscript(self, manuscript_number, issue):
"""
Schedule a manuscript for publication.
Parameters
----------
`manuscript_number`: int
Manuscript number.
`issue`: int
Issue number: 2019-1
Returns
-------
bool: True if the manuscript was scheduled successfully, False otherwise.
"""
input = issue.split("-")
year = input[0]
period = input[1]
query = f"""
SELECT issue_ID
FROM Issue
WHERE `year`={year} AND `period`={period};
"""
success = False
try:
cursor = self.conn.cursor()
cursor.execute(query)
row = cursor.fetchone()
if row:
issue_id = row[0]
query = f"""
SELECT Manuscript.`status`
FROM Manuscript
WHERE Manuscript.`manuscript_number` = {manuscript_number};
"""
cursor.execute(query)
res = cursor.fetchone()
manuscript_status = res[0] if res else ""
query = f"""
SELECT sum(Manuscript.`page_count`)
FROM Manuscript
GROUP BY Manuscript.`Issue_issue_ID`, Manuscript.`status`
HAVING Manuscript.`Issue_issue_ID` = {issue_id} AND Manuscript.`status`='schedule for publication';
"""
cursor.execute(query)
res = cursor.fetchone()
curr_page_total = res[0] if res else 0
query = f"""
SELECT page_count
FROM Manuscript
WHERE manuscript_number = {manuscript_number};
"""
cursor.execute(query)
res = cursor.fetchone()
manuscript_page = res[0] if res else 0
curr_page_total += manuscript_page
if manuscript_status == "ready" and curr_page_total <= 100:
query = f"""
UPDATE Manuscript
SET status = 'schedule for publication', Issue_issue_ID = {issue_id}
WHERE manuscript_number = {manuscript_number};
"""
cursor.execute(query)
self.conn.commit()
success = True
else:
print("Manuscript not ready or page count exceeds 100")
success = False
except Error as error:
print(error)
finally:
cursor.close()
return success
def publish_issue(self, issue):
input = issue.split("-")
year = input[0]
period = input[1]
query = f"""
SELECT COUNT(Manuscript.`manuscript_number`)
FROM Manuscript
LEFT JOIN Issue
ON Manuscript.`Issue_issue_ID` = Issue.`issue_ID`
WHERE `year`={year} AND `period`={period};
"""
query2 = f"""
SELECT Manuscript.`manuscript_number`
FROM Manuscript
LEFT JOIN Issue
ON Manuscript.`Issue_issue_ID` = Issue.`issue_ID`
WHERE `year`={year} AND `period`={period};
"""
success = False
try:
cursor = self.conn.cursor()
cursor.execute(query)
result = cursor.fetchall()
if result[0][0] >= 1:
cursor.execute(query2)
result = cursor.fetchall()
today = date.today()
for row in result:
query3 = f"""
UPDATE Manuscript
SET status = 'published', status_change_date='{today}'
WHERE manuscript_number = {row[0]};
"""
cursor.execute(query3)
query4 = f"""
SELECT issue_ID
FROM Issue
WHERE `year`={year} AND `period`={period};
"""
cursor.execute(query4)
row = cursor.fetchone();
if row:
issue_id = row[0]
query5 = f"""
UPDATE Issue
SET `publication_date`= '{today}'
WHERE `issue_ID` = {issue_id};
"""
cursor.execute(query5)
self.conn.commit()
success = True
except Error as error:
print(error)
finally:
cursor.close()
return success
def reset_database(self):
"""
Reset the database to its initial state.
"""
queries = [
"SET FOREIGN_KEY_CHECKS = 0;",
"TRUNCATE TABLE RICodes;",
"TRUNCATE TABLE Affiliation;",
"TRUNCATE TABLE Journal;",
"TRUNCATE TABLE Issue;",
"TRUNCATE TABLE Editor;",
"TRUNCATE TABLE Reviewer;",
"TRUNCATE TABLE Author;",
"TRUNCATE TABLE Manuscript_Author;",
"TRUNCATE TABLE Journal_has_RICodes;",
"TRUNCATE TABLE Reviewer_has_Manuscript;",
"TRUNCATE TABLE Manuscript;",
"TRUNCATE TABLE Reviewer_has_RICodes;",
"TRUNCATE TABLE credentials;",
"SET FOREIGN_KEY_CHECKS = 1;"
]
success=False
try:
cursor = self.conn.cursor()
# cursor.execute(queries, multi=True)
for query in queries:
cursor.execute(query)
self.conn.commit()
success = True
except Error as error:
print(error)
finally:
cursor.close()
return success
def handle_request(self, request: str):
"""
Handle a request from the user.
Parameters
----------
`request`: str
Request from the user.
Returns
-------
None
"""
request_tokens = shlex.split(request)
request_type = request_tokens[0].lower() if len(request_tokens) > 0 else ""
if request_type == "status":
print(f"Editor ID: {self.editor_id}\n{self.status()}")
elif request_type == "register":
if len(request_tokens) != 4:
print("Invalid request: too few arguments.")
return
if request_tokens[1].lower() != "editor":
print("Invalid request.")
return
f_name = request_tokens[2]
l_name = request_tokens[3]
if self.register_editor(f_name, l_name):
print("Editor registered successfully.")
else:
print("Author registration failed.")
elif request_type == "login":
if len(request_tokens) != 2:
print("Invalid request: too few arguments.")
return
try:
editor_id = int(request_tokens[1])
self.__init__(editor_id)
except ValueError:
print("Invalid request: editor ID must be an integer.")
return
elif request_type == "status":
print(f"Editor ID: {self.editor_id}\n{self.status()}")
elif request_type == "assign":
if len(request_tokens) != 3:
print("Invalid request: too few arguments.")
return
try:
manuscript_number = int(request_tokens[1])
reviewer_id = int(request_tokens[2])
except ValueError:
print("Invalid request: manuscript number and reviewer ID must be integers.")
return
if self.assign_reviewer(manuscript_number, reviewer_id):
print("Reviewer assigned successfully.")
else:
print("Reviewer assignment failed.")
elif request_type == "reject":
if len(request_tokens) != 2:
print("Invalid request: too few arguments.")
return
try:
manuscript_number = int(request_tokens[1])
except ValueError:
print("Invalid request: manuscript number must be an integer.")
return
if self.reject_manuscript(manuscript_number):
print("Manuscript rejected successfully.")
else:
print("Manuscript rejection failed.")
elif request_type == "accept":
if len(request_tokens) != 2:
print("Invalid request: too few arguments.")
return
try:
manuscript_number = int(request_tokens[1])
except ValueError:
print("Invalid request: manuscript number must be an integer.")
return
if self.accept_manuscript(manuscript_number):
print("Manuscript accepted successfully.")
else:
print("Manuscript acceptance failed.")
elif request_type == "schedule":
if len(request_tokens) != 3:
print("Invalid request: too few arguments.")
return
try:
manuscript_number = int(request_tokens[1])
issue = request_tokens[2]
except ValueError:
print("Invalid request: manuscript number must be an integer.")
return
if self.schedule_manuscript(manuscript_number, issue):
print("Manuscript scheduled successfully.")
else:
print("Manuscript scheduling failed.")
elif request_type == "publish":
if len(request_tokens) != 2:
print("Invalid request: too few arguments.")
return
issue = request_tokens[1]
if self.publish_issue(issue):
print("Issue published successfully.")
else:
print("Issue publishing failed.")
elif request_type == "reset":
self.reset_database()
print("Database reset successfully.")
return -1
elif request_type == "exit":
print("Exiting...")
exit(0)
else:
print("Invalid request.")
def test_editor():
# the mysql self.connection instance.
db_config = read_db_config()
conn = MySQLConnection(**db_config)
print("Testing Editor instantiation, login, and status...")
editor_id = input("Enter editor ID: ")
editor = Editor(editor_id, conn)
if editor:
print("Editor logged in successfully.")
else:
print("Editor login failed.")
return
print("\n\nTesting assignment of reviewers...\n")
print("Testing assignment of reviewers")
for _ in range(2):
manuscript_number = int(input("Manuscript number: "))
reviewer_id = int(input("Reviewer ID: "))
print("Before assignment:")
cursor = conn.cursor()
cursor.execute("""
SELECT
Manuscript_manuscript_number AS Manuscript_NO,
Reviewer_reviewer_id AS Reviewer_ID
FROM Reviewer_has_Manuscript""")
for row in cursor:
print(row)
cursor.close()
status = editor.assign_reviewer(manuscript_number, reviewer_id)
if status:
print("Manuscript assigned successfully.")
else:
print("Manuscript assignment failed.")
print("After assignment:")
cursor = conn.cursor()
cursor.execute("""
SELECT
Manuscript_manuscript_number AS Manuscript_NO,
Reviewer_reviewer_id AS Reviewer_ID
FROM Reviewer_has_Manuscript""")
for row in cursor:
print(row)
cursor.close()
print()
print("\n\nTesting rejection of manuscripts...\n")
for _ in range(5):
manuscript_number = int(input("Manuscript number: "))
print("Before rejection:")
cursor = conn.cursor()
cursor.execute(f"""
SELECT manuscript_number, status
FROM Manuscript
WHERE manuscript_number = {manuscript_number}""")
for row in cursor:
print(row)
cursor.close()
status = editor.reject_manuscript(manuscript_number)
if status:
print("Manuscript rejected successfully.")
else:
print("Manuscript rejection failed.")
print("After rejection:")
cursor = conn.cursor()
cursor.execute(f"""
SELECT manuscript_number, status
FROM Manuscript
WHERE manuscript_number = {manuscript_number}""")
for row in cursor:
print(row)
cursor.close()
print()
print("\n\nTesting acceptance of manuscripts...\n")
for _ in range(5):
manuscript_number = int(input("Manuscript number: "))
print("Before acceptance:")
cursor = conn.cursor()
cursor.execute(f"""
SELECT manuscript_number, status
FROM Manuscript
WHERE manuscript_number = {manuscript_number}""")
for row in cursor:
print(row)
cursor.close()
status = editor.accept_manuscript(manuscript_number)
if status:
print("Manuscript accepted successfully.")
else:
print("Manuscript acceptance failed.")
print("After acceptance:")
cursor = conn.cursor()
cursor.execute(f"""
SELECT manuscript_number, status
FROM Manuscript
WHERE manuscript_number = {manuscript_number}""")
for row in cursor:
print(row)
cursor.close()
print()
def test_editor_handle_request():
"""
Function to test editor handling message queries
"""
# the mysql self.connection instance.
db_config = read_db_config()
conn = MySQLConnection(**db_config)
print("Testing editor handling message queries...")
editor_id = input("Enter editor ID: ")
editor = Editor(editor_id, conn)
if editor:
print("Editor logged in successfully.")
else:
print("Editor login failed.")
return
while True:
request = input("Enter request: ")
res = editor.handle_request(request)
if res and res == -1:
return
else:
print(f"Editor status: \n{editor.status()}")
return
if __name__ == '__main__':
# test editor
# test_editor()
test_editor_handle_request()