-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
61 lines (42 loc) · 1.68 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from pydoc import classname
from django.db.models import get_model
from django.conf import settings
try:
import permalinks
HAS_PERMALINKS = True
except ImportError, e:
HAS_PERMALINKS = False
try:
NO_APP_GET_ABSOLUTE_URL = settings.NO_APP_GET_ABSOLUTE_URL
except AttributeError:
NO_APP_GET_ABSOLUTE_URL = False
class NoOverrides(Exception):
"""is thrown if there is no permalinks module available"""
def __init__(self, msg):
super(NoOverrides, self).__init__(msg)
class URLResolver(object):
def __init__(self):
self.lookup_cache = dict()
def check_configured(self):
if HAS_PERMALINKS == False:
raise NoOverrides(u"To use the get_permalink tag create a permalinks module in your PYTHONPATH.")
def get_app_class(self, app_name):
cls_inst = getattr(permalinks, app_name, None)
if cls_inst == None and NO_APP_GET_ABSOLUTE_URL == True:
raise NoOverrides(u"No %s class in permlainks module." %(app_name))
return cls_inst
def get_permalink_callable(self, obj):
cls_inst = self.get_app_class( obj._meta.app_label )
meth_name = 'get_%s_url' %(obj._meta.object_name)
meth_name = meth_name.lower()
func = getattr(cls_inst, meth_name, None)
if func == None and NO_APP_GET_ABSOLUTE_URL == True:
raise NoOverrides(u"No %s classmethod in class %s." %(meth_name, cls_inst.__name__))
return func
def get_permalink(self, obj):
self.check_configured()
func = self.get_permalink_callable(obj)
if func:
return func( obj )
return obj.get_absolute_url()
resolver = URLResolver()