forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
41 lines (31 loc) · 1.46 KB
/
models.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
from django.urls import reverse
class UrlMixin(object):
def get_url(self, name, **add_kwargs):
kwargs = self.get_url_kwargs()
kwargs.update(add_kwargs)
return reverse(name, kwargs=kwargs)
def get_display_url(self):
return self.get_absolute_url()
def get_absolute_url(self):
if not hasattr(self, 'ABSOLUTE_URL_NAME'):
raise NotImplementedError("Model %r doesn't have absolute url" % self)
return self.get_url(self.ABSOLUTE_URL_NAME)
def get_edit_url(self):
if not hasattr(self, 'EDIT_URL_NAME'):
raise NotImplementedError("Model %r doesn't have absolute url" % self)
return self.get_url(self.EDIT_URL_NAME)
def install_defer_logger():
import logging
import traceback
from django.db.models.query_utils import DeferredAttribute
logger = logging.getLogger('django.db.deferred')
orig_get = DeferredAttribute.__get__
logger.warning("Installing logger for deferred model fields...")
def get(self, instance, cls=None):
if instance is None:
return self
if self.field_name not in instance.__dict__:
filename, linenum, funcname, command = tuple(traceback.extract_stack()[-2])
logger.warning("Resolving deferred: %s.%s in %s, line %s, func %s: %s", instance.__class__.__name__, self.field_name, filename, linenum, funcname, command)
return orig_get(self, instance, cls=cls)
DeferredAttribute.__get__ = get