From: olivier <> Date: Mon, 25 Jan 2010 19:08:13 +0000 (+0000) Subject: begin to couple the web view with new CREM models ; junk some leftovers X-Git-Tag: 1.1~579 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=3310b42e40104de14e3be168a494951f455256a5;p=telemeta.git begin to couple the web view with new CREM models ; junk some leftovers --- diff --git a/telemeta/admin.py b/telemeta/admin.py deleted file mode 100644 index ae8784ab..00000000 --- a/telemeta/admin.py +++ /dev/null @@ -1,33 +0,0 @@ -from django.contrib import admin -from telemeta.models import MediaCollection, MediaItem, MediaPart, MediaCore -from django import forms - -class MediaCoreAdminForm(forms.ModelForm): - def clean(self): - data = forms.ModelForm.clean(self) - id = None - if data.has_key("id"): - id = data["id"] = data["id"].strip() - if not id: - raise forms.ValidationError(u"id field is required") - if not MediaCore.is_well_formed_id(id): - raise forms.ValidationError(u"'%s' is not a well-formed id" % id) - return data - -class MediaCollectionAdminForm(MediaCoreAdminForm): - class Meta: - model = MediaCollection - -class MediaItemAdminForm(MediaCoreAdminForm): - class Meta: - model = MediaItem - -class MediaCollectionAdmin(admin.ModelAdmin): - form = MediaCollectionAdminForm - -class MediaItemAdmin(admin.ModelAdmin): - form = MediaItemAdminForm - -admin.site.register(MediaCollection, MediaCollectionAdmin) -admin.site.register(MediaItem, MediaItemAdmin) -admin.site.register(MediaPart) diff --git a/telemeta/models.py b/telemeta/models.py deleted file mode 100644 index e69de29b..00000000 diff --git a/telemeta/models/crem.py b/telemeta/models/crem.py index 0db0975d..156bea5c 100755 --- a/telemeta/models/crem.py +++ b/telemeta/models/crem.py @@ -131,6 +131,10 @@ class MediaCollection(MediaResource): element_type = 'collection' PUBLIC_ACCESS_CHOICES = (('none', 'none'), ('metadata', 'metadata'), ('metadata', 'full')) + published_code_regex = 'CNRSMH_E_[0-9]{4}(?:_[0-9]{3}){2}' + unpublished_code_regex = 'CNRSMH_I_[0-9]{4}_[0-9]{3}' + code_regex = '(?:%s|%s)' % (published_code_regex, unpublished_code_regex) + reference = models.CharField(unique=True, max_length=250, null=True) physical_format = WeakForeignKey('PhysicalFormat', related_name="collections", null=True) @@ -215,9 +219,9 @@ class MediaCollection(MediaResource): def is_valid_code(self, code): "Check if the collection code is well formed" if self.is_published: - regex = '^CNRSMH_E_[0-9]{4}(_[0-9]{3}){2}$' + regex = '^' + self.published_code_regex + '$' else: - regex = '^CNRSMH_I_[0-9]{4}_[0-9]{3}$' + regex = '^' + self.unpublished_code_regex + '$' if re.match(regex, code): return True @@ -239,6 +243,10 @@ class MediaItem(MediaResource): element_type = 'item' PUBLIC_ACCESS_CHOICES = (('none', 'none'), ('metadata', 'metadata'), ('full', 'full')) + published_code_regex = MediaCollection.published_code_regex + '(?:_[0-9]{2}){1,2}' + unpublished_code_regex = MediaCollection.unpublished_code_regex + '_[0-9]{2,3}(?:_[0-9]{2}){0,2}' + code_regex = '(?:%s|%s)' % (published_code_regex, unpublished_code_regex) + collection = models.ForeignKey('MediaCollection', related_name="items") track = models.CharField(max_length=250, default="") old_code = models.CharField(unique=True, max_length=250, null=True) @@ -281,12 +289,15 @@ class MediaItem(MediaResource): def is_valid_code(self, code): "Check if the item code is well formed" + if not re.match('^' + self.collection.code, self.code): + return false + if self.collection.is_published: - regex = '^' + self.collection.code + '(_[0-9]{2}){1,2}$' + regex = '^' + self.published_code_regex + '$' else: - regex = '^' + self.collection.code + '_[0-9]{2,3}(_[0-9]{2}){0,2}$' + regex = '^' + self.unpublished_code_regex + '$' - if re.match(regex, self.code): + if re.match(regex, code): return True return False diff --git a/telemeta/models/cremquery.py b/telemeta/models/cremquery.py index 8c3477bf..1c72195f 100644 --- a/telemeta/models/cremquery.py +++ b/telemeta/models/cremquery.py @@ -36,6 +36,7 @@ from django.db.models import Manager, Q from telemeta.models.core import EnhancedQuerySet, EnhancedManager import re +from django.core.exceptions import ObjectDoesNotExist class CoreQuerySet(EnhancedQuerySet): "Base class for all query sets" @@ -74,6 +75,21 @@ class CoreManager(EnhancedManager): "" return self.get_query_set().none(*args, **kwargs) + def get(self, **kwargs): + if kwargs.has_key('code_or_id'): + try: + args = kwargs.copy() + args['code'] = kwargs['code_or_id'] + args.pop('code_or_id') + return super(CoreManager, self).get(**args) + except ObjectDoesNotExist: + args = kwargs.copy() + args['id'] = kwargs['code_or_id'] + args.pop('code_or_id') + return super(CoreManager, self).get(**args) + + return super(CoreManager, self).get(**kwargs) + class MediaCollectionQuerySet(CoreQuerySet): def quick_search(self, pattern): @@ -202,6 +218,7 @@ class MediaCollectionManager(CoreManager): return result + class MediaItemQuerySet(CoreQuerySet): "Base class for all media item query sets" diff --git a/telemeta/templates/telemeta/inc/dublincore.html b/telemeta/templates/telemeta/inc/dublincore.html new file mode 100644 index 00000000..2c6134bb --- /dev/null +++ b/telemeta/templates/telemeta/inc/dublincore.html @@ -0,0 +1,2 @@ +{% extends "telemeta_default/inc/dublincore.html" %} + diff --git a/telemeta/templates/telemeta_default/collection.m3u b/telemeta/templates/telemeta_default/collection.m3u index 7bb028e5..259dc4ba 100644 --- a/telemeta/templates/telemeta_default/collection.m3u +++ b/telemeta/templates/telemeta_default/collection.m3u @@ -1,3 +1,3 @@ #EXTM3U{% load telemeta_utils %}{% for item in collection.items.all %} #EXTINF:{{ item.get_duration }},{{ item.title }} -http://{{ host }}{% url telemeta-item-export item.id|urlencode,"mp3" %}{% endfor %} +http://{{ host }}{% url telemeta-item-export item|code_or_id,"mp3" %}{% endfor %} diff --git a/telemeta/templates/telemeta_default/collection_detail.html b/telemeta/templates/telemeta_default/collection_detail.html index b7313314..62399bef 100644 --- a/telemeta/templates/telemeta_default/collection_detail.html +++ b/telemeta/templates/telemeta_default/collection_detail.html @@ -7,28 +7,28 @@ {% endblock %} -{% if object %} +{% if collection %} {% block submenu %} -
Listen to this collection - (M3U, - XSPF)
+ (M3U, + XSPF) {% if 0 %} {# Use 1/0 for alternate player #} {% else %} - {% if object.deposant_cnrs %}No such collection
-{% endif %} \ No newline at end of file +{% endif %} diff --git a/telemeta/templates/telemeta_default/collection_xspf.xml b/telemeta/templates/telemeta_default/collection_xspf.xml index 2e8066c0..f7b72e6f 100644 --- a/telemeta/templates/telemeta_default/collection_xspf.xml +++ b/telemeta/templates/telemeta_default/collection_xspf.xml @@ -5,7 +5,7 @@ {% with collection.to_dublincore.flatten as dc %}Download: {% for format in export_formats %} - {{ format.name }} + {{ format.name }} {% endfor %}
No such item
diff --git a/telemeta/templates/telemeta_default/mediaitem_xspf.xml b/telemeta/templates/telemeta_default/mediaitem_xspf.xml index ece81d37..78ed4cc5 100644 --- a/telemeta/templates/telemeta_default/mediaitem_xspf.xml +++ b/telemeta/templates/telemeta_default/mediaitem_xspf.xml @@ -5,9 +5,9 @@ {% endblock %} diff --git a/telemeta/templatetags/telemeta_utils.py b/telemeta/templatetags/telemeta_utils.py index 39c5be2a..622cec0a 100644 --- a/telemeta/templatetags/telemeta_utils.py +++ b/telemeta/templatetags/telemeta_utils.py @@ -1,5 +1,8 @@ from django import template from django.utils.http import urlquote +from telemeta.models import MediaItem, MediaCollection +from django.core.urlresolvers import reverse +import telemeta.models.dublincore as dc register = template.Library() @@ -69,3 +72,24 @@ def build_query_string(vars): return "&".join(args) return '' +@register.filter +def code_or_id(resource): + if resource.code: + return resource.code + else: + return resource.id + +@register.filter +def is_item(resource): + return isinstance(resource, MediaItem) + +@register.filter +def is_collection(resource): + return isinstance(resource, MediaCollection) + +@register.filter +def to_dublincore(resource): + if isinstance(resource, MediaItem): + return dc.express_item(resource) + else: + return dc.express_collection(resource) diff --git a/telemeta/tests/model_tests.py b/telemeta/tests/model_tests.py index db45bae2..ae5e3b49 100644 --- a/telemeta/tests/model_tests.py +++ b/telemeta/tests/model_tests.py @@ -322,9 +322,11 @@ class RelatedDeleteTestCase(unittest.TestCase): self.assertEquals(q[0].publisher, self.publisher2) self.assertEquals(q[0].publisher_collection, None) - def testOnDeleteCascadeMultiple(self): + def testOnDeleteMultiple(self): Publisher.objects.all().delete() self.assertEquals(Publisher.objects.count(), 0) self.assertEquals(PublisherCollection.objects.count(), 0) self.assertEquals(MediaCollection.objects.count(), 2) + + diff --git a/telemeta/urls.py b/telemeta/urls.py index 160163d0..9fded5a4 100644 --- a/telemeta/urls.py +++ b/telemeta/urls.py @@ -52,8 +52,6 @@ all_items = { 'queryset': MediaItem.objects.all(), } all_collections = { 'queryset': MediaCollection.objects.all(), } # ID's regular expressions -i_ex = MediaItem.id_regex -c_ex = MediaCollection.id_regex export_extensions = "|".join(web_view.list_export_extensions()) htdocs = os.path.dirname(__file__) + '/htdocs' @@ -65,22 +63,22 @@ urlpatterns = patterns('', url(r'^items/$', 'django.views.generic.list_detail.object_list', dict(all_items, paginate_by=20, template_name="telemeta/mediaitem_list.html"), name="telemeta-items"), - url(r'^items/(?P[A-Z0-9_]+)/$', web_view.collection_detail,
+ dict(template="telemeta/collection_detail.html"), name="telemeta-collection-detail"),
+ url(r'^collections/(?P[A-Z0-9_]+)/dc/$', web_view.collection_detail,
+ dict(template="telemeta/collection_detail_dc.html"), name="telemeta-collection-dublincore"),
+ url(r'^collections/(?P[A-Z0-9_]+)/collection_xspf.xml$',
web_view.collection_playlist,
dict(template="telemeta/collection_xspf.xml", mimetype="application/xspf+xml"),
name="telemeta-collection-xspf"),
- url(r'^collections/(?P' + c_ex + ')/collection.m3u$',
+ url(r'^collections/(?P[A-Z0-9_]+)/collection.m3u$',
web_view.collection_playlist,
dict(template="telemeta/collection.m3u", mimetype="audio/mpegurl"),
name="telemeta-collection-m3u"),
diff --git a/telemeta/web/base.py b/telemeta/web/base.py
index 805a7074..ef7e4fa3 100644
--- a/telemeta/web/base.py
+++ b/telemeta/web/base.py
@@ -54,6 +54,7 @@ from telemeta.analysis import *
from telemeta.analysis.vamp import *
import telemeta.interop.oai as oai
from telemeta.interop.oaidatasource import TelemetaOAIDataSource
+from django.core.exceptions import ObjectDoesNotExist
class WebView(Component):
"""Provide web UI methods"""
@@ -69,9 +70,14 @@ class WebView(Component):
context = Context({})
return HttpResponse(template.render(context))
- def item_detail(self, request, item_id, template='telemeta/mediaitem_detail.html'):
+ def collection_detail(self, request, code, template=''):
+ collection = MediaCollection.objects.get(code=code)
+ return render_to_response(template, {'collection': collection})
+
+
+ def item_detail(self, request, item_key, template='telemeta/mediaitem_detail.html'):
"""Show the details of a given item"""
- item = MediaItem.objects.get(pk=item_id)
+ item = MediaItem.objects.get(code_or_id=item_key)
formats = []
for exporter in self.exporters:
@@ -88,7 +94,11 @@ class WebView(Component):
analyzers = []
for analyzer in self.analyzers:
- value = analyzer.render(item)
+ if item.file:
+ value = analyzer.render(item)
+ else:
+ value = 'N/A'
+
analyzers.append({'name':analyzer.get_name(),
'id':analyzer.get_id(),
'unit':analyzer.get_unit(),
@@ -105,7 +115,7 @@ class WebView(Component):
'visualizers': visualizers, 'visualizer_id': visualizer_id,
'analysers': analyzers, 'vamp_plugins': vamp_plugin_list})
- def item_visualize(self, request, item_id, visualizer_id, width, height):
+ def item_visualize(self, request, item_key, visualizer_id, width, height):
for visualizer in self.visualizers:
if visualizer.get_id() == visualizer_id:
break
@@ -113,7 +123,7 @@ class WebView(Component):
if visualizer.get_id() != visualizer_id:
raise Http404
- item = MediaItem.objects.get(pk=item_id)
+ item = MediaItem.objects.get(code_or_id=item_key)
visualizer.set_colors((255,255,255), 'purple')
stream = visualizer.render(item, width=int(width), height=int(height))
@@ -127,7 +137,7 @@ class WebView(Component):
list.append(exporter.get_file_extension())
return list
- def item_export(self, request, item_id, extension):
+ def item_export(self, request, item_key, extension):
"""Export a given media item in the specified format (OGG, FLAC, ...)"""
for exporter in self.exporters:
if exporter.get_file_extension() == extension:
@@ -140,7 +150,7 @@ class WebView(Component):
exporter.set_cache_dir(settings.TELEMETA_EXPORT_CACHE_DIR)
- item = MediaItem.objects.get(pk=item_id)
+ item = MediaItem.objects.get(code_or_id=item_key)
infile = item.file.path
metadata = item.to_dublincore().flatten()
@@ -306,18 +316,20 @@ class WebView(Component):
return self.edit_enumeration(request, enumeration_id)
- def collection_playlist(self, request, collection_id, template, mimetype):
- collection = MediaCollection.objects.get(id__exact=collection_id)
- if not collection:
+ def collection_playlist(self, request, code, template, mimetype):
+ try:
+ collection = MediaCollection.objects.get(code=code)
+ except ObjectDoesNotExist:
raise Http404
template = loader.get_template(template)
context = Context({'collection': collection, 'host': request.META['HTTP_HOST']})
return HttpResponse(template.render(context), mimetype=mimetype)
- def item_playlist(self, request, item_id, template, mimetype):
- item = MediaItem.objects.get(id__exact=item_id)
- if not item:
+ def item_playlist(self, request, item_key, template, mimetype):
+ try:
+ item = MediaItem.objects.get(code_or_id=item_key)
+ except ObjectDoesNotExist:
raise Http404
template = loader.get_template(template)