1212from django .core .exceptions import ValidationError as DjangoValidationError
1313from django .core .files .uploadedfile import UploadedFile
1414from django .db import IntegrityError , transaction
15- from django .db .models import Prefetch , Q
15+ from django .db .models import (
16+ BooleanField ,
17+ Exists ,
18+ ExpressionWrapper ,
19+ OuterRef ,
20+ Prefetch ,
21+ Q ,
22+ )
1623from django .db .models .functions import Greatest
1724from django .http import Http404
1825from django .shortcuts import get_object_or_404 , redirect
@@ -181,10 +188,10 @@ def get(self, request, date, slug):
181188
182189@extend_schema (
183190 description = (
184- "List translations submitted by a user, and created, approved or rejected "
185- "within the date range, most recently changed first. Defaults to the last "
186- f"{ USER_TRANSLATIONS_DEFAULT_DAYS } days; the date range can span at most "
187- f"{ USER_TRANSLATIONS_MAX_DAYS } days."
191+ "List translations submitted by a user, and created, approved, unapproved, "
192+ "rejected or unrejected within the date range, most recently changed "
193+ f"first. Defaults to the last { USER_TRANSLATIONS_DEFAULT_DAYS } days; the date "
194+ f"range can span at most { USER_TRANSLATIONS_MAX_DAYS } days."
188195 ),
189196 parameters = [
190197 OpenApiParameter (
@@ -202,6 +209,15 @@ def get(self, request, date, slug):
202209 ),
203210 OpenApiParameter ("project" , str , description = "Project slug" ),
204211 OpenApiParameter ("locale" , str , description = "Locale code" ),
212+ OpenApiParameter (
213+ "include" ,
214+ str ,
215+ enum = ["restored" ],
216+ description = (
217+ "Use `restored` to also list translations by other users that the "
218+ "user unrejected, e.g. by uploading a matching suggestion."
219+ ),
220+ ),
205221 ],
206222)
207223class UserTranslationListView (generics .ListAPIView ):
@@ -247,12 +263,33 @@ def get_queryset(self):
247263 }
248264 )
249265
266+ include = self .request .query_params .get ("include" )
267+ if include not in (None , "" , "restored" ):
268+ raise ValidationError (
269+ {"include" : ["The only supported value is restored." ]}
270+ )
271+ # ActionLog keeps every unrejection, while unrejected_user only tracks the
272+ # latest one.
273+ unrejected_by_user = ActionLog .objects .filter (
274+ performed_by = user ,
275+ action_type = ActionLog .ActionType .TRANSLATION_UNREJECTED ,
276+ )
277+ users = Q (user = user )
278+ if include == "restored" :
279+ # A subquery here would make Postgres scan the whole translation table.
280+ restored_ids = list (
281+ unrejected_by_user .values_list ("translation_id" , flat = True )
282+ )
283+ users |= Q (pk__in = restored_ids )
284+
250285 visible_projects = Project .objects .visible_for (self .request .user )
251286 qs = Translation .objects .filter (
287+ users ,
252288 Q (date__gte = start , date__lt = end )
253289 | Q (approved_date__gte = start , approved_date__lt = end )
254- | Q (rejected_date__gte = start , rejected_date__lt = end ),
255- user = user ,
290+ | Q (unapproved_date__gte = start , unapproved_date__lt = end )
291+ | Q (rejected_date__gte = start , rejected_date__lt = end )
292+ | Q (unrejected_date__gte = start , unrejected_date__lt = end ),
256293 entity__resource__project__in = visible_projects ,
257294 )
258295
@@ -273,7 +310,20 @@ def get_queryset(self):
273310 ),
274311 )
275312 )
276- .annotate (last_change = Greatest ("date" , "approved_date" , "rejected_date" ))
313+ .annotate (
314+ last_change = Greatest (
315+ "date" ,
316+ "approved_date" ,
317+ "unapproved_date" ,
318+ "rejected_date" ,
319+ "unrejected_date" ,
320+ ),
321+ restored = ExpressionWrapper (
322+ Exists (unrejected_by_user .filter (translation = OuterRef ("pk" )))
323+ & ~ Q (user = user ),
324+ output_field = BooleanField (),
325+ ),
326+ )
277327 .order_by ("-last_change" , "-pk" )
278328 )
279329
@@ -1088,7 +1138,7 @@ class UploadSuggestionsView(UploadView):
10881138 "replaced or rejected: every uploaded translation is stored as a "
10891139 "suggestion, unless the string already has an unrejected translation "
10901140 "with the same value, in any review state. A rejected translation "
1091- "matching the upload is un-rejected instead, becoming a pending "
1141+ "matching the upload is unrejected instead, becoming a pending "
10921142 "suggestion again. "
10931143 "Keys not found in Pontoon are ignored, and the fuzzy flag of the "
10941144 "uploaded file is ignored as well, as a suggestion is unreviewed by "
0 commit comments