def __new__(mcs, name, bases, attrs): new_class = super(URLNodeMetaClass, mcs).__new__(mcs, name, bases, attrs) # Update the table name. # Inspired by from Django-CMS, (c) , BSD licensed. if name not in ['UrlNode', 'Page', 'HtmlPage']: meta = new_class._meta # Make sure only values are updated if there is no manual edit, or a proxy model for UrlNode (e.g. HtmlPage) if meta.db_table.startswith( meta.app_label + '_') and meta.db_table != 'fluent_pages_urlnode': model_name = meta.db_table[len(meta.app_label) + 1:] meta.db_table = truncate_name( "pagetype_{0}_{1}".format(meta.app_label, model_name), connection.ops.max_name_length()) if hasattr(meta, 'original_attrs'): # Make sure that the Django 1.7 migrations also pick up this change! # Changing the db_table beforehand might be cleaner, # but also requires duplicating the whole algorithm that Django uses. meta.original_attrs['db_table'] = meta.db_table return new_class
def __new__(mcs, name, bases, attrs): new_class = super(ContentItemMetaClass, mcs).__new__(mcs, name, bases, attrs) db_table = new_class._meta.db_table app_label = new_class._meta.app_label if name != 'ContentItem': if db_table.startswith(app_label + '_'): model_name = db_table[len(app_label) + 1:] new_class._meta.db_table = truncate_name("contentitem_%s_%s" % (app_label, model_name), connection.ops.max_name_length()) if hasattr(new_class._meta, 'original_attrs'): # Make sure that the Django 1.7 migrations also pick up this change! # Changing the db_table beforehand might be cleaner, # but also requires duplicating the whole algorithm that Django uses. new_class._meta.original_attrs['db_table'] = new_class._meta.db_table # Enforce good manners. The name is often not visible, except for the delete page. if not new_class._meta.abstract: if not hasattr(new_class, '__str__') or new_class.__str__ == ContentItem.__str__: if PY3: raise TypeError("The {0} class should implement a __str__() function.".format(name)) else: # The first check is for python_2_unicode_compatible tricks, also check for __unicode__ only. if not hasattr(new_class, '__unicode__') or new_class.__unicode__ == ContentItem.__unicode__: raise TypeError("The {0} class should implement a __unicode__() or __str__() function.".format(name)) return new_class
def __new__(mcs, name, bases, attrs): new_class = super(URLNodeMetaClass, mcs).__new__(mcs, name, bases, attrs) # Update the table name. # Inspired by from Django-CMS, (c) , BSD licensed. if name not in ['UrlNode', 'Page', 'HtmlPage']: meta = new_class._meta # Make sure only values are updated if there is no manual edit, or a proxy model for UrlNode (e.g. HtmlPage) if meta.db_table.startswith(meta.app_label + '_') and meta.db_table != 'fluent_pages_urlnode': model_name = meta.db_table[len(meta.app_label) + 1:] meta.db_table = truncate_name("pagetype_{0}_{1}".format(meta.app_label, model_name), connection.ops.max_name_length()) if hasattr(meta, 'original_attrs'): # Make sure that the Django 1.7 migrations also pick up this change! # Changing the db_table beforehand might be cleaner, # but also requires duplicating the whole algorithm that Django uses. meta.original_attrs['db_table'] = meta.db_table return new_class
def get_db_table(module, model_name, meta=None, prefix=None): """ Generate a db_table for a model that's being constructed. """ from django.db import connection # This function is used in the metaclasses of django-fluent-pages # and django-fluent-contents to generate the db_table with a prefix. # This duplicates the logic of Django's Options class # to make sure the "db_table" is set in advance while creating the class. # Setting the db_table afterwards breaks Django 1.7 migrations, # which look at Options.original_attrs['db_table'] instead. app_label = getattr(meta, 'app_label', None) if not app_label: # This part differs between Django versions. # As of Django 1.7, the app framework is used. app_label = get_app_label(module) model_name = model_name.lower() db_table = "{0}{1}_{2}".format(prefix or '', app_label, model_name) return truncate_name(db_table, connection.ops.max_name_length())