import datetime import json try: from django.apps.registry import apps except ImportError: try: from django.apps import apps # Fix Django 1.7 import issue except ImportError: pass from django.core.serializers.json import DjangoJSONEncoder from django.http import HttpResponse from django.core.urlresolvers import reverse, resolve, NoReverseMatch from django.contrib import admin from django.contrib.admin import AdminSite from django.utils.encoding import smart_text from django.utils.text import capfirst from django.contrib import messages from django.utils.encoding import force_text from django.utils.functional import Promise class JsonResponse(HttpResponse): """ An HTTP response class that consumes data to be serialized to JSON. :param data: Data to be dumped into json. By default only ``dict`` objects are allowed to be passed due to a security flaw before EcmaScript 5. See the ``safe`` parameter for more information. :param encoder: Should be an json encoder class. Defaults to ``django.core.serializers.json.DjangoJSONEncoder``. :param safe: Controls if only ``dict`` objects may be serialized. Defaults to ``True``. """ def __init__(self, data, encoder=DjangoJSONEncoder, safe=True, **kwargs): if safe and not isinstance(data, dict): raise TypeError('In order to allow non-dict objects to be ' 'serialized set the safe parameter to False') kwargs.setdefault('content_type', 'application/json') data = json.dumps(data, cls=encoder) super(JsonResponse, self).__init__(content=data, **kwargs) def get_app_list(context, order=True): admin_site = get_admin_site(context.get('current_app', '')) request = context['request'] app_dict = {} for model, model_admin in admin_site._registry.items(): app_label = model._meta.app_label try: has_module_perms = model_admin.has_module_permission(request) except AttributeError: has_module_perms = request.user.has_module_perms(app_label) # Fix Django < 1.8 issue if has_module_perms: perms = model_admin.get_model_perms(request) # Check whether user has any perm for this module. # If so, add the module to the model_list. if True in perms.values(): info = (app_label, model._meta.model_name) model_dict = { 'name': capfirst(model._meta.verbose_name_plural), 'object_name': model._meta.object_name, 'perms': perms, } if perms.get('change', False): try: model_dict['admin_url'] = reverse('admin:%s_%s_changelist' % info, current_app=admin_site.name) except NoReverseMatch: pass if perms.get('add', False): try: model_dict['add_url'] = reverse('admin:%s_%s_add' % info, current_app=admin_site.name) except NoReverseMatch: pass if app_label in app_dict: app_dict[app_label]['models'].append(model_dict) else: try: name = apps.get_app_config(app_label).verbose_name except NameError: name = app_label.title() app_dict[app_label] = { 'name': name, 'app_label': app_label, 'app_url': reverse( 'admin:app_list', kwargs={'app_label': app_label}, current_app=admin_site.name, ), 'has_module_perms': has_module_perms, 'models': [model_dict], } # Sort the apps alphabetically. app_list = list(app_dict.values()) if order: app_list.sort(key=lambda x: x['name'].lower()) # Sort the models alphabetically within each app. for app in app_list: app['models'].sort(key=lambda x: x['name']) return app_list def get_admin_site(current_app): try: resolver_match = resolve(reverse('%s:index' % current_app)) for func_closure in resolver_match.func.func_closure: if isinstance(func_closure.cell_contents, AdminSite): return func_closure.cell_contents except: pass return admin.site def get_admin_site_name(context): return get_admin_site(context).name class LazyDateTimeEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, datetime.datetime) or isinstance(obj, datetime.date): return obj.isoformat() elif isinstance(obj, Promise): return force_text(obj) return self.encode(obj) def get_model_instance_label(instance): if getattr(instance, "related_label", None): return instance.related_label() return smart_text(instance) class SuccessMessageMixin(object): """ Adds a success message on successful form submission. """ success_message = '' def form_valid(self, form): response = super(SuccessMessageMixin, self).form_valid(form) success_message = self.get_success_message(form.cleaned_data) if success_message: messages.success(self.request, success_message) return response def get_success_message(self, cleaned_data): return self.success_message % cleaned_data