Skip to content

Commit

Permalink
fix: fix admin & check issue
Browse files Browse the repository at this point in the history
  • Loading branch information
joshyu committed Mar 14, 2024
1 parent 71b8c03 commit 787785f
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 87 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Unreleased
==========
* Introduced Django 4.2 support.
* Dropped Support for Django<3.1
* Dropped the dependency of `aldryn-translation-tools`

1.0.2 (2023-02-24)
==================
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include README.rst
include CHANGELOG.rst
recursive-include aldryn_redirects/locale *
recursive-include aldryn_redirects/migrations *
recursive-include aldryn_redirects/static *
recursive-include aldryn_redirects/templates *

recursive-exclude * *.pyc
67 changes: 66 additions & 1 deletion aldryn_redirects/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from django.conf import settings
from django.contrib import admin, messages
from django.forms import widgets
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils import timezone
from django.utils.encoding import force_str
from django.utils.translation import gettext, gettext_lazy as _

from aldryn_translation_tools.admin import AllTranslationsMixin
from cms.utils.i18n import get_current_language
from cms.utils.urlutils import admin_reverse

from parler.admin import TranslatableAdmin
from tablib import Dataset

Expand Down Expand Up @@ -46,6 +50,67 @@ def delete_selected(self, request, queryset):
self.message_user(request, msg)


class AllTranslationsMixin(object):

@property
def media(self):
return super().media + widgets.Media(
css={'all': ('css/admin/all-translations-mixin.css', ), }
)

@admin.display(
description='Translations'
)
def all_translations(self, obj):
"""
Adds a property to the list_display that lists all translations with
links directly to their change forms. Includes CSS to style the links
to looks like tags with color indicating current language, active and
inactive translations.
A similar capability is in HVAD, and now there is this for
Parler-based projects.
"""
available = list(obj.get_available_languages())
current = get_current_language()
langs = []
for code, lang_name in settings.LANGUAGES:
classes = ["lang-code", ]
title = force_str(lang_name)
if code == current:
classes += ["current", ]
if code in available:
classes += ["active", ]
title += " (translated)"
else:
title += " (untranslated)"
change_form_url = admin_reverse(
'{app_label}_{model_name}_change'.format(
app_label=obj._meta.app_label.lower(),
model_name=obj.__class__.__name__.lower(),
), args=(obj.id, )
)
link = '<a class="{classes}" href="{url}?language={code}" title="{title}">{code}</a>'.format(
classes=' '.join(classes),
url=change_form_url,
code=code,
title=title,
)
langs.append(link)
return ''.join(langs)

def get_list_display(self, request):
"""
Unless the the developer has already placed "all_translations" in the
list_display list (presumably specifically where she wants it), append
the list of translations to the end.
"""
list_display = super().get_list_display(request)
if 'all_translations' not in list_display:
list_display = list(list_display) + ['all_translations', ]
return list_display


@admin.register(Redirect)
class RedirectAdmin(DeletionMixin, AllTranslationsMixin, TranslatableAdmin):
list_display = ('old_path',)
Expand Down
81 changes: 0 additions & 81 deletions aldryn_redirects/south_migrations/0001_initial.py

This file was deleted.

Empty file.
23 changes: 23 additions & 0 deletions aldryn_redirects/static/css/admin/all-translations-mixin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
a.lang-code {
background-color: #BFBFBF; /* hsl(0, 0%, 75%); */
border-radius: 3px;
color: #fff !important;
display: inline-block;
font-size: 0.75em;
letter-spacing: 0.05em;
line-height: 1em;
margin-right: 0.25em;
padding: 3px;
text-decoration: none;
text-transform: uppercase;
}
a.lang-code.active {
background-color: #3B99FC; /* hsl(211, 97%, 61%) */
}
a.lang-code.current {
background-color: #999999; /* hsl(0, 0%, 60%); */
font-weight: bold;
}
a.lang-code.current.active {
background-color: #3178BE; /* hsl(211, 59%, 48%) */
}
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
REQUIREMENTS = [
'tablib',
'django-parler',
'aldryn-translation-tools',
]

CLASSIFIERS = [
Expand Down
1 change: 0 additions & 1 deletion tests/requirements/requirements_base.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
aldryn-translation-tools
asgiref
beautifulsoup4
coverage>=4.4.2
Expand Down
11 changes: 8 additions & 3 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from __future__ import division, print_function

from django.contrib.sites.models import Site
from django.http.response import HttpResponse
from django.test import TestCase
from django.test.client import RequestFactory

from aldryn_redirects.middleware import RedirectFallbackMiddleware
from aldryn_redirects.models import StaticRedirect


def get_response():
return HttpResponse()


class RedirectFallbackMiddlewareTestCase(TestCase):
def setUp(self, *args, **kwargs):
super(RedirectFallbackMiddlewareTestCase, self).setUp(*args, **kwargs)
Expand All @@ -20,7 +25,7 @@ def test_redirect_found(self):
redirect.sites.add(self.site)
redirect.query_params.create(key='query1', value='param1')

response = RedirectFallbackMiddleware().process_request(self.request)
response = RedirectFallbackMiddleware(get_response).process_request(self.request)

self.assertEquals(response.status_code, 301)
self.assertEquals(response.url, 'http://example.com/dest?keep=this')
Expand All @@ -31,10 +36,10 @@ def test_redirect_found_from_root(self):
redirect.query_params.create(key='query1', value='param1')
request_from_root = RequestFactory().get('http://example.com/?query1=param1')

response = RedirectFallbackMiddleware().process_request(request_from_root)
response = RedirectFallbackMiddleware(get_response).process_request(request_from_root)

self.assertEquals(response.status_code, 301)
self.assertEquals(response.url, 'http://example.com/dest?keep=this')

def test_redirect_not_found(self):
self.assertIsNone(RedirectFallbackMiddleware().process_request(self.request))
self.assertIsNone(RedirectFallbackMiddleware(get_response).process_request(self.request))

0 comments on commit 787785f

Please sign in to comment.