Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update template for django 3 #17

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lock_tokens/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.db.models import Count
from django.utils.translation import gettext as _

from lock_tokens.exceptions import AlreadyLockedError
from lock_tokens.sessions import (
Expand All @@ -13,8 +14,14 @@


class LockableModelAdmin(admin.ModelAdmin):
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
# use custom template in change view, but use default in add view, as lock is not used there
self.change_form_template = 'admin/lock_tokens_change_form.html'
if add:
self.change_form_template = None

change_form_template = 'admin/lock_tokens_change_form.html'
return super(LockableModelAdmin, self).render_change_form(request, context, add, change,
form_url, obj)

def change_view(self, request, object_id, form_url="", extra_context=None):
extra_context = extra_context or {}
Expand All @@ -32,22 +39,25 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
lock_for_session(obj, request.session, force_new=force_new_session_lock)
extra_context["lock_token"] = request.session[get_session_key(obj)]
except AlreadyLockedError:
messages.add_message(request, messages.ERROR, "You cannot edit this "
"object, it has been locked. Come back later.")
messages.add_message(
request,
messages.ERROR,
_("You cannot edit this object, it has been locked. Come back later.")
)
return super(LockableModelAdmin, self).change_view(request, object_id,
form_url, extra_context)

def save_model(self, request, obj, form, change):
if change:
if not check_for_session(obj, request.session):
raise PermissionDenied("Invalid lock, cannot save.")
raise PermissionDenied(_("Invalid lock, cannot save."))
try:
# Renew lock to make sure it is still valid when saving
lock_for_session(obj, request.session)
except AlreadyLockedError:
# Race condition: lock expired just after the check_for_session call and
# someone else locked the object before the lock_for_session call
raise PermissionDenied("Lock has expired, cannot save.")
raise PermissionDenied(_("Lock has expired, cannot save."))
super(LockableModelAdmin, self).save_model(request, obj, form, change)
if change:
unlock_for_session(obj, request.session)
Expand Down
Binary file added lock_tokens/locale/de/LC_MESSAGES/django.mo
Binary file not shown.
22 changes: 22 additions & 0 deletions lock_tokens/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
msgid ""
msgstr ""
"Project-Id-Version: lock_tokens\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-03-08 08:38-0600\n"
"PO-Revision-Date: 2021-03-08 09:06+0100\n"
"Last-Translator: Liip\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language-Team: \n"
"X-Generator: Poedit 2.3\n"

msgid "You cannot edit this object, it has been locked. Come back later."
msgstr "Sie können dieses Objekt nicht bearbeiten, es wurde gesperrt. Versuchen Sie es später erneut."

msgid "Invalid lock, cannot save."
msgstr "Ungültige Sperre, kann nicht gespeichert werden."

msgid "Lock has expired, cannot save."
msgstr "Die Sperre ist abgelaufen, kann nicht gespeichert werden."
2 changes: 1 addition & 1 deletion lock_tokens/templates/api/load_client.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load staticfiles %}
{% load static %}
{% if csrf %}{% csrf_token %}{% endif %}
<script src="{% static "lock_tokens/js/lock_tokens.js" %}" type="text/javascript"></script>
<script type="text/javascript">
Expand Down
7 changes: 3 additions & 4 deletions lock_tokens/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from django.conf.urls import url

from django.urls import re_path
from lock_tokens.views import LockTokenDetailView, LockTokenListView


app_name = 'lock_tokens'

urlpatterns = [
url(r'^(?P<app_label>\w+)/(?P<model>\w+)/(?P<object_id>\d+)/$',
re_path(r'^(?P<app_label>\w+)/(?P<model>\w+)/(?P<object_id>\d+)/$',
LockTokenListView.as_view(), name='list-view'),
url(r'^(?P<app_label>\w+)/(?P<model>\w+)/(?P<object_id>\d+)/(?P<token>\w+)/$',
re_path(r'^(?P<app_label>\w+)/(?P<model>\w+)/(?P<object_id>\d+)/(?P<token>\w+)/$',
LockTokenDetailView.as_view(), name='detail-view'),
]