|
1
|
|
|
import django |
|
2
|
|
|
try: |
|
3
|
|
|
from django.apps import apps |
|
4
|
|
|
except ImportError: |
|
5
|
|
|
pass |
|
6
|
|
|
|
|
7
|
|
|
from crudbuilder.registry import register |
|
8
|
|
|
from six import with_metaclass |
|
9
|
|
|
from django.contrib.contenttypes.models import ContentType |
|
10
|
|
|
|
|
11
|
|
|
from crudbuilder.exceptions import NotModelException |
|
12
|
|
|
from crudbuilder import helpers |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class BaseBuilder(object): |
|
16
|
|
|
def __init__( |
|
17
|
|
|
self, |
|
18
|
|
|
app, |
|
19
|
|
|
model, |
|
20
|
|
|
crud |
|
21
|
|
|
): |
|
22
|
|
|
|
|
23
|
|
|
self.model = model |
|
24
|
|
|
self.app = app |
|
25
|
|
|
self.crud = crud |
|
26
|
|
|
|
|
27
|
|
|
self.custom_modelform = self._has_crud_attr('custom_modelform') |
|
28
|
|
|
self.modelform_excludes = self._has_crud_attr('modelform_excludes') |
|
29
|
|
|
self.detailview_excludes = self._has_crud_attr('detailview_excludes') |
|
30
|
|
|
self.createupdate_forms = self._has_crud_attr('createupdate_forms') |
|
31
|
|
|
|
|
32
|
|
|
# django tables2 |
|
33
|
|
|
self.custom_table2 = self._has_crud_attr('custom_table2') |
|
34
|
|
|
self.tables2_fields = self._has_crud_attr('tables2_fields') |
|
35
|
|
|
self.tables2_css_class = self._has_crud_attr('tables2_css_class') |
|
36
|
|
|
self.tables2_pagination = self._has_crud_attr('tables2_pagination') |
|
37
|
|
|
|
|
38
|
|
|
self.permission_required = self._has_crud_attr('permission_required') |
|
39
|
|
|
self.permissions = self._has_crud_attr('permissions') |
|
40
|
|
|
self.login_required = self._has_crud_attr('login_required') |
|
41
|
|
|
|
|
42
|
|
|
self.custom_templates = self._has_crud_attr('custom_templates') |
|
43
|
|
|
self.custom_queryset = self._has_crud_attr('custom_queryset') |
|
44
|
|
|
self.custom_context = self._has_crud_attr('custom_context') |
|
45
|
|
|
self.inlineformset = self.get_inlineformset |
|
46
|
|
|
self.custom_postfix_url = self.postfix_url |
|
47
|
|
|
|
|
48
|
|
|
@property |
|
49
|
|
|
def get_model_class(self): |
|
50
|
|
|
"""Returns model class""" |
|
51
|
|
|
try: |
|
52
|
|
|
c = ContentType.objects.get(app_label=self.app, model=self.model) |
|
53
|
|
|
except ContentType.DoesNotExist: |
|
54
|
|
|
# try another kind of resolution |
|
55
|
|
|
# fixes a situation where a proxy model is defined in some external app. |
|
56
|
|
|
if django.VERSION >= (1, 7): |
|
57
|
|
|
return apps.get_model(self.app, self.model) |
|
58
|
|
|
else: |
|
59
|
|
|
return c.model_class() |
|
60
|
|
|
|
|
61
|
|
|
def _has_crud_attr(self, attr): |
|
62
|
|
|
return getattr(self.crud, attr, None) |
|
63
|
|
|
|
|
64
|
|
|
def view_permission(self, view_type): |
|
65
|
|
|
if self.permissions: |
|
66
|
|
|
return self.permissions.get(view_type, None) |
|
67
|
|
|
else: |
|
68
|
|
|
return '{}.{}_{}'.format(self.app, self.model, view_type) |
|
69
|
|
|
|
|
70
|
|
|
@property |
|
71
|
|
|
def check_login_required(self): |
|
72
|
|
|
return True if self.login_required else False |
|
73
|
|
|
|
|
74
|
|
|
@property |
|
75
|
|
|
def check_permission_required(self): |
|
76
|
|
|
return True if self.permission_required else False |
|
77
|
|
|
|
|
78
|
|
|
@property |
|
79
|
|
|
def get_inlineformset(self): |
|
80
|
|
|
if self.crud.inlineformset: |
|
81
|
|
|
return self.crud.inlineformset().construct_formset() |
|
82
|
|
|
else: |
|
83
|
|
|
return None |
|
84
|
|
|
|
|
85
|
|
|
@property |
|
86
|
|
|
def postfix_url(self): |
|
87
|
|
|
return helpers.custom_postfix_url(self.crud(), self.model) |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
class MetaCrudRegister(type): |
|
91
|
|
|
def __new__(cls, clsname, bases, attrs): |
|
92
|
|
|
newclass = super( |
|
93
|
|
|
MetaCrudRegister, cls |
|
94
|
|
|
).__new__(cls, clsname, bases, attrs) |
|
95
|
|
|
|
|
96
|
|
|
if bases: |
|
97
|
|
|
if newclass.model: |
|
98
|
|
|
register(newclass.model, newclass) |
|
99
|
|
|
else: |
|
100
|
|
|
msg = "No model defined in {} class".format(newclass) |
|
101
|
|
|
raise NotModelException(msg) |
|
102
|
|
|
return newclass |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
class BaseCrudBuilder(with_metaclass(MetaCrudRegister)): |
|
106
|
|
|
model = None |
|
107
|
|
|
inlineformset = None |
|
108
|
|
|
|