Skip to content

Commit

Permalink
Add an API endpoint to retrieve User action data (mozilla#3455)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathjazz authored Dec 2, 2024
1 parent 8976927 commit e5a5696
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
25 changes: 24 additions & 1 deletion pontoon/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from graphene_django.views import GraphQLView

from django.urls import re_path
from django.urls import include, path, re_path

from pontoon.api import views
from pontoon.api.schema import schema
from pontoon.settings import DEV

Expand All @@ -13,4 +14,26 @@
r"^graphql/?$",
GraphQLView.as_view(schema=schema, graphiql=DEV),
),
# API v1
path(
"api/v1/",
include(
[
path(
# User actions
"user-actions/",
include(
[
# In a given project
path(
"<str:date>/project/<slug:slug>/",
views.get_user_actions,
name="pontoon.api.get_user_actions.project",
),
]
),
),
]
),
),
]
100 changes: 100 additions & 0 deletions pontoon/api/views.py
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,
},
}
)
2 changes: 1 addition & 1 deletion pontoon/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class LocaleConverter(StringConverter):
path("", include("pontoon.contributors.urls")),
path("", include("pontoon.localizations.urls")),
path("", include("pontoon.base.urls")),
path("", include("pontoon.api.urls")),
path("", include("pontoon.translate.urls")),
path("", include("pontoon.batch.urls")),
path("", include("pontoon.api.urls")),
path("", include("pontoon.homepage.urls")),
path("", include("pontoon.uxactionlog.urls")),
# Team page: Must be at the end
Expand Down

0 comments on commit e5a5696

Please sign in to comment.