view user_photos/admin.py @ 821:71db8076dc3d

Bandmap WIP: geocoding integrated with add form. Add form works. Before submitting the form, client side JS makes a geocode request to Google and populates hidden lat/lon fields with the result. Successfully created a model instance on the server side. Still need to update admin dashboard, admin approval, and give out badges for adding bands to the map. Once that is done, then work on displaying the map with filtering.
author Brian Neal <bgneal@gmail.com>
date Tue, 23 Sep 2014 20:40:31 -0500
parents e0e79451339d
children
line wrap: on
line source
"""Admin definitions for the user_photos application."""
from django.contrib import admin

from user_photos.models import Photo
from user_photos.s3 import delete_photos

IMG_TAG = """<a href="{url}"><img src="{thumb_url}" alt="thumbnail" /></a>"""

class PhotoAdmin(admin.ModelAdmin):
    date_hierarchy = 'upload_date'
    ordering = ['-upload_date']
    raw_id_fields = ['user']
    search_fields = ['user__username', 'user__email']
    list_display = ['__unicode__', 'thumbnail']
    actions = ['custom_delete']

    def thumbnail(self, obj):
        return IMG_TAG.format(url=obj.url, thumb_url=obj.thumb_url)
    thumbnail.allow_tags = True

    def custom_delete(self, request, qs):
        """Custom delete in order to remove images from the S3 bucket in
        addition to removing from the database.

        """
        delete_photos(qs)
        count = len(qs)
        qs.delete()

        if count == 1:
            msg_bit = "1 photo was"
        else:
            msg_bit = "{} photos were".format(count)

        self.message_user(request, "{} successfully deleted.".format(msg_bit))
    custom_delete.short_description = "Delete selected photos from DB & S3"

    def get_actions(self, request):
        """Remove the default delete selected action because we have installed
        our own.

        """
        actions = super(PhotoAdmin, self).get_actions(request)
        del actions['delete_selected']
        return actions


admin.site.register(Photo, PhotoAdmin)