Added media viewing and editing; gets media from config and creates a /thumbnail/ folder there

svn: r19869
This commit is contained in:
Doug Blank 2012-06-18 22:32:22 +00:00
parent 9841fb79ef
commit daa01defab
4 changed files with 46 additions and 2 deletions

View File

@ -31,6 +31,7 @@
<tr>
<td class="ColumnAttribute">{{mediaform.desc.label}}:</td>
<td class="ColumnValue" id="data" colspan="3">{% render mediaform.desc user action %}</td>
<td rowspan="5">{% media_link media.handle user action %}</td>
</tr>
<tr>
<td class="ColumnAttribute">{{mediaform.gramps_id.label}}:</td>

View File

@ -23,11 +23,17 @@
# forms.py forms for Django web project
# Django Modules:
from django import forms
from webapp.grampsdb.models import *
from django.forms.models import inlineformset_factory
from django.forms.models import BaseModelFormSet
from django.forms.widgets import TextInput
# Gramps Modules:
from webapp.grampsdb.models import *
import gen.mime
# Python Modules:
import datetime
class PersonForm(forms.ModelForm):
@ -197,6 +203,7 @@ class MediaForm(forms.ModelForm):
from webapp.libdjango import DjangoInterface
dji = DjangoInterface()
model = super(MediaForm, self).save(commit=False)
model.mime = gen.mime.get_type(model.path)
dobj = dp(self.cleaned_data['text'])
dji.add_date(model, dobj.serialize())
if commit:

View File

@ -26,10 +26,16 @@ from webapp.utils import _, boolean, update_last_changed
from webapp.grampsdb.models import Media
from webapp.grampsdb.forms import *
from webapp.libdjango import DjangoInterface
from gen.config import config
## Django Modules
from django.shortcuts import get_object_or_404, render_to_response, redirect
from django.template import Context, RequestContext
from django.http import HttpResponse
## Other Python Modules
from PIL import Image
import os
## Globals
dji = DjangoInterface()
@ -49,7 +55,30 @@ def process_media(request, context, handle, action, add_to=None): # view, edit,
action = request.POST.get("action")
# Handle: edit, view, add, create, save, delete
if action == "add":
if action == "full":
# FIXME: path should come from config
media = Media.objects.get(handle=handle)
media_type, media_ext = media.mime.split("/", 1)
folder = config.get('behavior.addmedia-image-dir')
image = Image.open("%s/%s" % (folder, media.path))
response = HttpResponse(mimetype=media.mime)
image.save(response, media_ext.upper())
return response
elif action == "thumbnail":
media = Media.objects.get(handle=handle)
media_type, media_ext = media.mime.split("/", 1)
folder = config.get('behavior.addmedia-image-dir')
if os.path.exists("%s/thumbnail/%s" % (folder, media.path)):
image = Image.open("%s/thumbnail/%s" % (folder, media.path))
else:
image = Image.open("%s/%s" % (folder, media.path))
image.thumbnail((300,300), Image.ANTIALIAS)
os.makedirs("%s/thumbnail" % folder)
image.save("%s/thumbnail/%s" % (folder, media.path))
response = HttpResponse(mimetype=media.mime)
image.save(response, media_ext.upper())
return response
elif action == "add":
media = Media(gramps_id=dji.get_next_id(Media, "M"))
mediaform = MediaForm(instance=media)
mediaform.model = media

View File

@ -76,6 +76,7 @@ util_filters = [
util_tags = [
'render',
'media_link',
'render_name',
"get_person_from_handle",
"event_table",
@ -884,6 +885,12 @@ def display_date(obj):
else:
return ""
def media_link(handle, user, action):
retval = """<a href="%s"><img src="%s" /></a>""" % (
"/media/%s/full" % handle,
"/media/%s/thumbnail" % handle)
return retval
def render(formfield, user, action, id=None, url=None, *args):
if not user.is_authenticated():
action = "view"