forked from tttallis/django-singletons
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathadmin.py
66 lines (54 loc) · 2.22 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from django.contrib import admin
from django.utils.translation import ugettext as _
from django.utils.encoding import force_unicode
from django.http import HttpResponseRedirect
from functools import update_wrapper
class SingletonModelAdmin(admin.ModelAdmin):
change_form_template = "admin/singleton_models/change_form.html"
def has_add_permission(self, request):
""" Singleton pattern: prevent addition of new objects """
return False
def has_delete_permission(self, request, obj=None):
""" Singleton pattern: prevent deletion of The One object."""
return False
def get_urls(self):
try:
from django.conf.urls.defaults import patterns, url
except ImportError:
from django.conf.urls import patterns, url
def wrap(view):
def wrapper(*args, **kwargs):
return self.admin_site.admin_view(view)(*args, **kwargs)
return update_wrapper(wrapper, view)
info = self.model._meta.app_label, self.model._meta.model_name
urlpatterns = patterns('',
url(r'^history/$',
wrap(self.history_view),
{'object_id': '1'},
name='%s_%s_history' % info),
url(r'^$',
wrap(self.change_view),
{'object_id': '1'},
name='%s_%s_changelist' % info),
)
return urlpatterns
def response_change(self, request, obj):
"""
Determines the HttpResponse for the change_view stage.
"""
opts = obj._meta
msg = _('%(obj)s was changed successfully.') % {'obj': force_unicode(obj)}
if request.POST.has_key("_continue"):
self.message_user(request, msg + ' ' + _("You may edit it again below."))
return HttpResponseRedirect(request.path)
else:
self.message_user(request, msg)
return HttpResponseRedirect("../")
def change_view(self, request, object_id, extra_context=None):
if object_id == '1':
self.model.objects.get_or_create(pk=1)
return super(SingletonModelAdmin, self).change_view(
request,
object_id,
extra_context=extra_context,
)