django1/django/db/migrations/state.py

115 lines
3.8 KiB
Python
Raw Normal View History

from django.db import models
from django.db.models.loading import BaseAppCache
from django.db.models.options import DEFAULT_NAMES
from django.utils.module_loading import import_by_path
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
def clone(self):
"Returns an exact copy of this ProjectState"
return ProjectState(
models = dict((k, v.copy()) for k, v in self.models.items())
)
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():
model.render(self.app_cache)
return self.app_cache
@classmethod
def from_app_cache(cls, app_cache):
"Takes in an AppCache and returns a ProjectState matching it"
models = {}
for model in app_cache.get_models():
model_state = ModelState.from_model(model)
models[(model_state.app_label, model_state.name.lower())] = model_state
return cls(models)
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.
"""
def __init__(self, app_label, name, fields=None, options=None, bases=None):
self.app_label = app_label
self.name = name
self.fields = fields or []
self.options = options or {}
self.bases = bases or (models.Model, )
@classmethod
def from_model(cls, model):
"""
Feed me a model, get a ModelState representing it out.
"""
# Deconstruct the fields
fields = []
for field in model._meta.local_fields:
name, path, args, kwargs = field.deconstruct()
field_class = import_by_path(path)
fields.append((name, field_class(*args, **kwargs)))
# Extract the options
options = {}
for name in DEFAULT_NAMES:
# Ignore some special options
if name in ["app_cache", "app_label"]:
continue
if name in model._meta.original_attrs:
options[name] = model._meta.original_attrs[name]
# Make our record
return cls(
model._meta.app_label,
model._meta.object_name,
fields,
options,
model.__bases__,
)
def clone(self):
"Returns an exact copy of this ModelState"
return self.__class__(
app_label = self.app_label,
name = self.name,
fields = self.fields,
options = self.options,
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)
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),
body,
)