PageRenderTime 358ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/feincms/content/application/models.py

https://github.com/guetux/feincms
Python | 399 lines | 308 code | 53 blank | 38 comment | 47 complexity | aafaf5e6d762b39cd41739cea68f1e93 MD5 | raw file
  1. """
  2. Third-party application inclusion support.
  3. """
  4. from __future__ import absolute_import, unicode_literals
  5. from email.utils import parsedate
  6. from time import mktime
  7. from random import SystemRandom
  8. import re
  9. from django.conf import settings
  10. from django.core.cache import cache
  11. from django.core.urlresolvers import (
  12. Resolver404, resolve, reverse, NoReverseMatch)
  13. from django.db import models
  14. from django.db.models import signals
  15. from django.http import HttpResponse
  16. from django.utils.functional import curry as partial, lazy, wraps
  17. from django.utils.http import http_date
  18. from django.utils.safestring import mark_safe
  19. from django.utils.translation import get_language, ugettext_lazy as _
  20. from feincms.admin.item_editor import ItemEditorForm
  21. from feincms.contrib.fields import JSONField
  22. from feincms.translations import short_language_code
  23. from feincms.utils import get_object
  24. def cycle_app_reverse_cache(*args, **kwargs):
  25. """Does not really empty the cache; instead it adds a random element to the
  26. cache key generation which guarantees that the cache does not yet contain
  27. values for all newly generated keys"""
  28. cache.set('app_reverse_cache_generation', str(SystemRandom().random()))
  29. # Set the app_reverse_cache_generation value once per startup (at least).
  30. # This protects us against offline modifications of the database.
  31. cycle_app_reverse_cache()
  32. def app_reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None,
  33. *vargs, **vkwargs):
  34. """
  35. Reverse URLs from application contents
  36. Works almost like Django's own reverse() method except that it resolves
  37. URLs from application contents. The second argument, ``urlconf``, has to
  38. correspond to the URLconf parameter passed in the ``APPLICATIONS`` list
  39. to ``Page.create_content_type``::
  40. app_reverse('mymodel-detail', 'myapp.urls', args=...)
  41. or
  42. app_reverse('mymodel-detail', 'myapp.urls', kwargs=...)
  43. The second argument may also be a request object if you want to reverse
  44. an URL belonging to the current application content.
  45. """
  46. # First parameter might be a request instead of an urlconf path, so
  47. # we'll try to be helpful and extract the current urlconf from it
  48. extra_context = getattr(urlconf, '_feincms_extra_context', {})
  49. appconfig = extra_context.get('app_config', {})
  50. urlconf = appconfig.get('urlconf_path', urlconf)
  51. cache_generation = cache.get('app_reverse_cache_generation')
  52. if cache_generation is None:
  53. # This might never happen. Still, better be safe than sorry.
  54. cycle_app_reverse_cache()
  55. cache_generation = cache.get('app_reverse_cache_generation')
  56. cache_key = '%s-%s-%s-%s' % (
  57. urlconf,
  58. get_language(),
  59. getattr(settings, 'SITE_ID', 0),
  60. cache_generation)
  61. url_prefix = cache.get(cache_key)
  62. if url_prefix is None:
  63. appcontent_class = ApplicationContent._feincms_content_models[0]
  64. content = appcontent_class.closest_match(urlconf)
  65. if content is not None:
  66. if urlconf in appcontent_class.ALL_APPS_CONFIG:
  67. # We have an overridden URLconf
  68. app_config = appcontent_class.ALL_APPS_CONFIG[urlconf]
  69. urlconf = app_config['config'].get('urls', urlconf)
  70. prefix = content.parent.get_absolute_url()
  71. prefix += '/' if prefix[-1] != '/' else ''
  72. url_prefix = (urlconf, prefix)
  73. cache.set(cache_key, url_prefix)
  74. if url_prefix:
  75. # vargs and vkwargs are used to send through additional parameters
  76. # which are uninteresting to us (such as current_app)
  77. return reverse(
  78. viewname,
  79. url_prefix[0],
  80. args=args,
  81. kwargs=kwargs,
  82. prefix=url_prefix[1],
  83. *vargs, **vkwargs)
  84. raise NoReverseMatch("Unable to find ApplicationContent for %r" % urlconf)
  85. #: Lazy version of ``app_reverse``
  86. app_reverse_lazy = lazy(app_reverse, str)
  87. def permalink(func):
  88. """
  89. Decorator that calls app_reverse()
  90. Use this instead of standard django.db.models.permalink if you want to
  91. integrate the model through ApplicationContent. The wrapped function
  92. must return 4 instead of 3 arguments::
  93. class MyModel(models.Model):
  94. @appmodels.permalink
  95. def get_absolute_url(self):
  96. return ('myapp.urls', 'model_detail', (), {'slug': self.slug})
  97. """
  98. def inner(*args, **kwargs):
  99. return app_reverse(*func(*args, **kwargs))
  100. return wraps(func)(inner)
  101. APPLICATIONCONTENT_RE = re.compile(r'^([^/]+)/([^/]+)$')
  102. class ApplicationContent(models.Model):
  103. #: parameters is used to serialize instance-specific data which will be
  104. # provided to the view code. This allows customization (e.g. "Embed
  105. # MyBlogApp for blog <slug>")
  106. parameters = JSONField(null=True, editable=False)
  107. ALL_APPS_CONFIG = {}
  108. class Meta:
  109. abstract = True
  110. verbose_name = _('application content')
  111. verbose_name_plural = _('application contents')
  112. @classmethod
  113. def initialize_type(cls, APPLICATIONS):
  114. for i in APPLICATIONS:
  115. if not 2 <= len(i) <= 3:
  116. raise ValueError(
  117. "APPLICATIONS must be provided with tuples containing at"
  118. " least two parameters (urls, name) and an optional extra"
  119. " config dict")
  120. urls, name = i[0:2]
  121. if len(i) == 3:
  122. app_conf = i[2]
  123. if not isinstance(app_conf, dict):
  124. raise ValueError(
  125. "The third parameter of an APPLICATIONS entry must be"
  126. " a dict or the name of one!")
  127. else:
  128. app_conf = {}
  129. cls.ALL_APPS_CONFIG[urls] = {
  130. "urls": urls,
  131. "name": name,
  132. "config": app_conf
  133. }
  134. cls.add_to_class(
  135. 'urlconf_path',
  136. models.CharField(_('application'), max_length=100, choices=[
  137. (c['urls'], c['name']) for c in cls.ALL_APPS_CONFIG.values()])
  138. )
  139. class ApplicationContentItemEditorForm(ItemEditorForm):
  140. app_config = {}
  141. custom_fields = {}
  142. def __init__(self, *args, **kwargs):
  143. super(ApplicationContentItemEditorForm, self).__init__(
  144. *args, **kwargs)
  145. instance = kwargs.get("instance", None)
  146. if instance:
  147. try:
  148. # TODO use urlconf_path from POST if set
  149. # urlconf_path = request.POST.get('...urlconf_path',
  150. # instance.urlconf_path)
  151. self.app_config = cls.ALL_APPS_CONFIG[
  152. instance.urlconf_path]['config']
  153. except KeyError:
  154. self.app_config = {}
  155. self.custom_fields = {}
  156. admin_fields = self.app_config.get('admin_fields', {})
  157. if isinstance(admin_fields, dict):
  158. self.custom_fields.update(admin_fields)
  159. else:
  160. get_fields = get_object(admin_fields)
  161. self.custom_fields.update(
  162. get_fields(self, *args, **kwargs))
  163. for k, v in self.custom_fields.items():
  164. self.fields[k] = v
  165. def save(self, commit=True, *args, **kwargs):
  166. # Django ModelForms return the model instance from save. We'll
  167. # call save with commit=False first to do any necessary work &
  168. # get the model so we can set .parameters to the values of our
  169. # custom fields before calling save(commit=True)
  170. m = super(ApplicationContentItemEditorForm, self).save(
  171. commit=False, *args, **kwargs)
  172. m.parameters = dict(
  173. (k, self.cleaned_data[k])
  174. for k in self.custom_fields if k in self.cleaned_data)
  175. if commit:
  176. m.save(**kwargs)
  177. return m
  178. # This provides hooks for us to customize the admin interface for
  179. # embedded instances:
  180. cls.feincms_item_editor_form = ApplicationContentItemEditorForm
  181. # Clobber the app_reverse cache when saving application contents
  182. # and/or pages
  183. page_class = cls.parent.field.rel.to
  184. signals.post_save.connect(cycle_app_reverse_cache, sender=cls)
  185. signals.post_delete.connect(cycle_app_reverse_cache, sender=cls)
  186. signals.post_save.connect(cycle_app_reverse_cache, sender=page_class)
  187. signals.post_delete.connect(cycle_app_reverse_cache, sender=page_class)
  188. def __init__(self, *args, **kwargs):
  189. super(ApplicationContent, self).__init__(*args, **kwargs)
  190. self.app_config = self.ALL_APPS_CONFIG.get(
  191. self.urlconf_path, {}).get('config', {})
  192. def process(self, request, **kw):
  193. page_url = self.parent.get_absolute_url()
  194. # Provide a way for appcontent items to customize URL processing by
  195. # altering the perceived path of the page:
  196. if "path_mapper" in self.app_config:
  197. path_mapper = get_object(self.app_config["path_mapper"])
  198. path, page_url = path_mapper(
  199. request.path,
  200. page_url,
  201. appcontent_parameters=self.parameters
  202. )
  203. else:
  204. path = request._feincms_extra_context['extra_path']
  205. # Resolve the module holding the application urls.
  206. urlconf_path = self.app_config.get('urls', self.urlconf_path)
  207. try:
  208. fn, args, kwargs = resolve(path, urlconf_path)
  209. except (ValueError, Resolver404):
  210. raise Resolver404(str('Not found (resolving %r in %r failed)') % (
  211. path, urlconf_path))
  212. # Variables from the ApplicationContent parameters are added to request
  213. # so we can expose them to our templates via the appcontent_parameters
  214. # context_processor
  215. request._feincms_extra_context.update(self.parameters)
  216. # Save the application configuration for reuse elsewhere
  217. request._feincms_extra_context.update({
  218. 'app_config': dict(
  219. self.app_config,
  220. urlconf_path=self.urlconf_path,
  221. ),
  222. })
  223. view_wrapper = self.app_config.get("view_wrapper", None)
  224. if view_wrapper:
  225. fn = partial(
  226. get_object(view_wrapper),
  227. view=fn,
  228. appcontent_parameters=self.parameters
  229. )
  230. output = fn(request, *args, **kwargs)
  231. if isinstance(output, HttpResponse):
  232. if self.send_directly(request, output):
  233. return output
  234. elif output.status_code == 200:
  235. # If the response supports deferred rendering, render the
  236. # response right now. We do not handle template response
  237. # middleware.
  238. if hasattr(output, 'render') and callable(output.render):
  239. output.render()
  240. self.rendered_result = mark_safe(
  241. output.content.decode('utf-8'))
  242. self.rendered_headers = {}
  243. # Copy relevant headers for later perusal
  244. for h in ('Cache-Control', 'Last-Modified', 'Expires'):
  245. if h in output:
  246. self.rendered_headers.setdefault(
  247. h, []).append(output[h])
  248. elif isinstance(output, tuple) and 'view' in kw:
  249. kw['view'].template_name = output[0]
  250. kw['view'].request._feincms_extra_context.update(output[1])
  251. else:
  252. self.rendered_result = mark_safe(output)
  253. return True # successful
  254. def send_directly(self, request, response):
  255. mimetype = response.get('Content-Type', 'text/plain')
  256. if ';' in mimetype:
  257. mimetype = mimetype.split(';')[0]
  258. mimetype = mimetype.strip()
  259. return (response.status_code != 200
  260. or request.is_ajax()
  261. or getattr(response, 'standalone', False)
  262. or mimetype not in ('text/html', 'text/plain'))
  263. def render(self, **kwargs):
  264. return getattr(self, 'rendered_result', '')
  265. def finalize(self, request, response):
  266. headers = getattr(self, 'rendered_headers', None)
  267. if headers:
  268. self._update_response_headers(request, response, headers)
  269. def _update_response_headers(self, request, response, headers):
  270. """
  271. Combine all headers that were set by the different content types
  272. We are interested in Cache-Control, Last-Modified, Expires
  273. """
  274. # Ideally, for the Cache-Control header, we'd want to do some
  275. # intelligent combining, but that's hard. Let's just collect and unique
  276. # them and let the client worry about that.
  277. cc_headers = set(('must-revalidate',))
  278. for x in (cc.split(",") for cc in headers.get('Cache-Control', ())):
  279. cc_headers |= set((s.strip() for s in x))
  280. if len(cc_headers):
  281. response['Cache-Control'] = ", ".join(cc_headers)
  282. else: # Default value
  283. response['Cache-Control'] = 'no-cache, must-revalidate'
  284. # Check all Last-Modified headers, choose the latest one
  285. lm_list = [parsedate(x) for x in headers.get('Last-Modified', ())]
  286. if len(lm_list) > 0:
  287. response['Last-Modified'] = http_date(mktime(max(lm_list)))
  288. # Check all Expires headers, choose the earliest one
  289. lm_list = [parsedate(x) for x in headers.get('Expires', ())]
  290. if len(lm_list) > 0:
  291. response['Expires'] = http_date(mktime(min(lm_list)))
  292. @classmethod
  293. def closest_match(cls, urlconf_path):
  294. page_class = cls.parent.field.rel.to
  295. contents = cls.objects.filter(
  296. parent__in=page_class.objects.active(),
  297. urlconf_path=urlconf_path,
  298. ).order_by('pk').select_related('parent')
  299. if len(contents) > 1:
  300. try:
  301. current = short_language_code(get_language())
  302. return [
  303. content for content in contents if
  304. short_language_code(content.parent.language) == current
  305. ][0]
  306. except (AttributeError, IndexError):
  307. pass
  308. try:
  309. return contents[0]
  310. except IndexError:
  311. pass
  312. return None