Matthew Schinckel avatar Matthew Schinckel committed 412a69f

Add support for api history viewing in admin.

Comments (0)

Files changed (1)

rest_api/admin.py

+
+from django import forms, template
+from django.contrib.admin import ModelAdmin
+from django.utils.functional import update_wrapper
+from django.contrib.contenttypes.models import ContentType
+from django.shortcuts import get_object_or_404, render_to_response
+from django.utils.text import capfirst
+from django.utils.encoding import force_unicode
+from django.contrib.admin.util import unquote
+from django.utils.translation import ugettext as _
+
+
+def api_history_view(self, request, object_id, extra_context=None):
+    "The 'history' admin view for this model."
+    from rest_api.models import LogEntry
+    model = self.model
+    opts = model._meta
+    app_label = opts.app_label
+    action_list = LogEntry.objects.filter(
+        object_id = object_id,
+        content_type__id__exact = ContentType.objects.get_for_model(model).id
+    ).select_related().order_by('action_time')
+    # If no history was found, see whether this object even exists.
+    obj = get_object_or_404(model, pk=unquote(object_id))
+    context = {
+        'title': _('Change history: %s') % force_unicode(obj),
+        'action_list': action_list,
+        'module_name': capfirst(force_unicode(opts.verbose_name_plural)),
+        'object': obj,
+        'root_path': self.admin_site.root_path,
+        'app_label': app_label,
+    }
+    context.update(extra_context or {})
+    context_instance = template.RequestContext(request, current_app=self.admin_site.name)
+    return render_to_response(self.object_history_template or [
+        "admin/%s/%s/object_history.html" % (app_label, opts.object_name.lower()),
+        "admin/%s/object_history.html" % app_label,
+        "admin/object_history.html"
+    ], context, context_instance=context_instance)
+
+ModelAdmin.api_history_view = api_history_view
+
+def get_urls(self):
+    from django.conf.urls.defaults 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.module_name
+
+    urlpatterns = patterns('',
+        url(r'^$',
+            wrap(self.changelist_view),
+            name='%s_%s_changelist' % info),
+        url(r'^add/$',
+            wrap(self.add_view),
+            name='%s_%s_add' % info),
+        url(r'^(.+)/history/$',
+            wrap(self.history_view),
+            name='%s_%s_api_history' % info),
+        url(r'^(.+)/api-history/$',
+            wrap(self.api_history_view),
+            name='%s_%s_history' % info),
+        url(r'^(.+)/delete/$',
+            wrap(self.delete_view),
+            name='%s_%s_delete' % info),
+        url(r'^(.+)/$',
+            wrap(self.change_view),
+            name='%s_%s_change' % info),
+    )
+    return urlpatterns
+
+ModelAdmin.get_urls = get_urls
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.