2013-05-11 00:07:13 +08:00
|
|
|
from django.db import models
|
|
|
|
from django.db.models.loading import BaseAppCache
|
2013-05-19 18:35:17 +08:00
|
|
|
from django.db.models.options import DEFAULT_NAMES
|
2013-05-18 19:49:56 +08:00
|
|
|
from django.utils.module_loading import import_by_path
|
2013-05-11 00:07:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ProjectState(object):
|
|
|
|
"""
|
|
|
|
Represents the entire project's overall state.
|
|
|
|
This is the item that is passed around - we do it here rather than at the
|
|
|
|
app level so that cross-app FKs/etc. resolve properly.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, models=None):
|
|
|
|
self.models = models or {}
|
|
|
|
self.app_cache = None
|
|
|
|
|
2013-05-19 00:30:34 +08:00
|
|
|
def add_model_state(self, model_state):
|
|
|
|
self.models[(model_state.app_label, model_state.name.lower())] = model_state
|
|
|
|
|
2013-05-11 00:07:13 +08:00
|
|
|
def clone(self):
|
|
|
|
"Returns an exact copy of this ProjectState"
|
2013-05-18 19:49:56 +08:00
|
|
|
return ProjectState(
|
2013-05-30 00:47:10 +08:00
|
|
|
models = dict((k, v.clone()) for k, v in self.models.items())
|
2013-05-11 00:07:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def render(self):
|
|
|
|
"Turns the project state into actual models in a new AppCache"
|
|
|
|
if self.app_cache is None:
|
|
|
|
self.app_cache = BaseAppCache()
|
2013-05-19 00:30:34 +08:00
|
|
|
for model in self.models.values():
|
2013-05-11 00:07:13 +08:00
|
|
|
model.render(self.app_cache)
|
|
|
|
return self.app_cache
|
|
|
|
|
2013-05-18 17:48:46 +08:00
|
|
|
@classmethod
|
|
|
|
def from_app_cache(cls, app_cache):
|
|
|
|
"Takes in an AppCache and returns a ProjectState matching it"
|
2013-05-18 19:49:56 +08:00
|
|
|
models = {}
|
2013-05-18 17:48:46 +08:00
|
|
|
for model in app_cache.get_models():
|
2013-05-18 19:49:56 +08:00
|
|
|
model_state = ModelState.from_model(model)
|
|
|
|
models[(model_state.app_label, model_state.name.lower())] = model_state
|
|
|
|
return cls(models)
|
2013-05-18 17:48:46 +08:00
|
|
|
|
2013-05-11 00:07:13 +08:00
|
|
|
|
|
|
|
class ModelState(object):
|
|
|
|
"""
|
|
|
|
Represents a Django Model. We don't use the actual Model class
|
|
|
|
as it's not designed to have its options changed - instead, we
|
|
|
|
mutate this one and then render it into a Model as required.
|
2013-05-31 01:21:32 +08:00
|
|
|
|
|
|
|
Note that while you are allowed to mutate .fields, you are not allowed
|
|
|
|
to mutate the Field instances inside there themselves - you must instead
|
|
|
|
assign new ones, as these are not detached during a clone.
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def __init__(self, app_label, name, fields, options=None, bases=None):
|
2013-05-11 00:07:13 +08:00
|
|
|
self.app_label = app_label
|
|
|
|
self.name = name
|
2013-05-30 00:47:10 +08:00
|
|
|
self.fields = fields
|
2013-05-11 00:07:13 +08:00
|
|
|
self.options = options or {}
|
2013-05-18 19:49:56 +08:00
|
|
|
self.bases = bases or (models.Model, )
|
2013-05-30 00:47:10 +08:00
|
|
|
# Sanity-check that fields is NOT a dict. It must be ordered.
|
|
|
|
if isinstance(self.fields, dict):
|
|
|
|
raise ValueError("ModelState.fields cannot be a dict - it must be a list of 2-tuples.")
|
2013-05-18 19:49:56 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_model(cls, model):
|
|
|
|
"""
|
|
|
|
Feed me a model, get a ModelState representing it out.
|
|
|
|
"""
|
|
|
|
# Deconstruct the fields
|
|
|
|
fields = []
|
2013-09-01 05:18:44 +08:00
|
|
|
for field in model._meta.local_fields:
|
2013-05-18 19:49:56 +08:00
|
|
|
name, path, args, kwargs = field.deconstruct()
|
|
|
|
field_class = import_by_path(path)
|
|
|
|
fields.append((name, field_class(*args, **kwargs)))
|
2013-05-19 18:35:17 +08:00
|
|
|
# Extract the options
|
|
|
|
options = {}
|
|
|
|
for name in DEFAULT_NAMES:
|
|
|
|
# Ignore some special options
|
|
|
|
if name in ["app_cache", "app_label"]:
|
|
|
|
continue
|
2013-07-02 18:19:02 +08:00
|
|
|
elif name in model._meta.original_attrs:
|
|
|
|
if name == "unique_together":
|
|
|
|
options[name] = set(model._meta.original_attrs["unique_together"])
|
|
|
|
else:
|
|
|
|
options[name] = model._meta.original_attrs[name]
|
2013-05-18 19:49:56 +08:00
|
|
|
# Make our record
|
2013-06-23 00:15:51 +08:00
|
|
|
bases = tuple(model for model in model.__bases__ if (not hasattr(model, "_meta") or not model._meta.abstract))
|
|
|
|
if not bases:
|
|
|
|
bases = (models.Model, )
|
2013-05-18 19:49:56 +08:00
|
|
|
return cls(
|
|
|
|
model._meta.app_label,
|
|
|
|
model._meta.object_name,
|
|
|
|
fields,
|
2013-05-19 18:35:17 +08:00
|
|
|
options,
|
2013-06-23 00:15:51 +08:00
|
|
|
bases,
|
2013-05-18 19:49:56 +08:00
|
|
|
)
|
2013-05-11 00:07:13 +08:00
|
|
|
|
|
|
|
def clone(self):
|
|
|
|
"Returns an exact copy of this ModelState"
|
2013-06-20 22:12:59 +08:00
|
|
|
# We deep-clone the fields using deconstruction
|
|
|
|
fields = []
|
|
|
|
for name, field in self.fields:
|
|
|
|
_, path, args, kwargs = field.deconstruct()
|
|
|
|
field_class = import_by_path(path)
|
|
|
|
fields.append((name, field_class(*args, **kwargs)))
|
|
|
|
# Now make a copy
|
2013-05-11 00:07:13 +08:00
|
|
|
return self.__class__(
|
|
|
|
app_label = self.app_label,
|
|
|
|
name = self.name,
|
2013-06-20 22:12:59 +08:00
|
|
|
fields = fields,
|
2013-05-31 01:21:32 +08:00
|
|
|
options = dict(self.options),
|
2013-05-11 00:07:13 +08:00
|
|
|
bases = self.bases,
|
|
|
|
)
|
|
|
|
|
|
|
|
def render(self, app_cache):
|
|
|
|
"Creates a Model object from our current state into the given app_cache"
|
|
|
|
# First, make a Meta object
|
|
|
|
meta_contents = {'app_label': self.app_label, "app_cache": app_cache}
|
|
|
|
meta_contents.update(self.options)
|
2013-07-02 18:19:02 +08:00
|
|
|
if "unique_together" in meta_contents:
|
|
|
|
meta_contents["unique_together"] = list(meta_contents["unique_together"])
|
2013-05-11 00:07:13 +08:00
|
|
|
meta = type("Meta", tuple(), meta_contents)
|
|
|
|
# Then, work out our bases
|
|
|
|
# TODO: Use the actual bases
|
|
|
|
# Turn fields into a dict for the body, add other bits
|
|
|
|
body = dict(self.fields)
|
|
|
|
body['Meta'] = meta
|
|
|
|
body['__module__'] = "__fake__"
|
|
|
|
# Then, make a Model object
|
|
|
|
return type(
|
|
|
|
self.name,
|
2013-05-19 00:30:34 +08:00
|
|
|
tuple(self.bases),
|
2013-05-11 00:07:13 +08:00
|
|
|
body,
|
|
|
|
)
|
2013-06-20 22:19:30 +08:00
|
|
|
|
|
|
|
def get_field_by_name(self, name):
|
|
|
|
for fname, field in self.fields:
|
|
|
|
if fname == name:
|
|
|
|
return field
|
|
|
|
raise ValueError("No field called %s on model %s" % (name, self.name))
|