Source

django-helper / django_helper / admin.py

Full commit
Kevin Veroneau 07d1dc4 
Kevin Veroneau d5fc239 


Kevin Veroneau 07d1dc4 
















Kevin Veroneau d5fc239 











































































from django.contrib import admin
from django_helper.models import TemplateTag, ContextProcessor, Middleware, Model, FieldType, Field
from django.conf.urls import patterns
from django.shortcuts import render_to_response

class TemplateTagAdmin(admin.ModelAdmin):
    list_filter = ('tag_type', 'app_name', 'takes_context', )
    list_display = ('tag_name', 'tag_type', )
    filter_horizontal = ('required_context', )

class ContextProcessorAdmin(admin.ModelAdmin):
    list_filter = ('app_name', )
    filter_horizontal = ('required_middleware', )

class MiddlewareAdmin(admin.ModelAdmin):
    list_filter = ('process_request', 'process_view', 'process_template_response', 'process_response', 'process_exception', )
    list_display = ('name', 'process_request', 'process_view', 'process_template_response', 'process_response', 'process_exception', )

admin.site.register(TemplateTag, TemplateTagAdmin)
admin.site.register(ContextProcessor, ContextProcessorAdmin)
admin.site.register(Middleware, MiddlewareAdmin)

class FieldInline(admin.TabularInline):
    model = Field
    fk_name = 'model'
    fields = ('name', 'field_type',)

class ModelAdmin(admin.ModelAdmin):
    inlines = [FieldInline]
    filter_horizontal = ('exclude', 'fields', 'filter_horizontal', 'filter_vertical', 'list_display', 'list_filter', 'raw_id_fields', )
    actions = ['export_models', 'export_admins']
    fieldsets = (
        (None, {
            'fields': ('name', 'unicode', 'unicode_str', 'absolute_url', )
        }),
        ('ModelAdmin', {
            'classes': ('collapse', ),
            'fields': ('date_hierarchy', 'exclude', 'fields', 'filter_horizontal', 'filter_vertical', 'inlines', 'list_display', 'list_filter', 'raw_id_fields', )
        }),
    )
    def get_urls(self):
        urls = super(ModelAdmin, self).get_urls()
        my_urls = patterns('',
            (r'^(?P<object_id>\d+)/export/models.py$', self.admin_site.admin_view(self.export_model)),
            (r'^(?P<object_id>\d+)/export/admin.py$', self.admin_site.admin_view(self.export_admin)),
        )
        return my_urls + urls
    def export_model(self, req, object_id):
        return render_to_response("admin/django_helper/model.txt", {'model':Model.objects.get(pk=object_id)}, mimetype='text/plain')
    def export_admin(self, req, object_id):
        return render_to_response("admin/django_helper/admin.txt", {'model':Model.objects.get(pk=object_id)}, mimetype='text/plain')
    def export_models(self, req, queryset):
        return render_to_response("admin/django_helper/models.txt", {'model_list':queryset}, mimetype='text/plain')
    export_models.short_description = "Export selected to a models.py file"
    def export_admins(self, req, queryset):
        return render_to_response("admin/django_helper/admins.txt", {'model_list':queryset}, mimetype='text/plain')
    export_admins.short_description = "Export selected to a admin.py file"

class FieldTypeAdmin(admin.ModelAdmin):
    list_filter = ('backend_storage',)
    list_display = ('__unicode__', 'backend_storage', )

class FieldAdmin(admin.ModelAdmin):
    list_filter = ('model', 'field_type', )
    list_display = ('__unicode__', 'model', 'field_type', )
    fieldsets = (
        (None, {
            'fields': ('name', 'model', 'field_type')
        }),
        ('Field options', {
            'classes': ('collapse', ),
            'fields': ('null', 'blank', 'db_column', 'db_index', 'db_tablespace', 'default', 'editable', 'help_text', 'primary_key', 'unique', 'unique_for_date', 'unique_for_month', 'unique_for_year', 'verbose_name',)
        }),
        ('String options', {
            'classes': ('collapse', ),
            'fields': ('max_length', )
        }),
        ('DateTime options', {
            'classes': ('collapse', ),
            'fields': ('auto_now', 'auto_now_add', )
        }),
        ('Integer options', {
            'classes': ('collapse', ),
            'fields': ('max_digits', 'decimal_places', )
        }),
        ('Related field options', {
            'classes': ('collapse', ),
            'fields': ('othermodel', 'limit_choices_to', 'related_name', 'to_field', 'on_delete', 'through', 'db_table', )
        }),
        ('Other options', {
            'classes': ('collapse', ),
            'fields': ('upload_to', )
        }),
    )

admin.site.register(Model, ModelAdmin)
admin.site.register(FieldType, FieldTypeAdmin)
admin.site.register(Field, FieldAdmin)