1
1
from django .db import models
2
+ from typing import Dict
2
3
3
4
from specifyweb .businessrules .exceptions import AbortSave
4
5
from . import model_extras
5
6
from .case_insensitive_bool import BooleanField , NullBooleanField
7
+ from .datamodel import Datamodel , Table , Relationship , Field
6
8
from .deletion_rules import SPECIAL_DELETION_RULES , ADDITIONAL_DELETE_BLOCKERS
7
9
8
10
appname = __name__ .split ('.' )[- 2 ]
16
18
'Collector' : ('ordernumber' ,),
17
19
}
18
20
19
- def make_model (module , table , datamodel ) :
21
+ def make_model (module : str , table : Table , datamodel : Datamodel ) -> models . base . ModelBase :
20
22
"""Returns a Django model class based on the
21
23
definition of a Specify table.
22
24
"""
@@ -64,18 +66,16 @@ def save(self, *args, **kwargs):
64
66
65
67
return model
66
68
67
- def make_id_field (column ) :
69
+ def make_id_field (column : str ) -> models . AutoField :
68
70
return models .AutoField (primary_key = True , db_column = column .lower ())
69
71
70
- def protect (collector , field , sub_objs , using ):
72
+ def protect (collector : models . deletion . Collector , field : models . Field , sub_objs : models . query . QuerySet , using : str ):
71
73
if hasattr (collector , 'delete_blockers' ):
72
74
collector .delete_blockers .append ((field , sub_objs ))
73
75
else :
74
76
models .PROTECT (collector , field , sub_objs , using )
75
77
76
-
77
-
78
- def make_relationship (modelname , rel , datamodel ):
78
+ def make_relationship (modelname : str , rel : Relationship , datamodel : Datamodel ) -> models .ForeignKey or models .OneToOneField :
79
79
"""Return a Django relationship field for the given relationship definition.
80
80
81
81
modelname - name of the model this field will be part of
@@ -107,7 +107,7 @@ def make_relationship(modelname, rel, datamodel):
107
107
else :
108
108
on_delete = protect
109
109
110
- def make_to_one (Field ) :
110
+ def make_to_one (Field : type ) -> models . ForeignKey or models . OneToOneField :
111
111
"""Setup a field of the given 'Field' type which can be either
112
112
ForeignKey (many-to-one) or OneToOneField.
113
113
"""
@@ -134,7 +134,7 @@ class make_field(object):
134
134
mechanism to factor out common aspects of Field configuration.
135
135
"""
136
136
@classmethod
137
- def get_field_class (cls , fld ):
137
+ def get_field_class (cls : type , fld ) -> models . Field :
138
138
"""Return the Django model field class to be used for
139
139
the given field definition. Defaults to returning the
140
140
'field_class' attribute of the class, but can be overridden
@@ -143,7 +143,7 @@ def get_field_class(cls, fld):
143
143
return cls .field_class
144
144
145
145
@classmethod
146
- def make_args (cls , fld ) :
146
+ def make_args (cls , fld : Field ) -> Dict [ str , str or bool ] :
147
147
"""Return a dict of arguments for the field constructor
148
148
based on the XML definition. These are common arguements
149
149
used by most field types.
@@ -154,7 +154,7 @@ def make_args(cls, fld):
154
154
unique = fld .unique ,
155
155
null = not fld .required )
156
156
157
- def __new__ (cls , fld , fldargs ):
157
+ def __new__ (cls , fld : Field , fldargs : Dict [ str , bool ] ):
158
158
"""Override the instance constructor to return configured instances
159
159
of the appropriant Django model field for given parameters.
160
160
@@ -206,7 +206,7 @@ class make_decimal_field(make_field):
206
206
field_class = models .DecimalField
207
207
208
208
@classmethod
209
- def make_args (cls , fld ):
209
+ def make_args (cls , fld : Field ):
210
210
"""Augment the standard field options with those specific
211
211
to Decimal fields.
212
212
"""
@@ -224,15 +224,15 @@ def make_args(cls, fld):
224
224
class make_boolean_field (make_field ):
225
225
"""A specialization of make_field for Boolean type fields."""
226
226
@classmethod
227
- def get_field_class (cls , fld ):
227
+ def get_field_class (cls , fld : Field ):
228
228
"""Django differentiates between boolean fields which
229
229
can contain nulls and those that cannot with different
230
230
types.
231
231
"""
232
232
return BooleanField if fld .required else NullBooleanField
233
233
234
234
@classmethod
235
- def make_args (cls , fld ):
235
+ def make_args (cls , fld : Field ):
236
236
"""Make False the default as it was in Django 1.5"""
237
237
args = super (make_boolean_field , cls ).make_args (fld )
238
238
if fld .required :
@@ -257,7 +257,9 @@ def make_args(cls, fld):
257
257
'java.lang.Boolean' : make_boolean_field ,
258
258
}
259
259
260
- def build_models (module , datamodel ):
260
+ # Build the table information as Django models
261
+ # See .models.py
262
+ def build_models (module : str , datamodel : Datamodel ):
261
263
return { model .specify_model .tableId : model
262
264
for table in datamodel .tables
263
265
for model in [ make_model (module , table , datamodel ) ]}
0 commit comments