from django.contrib import admin
from django.db.models import F
from cityhallmonitor.models import \
    Action, Person, BodyType, Body, \
    MatterAttachment, MatterStatus, MatterType, Matter, \
    MatterSponsor, VoteType, Event, EventItem, Subscription
    

class ActionAdmin(admin.ModelAdmin):
    list_display = ('name', 'active_flag', 'last_modified',)
    list_filter = ('active_flag',)
admin.site.register(Action, ActionAdmin)


class PersonAdmin(admin.ModelAdmin):
    list_display = ('last_name', 'first_name', 'active_flag', 'last_modified')
    list_filter = ('active_flag',)
admin.site.register(Person, PersonAdmin)


class BodyTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'last_modified')    
admin.site.register(BodyType, BodyTypeAdmin)

class BodyAdmin(admin.ModelAdmin):
    list_display = ('name', 'body_type', 'active_flag', 'last_modified')
    list_filter = ('active_flag',)    
admin.site.register(Body, BodyAdmin)


class MatterStatusAdmin(admin.ModelAdmin):
    list_display = ('name', 'active_flag', 'last_modified',)
    list_filter = ('active_flag',)
admin.site.register(MatterStatus, MatterStatusAdmin)

class MatterTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'active_flag', 'last_modified',)
    list_filter = ('active_flag',)
admin.site.register(MatterType, MatterTypeAdmin)

class MatterAdmin(admin.ModelAdmin):
    list_display = ('display_val', 'matter_status', 'matter_type', 'intro_date', 'last_modified')
    list_filter = ('matter_status', 'matter_type')
    search_fields = ['name', 'title']
    
    # Hide unused fields in admin form
    exclude = (
        'date1', 'date2',
        'ex_text1', 'ex_text2', 'ex_text3', 'ex_text4', 'ex_text5',
        'ex_text6', 'ex_text7', 'ex_text8', 'ex_text9', 'ex_text10',
        'ex_date1', 'ex_date2', 'ex_date3', 'ex_date4', 'ex_date5',
        'ex_date6', 'ex_date7', 'ex_date8', 'ex_date9', 'ex_date10'
    )
    
    def get_queryset(self, request):
        """
        Annotate queryset with value of __str__, so we can sort on it.
        """
        qs = super(MatterAdmin, self).get_queryset(request)
        return qs.annotate(display_str=(F('title') or F('name') or F('file')))
    
    def display_val(self, obj):
        return obj.display_str
    display_val.admin_order_field = 'display_str'
    display_val.short_description = 'Matter'

admin.site.register(Matter, MatterAdmin)

class MatterAttachmentAdmin(admin.ModelAdmin):
    list_display = ('name', 'hyperlink', 'link_obtained_at', 'matter')
    search_fields = ('matter__id',)
admin.site.register(MatterAttachment, MatterAttachmentAdmin)

class MatterSponsorAdmin(admin.ModelAdmin):
    list_display = ('person', 'matter')
    list_filter = ('person',)

admin.site.register(MatterSponsor, MatterSponsorAdmin)

admin.site.register(VoteType)

class EventAdmin(admin.ModelAdmin):
    list_display = ('__str__', 'date', 'time', 'last_modified',)
    search_fields = ['body__name']
    ordering = ('-date', '-time')
admin.site.register(Event, EventAdmin)

admin.site.register(EventItem)

class SubscriptionAdmin(admin.ModelAdmin):
    list_display = ('email', 'query', 'last_check', 'active')
    search_fields = ('email', 'query')    
admin.site.register(Subscription, SubscriptionAdmin)


@@ -249,16 +249,32 @@
 cription
+, %5C%0A    Document
 %0A    %0A%0Ac
@@ -278,22 +278,27 @@
 %0A%0Aclass 
-Action
+DirtyFields
 Admin(ad
@@ -306,32 +306,123 @@
 in.ModelAdmin):%0A
+    readonly_fields = ('created_at', 'updated_at',)%0A%0A%0Aclass ActionAdmin(DirtyFieldsAdmin):%0A
     list_display
@@ -558,35 +558,35 @@
 PersonAdmin(
-admin.Model
+DirtyFields
 Admin):%0A    
@@ -754,35 +754,35 @@
 dyTypeAdmin(
-admin.Model
+DirtyFields
 Admin):%0A    
@@ -868,24 +868,25 @@
 TypeAdmin)%0A%0A
+%0A
 class BodyAd
@@ -885,35 +885,35 @@
 s BodyAdmin(
-admin.Model
+DirtyFields
 Admin):%0A    
@@ -1079,35 +1079,35 @@
 StatusAdmin(
-admin.Model
+DirtyFields
 Admin):%0A    
@@ -1244,32 +1244,33 @@
 erStatusAdmin)%0A%0A
+%0A
 class MatterType
@@ -1271,35 +1271,35 @@
 erTypeAdmin(
-admin.Model
+DirtyFields
 Admin):%0A    
@@ -1432,32 +1432,33 @@
 tterTypeAdmin)%0A%0A
+%0A
 class MatterAdmi
@@ -1455,35 +1455,35 @@
 MatterAdmin(
-admin.Model
+DirtyFields
 Admin):%0A    
@@ -2504,32 +2504,33 @@
 , MatterAdmin)%0A%0A
+%0A
 class MatterAtta
@@ -2537,35 +2537,35 @@
 chmentAdmin(
-admin.Model
+DirtyFields
 Admin):%0A    
@@ -2729,16 +2729,17 @@
 Admin)%0A%0A
+%0A
 class Ma
@@ -2751,35 +2751,35 @@
 ponsorAdmin(
-admin.Model
+DirtyFields
 Admin):%0A    
@@ -2932,16 +2932,17 @@
 eType)%0A%0A
+%0A
 class Ev
@@ -2946,35 +2946,35 @@
  EventAdmin(
-admin.Model
+DirtyFields
 Admin):%0A    
@@ -3135,32 +3135,33 @@
 t, EventAdmin)%0A%0A
+%0A
 admin.site.regis
@@ -3176,16 +3176,17 @@
 tItem)%0A%0A
+%0A
 class Su
@@ -3367,20 +3367,334 @@
 SubscriptionAdmin)%0A%0A
+%0Aclass DocumentAdmin(DirtyFieldsAdmin):%0A    list_display = ('sort_date', '__str__', 'is_routine',)%0A    list_display_links = ('__str__',)%0A    list_filter = ('is_routine',)%0A    %0A   %0A    # Hide unused fields in admin form%0A    exclude = (%0A        'matter_attachment',%0A    )%0Aadmin.site.register(Document, DocumentAdmin)

Add base admin class for uneditable timestamp fields; add Document admin