-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignmentHandler.py
144 lines (125 loc) · 5.31 KB
/
assignmentHandler.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
import os
import random
from enum import Enum
import shutil
import databaseHandler
# Remove the imports below, handle all logic in browserHandler
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
import re
from browserHandler import SetupBrowser
import logins as ac
import base64
import elements as xpath
from glob import glob
class Status(Enum):
all_students_submitted = 1,
no_student_submitted = 0,
some_students_submitted = 10
def select_random_student(array):
new_list = list(array)
random.shuffle(new_list)
i = random.choice(new_list)
return i
def check_assignment_submission_statuses(array):
new_list = list(array)
res = all(ele == new_list[0] for ele in new_list)
if res:
if new_list[0] == 0:
# print("All have not submitted")
return Status.no_student_submitted
elif new_list[0] == 1:
# print("All have submitted")
return Status.all_students_submitted
else:
# print("Some Users have not submitted")
return Status.some_students_submitted
def get_specific_students_who_submitted(array):
status = check_assignment_submission_statuses(array)
students_who_submitted_list = []
if status == Status.some_students_submitted:
for index, x in enumerate(array):
if x == 1:
students_who_submitted_list.append(index)
if status == status.some_students_submitted:
print(students_who_submitted_list)
return students_who_submitted_list
def get_specific_students_who_did_not_submit(array):
status = check_assignment_submission_statuses(array)
students_who_did_not_submit_list = []
if status == Status.some_students_submitted:
for index, x in enumerate(array):
if x == 0:
students_who_did_not_submit_list.append(index)
if status == status.some_students_submitted:
print(students_who_did_not_submit_list)
return students_who_did_not_submit_list
def download_assignment_student(array, assignment):
status = check_assignment_submission_statuses(array)
if status == Status.some_students_submitted:
x = get_specific_students_who_submitted(array)
i = select_random_student(x)
browser = SetupBrowser()
browser.login(ac.ids[i], base64.b64decode(ac.paswds[i]).decode("utf-8"))
get_link = browser.fetch_assignment_download_link(assignment)
browser.assignment_download_manager(get_link)
print(i)
elif status == Status.all_students_submitted:
print("No file to Download - All students have submitted the Assignment")
elif status == Status.no_student_submitted:
print("No file to Download - No student submitted")
else:
print("Download - unknown error")
def prepare_assignment_file(directory, index):
# Find file in directory
project_files = glob(directory + "*.docx") + glob(directory + "*.pdf")
print(project_files)
assignment = project_files[0]
assignment = assignment.replace(directory, "")
print(assignment)
# Removing names and ids from assignment name
lexicals = ["Arbaz", "Ahmed", "Mughal", "Hira", "Sher", "Saad", "Muhammad", "M\.", "Mohammad",
".Sadeeq"] + ac.ids + ac.students
for lexical in lexicals:
pattern = re.compile(lexical, re.IGNORECASE)
assignment = pattern.sub("", assignment)
final_filename = assignment.strip()
extension = ""
if ".docx" in final_filename:
extension = ".docx"
else:
extension = ".pdf"
final_filename = final_filename.replace(".docx", "")
final_filename = final_filename.replace(".pdf", "")
print(final_filename)
# saving file
final_filename = directory + final_filename + " - " + ac.students[index] + " " + ac.ids[index] + extension
shutil.copy(project_files[0], final_filename)
return final_filename
def remove_assignment_file(assignment_file):
if os.path.isfile(assignment_file):
os.remove(assignment_file)
else:
print("The file does not exist")
def upload_assignment_student(array, assignment_link):
status = check_assignment_submission_statuses(array)
if status == Status.some_students_submitted:
nally_students = get_specific_students_who_did_not_submit(array)
for student in nally_students:
browser = SetupBrowser()
browser.login(ac.ids[student], base64.b64decode(ac.paswds[student]).decode("utf-8"))
assignment_upload_link = browser.fetch_assignment_upload_link(assignment_link)
directory = browser.prefs.get("download.default_directory")
print(directory)
filex = prepare_assignment_file(directory + "/", student)
print(filex)
browser.upload_given_assignment(assignment_upload_link, filex)
print("Uploaded Assignment of " + ac.ids[student] + " " + assignment_link)
databaseHandler.update_student_assign_status_to_positive(ac.students[student], assignment_link)
remove_assignment_file(filex)
elif status == Status.all_students_submitted:
print("No file to Upload - All students have submitted the Assignment")
elif status == Status.no_student_submitted:
print("No file to Upload - No student submitted")
else:
print("Upload - unknown error")