Source

django-helper / django_helper / models.py

Full commit
from django.db import models
from django.conf import settings

APP_NAMES = []
for app in settings.INSTALLED_APPS:
    APP_NAMES.append((app, app))

class Middleware(models.Model):
    name = models.CharField(max_length=40)
    app_name = models.CharField(max_length=60, choices=APP_NAMES)
    process_request = models.BooleanField()
    process_view = models.BooleanField()
    process_template_response = models.BooleanField()
    process_response = models.BooleanField()
    process_exception = models.BooleanField()
    description = models.TextField()
    added_on = models.DateField(auto_now_add=True)
    def __unicode__(self):
        return u"%s" % self.name

class ContextProcessor(models.Model):
    name = models.SlugField()
    app_name = models.CharField(max_length=60, choices=APP_NAMES)
    required_middleware = models.ManyToManyField(Middleware, blank=True)
    description = models.TextField()
    added_on = models.DateField(auto_now_add=True)
    def __unicode__(self):
        return u"%s" % self.name

class TemplateTag(models.Model):
    TYPE_LIST = (
        ('Tag', (
            (1, 'Custom'),
            (2, 'simple_tag'),
            (3, 'inclusion_tag'),
            (4, 'assignment_tag'),
            (5, 'Block'),
                 )
         ),
        ('Filter', (
            (51, 'Integer'),
            (52, 'String'),
            (53, 'Object'),
            (54, 'Other'),
                    )
         )
    )
    tag_name = models.SlugField()  # Use SlugField as it fits most of the validation checks for how a tag is formatted.
    tag_type = models.PositiveSmallIntegerField(choices=TYPE_LIST)
    app_name = models.CharField(max_length=60, choices=APP_NAMES)
    parameters = models.CharField(max_length=60, blank=True)
    takes_context = models.BooleanField()
    required_context = models.ManyToManyField(ContextProcessor, blank=True)
    template = models.CharField(max_length=80, blank=True, help_text='Template used by the tag/filter, if any.')
    examples = models.TextField()
    description = models.TextField()
    added_on = models.DateField(auto_now_add=True)
    def __unicode__(self):
        return u"%s" % self.tag_name

# Model designer components

class Model(models.Model):
    name = models.CharField(max_length=40)
    unicode = models.ForeignKey('Field', blank=True, null=True, related_name='unicode_set')
    unicode_str = models.CharField(max_length=80, blank=True)
    absolute_url = models.CharField(max_length=80, blank=True)
    date_hierarchy = models.ForeignKey('Field', blank=True, null=True, related_name='hierarchy_set')
    exclude = models.ManyToManyField('Field', blank=True, related_name='exclude_set')
    fields = models.ManyToManyField('Field', blank=True, related_name='field_set')
    filter_horizontal = models.ManyToManyField('Field', blank=True, related_name='hfilter_set')
    filter_vertical = models.ManyToManyField('Field', blank=True, related_name='vfilter_set')
    inlines = models.ForeignKey('self', blank=True, null=True)
    list_display = models.ManyToManyField('Field', blank=True, related_name='display_set')
    list_filter = models.ManyToManyField('Field', blank=True, related_name='filter_set')
    raw_id_fields = models.ManyToManyField('Field', blank=True, related_name='rawid_set')
    def __unicode__(self):
        return u"%s" % self.name

class FieldType(models.Model):
    STORAGE_TYPES = (
        (0, 'Custom'),
        (1, 'String'),
        (2, 'Integer'),
        (3, 'Boolean'),
        (4, 'DateTime'),
    )
    name = models.CharField(max_length=40, help_text='Case-sensitive, used during model generation.')
    backend_storage = models.PositiveSmallIntegerField(choices=STORAGE_TYPES, help_text='Used to determine how the field parameters are generated.')
    def __unicode__(self):
        return u"%s" % self.name

class Field(models.Model):
    DELETE_CHOICES = (
        (0, 'CASCADE'),
        (1, 'PROTECT'),
        (2, 'SET_NULL'),
        (3, 'SET_DEFAULT'),
        (5, 'DO_NOTHING'),
    )
    name = models.CharField(max_length=40)
    model = models.ForeignKey(Model)
    field_type = models.ForeignKey(FieldType)
    null = models.BooleanField()
    blank = models.BooleanField()
    db_column = models.CharField(max_length=40, blank=True, null=True)
    db_index = models.BooleanField()
    db_tablespace = models.CharField(max_length=40, blank=True, null=True)
    default = models.CharField(max_length=80, blank=True, null=True)
    editable = models.BooleanField()
    help_text = models.CharField(max_length=80, blank=True, null=True)
    primary_key = models.BooleanField()
    unique = models.BooleanField()
    unique_for_date = models.ForeignKey('self', blank=True, null=True, related_name='date_set')
    unique_for_month = models.ForeignKey('self', blank=True, null=True, related_name='month_set')
    unique_for_year = models.ForeignKey('self', blank=True, null=True, related_name='year_set')
    verbose_name = models.CharField(max_length=40, blank=True, null=True)
    max_length = models.IntegerField(blank=True, null=True)
    auto_now = models.BooleanField()
    auto_now_add = models.BooleanField()
    max_digits = models.IntegerField(blank=True, null=True)
    decimal_places = models.IntegerField(blank=True, null=True)
    upload_to = models.CharField(max_length=80, blank=True, null=True)
    othermodel = models.ForeignKey(Model, blank=True, null=True, related_name='othermodel_set')
    limit_choices_to = models.CharField(max_length=40, blank=True, null=True)
    related_name = models.CharField(max_length=40, blank=True, null=True)
    to_field = models.ForeignKey('self', blank=True, null=True, related_name='foreign_set')
    on_delete = models.PositiveSmallIntegerField(choices=DELETE_CHOICES, blank=True, null=True)
    through = models.ForeignKey(Model, blank=True, null=True, related_name='through_set')
    db_table = models.CharField(max_length=40, blank=True, null=True)
    def __unicode__(self):
        return u"%s" % self.name
    def get_field_options(self):
        ft = self.field_type.backend_storage
        options = []
        if ft == 1:
            max_length = self.max_length if self.max_length else 75
            options.append("max_length=%d" % max_length)
        exclude = ['id', 'name', 'model', 'field_type']
        for f in self._meta.fields:
            if f.name in exclude:
                continue
            if getattr(self, f.attname):
                options.append("%s=%s" % (f.name, getattr(self, f.attname)))
        return ', '.join(options)