Skip to content
Newer
Older
100644 47 lines (39 sloc) 1.5 KB
3e38904 @sveetch first commit
authored May 5, 2012
1 # -*- coding: utf-8 -*-
2 """
3 Various utilities
4 """
5 from django.conf import settings
6 from django.contrib.sites.models import Site
7 from django.template import Variable, TemplateSyntaxError, VariableDoesNotExist
8
9 def get_site_metas(with_static=False, with_media=False, extra={}):
10 """
11 Return metas from the current *Site* and settings
12
13 Added Site metas will be callable in templates like this ``SITE.themetaname``
14
15 This can be used in code out of a Django requests (like in management commands) or in
16 a context processor to get the *Site* urls.
17
18 Default metas returned :
19
20 * name: Current *Site* entry name;
21 * domain: Current *Site* entry domain;
22 * web_url: The Current *Site* entry domain prefixed with the http protocol;
23
24 Optionally it can also return ``STATIC_URL`` and ``MEDIA_URL`` if needed (like out
25 of Django requests).
26 """
27 site_current = Site.objects.get_current()
28 metas = {
29 'SITE': {
30 'name': site_current.name,
31 'domain': site_current.domain,
32 'web_url': 'http://%s' % site_current.domain, # TODO: This will be problematic in a HTTPS environment
33 }
34 }
35 if with_media:
36 metas['MEDIA_URL'] = getattr(settings, 'MEDIA_URL', '')
37 if with_static:
38 metas['STATIC_URL'] = getattr(settings, 'STATIC_URL', '')
39 metas.update(extra)
40 return metas
41
42 def site_metas(request):
43 """
44 Context processor to add the current *Site* metas to the context
45 """
46 return get_site_metas()
Something went wrong with that request. Please try again.