summaryrefslogtreecommitdiff
path: root/app/photos/admin.py
blob: dbcca6471dd1cf2cee0ae6a04355fe70e2761022 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from django.contrib import admin
from django.contrib.gis.admin import OSMGeoAdmin
from django.conf.urls import patterns
from django.conf.urls import url
from django.utils.translation import ungettext, ugettext_lazy as _
from photos.models import Photo, PhotoGallery, LuxImage, Gallery
from django.shortcuts import render
from django.contrib.admin import helpers
from django.http import HttpResponseRedirect


from .forms import UploadZipForm


class LuxImageAdmin(OSMGeoAdmin):
    list_display = ('pk', 'admin_thumbnail', 'pub_date',)
    list_filter = ('pub_date',)
    search_fields = ['title', 'caption']

admin.site.register(LuxImage, LuxImageAdmin)


class GalleryAdmin(OSMGeoAdmin):
    list_display = ('title', 'location', 'pub_date')
    list_filter = ('location',)

    def get_urls(self):
        urls = super(GalleryAdmin, self).get_urls()
        custom_urls = [
            url(
                r'^upload_zip/$',
                self.admin_site.admin_view(self.upload_zip),
                name='upload_zip'
            )
        ]
        return custom_urls + urls

    def upload_zip(self, request):
        context = {
            'title': _('Upload a zip archive of photos'),
            'app_label': self.model._meta.app_label,
            'opts': self.model._meta,
            'has_change_permission': self.has_change_permission(request)
        }

        # Handle form request
        if request.method == 'POST':
            form = UploadZipForm(request.POST, request.FILES)
            if form.is_valid():
                form.save(request=request)
                return HttpResponseRedirect('..')
        else:
            form = UploadZipForm()
        context['form'] = form
        context['adminform'] = helpers.AdminForm(form,
                                                 list([(None, {'fields': form.base_fields})]),
                                                 {})
        return render(request, 'admin/upload_zip.html', context)

admin.site.register(Gallery, GalleryAdmin)


class PhotoAdmin(OSMGeoAdmin):
    list_display = ('title', 'admin_thumbnail', 'flickr_id', 'pub_date',)
    list_filter = ('pub_date',)
    search_fields = ['title', 'description']
    fieldsets = (
        (None, {
            'fields': (
                ('title', 'description'),
                'pub_date',
                'tags',
                ('lat', 'lon')
            )
        }),
        ('Exif Data', {
            'fields': (
                'exif_aperture',
                'exif_exposure',
                'exif_iso',
                'exif_focal_length',
                'exif_lens',
                'exif_date',
                'exif_make',
                'exif_model'
            ),
            'classes': ('collapse')
        }),
        ('Flickr Data', {
            'fields': (
                'flickr_id',
                'flickr_owner',
                'flickr_farm',
                'flickr_server',
                'flickr_secret',
                'flickr_originalsecret'
            ),
            'classes': ('collapse')
        }),
    )

    # Options for OSM map Using custom ESRI topo map
    default_lon = -9285175
    default_lat = 4025046
    default_zoom = 6
    units = True
    scrollable = False
    map_width = 700
    map_height = 425
    map_template = 'gis/admin/osm.html'
    openlayers_url = '/static/admin/js/OpenLayers.js'


admin.site.register(Photo, PhotoAdmin)


class PhotoGalleryAdmin(OSMGeoAdmin):
    list_display = ('set_title', 'region', 'location', 'pub_date')
    list_filter = ('region', 'location')
    fieldsets = (
        (None, {
            'fields': (
                ('set_id', 'set_title', 'set_desc'),
                'set_slug',
                'primary',
                'location',
                'region',
                'photos',
                'pub_date'
            )
        }),
    )

    def get_urls(self):
        urls = super(PhotoGalleryAdmin, self).get_urls()
        custom_urls = [
            url(
                r'^upload_zip/$',
                self.admin_site.admin_view(self.upload_zip),
                name='upload_zip'
            )
        ]
        return custom_urls + urls

    def upload_zip(self, request):
        context = {
            'title': _('Upload a zip archive of photos'),
            'app_label': self.model._meta.app_label,
            'opts': self.model._meta,
            'has_change_permission': self.has_change_permission(request)
        }

        # Handle form request
        if request.method == 'POST':
            form = UploadZipForm(request.POST, request.FILES)
            if form.is_valid():
                form.save(request=request)
                return HttpResponseRedirect('..')
        else:
            form = UploadZipForm()
        context['form'] = form
        context['adminform'] = helpers.AdminForm(form,
                                                 list([(None, {'fields': form.base_fields})]),
                                                 {})
        return render(request, 'admin/upload_zip.html', context)

admin.site.register(PhotoGallery, PhotoGalleryAdmin)