| 1 | # Copyright (C) 2007 Samalyse SARL |
|---|
| 2 | # All rights reserved. |
|---|
| 3 | # |
|---|
| 4 | # This software is licensed as described in the file COPYING, which |
|---|
| 5 | # you should have received as part of this distribution. The terms |
|---|
| 6 | # are also available at http://svn.parisson.org/telemeta/TelemetaLicense. |
|---|
| 7 | # |
|---|
| 8 | # Author: Olivier Guilyardi <olivier@samalyse.com> |
|---|
| 9 | |
|---|
| 10 | import telemeta |
|---|
| 11 | from django.template import Context, loader |
|---|
| 12 | from django import template |
|---|
| 13 | from django.http import HttpResponse |
|---|
| 14 | from django.http import Http404 |
|---|
| 15 | from telemeta.models import MediaItem |
|---|
| 16 | from telemeta.models import MediaCollection |
|---|
| 17 | from django.shortcuts import render_to_response |
|---|
| 18 | import re |
|---|
| 19 | from telemeta.core import * |
|---|
| 20 | from telemeta.export import * |
|---|
| 21 | from telemeta.visualization import * |
|---|
| 22 | from django.conf import settings |
|---|
| 23 | import os |
|---|
| 24 | |
|---|
| 25 | class WebView(Component): |
|---|
| 26 | """Provide web UI methods""" |
|---|
| 27 | |
|---|
| 28 | exporters = ExtensionPoint(IExporter) |
|---|
| 29 | visualizers = ExtensionPoint(IMediaItemVisualizer) |
|---|
| 30 | |
|---|
| 31 | def index(self, request): |
|---|
| 32 | """Render the homepage""" |
|---|
| 33 | |
|---|
| 34 | template = loader.get_template('index.html') |
|---|
| 35 | context = Context({}) |
|---|
| 36 | return HttpResponse(template.render(context)) |
|---|
| 37 | |
|---|
| 38 | def item_detail(self, request, item_id, template='mediaitem_detail.html'): |
|---|
| 39 | """Show the details of a given item""" |
|---|
| 40 | item = MediaItem.objects.get(pk=item_id) |
|---|
| 41 | formats = [] |
|---|
| 42 | for exporter in self.exporters: |
|---|
| 43 | formats.append(exporter.get_format()) |
|---|
| 44 | visualizers = [] |
|---|
| 45 | for visualizer in self.visualizers: |
|---|
| 46 | visualizers.append({'name':visualizer.get_name(), 'id': |
|---|
| 47 | visualizer.get_id()}) |
|---|
| 48 | if request.REQUEST.has_key('visualizer_id'): |
|---|
| 49 | visualizer_id = request.REQUEST['visualizer_id'] |
|---|
| 50 | else: |
|---|
| 51 | visualizer_id = 'waveform' |
|---|
| 52 | |
|---|
| 53 | return render_to_response(template, |
|---|
| 54 | {'item': item, 'export_formats': formats, |
|---|
| 55 | 'visualizers': visualizers, 'visualizer_id': visualizer_id}) |
|---|
| 56 | |
|---|
| 57 | def item_visualize(self, request, item_id, visualizer_id): |
|---|
| 58 | for visualizer in self.visualizers: |
|---|
| 59 | if visualizer.get_id() == visualizer_id: |
|---|
| 60 | break |
|---|
| 61 | |
|---|
| 62 | if visualizer.get_id() != visualizer_id: |
|---|
| 63 | raise Http404 |
|---|
| 64 | |
|---|
| 65 | item = MediaItem.objects.get(pk=item_id) |
|---|
| 66 | |
|---|
| 67 | stream = visualizer.render(item) |
|---|
| 68 | response = HttpResponse(stream, mimetype = 'image/png') |
|---|
| 69 | return response |
|---|
| 70 | |
|---|
| 71 | def item_export(self, request, item_id, format): |
|---|
| 72 | """Export a given media item in the specified format (OGG, FLAC, ...)""" |
|---|
| 73 | for exporter in self.exporters: |
|---|
| 74 | if exporter.get_format() == format: |
|---|
| 75 | break |
|---|
| 76 | |
|---|
| 77 | if exporter.get_format() != format: |
|---|
| 78 | raise Http404 |
|---|
| 79 | |
|---|
| 80 | mime_type = exporter.get_mime_type() |
|---|
| 81 | |
|---|
| 82 | exporter.set_cache_dir(settings.TELEMETA_EXPORT_CACHE_DIR) |
|---|
| 83 | |
|---|
| 84 | item = MediaItem.objects.get(pk=item_id) |
|---|
| 85 | |
|---|
| 86 | infile = settings.MEDIA_ROOT + "/" + item.file |
|---|
| 87 | metadata = item.to_dublincore().flatten() |
|---|
| 88 | stream = exporter.process(item.id, infile, metadata) |
|---|
| 89 | |
|---|
| 90 | response = HttpResponse(stream, mimetype = mime_type) |
|---|
| 91 | response['Content-Disposition'] = 'attachment; filename="download.' + \ |
|---|
| 92 | exporter.get_file_extension() + '"' |
|---|
| 93 | return response |
|---|
| 94 | |
|---|
| 95 | def quick_search(self, request): |
|---|
| 96 | """Perform a simple search through collections and items core metadata""" |
|---|
| 97 | pattern = request.REQUEST["pattern"] |
|---|
| 98 | collections = MediaCollection.objects.quick_search(pattern) |
|---|
| 99 | items = MediaItem.objects.quick_search(pattern) |
|---|
| 100 | return render_to_response('search_results.html', |
|---|
| 101 | {'pattern': pattern, 'collections': collections, |
|---|
| 102 | 'items': items}) |
|---|
| 103 | |
|---|
| 104 | def __get_enumerations_list(self): |
|---|
| 105 | from django.db.models import get_models |
|---|
| 106 | models = get_models(telemeta.models) |
|---|
| 107 | |
|---|
| 108 | enumerations = [] |
|---|
| 109 | for model in models: |
|---|
| 110 | if getattr(model, "is_enumeration", False): |
|---|
| 111 | enumerations.append({"name": model._meta.verbose_name_plural, |
|---|
| 112 | "id": model._meta.module_name}) |
|---|
| 113 | return enumerations |
|---|
| 114 | |
|---|
| 115 | def __get_admin_context_vars(self): |
|---|
| 116 | return {"enumerations": self.__get_enumerations_list()} |
|---|
| 117 | |
|---|
| 118 | def admin_index(self, request): |
|---|
| 119 | return render_to_response('admin.html', self. __get_admin_context_vars()) |
|---|
| 120 | |
|---|
| 121 | def __get_enumeration(self, id): |
|---|
| 122 | from django.db.models import get_models |
|---|
| 123 | models = get_models(telemeta.models) |
|---|
| 124 | for model in models: |
|---|
| 125 | if model._meta.module_name == id: |
|---|
| 126 | break |
|---|
| 127 | |
|---|
| 128 | if model._meta.module_name != id: |
|---|
| 129 | return None |
|---|
| 130 | |
|---|
| 131 | return model |
|---|
| 132 | |
|---|
| 133 | def edit_enumeration(self, request, enumeration_id): |
|---|
| 134 | |
|---|
| 135 | enumeration = self.__get_enumeration(enumeration_id) |
|---|
| 136 | if enumeration == None: |
|---|
| 137 | raise Http404 |
|---|
| 138 | |
|---|
| 139 | vars = self.__get_admin_context_vars() |
|---|
| 140 | vars["enumeration_id"] = enumeration._meta.module_name |
|---|
| 141 | vars["enumeration_name"] = enumeration._meta.verbose_name |
|---|
| 142 | vars["enumeration_name_plural"] = enumeration._meta.verbose_name_plural |
|---|
| 143 | vars["enumeration_values"] = enumeration.objects.all() |
|---|
| 144 | return render_to_response('enumeration_edit.html', vars) |
|---|
| 145 | |
|---|
| 146 | def add_to_enumeration(self, request, enumeration_id): |
|---|
| 147 | |
|---|
| 148 | enumeration = self.__get_enumeration(enumeration_id) |
|---|
| 149 | if enumeration == None: |
|---|
| 150 | raise Http404 |
|---|
| 151 | |
|---|
| 152 | enumeration_value = enumeration(value=request.POST['value']) |
|---|
| 153 | enumeration_value.save() |
|---|
| 154 | |
|---|
| 155 | return self.edit_enumeration(request, enumeration_id) |
|---|
| 156 | |
|---|
| 157 | def update_enumeration(self, request, enumeration_id): |
|---|
| 158 | |
|---|
| 159 | enumeration = self.__get_enumeration(enumeration_id) |
|---|
| 160 | if enumeration == None: |
|---|
| 161 | raise Http404 |
|---|
| 162 | |
|---|
| 163 | if request.POST.has_key("remove"): |
|---|
| 164 | enumeration.objects.filter(id__in=request.POST.getlist('sel')).delete() |
|---|
| 165 | |
|---|
| 166 | return self.edit_enumeration(request, enumeration_id) |
|---|
| 167 | |
|---|
| 168 | def edit_enumeration_value(self, request, enumeration_id, value_id): |
|---|
| 169 | |
|---|
| 170 | enumeration = self.__get_enumeration(enumeration_id) |
|---|
| 171 | if enumeration == None: |
|---|
| 172 | raise Http404 |
|---|
| 173 | |
|---|
| 174 | vars = self.__get_admin_context_vars() |
|---|
| 175 | vars["enumeration_id"] = enumeration._meta.module_name |
|---|
| 176 | vars["enumeration_name"] = enumeration._meta.verbose_name |
|---|
| 177 | vars["enumeration_name_plural"] = enumeration._meta.verbose_name_plural |
|---|
| 178 | vars["enumeration_record"] = enumeration.objects.get(id__exact=value_id) |
|---|
| 179 | return render_to_response('enumeration_edit_value.html', vars) |
|---|
| 180 | |
|---|
| 181 | def update_enumeration_value(self, request, enumeration_id, value_id): |
|---|
| 182 | |
|---|
| 183 | if request.POST.has_key("save"): |
|---|
| 184 | enumeration = self.__get_enumeration(enumeration_id) |
|---|
| 185 | if enumeration == None: |
|---|
| 186 | raise Http404 |
|---|
| 187 | |
|---|
| 188 | record = enumeration.objects.get(id__exact=value_id) |
|---|
| 189 | record.value = request.POST["value"] |
|---|
| 190 | record.save() |
|---|
| 191 | |
|---|
| 192 | return self.edit_enumeration(request, enumeration_id) |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|