1
1
# -*- coding: utf-8 -*-
2
- import warnings
3
2
from django import template
4
3
from django .contrib .contenttypes .models import ContentType
5
4
from django .core .exceptions import ObjectDoesNotExist , ImproperlyConfigured
6
5
from django .http import HttpRequest
7
6
from django .template .loader import render_to_string
8
7
from django .utils .html import escape
9
8
from seo .models import Seo , Url
9
+ import warnings
10
10
11
11
INTENTS = ['title' , 'keywords' , 'description' , ]
12
12
@@ -18,35 +18,44 @@ def __init__(self, intent, object, variable):
18
18
self .object = object
19
19
self .variable = variable
20
20
21
+ def _process_var_argument (self , context ):
22
+ if self .variable is None :
23
+ return escape (getattr (seo , self .intent ))
24
+ else :
25
+ context [self .variable ] = getattr (seo , self .intent )
26
+ return u''
27
+
21
28
def render (self , context ):
22
29
if self .object is None :
23
- object = None
30
+ # search by URL
31
+ try :
32
+ request = context ['request' ]
33
+ except KeyError :
34
+ warnings .warn ('`request` was not found in context. Add "django.core.context_processors.request" to `TEMPLATE_CONTEXT_PROCESSORS` in your settings.py.' )
35
+ else :
36
+ object = Url .objects .get (url = request .path_info )
37
+ seo = Seo .objects .get (
38
+ content_type = ContentType .objects .get_for_model (
39
+ object .__class__ ),
40
+ object_id = object .id )
41
+ return self ._process_var_argument (context )
42
+
24
43
else :
44
+ # Tyr to retrieve object from context
25
45
object = template .Variable (self .object ).resolve (context )
26
- try :
27
- if object is None :
28
- raise ObjectDoesNotExist
29
- seo = Seo .objects .get (
30
- content_type = ContentType .objects .get_for_model (
31
- object .__class__ ),
32
- object_id = object .id )
33
- except ObjectDoesNotExist :
34
- if 'request' not in context :
35
- warnings .warn ('`request` was not found in context. Is "django.core.context_processors.request" enabled in `TEMPLATE_CONTEXT_PROCESSORS`?' )
36
- path_info = context ['request' ].path_info
37
46
try :
38
- object = Url .objects .get (url = path_info )
39
47
seo = Seo .objects .get (
40
48
content_type = ContentType .objects .get_for_model (
41
49
object .__class__ ),
42
50
object_id = object .id )
43
- except ObjectDoesNotExist :
44
- seo = Seo ()
45
- if self .variable is None :
46
- return escape (getattr (seo , self .intent ))
47
- else :
48
- context [self .variable ] = getattr (seo , self .intent )
49
- return u''
51
+ except Seo .DoesNotExist :
52
+ raise
53
+ else :
54
+ return self ._process_var_argument (context )
55
+
56
+ # silent fallback
57
+ return u''
58
+
50
59
51
60
def seo_tag (parser , token ):
52
61
"""Get seo data for object"""
0 commit comments