Skip to content

Commit 836b3ca

Browse files
committed
Apply code formatting and sort imports
1 parent 7e1ba24 commit 836b3ca

22 files changed

+875
-474
lines changed

grader_service/auth/auth.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
from grader_service.utils import maybe_future, url_path_join
1111

12-
from .login import LoginHandler
1312
from ..handlers.base_handler import BaseHandler
13+
from .login import LoginHandler
1414

1515

1616
class Authenticator(LoggingConfigurable):
@@ -512,7 +512,7 @@ async def get_authenticated_user(self, handler, data):
512512
self.log.warning("User %r not allowed.", username)
513513
return
514514

515-
async def refresh_user(self, user, handler:BaseHandler=None):
515+
async def refresh_user(self, user, handler: BaseHandler = None):
516516
"""Refresh auth data for a given user
517517
518518
Allows refreshing or invalidating auth data.
@@ -538,7 +538,9 @@ async def refresh_user(self, user, handler:BaseHandler=None):
538538
Any fields not present will be left unchanged.
539539
This can include updating `.admin` or `.auth_state` fields.
540540
"""
541-
user.is_admin = handler.authenticator.is_admin(handler=self, authentication={'name': user.name})
541+
user.is_admin = handler.authenticator.is_admin(
542+
handler=self, authentication={"name": user.name}
543+
)
542544
return True
543545

544546
def is_admin(self, handler, authentication):

grader_service/handlers/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
health,
1515
lectures,
1616
permission,
17-
submissions,
1817
roles,
19-
users
18+
submissions,
19+
users,
2020
)
2121
from grader_service.handlers.handler_utils import GitRepoType
2222

grader_service/handlers/assignment.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,18 @@ async def get(self, lecture_id: int):
9797
.all()
9898
)
9999
else:
100-
assignments = [a for a in role.lecture.assignments if (a.deleted == DeleteState.active)]
100+
assignments = [
101+
a for a in role.lecture.assignments if (a.deleted == DeleteState.active)
102+
]
101103

102104
# Handle the case that the user wants to include submissions
103105
include_submissions = self.get_argument("include-submissions", "true") == "true"
104106
if include_submissions:
105107
assignids = [a.id for a in assignments]
106108
if self.user.is_admin:
107-
results = self.session.query(Submission).filter(Submission.assignid.in_(assignids)).all()
109+
results = (
110+
self.session.query(Submission).filter(Submission.assignid.in_(assignids)).all()
111+
)
108112
else:
109113
results = (
110114
self.session.query(Submission)
@@ -275,7 +279,6 @@ async def get(self, lecture_id: int, assignment_id: int):
275279
raise HTTPError(HTTPStatus.NOT_FOUND, reason="Assignment not found")
276280
self.write_json(assignment)
277281

278-
279282
@authorize([Scope.instructor, Scope.admin])
280283
async def delete(self, lecture_id: int, assignment_id: int):
281284
"""Soft or Hard-Deletes a specific assignment.
@@ -297,7 +300,9 @@ async def delete(self, lecture_id: int, assignment_id: int):
297300

298301
if hard_delete:
299302
if not self.user.is_admin:
300-
raise HTTPError(HTTPStatus.FORBIDDEN, reason="Only Admins can hard-delete assignment.")
303+
raise HTTPError(
304+
HTTPStatus.FORBIDDEN, reason="Only Admins can hard-delete assignment."
305+
)
301306

302307
self.delete_assignment_files(assignment)
303308
self.session.delete(assignment)

grader_service/handlers/base_handler.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ def check_authorization(
7070
raise HTTPError(403)
7171
elif lecture_id is None and "/lectures" in self.request.path and self.request.method == "GET":
7272
return True
73-
if re.match(r"/api/users/(?P<username>[^/]+)/submissions/?", self.request.path) and self.request.method == "GET":
73+
if (
74+
re.match(r"/api/users/(?P<username>[^/]+)/submissions/?", self.request.path)
75+
and self.request.method == "GET"
76+
):
7477
return True
7578

76-
is_admin = self.authenticator.is_admin(handler=self, authentication={'name': self.user.name})
79+
is_admin = self.authenticator.is_admin(handler=self, authentication={"name": self.user.name})
7780

7881
role = self.session.get(Role, (self.user.id, lecture_id))
7982

@@ -835,7 +838,7 @@ def get_latest_submissions(
835838
subquery,
836839
(Submission.user_id == subquery.c.user_id)
837840
& (Submission.date == subquery.c.max_date)
838-
& (Submission.assignid == assignment_id)
841+
& (Submission.assignid == assignment_id),
839842
)
840843
.order_by(Submission.id)
841844
)
@@ -885,7 +888,7 @@ def get_best_submissions(
885888
subquery,
886889
(Submission.user_id == subquery.c.user_id)
887890
& (Submission.score == subquery.c.max_score)
888-
& (Submission.assignid == assignment_id)
891+
& (Submission.assignid == assignment_id),
889892
)
890893
.order_by(Submission.id)
891894
)
@@ -916,10 +919,14 @@ def delete_assignment_files(self, assignment: Assignment):
916919
def delete_submission_files(self, submission: Submission):
917920
# delete all associated directories of the submission
918921
assignment_path = os.path.abspath(
919-
os.path.join(self.gitbase, submission.assignment.lecture.code, str(submission.assignment.id))
922+
os.path.join(
923+
self.gitbase, submission.assignment.lecture.code, str(submission.assignment.id)
924+
)
920925
)
921926
tmp_assignment_path = os.path.abspath(
922-
os.path.join(self.tmpbase, submission.assignment.lecture.code, str(submission.assignment.id))
927+
os.path.join(
928+
self.tmpbase, submission.assignment.lecture.code, str(submission.assignment.id)
929+
)
923930
)
924931
target_names = {submission.user.name, str(submission.id)}
925932
matching_dirs = []
@@ -968,7 +975,9 @@ def construct_git_dir(
968975
elif repo_type in {GitRepoType.AUTOGRADE, GitRepoType.FEEDBACK}:
969976
type_path = os.path.join(assignment_path, repo_type, "user")
970977
if repo_type == GitRepoType.AUTOGRADE:
971-
if (submission is None) or (not self.user.is_admin and self.get_role(lecture.id).role < Scope.tutor):
978+
if (submission is None) or (
979+
not self.user.is_admin and self.get_role(lecture.id).role < Scope.tutor
980+
):
972981
raise HTTPError(403)
973982
path = os.path.join(type_path, submission.user.name)
974983
else:

grader_service/handlers/lectures.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ async def delete(self, lecture_id: int):
144144

145145
if hard_delete:
146146
if not self.user.is_admin:
147-
raise HTTPError(HTTPStatus.FORBIDDEN, reason="Only Admins can hard-delete lecture.")
147+
raise HTTPError(
148+
HTTPStatus.FORBIDDEN, reason="Only Admins can hard-delete lecture."
149+
)
148150

149151
self.delete_lecture_files(lecture)
150152
self.session.delete(lecture)
@@ -164,8 +166,7 @@ async def delete(self, lecture_id: int):
164166
if a.status in ["released", "complete"]:
165167
self.session.rollback()
166168
raise HTTPError(
167-
HTTPStatus.CONFLICT,
168-
"Cannot delete released or completed assignments!",
169+
HTTPStatus.CONFLICT, "Cannot delete released or completed assignments!"
169170
)
170171
a.deleted = DeleteState.deleted
171172
self.session.commit()

grader_service/handlers/roles.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,19 @@ async def post(self, lecture_id: int):
8686

8787
roles = []
8888
for user_req in body["users"]:
89-
9089
user = self.session.query(User).filter(User.name == user_req["username"]).one_or_none()
9190
if user is None:
9291
self.session.rollback()
93-
raise HTTPError(HTTPStatus.NOT_FOUND, reason=f"User {user_req['username']} not found")
94-
95-
role = self.session.query(Role) \
96-
.filter(Role.user_id == user.id) \
97-
.filter(Role.lectid == lecture_id) \
92+
raise HTTPError(
93+
HTTPStatus.NOT_FOUND, reason=f"User {user_req['username']} not found"
94+
)
95+
96+
role = (
97+
self.session.query(Role)
98+
.filter(Role.user_id == user.id)
99+
.filter(Role.lectid == lecture_id)
98100
.one_or_none()
101+
)
99102
if role is None:
100103
role = Role()
101104
role.user_id = user.id
@@ -144,10 +147,12 @@ async def delete(self, lecture_id: int):
144147
self.session.rollback()
145148
raise HTTPError(HTTPStatus.NOT_FOUND, reason=f"User {username} was not found")
146149

147-
role = self.session.query(Role) \
148-
.filter(Role.user_id == user.id) \
149-
.filter(Role.lectid == lecture_id) \
150+
role = (
151+
self.session.query(Role)
152+
.filter(Role.user_id == user.id)
153+
.filter(Role.lectid == lecture_id)
150154
.one_or_none()
155+
)
151156
if role is not None:
152157
self.session.delete(role)
153158
self.session.commit()

grader_service/handlers/submissions.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ async def get(self, username: str):
188188
self.validate_parameters("format")
189189
response_format = self.get_argument("format", "json")
190190
if response_format not in ["json", "csv"]:
191-
raise HTTPError(HTTPStatus.BAD_REQUEST, reason="Response format can either be 'json' or 'csv'")
191+
raise HTTPError(
192+
HTTPStatus.BAD_REQUEST, reason="Response format can either be 'json' or 'csv'"
193+
)
192194

193195
if not self.user.is_admin:
194196
if username != self.user.name:
@@ -533,7 +535,11 @@ async def get(self, lecture_id: int, assignment_id: int, submission_id: int):
533535
lecture_id, assignment_id, submission_id
534536
)
535537
submission = self.get_submission(lecture_id, assignment_id, submission_id)
536-
if not self.user.is_admin and self.get_role(lecture_id).role == Scope.student and submission.user_id != self.user.id:
538+
if (
539+
not self.user.is_admin
540+
and self.get_role(lecture_id).role == Scope.student
541+
and submission.user_id != self.user.id
542+
):
537543
raise HTTPError(HTTPStatus.NOT_FOUND)
538544
self.write_json(submission)
539545

@@ -601,19 +607,26 @@ async def delete(self, lecture_id: int, assignment_id: int, submission_id: int):
601607

602608
if hard_delete:
603609
if not self.user.is_admin:
604-
raise HTTPError(HTTPStatus.FORBIDDEN, reason="Only Admins can hard-delete submission.")
610+
raise HTTPError(
611+
HTTPStatus.FORBIDDEN, reason="Only Admins can hard-delete submission."
612+
)
605613

606614
self.delete_submission_files(submission)
607615
self.session.delete(submission)
608616
self.session.commit()
609617
else:
610618
# Do not allow students to delete other users' submissions
611-
if not self.user.is_admin and self.get_role(lecture_id).role < Scope.instructor and submission.user_id != self.user.id:
619+
if (
620+
not self.user.is_admin
621+
and self.get_role(lecture_id).role < Scope.instructor
622+
and submission.user_id != self.user.id
623+
):
612624
raise HTTPError(HTTPStatus.NOT_FOUND, reason="Submission to delete not found.")
613625

614626
if submission.feedback_status != FeedbackStatus.NOT_GENERATED:
615627
raise HTTPError(
616-
HTTPStatus.FORBIDDEN, reason="Only submissions without feedback can be deleted."
628+
HTTPStatus.FORBIDDEN,
629+
reason="Only submissions without feedback can be deleted.",
617630
)
618631

619632
# if assignment has deadline and it has already passed

grader_service/handlers/users.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class UserBaseHandler(GraderBaseHandler):
1616
"""
1717
Tornado Handler class for http requests to /users.
1818
"""
19+
1920
@authorize([Scope.admin])
2021
async def get(self):
2122
"""
@@ -27,6 +28,7 @@ async def get(self):
2728
self.set_status(HTTPStatus.OK)
2829
self.write_json(user)
2930

31+
3032
@register_handler(r"\/api\/users\/(?P<username>[^\/]+)\/?", VersionSpecifier.ALL)
3133
class UserObjectBaseHandler(GraderBaseHandler):
3234
"""

grader_service/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import tornado
1919
import uvloop as uvloop
2020
from jupyterhub.log import log_request
21-
from sqlalchemy import create_engine, Engine, event
21+
from sqlalchemy import Engine, create_engine, event
2222
from sqlalchemy.orm import scoped_session, sessionmaker
2323
from tornado.httpserver import HTTPServer
2424
from traitlets import (
@@ -41,6 +41,7 @@
4141

4242
from grader_service import __version__
4343
from grader_service.auth.auth import Authenticator
44+
4445
# run __init__.py to register handlers
4546
from grader_service.auth.dummy import DummyAuthenticator
4647
from grader_service.autograding.celery.app import CeleryApp
@@ -406,7 +407,9 @@ def init_roles(self):
406407

407408
user = db.query(User).filter(User.name == username).one_or_none()
408409
if user is None:
409-
self.log.info(f"Adding new user with username {username} and display name {display_name}")
410+
self.log.info(
411+
f"Adding new user with username {username} and display name {display_name}"
412+
)
410413
user = User()
411414
user.name = username
412415
user.display_name = display_name

0 commit comments

Comments
 (0)