[open-ils-commits] r200 - in servres/trunk/conifer: syrup templates templates/components (gfawcett)
svn at svn.open-ils.org
svn at svn.open-ils.org
Wed Mar 18 22:56:09 EDT 2009
Author: gfawcett
Date: 2009-03-18 22:56:07 -0400 (Wed, 18 Mar 2009)
New Revision: 200
Modified:
servres/trunk/conifer/syrup/models.py
servres/trunk/conifer/syrup/views.py
servres/trunk/conifer/templates/components/course.xhtml
servres/trunk/conifer/templates/item_add_elec.xhtml
servres/trunk/conifer/templates/item_add_heading.xhtml
servres/trunk/conifer/templates/item_add_url.xhtml
Log:
Add/edit extended metadata on all item forms.
God forgive me for the ugliness I push into views.py with this
commit. I'll clean it up once the dust settles, honest.
Modified: servres/trunk/conifer/syrup/models.py
===================================================================
--- servres/trunk/conifer/syrup/models.py 2009-03-19 02:01:03 UTC (rev 199)
+++ servres/trunk/conifer/syrup/models.py 2009-03-19 02:56:07 UTC (rev 200)
@@ -446,32 +446,34 @@
metadata_attributes = {
- 'dc:contributor': 'Contributor',
- 'dc:coverage': 'Coverage',
- 'dc:creator': 'Creator',
- 'dc:date': 'Date',
- 'dc:description': 'Description',
- 'dc:format': 'Format',
- 'dc:identifier': 'Identifier',
- 'dc:language': 'Language',
- 'dc:publisher': 'Publisher',
- 'dc:relation': 'Relation',
- 'dc:rights': 'Rights',
- 'dc:source': 'Source',
- 'dc:subject': 'Subject',
- 'dc:title': 'Title',
- 'dc:type': 'Type',
- 'syrup:barcode': 'Barcode',
- 'syrup:enumeration': 'Enumeration',
- 'syrup:chronology': 'Chronology'}
+ 'dc:contributor': _('Contributor'),
+ 'dc:coverage': _('Coverage'),
+ 'dc:creator': _('Creator'),
+ 'dc:date': _('Date'),
+ 'dc:description': _('Description'),
+ 'dc:format': _('Format'),
+ 'dc:identifier': _('Identifier'),
+ 'dc:language': _('Language'),
+ 'dc:publisher': _('Publisher'),
+ 'dc:relation': _('Relation'),
+ 'dc:rights': _('Rights'),
+ 'dc:source': _('Source'),
+ 'dc:subject': _('Subject'),
+ 'dc:title': _('Title'),
+ 'dc:type': _('Type'),
+ 'syrup:barcode': _('Barcode'),
+ 'syrup:enumeration': _('Enumeration'),
+ 'syrup:chronology': _('Chronology')}
+metadata_attribute_choices = metadata_attributes.items()
+metadata_attribute_choices.sort(key=lambda (a,b): b)
class Metadata(m.Model):
"""Metadata for items."""
item = m.ForeignKey(Item)
#fixme, arbitrary sizes.
- name = m.CharField(max_length=128, choices=metadata_attributes.items())
+ name = m.CharField('Attribute', max_length=128, choices=metadata_attribute_choices)
value = m.CharField(max_length=4096)
#------------------------------------------------------------
Modified: servres/trunk/conifer/syrup/views.py
===================================================================
--- servres/trunk/conifer/syrup/views.py 2009-03-19 02:01:03 UTC (rev 199)
+++ servres/trunk/conifer/syrup/views.py 2009-03-19 02:56:07 UTC (rev 200)
@@ -33,6 +33,7 @@
import django.forms
import re
import sys
+from django.forms.models import modelformset_factory
#-----------------------------------------------------------------------------
# Z39.50 Support
@@ -595,11 +596,20 @@
if request.method != 'POST':
item = models.Item() # dummy object
+ metadata_formset = metadata_formset_class(queryset=item.metadata_set.all())
return g.render('item_add_%s.xhtml' % item_type.lower(),
**locals())
else:
# fixme, this will need refactoring. But not yet.
author = request.user.get_full_name() or request.user.username
+ item = models.Item() # dummy object
+ metadata_formset = metadata_formset_class(request.POST, queryset=item.metadata_set.all())
+ assert metadata_formset.is_valid()
+ def do_metadata(item):
+ for obj in [obj for obj in metadata_formset.cleaned_data if obj]: # ignore empty dicts
+ if not obj.get('DELETE'):
+ item.metadata_set.create(name=obj['name'], value=obj['value'])
+
if item_type == 'HEADING':
title = request.POST.get('title', '').strip()
if not title:
@@ -613,6 +623,8 @@
title=title,
)
item.save()
+ do_metadata(item)
+ item.save()
elif item_type == 'URL':
title = request.POST.get('title', '').strip()
url = request.POST.get('url', '').strip()
@@ -627,6 +639,8 @@
title=title,
url = url)
item.save()
+ do_metadata(item)
+ item.save()
elif item_type == 'ELEC':
title = request.POST.get('title', '').strip()
upload = request.FILES.get('file')
@@ -642,6 +656,8 @@
)
item.fileobj.save(upload.name, upload)
item.save()
+ do_metadata(item)
+ item.save()
else:
raise NotImplementedError
@@ -650,6 +666,11 @@
else:
return HttpResponseRedirect(course.course_url())
+metadata_formset_class = modelformset_factory(models.Metadata,
+ fields=['name','value'],
+ extra=3, can_delete=True)
+
+
@instructors_only
def item_edit(request, course_id, item_id):
course = get_object_or_404(models.Course, pk=course_id)
@@ -657,9 +678,13 @@
item_type = item.item_type
template = 'item_add_%s.xhtml' % item_type.lower()
parent_item = item.parent_heading
+
if request.method != 'POST':
+ metadata_formset = metadata_formset_class(queryset=item.metadata_set.all())
return g.render(template, **locals())
else:
+ metadata_formset = metadata_formset_class(request.POST, queryset=item.metadata_set.all())
+ assert metadata_formset.is_valid()
if 'file' in request.FILES:
# this is a 'replace-current-file' action.
upload = request.FILES.get('file')
@@ -668,6 +693,12 @@
else:
# generally update the item.
[setattr(item, k, v) for (k,v) in request.POST.items()]
+ # generally update the metadata
+ item.metadata_set.all().delete()
+ for obj in [obj for obj in metadata_formset.cleaned_data if obj]: # ignore empty dicts
+ if not obj.get('DELETE'):
+ item.metadata_set.create(name=obj['name'], value=obj['value'])
+
item.save()
return HttpResponseRedirect(item.parent_url())
Modified: servres/trunk/conifer/templates/components/course.xhtml
===================================================================
--- servres/trunk/conifer/templates/components/course.xhtml 2009-03-19 02:01:03 UTC (rev 199)
+++ servres/trunk/conifer/templates/components/course.xhtml 2009-03-19 02:56:07 UTC (rev 200)
@@ -61,5 +61,26 @@
<li><a href="${prefix}add/?item_type=PHYS">Physical Book/Document</a></li>
</ul>
</div>
-
+
+ <div py:def="item_metadata_formset_header()" py:strip="True">
+ <script type="text/javascript">
+ $(function() {$('#metadatatable').hide(); });
+ function show_metadata() {
+ $('#metadatatable').show();
+ $('#show_metadata').fadeOut();
+ }
+ </script>
+ </div>
+
+ <div py:def="item_metadata_formset()">
+ <p><a href="javascript:show_metadata();" id="show_metadata">Show more attributes</a></p>
+ <div id="metadatatable">
+ ${Markup(metadata_formset.management_form)}
+ <table>
+ <tr py:for="thing in metadata_formset.forms">
+ <td style="border: gray 1px solid; padding: 4; ">${Markup(thing.as_p())}</td>
+ </tr>
+ </table>
+ </div>
+ </div>
</html>
Modified: servres/trunk/conifer/templates/item_add_elec.xhtml
===================================================================
--- servres/trunk/conifer/templates/item_add_elec.xhtml 2009-03-19 02:01:03 UTC (rev 199)
+++ servres/trunk/conifer/templates/item_add_elec.xhtml 2009-03-19 02:56:07 UTC (rev 200)
@@ -13,6 +13,7 @@
<script type="text/javascript">
$(function() {$('input[@name="title"]').focus();});
</script>
+ ${item_metadata_formset_header()}
</head>
<body>
${course_banner(course)}
@@ -27,6 +28,7 @@
value="${item.title}"/></td></tr>
<tr><th>File</th><td><input type="file" name="file"/></td></tr>
</table>
+ ${item_metadata_formset()}
<p><input type="submit" value="Upload file and Create item"/></p>
</form>
</div>
Modified: servres/trunk/conifer/templates/item_add_heading.xhtml
===================================================================
--- servres/trunk/conifer/templates/item_add_heading.xhtml 2009-03-19 02:01:03 UTC (rev 199)
+++ servres/trunk/conifer/templates/item_add_heading.xhtml 2009-03-19 02:56:07 UTC (rev 200)
@@ -13,6 +13,7 @@
<script type="text/javascript">
$(function() {$('input[@name="title"]').focus();});
</script>
+ ${item_metadata_formset_header()}
</head>
<body>
${course_banner(course)}
@@ -25,6 +26,7 @@
value="${item.title}"/>
</td></tr>
</table>
+ ${item_metadata_formset()}
<p>
<input py:if="not is_edit" type="submit" value="Add heading"/>
<input py:if="is_edit" type="submit" value="Update heading"/>
Modified: servres/trunk/conifer/templates/item_add_url.xhtml
===================================================================
--- servres/trunk/conifer/templates/item_add_url.xhtml 2009-03-19 02:01:03 UTC (rev 199)
+++ servres/trunk/conifer/templates/item_add_url.xhtml 2009-03-19 02:56:07 UTC (rev 200)
@@ -13,6 +13,7 @@
<script type="text/javascript">
$(function() {$('input[@name="title"]').focus();});
</script>
+ ${item_metadata_formset_header()}
</head>
<body>
${course_banner(course)}
@@ -23,6 +24,7 @@
<tr><th>Title</th><td><input type="text" name="title" value="${item.title}"/></td></tr>
<tr><th>URL</th><td><input type="text" name="url" value="${item.url}"/></td></tr>
</table>
+ ${item_metadata_formset()}
<p>
<input py:if="not is_edit" type="submit" value="Add URL"/>
<input py:if="is_edit" type="submit" value="Update URL"/>
More information about the open-ils-commits
mailing list