summaryrefslogtreecommitdiff
path: root/app/sightings/admin.py
blob: f6ec2af27401ce7cc03b05ec376fee2449c98106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import copy
from django.contrib import admin
from django.contrib.gis.admin import OSMGeoAdmin
from .models import APClass, AP, Sighting, FieldNote

from photos.forms import GalleryForm
from utils.util import get_latlon
from utils.widgets import CustomSelectMultiple, LGEntryForm
from django.contrib.admin.options import FORMFIELD_FOR_DBFIELD_DEFAULTS

class GalleryFormPlus(GalleryForm):
    def __init__(self, *args, **kwargs):
        super(GalleryFormPlus, self).__init__(*args, **kwargs)
        self.base_fields['seen_by'].widget = CustomSelectMultiple()
    
    class Meta:
        model = Sighting
        fields = '__all__' 


@admin.register(APClass)
class APClassAdmin(admin.ModelAdmin):
    list_display = ('common_name', 'scientific_name', 'kind')
    list_filter = ('kind',)


class SightingInline(OSMGeoAdmin, admin.StackedInline):
    """
    This is very fragile and probably a bad idea since I copied this
    code straight from Django and it may change, but unless GeoDjango 
    adds a Mixin, this will have to do.
    """
    model = Sighting
    extra = 1
    lat, lon = get_latlon()
    default_lon = lon
    default_lat = lat
    default_zoom = 13
    units = True
    scrollable = False
    map_width = 700
    map_height = 425
    map_template = 'gis/admin/osm.html'
    openlayers_url = '/static/admin/js/OpenLayers.js'

    def __init__(self, parent_model, admin_site):
        self.admin_site = admin_site
        self.parent_model = parent_model
        self.opts = self.model._meta
        self.has_registered_model = admin_site.is_registered(self.model)
        overrides = copy.deepcopy(FORMFIELD_FOR_DBFIELD_DEFAULTS)
        for k, v in self.formfield_overrides.items():
            overrides.setdefault(k, {}).update(v)
        self.formfield_overrides = overrides
        if self.verbose_name is None:
            self.verbose_name = self.model._meta.verbose_name
        if self.verbose_name_plural is None:
            self.verbose_name_plural = self.model._meta.verbose_name_plural


@admin.register(AP)
class APAdmin(admin.ModelAdmin):
    form = LGEntryForm
    inlines = [
        SightingInline,
    ]
    list_display = ('common_name', 'have_seen', 'scientific_name', 'kind', 'code', 'apclass')
    list_filter = ('apclass__kind', 'apclass')
    search_fields = ['common_name', 'scientific_name']

    def have_seen(self, obj):
        return obj.seen
    have_seen.admin_order_field = 'seen'
    have_seen.boolean = True

    class Media:
        js = ('image-loader.js', 'next-prev-links.js')


@admin.register(Sighting)
class SightingAdmin(OSMGeoAdmin):
    list_filter = (('location', admin.RelatedOnlyFieldListFilter),)
    list_display = ('ap', 'location')
    # options for OSM map Using custom ESRI topo map
    lat, lon = get_latlon()
    default_lon = lon
    default_lat = lat
    default_zoom = 13
    units = True
    scrollable = False
    map_width = 700
    map_height = 425
    map_template = 'gis/admin/osm.html'
    openlayers_url = '/static/admin/js/OpenLayers.js'
    
    fieldsets = (
        ('', {
            'fields': (
                'ap',
                'point',
                'pub_date',
            ),
            'classes': (
                'show',
                'extrapretty',
            )
        }
        ),
    )

    class Media:
        js = ('next-prev-links.js', 'choices.min.js', 'autocomplete.js')
        css = {
            "all": ("choices.mapfix.css", "choices.min.css",)
        }


@admin.register(FieldNote)
class FieldNoteAdmin(admin.ModelAdmin):
    form = LGEntryForm

    def get_form(self, request, obj=None, **kwargs):
        form = super(FieldNoteAdmin, self).get_form(request, obj, **kwargs)
        form.base_fields['sighting'].label_from_instance = lambda obj: "{} - {} - {}".format(obj.ap_common_name, obj.pub_date.strftime("%Y-%b-%d"), obj.location_name)
        return form

    list_display = ('pk', 'ap_common_name', 'sighting_pub_date', 'sighting_location')
    fieldsets = (
        ('', {
            'fields': (
                'sighting',
                'body_markdown',
            ),
            'classes': (
                'show',
                'extrapretty',
                'wide'
            )
        }
        ),
    )

    class Media:
        js = ('image-loader.js', 'next-prev-links.js')
        css = {
            "all": ("my_styles.css",)
        }