Skip to content

Commit cb2dc7f

Browse files
committed
Fix inline imports
1 parent 3588867 commit cb2dc7f

File tree

11 files changed

+53
-30
lines changed

11 files changed

+53
-30
lines changed

django_sorcery/db/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,18 @@
143143

144144
from __future__ import absolute_import, print_function, unicode_literals
145145

146+
from . import middleware
146147
from .sqlalchemy import SQLAlchemy # noqa
147148
from .utils import dbdict
148149

149150

150151
databases = dbdict()
152+
153+
154+
class SQLAlchemyMiddleware(middleware.SQLAlchemyDBMiddleware):
155+
156+
db = databases
157+
158+
159+
# For backwards compat, we set it back to the middleware module
160+
middleware.SQLAlchemyMiddleware = SQLAlchemyMiddleware

django_sorcery/db/fields.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from django.core import validators as django_validators
1010
from django.forms import fields as djangofields
1111

12+
from .. import fields as sorceryfields
13+
1214

1315
__all__ = [
1416
"BigIntegerField",
@@ -208,9 +210,7 @@ def get_type(self, type_class, type_kwargs):
208210

209211
def get_form_class(self, kwargs):
210212
if self.type.enum_class:
211-
from ..fields import EnumField
212-
213-
return EnumField
213+
return sorceryfields.EnumField
214214

215215
return djangofields.TypedChoiceField
216216

django_sorcery/db/meta/__init__.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, print_function, unicode_literals
3+
import sys
4+
import types
35

46

5-
if True: # skipping importanize, imports need to be in this order for py2
6-
from .column import * # noqa
7-
from .model import * # noqa
8-
from .composite import * # noqa
9-
from .relations import * # noqa
7+
class lazy_module(types.ModuleType):
8+
def __init__(self, name, old_module, origins):
9+
super(lazy_module, self).__init__(name)
10+
self.origins = origins or {}
11+
self.__dict__.update(
12+
{
13+
"__file__": old_module.__file__,
14+
"__package__": old_module.__package__,
15+
"__path__": old_module.__path__,
16+
"__doc__": old_module.__doc__,
17+
}
18+
)
19+
20+
def __getattr__(self, name):
21+
if name in self.origins:
22+
module = __import__(self.__name__ + "." + self.origins[name], None, None, [name])
23+
return getattr(module, name)
24+
return types.ModuleType.__getattribute__(self, name)
25+
26+
27+
old_module = sys.modules[__name__]
28+
sys.modules[__name__] = lazy_module(
29+
__name__,
30+
old_module,
31+
{"model_info": "model", "Identity": "model", "relation_info": "relations", "column_info": "column"},
32+
)

django_sorcery/db/meta/relations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from django.core.exceptions import ImproperlyConfigured
88

9+
from ...fields import ModelChoiceField, ModelMultipleChoiceField
10+
911

1012
class relation_info(object):
1113
"""
@@ -114,8 +116,6 @@ def formfield(self, form_class=None, **kwargs):
114116
return form_class(self.related_model, **field_kwargs)
115117

116118
def get_form_class(self):
117-
from ...fields import ModelChoiceField, ModelMultipleChoiceField
118-
119119
if self.uselist:
120120
return ModelMultipleChoiceField
121121

django_sorcery/db/middleware.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import absolute_import, print_function, unicode_literals
33
import logging
44

5-
from . import databases
65
from .signals import all_signals
76

87

@@ -78,8 +77,3 @@ def commit(self, request, response):
7877

7978
def remove(self, request, response):
8079
self.db.remove()
81-
82-
83-
class SQLAlchemyMiddleware(SQLAlchemyDBMiddleware):
84-
85-
db = databases

django_sorcery/db/models.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from django.utils.text import camel_case_to_spaces
1414

15+
from ..forms import model_to_dict as real_model_to_dict
1516
from . import meta, signals
1617
from .mixins import CleanMixin
1718

@@ -39,9 +40,7 @@ def get_identity_key(model, kwargs):
3940

4041
def model_to_dict(instance, fields=None, exclude=None):
4142
warnings.warn("Deprecated, use django_sorcery.forms.model_to_dict instead.", DeprecationWarning)
42-
from ..forms import model_to_dict
43-
44-
return model_to_dict(instance, fields=fields, exclude=exclude)
43+
return real_model_to_dict(instance, fields=fields, exclude=exclude)
4544

4645

4746
def simple_repr(instance, fields=None):

django_sorcery/db/sqlalchemy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ..utils import make_args
1515
from . import fields
1616
from .composites import BaseComposite, CompositeField
17+
from .middleware import SQLAlchemyDBMiddleware
1718
from .models import Base, BaseMeta
1819
from .query import Query, QueryProperty
1920
from .relations import RelationsMixin
@@ -239,8 +240,6 @@ def make_middleware(self):
239240
"""
240241
Creates a middleware to be used in a django application
241242
"""
242-
from .middleware import SQLAlchemyDBMiddleware
243-
244243
return type(str("{}SQLAlchemyMiddleware".format(self.alias)), (SQLAlchemyDBMiddleware,), {"db": self})
245244

246245
def args(self, *args, **kwargs):

django_sorcery/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.forms import fields as djangofields
1010
from django.utils.translation import gettext_lazy
1111

12+
from .db import meta
1213
from .utils import suppress
1314

1415

@@ -110,7 +111,6 @@ def __init__(
110111

111112
self._choices = None
112113
self.model = model
113-
from .db import meta
114114

115115
self.model_info = meta.model_info(model)
116116
self.session = session
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, print_function, unicode_literals
33

4-
5-
if True:
6-
from .runner import ValidationRunner # noqa
7-
from .base import * # noqa
4+
from .base import * # noqa
5+
from .runner import ValidationRunner # noqa

django_sorcery/validators/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from django.core.exceptions import ValidationError
88
from django.utils.translation import gettext_lazy as _
99

10+
from ..db import meta
11+
1012

1113
class ValidateTogetherModelFields(object):
1214
"""
@@ -67,8 +69,6 @@ def __init__(self, session, *args, **kwargs):
6769
def __call__(self, m):
6870
clauses = [getattr(m.__class__, attr) == getattr(m, attr) for attr in self.attrs]
6971

70-
from ..db import meta
71-
7272
info = meta.model_info(m)
7373
state = info.sa_state(m)
7474
if state.persistent:

0 commit comments

Comments
 (0)