@@ -11,7 +11,9 @@ def _get_userprofile_school(userprofile):
11
11
elif hasattr (userprofile , "student" ):
12
12
return userprofile .student .class_field .teacher .school
13
13
else :
14
- LOGGER .error (f"Userprofile ID { userprofile .id } has no teacher or student attribute" )
14
+ LOGGER .error (
15
+ f"Userprofile ID { userprofile .id } has no teacher or student attribute"
16
+ )
15
17
return None
16
18
17
19
@@ -49,19 +51,32 @@ def can_play_or_delete_level(user, level):
49
51
# If the teacher is an admin, they can play any student's level in the school, otherwise only student levels
50
52
# from their own classes
51
53
if user .userprofile .teacher .is_admin and hasattr (level .owner , "student" ):
52
- return user .userprofile .teacher .school == level .owner .student .class_field .teacher .school
54
+ return (
55
+ user .userprofile .teacher .school
56
+ == level .owner .student .class_field .teacher .school
57
+ )
53
58
else :
54
59
return user .userprofile .teacher .teaches (level .owner )
55
60
56
61
57
62
def can_approve_level (user , level ):
58
- return hasattr (user .userprofile , "teacher" ) and level .shared_with .filter (id = user .id ).exists ()
63
+ return (
64
+ hasattr (user .userprofile , "teacher" )
65
+ and level .shared_with .filter (id = user .id ).exists ()
66
+ )
59
67
60
68
61
69
def can_play_level (user , level , early_access ):
62
- if not user .is_anonymous and hasattr (user .userprofile , "student" ) and user .userprofile .student .class_field :
70
+ if (
71
+ not user .is_anonymous
72
+ and hasattr (user .userprofile , "student" )
73
+ and user .userprofile .student .class_field
74
+ ):
63
75
# If the user is a student, check that the level isn't locked for their class
64
- return user .userprofile .student .class_field not in level .locked_for_class .all () and not level .needs_approval
76
+ return user .userprofile .id == level .owner_id or (
77
+ user .userprofile .student .class_field not in level .locked_for_class .all ()
78
+ and not level .needs_approval
79
+ )
65
80
elif level .default and not level .episode .in_development :
66
81
return True
67
82
elif level .anonymous :
@@ -90,7 +105,9 @@ def can_load_level(user, level):
90
105
owner_school = _get_userprofile_school (level .owner )
91
106
return user_school is not None and user_school == owner_school
92
107
else :
93
- return hasattr (user .userprofile , "teacher" ) and user .userprofile .teacher .teaches (level .owner )
108
+ return hasattr (
109
+ user .userprofile , "teacher"
110
+ ) and user .userprofile .teacher .teaches (level .owner )
94
111
95
112
96
113
def can_save_level (user , level ):
@@ -126,10 +143,16 @@ def has_permission(self, request, view):
126
143
def has_object_permission (self , request , view , obj ):
127
144
if request .user .is_anonymous :
128
145
return False
129
- elif hasattr (request .user .userprofile , "student" ) and request .user .userprofile .student .is_independent ():
146
+ elif (
147
+ hasattr (request .user .userprofile , "student" )
148
+ and request .user .userprofile .student .is_independent ()
149
+ ):
130
150
return False
131
151
# if the user is a teacher and the level is shared with them
132
- elif hasattr (request .user .userprofile , "teacher" ) and obj .shared_with .filter (id = request .user .id ).exists ():
152
+ elif (
153
+ hasattr (request .user .userprofile , "teacher" )
154
+ and obj .shared_with .filter (id = request .user .id ).exists ()
155
+ ):
133
156
return True
134
157
else :
135
158
return obj .owner == request .user .userprofile and not obj .needs_approval
@@ -168,18 +191,30 @@ def can_share_level_with(self, recipient, sharer):
168
191
and not (recipient_profile .student .is_independent ())
169
192
):
170
193
# Are they in the same class?
171
- return sharer_profile .student .class_field == recipient_profile .student .class_field
172
- elif hasattr (sharer_profile , "teacher" ) and sharer_profile .teacher .teaches (recipient_profile ):
194
+ return (
195
+ sharer_profile .student .class_field
196
+ == recipient_profile .student .class_field
197
+ )
198
+ elif hasattr (sharer_profile , "teacher" ) and sharer_profile .teacher .teaches (
199
+ recipient_profile
200
+ ):
173
201
# Is the recipient taught by the sharer?
174
202
return True
175
- elif hasattr (recipient_profile , "teacher" ) and recipient_profile .teacher .teaches (sharer_profile ):
203
+ elif hasattr (
204
+ recipient_profile , "teacher"
205
+ ) and recipient_profile .teacher .teaches (sharer_profile ):
176
206
# Is the sharer taught by the recipient?
177
207
return True
178
- elif hasattr (sharer_profile , "teacher" ) and hasattr (recipient_profile , "teacher" ):
208
+ elif hasattr (sharer_profile , "teacher" ) and hasattr (
209
+ recipient_profile , "teacher"
210
+ ):
179
211
# Are they in the same organisation?
180
212
return recipient_profile .teacher .school == sharer_profile .teacher .school
181
213
elif hasattr (sharer_profile , "teacher" ) and sharer_profile .teacher .is_admin :
182
- return recipient_profile .student .class_field .teacher .school == sharer_profile .teacher .school
214
+ return (
215
+ recipient_profile .student .class_field .teacher .school
216
+ == sharer_profile .teacher .school
217
+ )
183
218
else :
184
219
return False
185
220
0 commit comments