forked from mozilla/pontoon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API endpoint to retrieve User action data (mozilla#3455)
- Loading branch information
Showing
3 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from django.contrib.auth.decorators import login_required | ||
from django.http import JsonResponse | ||
from django.utils.timezone import make_aware | ||
from django.views.decorators.http import require_GET | ||
|
||
from pontoon.actionlog.models import ActionLog | ||
from pontoon.base.models import Project | ||
|
||
|
||
@require_GET | ||
@login_required(redirect_field_name="", login_url="/403") | ||
def get_user_actions(request, date, slug): | ||
try: | ||
start_date = make_aware(datetime.strptime(date, "%Y-%m-%d")) | ||
except ValueError: | ||
return JsonResponse( | ||
{ | ||
"error": "Invalid date format. Please use YYYY-MM-DD.", | ||
}, | ||
status=400, | ||
) | ||
|
||
end_date = start_date + timedelta(days=1) | ||
|
||
try: | ||
project = Project.objects.get(slug=slug) | ||
except Project.DoesNotExist: | ||
return JsonResponse( | ||
{ | ||
"error": "Project not found. Please use a valid project slug.", | ||
}, | ||
status=404, | ||
) | ||
|
||
actions = ActionLog.objects.filter( | ||
action_type__startswith="translation:", | ||
created_at__gte=start_date, | ||
created_at__lt=end_date, | ||
translation__entity__resource__project=project, | ||
) | ||
|
||
actions = actions.prefetch_related( | ||
"performed_by__profile", | ||
"translation__entity__resource", | ||
"translation__errors", | ||
"translation__warnings", | ||
"translation__locale", | ||
"entity__resource", | ||
"locale", | ||
) | ||
|
||
output = [] | ||
|
||
for action in actions: | ||
user = action.performed_by | ||
locale = action.locale or action.translation.locale | ||
entity = action.entity or action.translation.entity | ||
resource = entity.resource | ||
|
||
data = { | ||
"type": action.action_type, | ||
"date": action.created_at, | ||
"user": { | ||
"pk": user.pk, | ||
"name": user.display_name, | ||
"system_user": user.profile.system_user, | ||
}, | ||
"locale": { | ||
"pk": locale.pk, | ||
"code": locale.code, | ||
"name": locale.name, | ||
}, | ||
"entity": { | ||
"pk": entity.pk, | ||
"key": entity.key, | ||
}, | ||
"resource": { | ||
"pk": resource.pk, | ||
"path": resource.path, | ||
"format": resource.format, | ||
}, | ||
} | ||
|
||
if action.translation: | ||
data["translation"] = action.translation.serialize() | ||
|
||
output.append(data) | ||
|
||
return JsonResponse( | ||
{ | ||
"actions": output, | ||
"project": { | ||
"pk": project.pk, | ||
"slug": project.slug, | ||
"name": project.name, | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters