Skip to content

Instantly share code, notes, and snippets.

@liondancer

liondancer/models.py

Created Jan 27, 2015
Embed
What would you like to do?
from django.db import models
from django.contrib import admin
from PIL import Image
from Boothie.settings import MEDIA_ROOT, MEDIA_URL
from django.conf import settings
import os.path
from django.utils.html import format_html
from django.core.files.storage import FileSystemStorage
class Album(models.Model):
title = models.CharField(max_length=50)
# thumbnail = models.ImageField()
def __str__(self):
return self.title
def images(self):
lst = [x.photo.title for x in self.photo_set.all()]
return lst
class AlbumAdmin(admin.ModelAdmin):
# Set search_fields to enable a search box on the admin change list page.
# This should be set to a list of field names that will be searched whenever
# somebody submits a search query in that text box.
search_fields = ["title"]
# Set list_display to control which fields are displayed on the change list page of the admin.
list_display = ["title"]
class Photo(models.Model):
title = models.CharField(max_length=50, blank=True)
album = models.ForeignKey(Album)
photo = models.FileField(upload_to="album")
width = models.IntegerField(blank=True, null=True)
height = models.IntegerField(blank=True, null=True)
upload = models.DateTimeField(auto_now_add=True)
# thumbnail = models.ImageField()
def save(self, *args, **kwargs):
# Save image dimensions
# Save image
super(Photo, self).save(*args, **kwargs)
# get image
# pic = Image.open(os.path.join(MEDIA_ROOT, self.photo))
pic = Image.open(FileSystemStorage(location=os.path.join(MEDIA_ROOT, self.photo)))
# set Photo width and height
self.width, self.height = pic.size
# save object
super(Photo, self).save(*args, **kwargs)
def __str__(self):
return self.title
def albums_(self):
# https://docs.djangoproject.com/en/1.7/ref/models/querysets/#values-list
lst = [x[1] for x in self.album.value_list()]
return ', '.join(lst)
def size(self):
# Photo size
return "%s x %s" % (self.width, self.height)
def thumbnail(self):
# return """<a href="/media/%s"><img border="0" alt="" src="/media/%s" height="40" /></a>""" % (self.image.name, self.image.name)
thumbnail_html = """<a href=\"{{ MEDIA_URL }}%s\"><img border=\"0\" alt=\"\" src=\"{{ MEDIA_URL }}%s\" height=\"40\" /></a>""" % (self.photo.name, self.photo.name)
# thumbnail_html = "<a href=\"{0}{1}\"><img border=\"0\" alt=\"\" src=\"{2}{3}\" height=\"40\" /></a>".format(settings.MEDIA_URL, self.photo.name, settings.MEDIA_URL, self.photo.name)
return thumbnail_html
thumbnail.allow_tags = True
class PhotoAdmin(admin.ModelAdmin):
search_fields = ["title", "photo"]
list_display = ["photo", "thumbnail", "title", "album", "size"]
list_filter = ["album"]
STATIC_ROOT = os.path.join(BASE_DIR, "../collected_static/Boothie")
MEDIA_ROOT = os.path.join(BASE_DIR, "../media_root/Boothie/pics")
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment