Skip to content

Commit 3f333db

Browse files
committed
style: Run black on the rest of the project.
1 parent 7a4ac65 commit 3f333db

File tree

7 files changed

+80
-48
lines changed

7 files changed

+80
-48
lines changed

backend/sample_plugin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
A sample backend plugin for the Open edX Platform.
33
"""
44

5-
__version__ = '0.1.0'
5+
__version__ = "0.1.0"

backend/sample_plugin/models.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Database models for sample_plugin.
33
"""
4+
45
from django.contrib.auth import get_user_model
56
from django.db import models
67
from opaque_keys.edx.django.models import CourseKeyField
@@ -16,28 +17,26 @@ class CourseArchiveStatus(models.Model):
1617
"""
1718

1819
course_id = CourseKeyField(
19-
max_length=255,
20-
db_index=True,
21-
help_text="The unique identifier for the course."
20+
max_length=255, db_index=True, help_text="The unique identifier for the course."
2221
)
2322

2423
user = models.ForeignKey(
2524
get_user_model(),
2625
on_delete=models.CASCADE,
2726
related_name="course_archive_statuses",
28-
help_text="The user who this archive status is for."
27+
help_text="The user who this archive status is for.",
2928
)
3029

3130
is_archived = models.BooleanField(
3231
default=False,
3332
db_index=True, # Add index for performance on this frequently filtered field
34-
help_text="Whether the course is archived."
33+
help_text="Whether the course is archived.",
3534
)
3635

3736
archive_date = models.DateTimeField(
3837
null=True,
3938
blank=True,
40-
help_text="The date and time when the course was archived."
39+
help_text="The date and time when the course was archived.",
4140
)
4241

4342
created_at = models.DateTimeField(auto_now_add=True)
@@ -61,7 +60,6 @@ class Meta:
6160
# Ensure combination of course_id and user is unique
6261
constraints = [
6362
models.UniqueConstraint(
64-
fields=['course_id', 'user'],
65-
name='unique_user_course_archive_status'
63+
fields=["course_id", "user"], name="unique_user_course_archive_status"
6664
)
6765
]

backend/sample_plugin/serializers.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Serializers for the sample_plugin app.
33
"""
4+
45
from rest_framework import serializers
56

67
from sample_plugin.models import CourseArchiveStatus
@@ -18,12 +19,12 @@ class Meta:
1819

1920
model = CourseArchiveStatus
2021
fields = [
21-
'id',
22-
'course_id',
23-
'user',
24-
'is_archived',
25-
'archive_date',
26-
'created_at',
27-
'updated_at',
22+
"id",
23+
"course_id",
24+
"user",
25+
"is_archived",
26+
"archive_date",
27+
"created_at",
28+
"updated_at",
2829
]
29-
read_only_fields = ['id', 'created_at', 'updated_at', 'archive_date']
30+
read_only_fields = ["id", "created_at", "updated_at", "archive_date"]

backend/sample_plugin/signals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# """
1313
# Handle user retirement actions for this app.
1414
# """
15-
# pass
15+
# pass

backend/sample_plugin/urls.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
"""
22
URLs for sample_plugin.
33
"""
4+
45
from django.urls import include, path
56
from rest_framework.routers import DefaultRouter
67

78
from sample_plugin.views import CourseArchiveStatusViewSet
89

910
# Create a router and register our viewsets with it
1011
router = DefaultRouter()
11-
router.register(r'course-archive-status', CourseArchiveStatusViewSet, basename='course-archive-status')
12+
router.register(
13+
r"course-archive-status",
14+
CourseArchiveStatusViewSet,
15+
basename="course-archive-status",
16+
)
1217

1318
# The API URLs are now determined automatically by the router
1419
urlpatterns = [
15-
path('api/v1/', include(router.urls)),
20+
path("api/v1/", include(router.urls)),
1621
]

backend/sample_plugin/views.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Views for the sample_plugin app.
33
"""
4+
45
import logging
56

67
from django.utils import timezone
@@ -51,7 +52,7 @@ class CourseArchiveStatusPagination(PageNumberPagination):
5152
"""
5253

5354
page_size = 20
54-
page_size_query_param = 'page_size'
55+
page_size_query_param = "page_size"
5556
max_page_size = 100
5657

5758

@@ -60,7 +61,7 @@ class CourseArchiveStatusThrottle(UserRateThrottle):
6061
Throttle for the CourseArchiveStatus API.
6162
"""
6263

63-
rate = '60/minute'
64+
rate = "60/minute"
6465

6566

6667
class CourseArchiveStatusViewSet(viewsets.ModelViewSet):
@@ -78,9 +79,16 @@ class CourseArchiveStatusViewSet(viewsets.ModelViewSet):
7879
pagination_class = CourseArchiveStatusPagination
7980
throttle_classes = [CourseArchiveStatusThrottle, AnonRateThrottle]
8081
filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
81-
filterset_fields = ['course_id', 'user', 'is_archived']
82-
ordering_fields = ['course_id', 'user', 'is_archived', 'archive_date', 'created_at', 'updated_at']
83-
ordering = ['-updated_at']
82+
filterset_fields = ["course_id", "user", "is_archived"]
83+
ordering_fields = [
84+
"course_id",
85+
"user",
86+
"is_archived",
87+
"archive_date",
88+
"created_at",
89+
"updated_at",
90+
]
91+
ordering = ["-updated_at"]
8492

8593
def get_queryset(self):
8694
"""
@@ -95,7 +103,7 @@ def get_queryset(self):
95103
self._validate_query_params()
96104

97105
# Always use select_related to avoid N+1 queries
98-
base_queryset = CourseArchiveStatus.objects.select_related('user')
106+
base_queryset = CourseArchiveStatus.objects.select_related("user")
99107

100108
if user.is_staff or user.is_superuser:
101109
return base_queryset
@@ -108,12 +116,12 @@ def _validate_query_params(self):
108116
Validate query parameters to prevent injection.
109117
"""
110118
# Example validation for course_id format
111-
course_id = self.request.query_params.get('course_id')
119+
course_id = self.request.query_params.get("course_id")
112120
if course_id and not self._is_valid_course_id(course_id):
113121
logger.warning(
114122
"Invalid course_id in request: %s, user: %s",
115123
course_id,
116-
self.request.user.username
124+
self.request.user.username,
117125
)
118126
raise ValidationError({"course_id": "Invalid course ID format."})
119127

@@ -140,20 +148,24 @@ def perform_create(self, serializer):
140148
data = serializer.validated_data.copy()
141149

142150
# Set user to requesting user if not specified
143-
if 'user' not in data:
144-
data['user'] = self.request.user
151+
if "user" not in data:
152+
data["user"] = self.request.user
145153
# Only allow staff/superusers to create records for other users
146-
elif data['user'] != self.request.user and not (self.request.user.is_staff or self.request.user.is_superuser):
154+
elif data["user"] != self.request.user and not (
155+
self.request.user.is_staff or self.request.user.is_superuser
156+
):
147157
logger.warning(
148158
"Permission denied: User %s tried to create a record for user %s",
149159
self.request.user.username,
150-
data['user'].username
160+
data["user"].username,
161+
)
162+
raise PermissionDenied(
163+
"You do not have permission to create records for other users."
151164
)
152-
raise PermissionDenied("You do not have permission to create records for other users.")
153165

154166
# Set archive_date if is_archived is True
155-
if data.get('is_archived', False):
156-
data['archive_date'] = timezone.now()
167+
if data.get("is_archived", False):
168+
data["archive_date"] = timezone.now()
157169

158170
# Create the record
159171
instance = serializer.save(**data)
@@ -163,7 +175,7 @@ def perform_create(self, serializer):
163175
"CourseArchiveStatus created: course_id=%s, user=%s, is_archived=%s",
164176
instance.course_id,
165177
instance.user.username,
166-
instance.is_archived
178+
instance.is_archived,
167179
)
168180

169181
return instance
@@ -178,13 +190,13 @@ def perform_update(self, serializer):
178190
data = serializer.validated_data.copy()
179191

180192
# Handle archive_date if is_archived changes
181-
if 'is_archived' in data:
193+
if "is_archived" in data:
182194
# If changing from not archived to archived
183-
if data['is_archived'] and not instance.is_archived:
184-
data['archive_date'] = timezone.now()
195+
if data["is_archived"] and not instance.is_archived:
196+
data["archive_date"] = timezone.now()
185197
# If changing from archived to not archived
186-
elif not data['is_archived'] and instance.is_archived:
187-
data['archive_date'] = None
198+
elif not data["is_archived"] and instance.is_archived:
199+
data["archive_date"] = None
188200

189201
# Update the record
190202
updated_instance = serializer.save(**data)
@@ -194,7 +206,7 @@ def perform_update(self, serializer):
194206
"CourseArchiveStatus updated: course_id=%s, user=%s, is_archived=%s",
195207
updated_instance.course_id,
196208
updated_instance.user.username,
197-
updated_instance.is_archived
209+
updated_instance.is_archived,
198210
)
199211

200212
return updated_instance
@@ -208,7 +220,7 @@ def perform_destroy(self, instance):
208220
"CourseArchiveStatus deleted: course_id=%s, user=%s, by=%s",
209221
instance.course_id,
210222
instance.user.username,
211-
self.request.user.username
223+
self.request.user.username,
212224
)
213225

214226
# Delete the instance

backend/tests/test_api.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def user():
3030
Create and return a test user.
3131
"""
3232
return User.objects.create_user(
33-
username="testuser", email="[email protected]", password="password123"
33+
username="testuser",
34+
35+
password="password123",
3436
)
3537

3638

@@ -40,7 +42,9 @@ def another_user():
4042
Create and return another test user.
4143
"""
4244
return User.objects.create_user(
43-
username="anotheruser", email="[email protected]", password="password123"
45+
username="anotheruser",
46+
47+
password="password123",
4448
)
4549

4650

@@ -140,7 +144,11 @@ def test_create_course_archive_status(api_client, user, course_key):
140144
"""
141145
api_client.force_authenticate(user=user)
142146
url = reverse("sample_plugin:course-archive-status-list")
143-
data = {"course_id": str(course_key), "user": user.id, "is_archived": True}
147+
data = {
148+
"course_id": str(course_key),
149+
"user": user.id,
150+
"is_archived": True,
151+
}
144152
response = api_client.post(url, data, format="json")
145153

146154
assert response.status_code == status.HTTP_201_CREATED
@@ -166,7 +174,11 @@ def test_create_course_archive_status_for_another_user(
166174
"""
167175
api_client.force_authenticate(user=user)
168176
url = reverse("sample_plugin:course-archive-status-list")
169-
data = {"course_id": str(course_key), "user": another_user.id, "is_archived": True}
177+
data = {
178+
"course_id": str(course_key),
179+
"user": another_user.id,
180+
"is_archived": True,
181+
}
170182
response = api_client.post(url, data, format="json")
171183

172184
assert response.status_code == status.HTTP_403_FORBIDDEN
@@ -181,7 +193,11 @@ def test_staff_create_course_archive_status_for_another_user(
181193
"""
182194
api_client.force_authenticate(user=staff_user)
183195
url = reverse("sample_plugin:course-archive-status-list")
184-
data = {"course_id": str(course_key), "user": user.id, "is_archived": True}
196+
data = {
197+
"course_id": str(course_key),
198+
"user": user.id,
199+
"is_archived": True,
200+
}
185201
response = api_client.post(url, data, format="json")
186202

187203
assert response.status_code == status.HTTP_201_CREATED

0 commit comments

Comments
 (0)