Repository navigation
Expand file tree
/
Copy pathhelpers.py
More file actions
118 lines (107 loc) · 4.62 KB
/
Copy pathhelpers.py
File metadata and controls
118 lines (107 loc) · 4.62 KB
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
from utils.connect_db import connect_db
import datetime
from utils.logging_file import logger
def calculate_points(percentage):
if percentage >= 100:
return 100
elif percentage >= 75:
return 75
elif percentage >= 50:
return 50
else:
return 0
def getdiscord_from_cr():
try:
sql_query = "SELECT discord_id FROM public.contributors_registration WHERE github_url = %s"
return sql_query
except Exception as e:
logger.info(f"getdiscord_from_cr -- {e}")
raise Exception
def check_assignment_exist():
try:
sql_query = """SELECT EXISTS (SELECT 1 FROM public.github_classroom_data WHERE (discord_id IS NULL OR discord_id = %s) AND assignment_id = %s ); """
return sql_query
except Exception as e:
logger.info(f"getdiscord_from_cr -- {e}")
raise Exception
def save_classroom_records(data):
# Iterate over each object in the JSON array and insert into the database
conn, cur = connect_db()
for record in data:
try:
cur.execute("""
INSERT INTO public.github_classroom_data (assignment_id, assignment_name, assignment_url, c4gt_points, discord_id, github_username, points_available, points_awarded, roster_identifier, starter_code_url, student_repository_name, student_repository_url, submission_timestamp,updated_at)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s)
""", (
record['assignment_id'],
record['assignment']['title'],
record['assignment']['classroom']['url'],
record['c4gt_points'],
record['discord_id'],
record['students'][0]['login'],
record['points_available'],
record['points_awarded'],
record['roster_identifier'] if 'roster_identifier' in record else None,
record['starter_code_url'] if 'starter_code_url' in record else record['repository']['html_url'],
record['repository']['full_name'],
record['repository']['html_url'],
record['submission_timestamp'] if 'submission_timestamp' in record else datetime.datetime.now(),
record['updated_at']
))
conn.commit()
print("Record inserted successfully!")
except Exception as e:
conn.rollback()
logger.info(f"save_classroom_records -- {e}")
print("Error inserting record:", e)
# Close the cursor and connection
cur.close()
conn.close()
def update_classroom_records(data):
# Iterate over each object in the JSON array and update the corresponding record in the database
conn, cur = connect_db()
for record in data:
try:
cur.execute("""
UPDATE public.github_classroom_data
SET
assignment_name = %s,
assignment_url = %s,
c4gt_points = %s,
github_username = %s,
points_available = %s,
points_awarded = %s,
roster_identifier = %s,
starter_code_url = %s,
student_repository_name = %s,
student_repository_url = %s,
submission_timestamp = %s,
updated_at = %s
WHERE
assignment_id = %s
AND (discord_id IS NULL OR discord_id = %s);
""", (
record['assignment']['title'],
record['assignment']['classroom']['url'],
record['c4gt_points'],
record['students'][0]['login'],
record['points_available'],
record['points_awarded'],
record['roster_identifier'] if 'roster_identifier' in record else None,
record['starter_code_url'] if 'starter_code_url' in record else record['repository']['html_url'],
record['repository']['full_name'],
record['repository']['html_url'],
record['submission_timestamp'] if 'submission_timestamp' in record else datetime.datetime.now(),
record['updated_at'],
str(record['assignment_id']),
str(record['discord_id'])
))
conn.commit()
print("Record updated successfully!")
except Exception as e:
conn.rollback()
logger.info(e)
print("Error updating record:", e)
# Close the cursor and connection
cur.close()
conn.close()