def lookup_section(lookup_from): """ NB: `lookup_from` may either be an HTTP request, or a string representing an integer. """ Section = app_settings.get_extending_model() if lookup_from.__class__.__name__ == "WSGIRequest": path_map = _get_section_path_map() section_paths = path_map.keys() # Sort by shortest path to longest. section_paths.sort(lambda x, y: len(x) <= len(y) and 1 or -1) # Strips leading and trailing slashes path = lookup_from.path.strip("/") path_matches = [p for p in section_paths if path.startswith(p)] if len(path_matches) >= 1 and path_map.has_key(path): if app_settings.VALIDATE_GLOBALLY_UNIQUE_SLUGS: # If slugs have to be globally unique, we can shortcut to a # more efficient query. return Section.objects.get(slug=path_map[path]) else: # If slugs are not unique, then we need to search through all # matches for the slug that actually matches our path. sections = Section.objects.filter(slug=path_map[path]) if len(sections) == 1: return sections[0] else: for section in sections: if section.full_path == path: return section return None else: try: return Section.objects.get(pk=int(lookup_from)) except Section.DoesNotExist: return None
def _build_section_path_map(): """ Simple wrapper around Django's low level cache; stores and populates a list of all section urls using the low-level caching framework. """ paths = {} Section = app_settings.get_extending_model() for section in Section.objects.all(): paths[section.full_path] = section.slug cache.set(app_settings.PATH_CACHE_KEY, paths, app_settings.PATH_CACHE_TTL) return paths
def get_admin_urls(self, obj): self._patch_get_extending_model() opts = app_settings.get_extending_model()._meta prefix = "admin:%s_%s_" % (opts.app_label, opts.module_name) views = ('create', 'change','delete', 'move','related','order') arg = hasattr(obj, 'pk') and obj.pk or obj urls = {} for view in views: urls[view] = reverse(prefix + view, args=(arg,)) urls['index'] = self.admin_index_url return urls
def get_admin_urls(self, obj): self._patch_get_extending_model() opts = app_settings.get_extending_model()._meta prefix = "admin:%s_%s_" % (opts.app_label, opts.module_name) views = ('create', 'change', 'delete', 'move', 'related', 'order') arg = hasattr(obj, 'pk') and obj.pk or obj urls = {} for view in views: urls[view] = reverse(prefix + view, args=(arg, )) urls['index'] = self.admin_index_url return urls
class Meta: model = app_settings.get_extending_model() exclude = ('path', 'depth', 'numchild', 'order')
from django.core.exceptions import MiddlewareNotUsed, ImproperlyConfigured from django.contrib.flatpages.views import flatpage from django.db.models.loading import AppCache from django.http import Http404 from django.views.generic import simple from middleware import get_current_section, lookup_section import app_settings Section = app_settings.get_extending_model() def section(request, section_path=None, id_override=None): """ A view of a section. """ try: section = get_current_section() except MiddlewareNotUsed: lookup_from = id_override or request section = lookup_section(lookup_from) if section: return simple.direct_to_template(request, template="scaffold/section.html", extra_context={'section': section}) else: app_cache = AppCache() try: app_cache.get_app('flatpages') try: return flatpage(request, request.path_info)
from django.core.exceptions import MiddlewareNotUsed, ImproperlyConfigured from django.contrib.flatpages.views import flatpage from django.db.models.loading import AppCache from django.http import Http404 from django.shortcuts import render_to_response from django.template import RequestContext from middleware import get_current_section, lookup_section import app_settings Section = app_settings.get_extending_model() def section(request, section_path=None, id_override=None): """ A view of a section. """ try: section = get_current_section() except MiddlewareNotUsed: lookup_from = id_override or request section = lookup_section(lookup_from) if section: return render_to_response("scaffold/section.html", {'section': section}, context_instance=RequestContext(request)) else: app_cache = AppCache() try: app_cache.get_app('flatpages') try: return flatpage(request, request.path_info) except Http404: pass
class SectionsMiddleware(object): """ Middleware that stores the current section (if any) in the thread of the currently executing request """ def process_request(self, request): """ Determine the section from the request and store it in the currently executing thread where anyone can grab it (remember, in Django, there's one request per thread).""" section = lookup_section(request) _thread_locals.scaffold_middleware_enabled = True _thread_locals.section = section def reset_section_path_map(sender, **kwargs): _build_section_path_map() # Rebuild path map when a section is saved or removed. # See http://code.djangoproject.com/wiki/[...] # Signals#Helppost_saveseemstobeemittedtwiceforeachsave # for an explanation of why dispatch_uid is needed. post_save.connect(reset_section_path_map, sender=app_settings.get_extending_model(), dispatch_uid="paths-reset") post_delete.connect(reset_section_path_map, sender=app_settings.get_extending_model(), dispatch_uid="paths-reset")
from django.http import HttpResponseBadRequest, HttpResponseServerError, Http404 from django.shortcuts import get_object_or_404 from django.utils.encoding import force_unicode from django.forms.util import ErrorList from django.utils.functional import update_wrapper from django.utils.html import escape from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ from django.views.generic.base import TemplateView, RedirectView from forms import SectionForm import app_settings app_name = app_settings.EXTENDING_APP_NAME allow_associated_ordering = app_settings.ALLOW_ASSOCIATED_ORDERING model_proxy = app_settings.get_extending_model() csrf_protect_m = admin.options.csrf_protect_m class ExtraContextTemplateView(TemplateView): extra_context = {} def get_context_data(self, **kwargs): context = super(ExtraContextTemplateView, self).get_context_data(**kwargs) context.update(self.extra_context) return context class SectionAdmin(admin.ModelAdmin): class Media: css = {
def admin_index_url(self): self._patch_get_extending_model() opts = app_settings.get_extending_model()._meta return reverse( 'admin:%s_%s_changelist' % (opts.app_label, opts.module_name) )
def admin_index_url(self): self._patch_get_extending_model() opts = app_settings.get_extending_model()._meta return reverse('admin:%s_%s_changelist' % (opts.app_label, opts.module_name))
class SectionsMiddleware(object): """ Middleware that stores the current section (if any) in the thread of the currently executing request """ def process_request(self, request): """ Determine the section from the request and store it in the currently executing thread where anyone can grab it (remember, in Django, there's one request per thread).""" section = lookup_section(request) _thread_locals.scaffold_middleware_enabled = True _thread_locals.section = section def reset_section_path_map(sender, **kwargs): _build_section_path_map() # Rebuild path map when a section is saved or removed. # See http://code.djangoproject.com/wiki/[...] # Signals#Helppost_saveseemstobeemittedtwiceforeachsave # for an explanation of why dispatch_uid is needed. post_save.connect(reset_section_path_map, sender=app_settings.get_extending_model(), dispatch_uid="paths-reset" ) post_delete.connect(reset_section_path_map, sender=app_settings.get_extending_model(), dispatch_uid="paths-reset" )