11"""
22Views for the sample_plugin app.
33"""
4+
45import logging
56
67from 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
6667class 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
0 commit comments