Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial implementation
Browse files Browse the repository at this point in the history
zerolab committed Sep 19, 2024
1 parent b7240c4 commit bf6b80b
Showing 4 changed files with 106 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "wagtailadmin/generic/base.html" %}
{% load i18n %}

{% block main_content %}
<div class="w-mt-6">
<p>
{{ view.confirmation_message }}
{% if view_url %}{{ view_url }}{% endif %}
</p>
<form method="POST">
{% csrf_token %}
<button type="submit" class="button">{% trans 'Yes, retry' %}</button>
<a href="{{ view.get_success_url }}" class="button button-secondary">{% trans "No, don't retry" %}</a>
</form>
</div>
{% endblock %}
82 changes: 80 additions & 2 deletions src/wagtail_localize_smartling/views.py
Original file line number Diff line number Diff line change
@@ -2,13 +2,24 @@

from typing import Any

from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse
from django.utils import timezone
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView
from wagtail.admin.views.generic import WagtailAdminTemplateMixin
from wagtail.admin.auth import permission_denied
from wagtail.admin.utils import get_valid_next_url_from_request
from wagtail.admin.views.generic import (
PermissionCheckedMixin,
WagtailAdminTemplateMixin,
)
from wagtail.models import Locale
from wagtail.permission_policies import ModelPermissionPolicy

from .models import Project
from .constants import UNTRANSLATED_STATUSES
from .models import Job, Project
from .templatetags.wagtail_localize_smartling_admin_tags import smartling_job_url
from .utils import (
format_smartling_project_url,
get_wagtail_source_locale,
@@ -74,3 +85,70 @@ def get_context_data(self, **kwargs):
context["suggested_source_locale_exists"] = False

return super().get_context_data(**context)


class SmartlingRetryJobView(
PermissionCheckedMixin, WagtailAdminTemplateMixin, TemplateView
):
_show_breadcrumbs = True
page_title = _("Retry Smartling job")
template_name = "wagtail_localize_smartling/admin/retry_job.html"
header_icon = "wagtail-localize-language"
object: Job = None
permission_policy = ModelPermissionPolicy(Job)
any_permission_required = ["add", "change", "delete", "view"]
jobs_report_url: str = ""

def get_breadcrumbs_items(self):
# TODO: link to the report view
return super().get_breadcrumbs_items() + [
{"url": self.jobs_report_url, "label": _("Smartling jobs")},
{"url": "", "label": _("Retry job")},
]

def get_object(self):
return get_object_or_404(Job, pk=self.kwargs.get("job_id"))

def dispatch(self, request, *args, **kwargs):
self.object = self.get_object()
# TODO: check this is the latest for the source
if self.object.status not in UNTRANSLATED_STATUSES:
return permission_denied(request)

self.jobs_report_url = reverse("wagtail_localize_smartling_jobs:index")
return super().dispatch(request, *args, **kwargs)

def get(self, request, *args, **kwargs):
context = self.get_context_data(object=self.object)
return self.render_to_response(context)

def post(self, request, *args, **kwargs):
due_date = self.object.due_date
Job.get_or_create_from_source_and_translation_data(
self.object.translation_source,
self.object.translations.all(),
user=request.user,
due_date=due_date if due_date and due_date >= timezone.now() else None,
)

return redirect(self.get_success_url())

def get_success_url(self):
return get_valid_next_url_from_request(self.request) or self.jobs_report_url

@property
def confirmation_message(self):
return _("Are you sure you want to retry this job?")

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["view"] = self
if smartling_url := smartling_job_url(self.object):
context["view_url"] = format_html(
'<a href="{}" title="Reference {}" target="_blank">{}</a>',
smartling_url,
self.object.reference_number,
_("View job in Smartling"),
)

return context
1 change: 1 addition & 0 deletions src/wagtail_localize_smartling/viewsets.py
Original file line number Diff line number Diff line change
@@ -198,6 +198,7 @@ class JobViewSet(ModelViewSet):
menu_label = _("Smartling jobs") # pyright: ignore[reportAssignmentType]
copy_view_enabled = False
inspect_view_enabled = True
url_namespace = "wagtail_localize_smartling_jobs"

@property
def permission_policy(self):
10 changes: 9 additions & 1 deletion src/wagtail_localize_smartling/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -4,12 +4,20 @@
from wagtail.admin.menu import MenuItem

from . import admin_urls
from .views import SmartlingRetryJobView
from .viewsets import smartling_job_viewset


@hooks.register("register_admin_urls") # pyright: ignore[reportOptionalCall]
def register_admin_urls():
return [path("smartling/", include(admin_urls))]
return [
path("smartling/", include(admin_urls)),
path(
"smartling-jobs/retry/<int:job_id>/",
SmartlingRetryJobView.as_view(),
name="wagtail_localize_smartling_retry_job",
),
]


@hooks.register("register_settings_menu_item") # pyright: ignore[reportOptionalCall]

0 comments on commit bf6b80b

Please sign in to comment.