Mercurial > public > madeira
changeset 48:b8a71c767dc0
For issue #5, create an mp3 application.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 24 Mar 2012 15:14:40 -0500 |
parents | 0e51e5be34b9 |
children | 0900c08055f0 |
files | madeira/band/urls.py madeira/mp3/__init__.py madeira/mp3/admin.py madeira/mp3/management/__init__.py madeira/mp3/management/commands/__init__.py madeira/mp3/management/commands/import_old_mp3.py madeira/mp3/models.py madeira/mp3/urls.py madeira/settings/base.py madeira/templates/mp3/collection_list.html madeira/urls.py |
diffstat | 8 files changed, 176 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/madeira/band/urls.py Tue Mar 20 19:47:20 2012 -0500 +++ b/madeira/band/urls.py Sat Mar 24 15:14:40 2012 -0500 @@ -17,7 +17,7 @@ (r'^photos/(\d+)$', 'photo_detail'), (r'^press_old/$', 'press_index'), (r'^press_old/(\d+)$', 'press_detail'), - (r'^songs/$', 'songs'), + (r'^songs_old/$', 'songs'), (r'^videos/$', 'videos_index'), (r'^videos/(\d+)$', 'video_detail'), )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/mp3/admin.py Sat Mar 24 15:14:40 2012 -0500 @@ -0,0 +1,29 @@ +""" +Automatic admin definitions for the models in the mp3 application. + +""" +from django.contrib import admin +from django.conf import settings + +from mp3.models import Collection, Song + + +class SongInline(admin.TabularInline): + model = Song + + +class CollectionAdmin(admin.ModelAdmin): + list_filter = ['date_added'] + list_display = ['title', 'date_added'] + inlines = [SongInline] + + class Media: + js = settings.THIRD_PARTY_JS['tiny_mce'] + + +class SongAdmin(admin.ModelAdmin): + list_display = ['title', 'collection'] + + +admin.site.register(Collection, CollectionAdmin) +admin.site.register(Song, SongAdmin)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/mp3/management/commands/import_old_mp3.py Sat Mar 24 15:14:40 2012 -0500 @@ -0,0 +1,64 @@ +""" +import_old_mp3.py - For importing mp3 data from the older version of this +website. + +""" +import datetime + +from django.core.management.base import LabelCommand +from django.utils import simplejson as json +from django.utils.html import linebreaks + +from mp3.models import Collection, Song + + +class Command(LabelCommand): + args = '<filename filename ...>' + help = 'Imports older mp3 & mp3 sets in JSON format' + + collections = {} + + def handle_label(self, filename, **options): + """ + Process the file of older mp3 & mp3 sets in JSON. Convert to the new + model scheme. + + """ + with open(filename, 'rb') as f: + items = json.load(f) + + for item in items: + if item['model'] == 'band.mp3_set': + self.process_mp3_set(item) + + for item in items: + if item['model'] == 'band.mp3': + self.process_mp3(item) + + def process_mp3_set(self, item): + + fields = item['fields'] + + description = linebreaks(fields['text'].strip()) + + coll = Collection( + id=item['pk'], + title=fields['title'].strip(), + date_added=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'), + description=description) + + coll.save() + self.collections[coll.pk] = coll + + def process_mp3(self, item): + + fields = item['fields'] + + song = Song( + id=item['pk'], + title=fields['title'].strip(), + description=fields['desc'].strip(), + file=fields['file'], + collection=self.collections[fields['mp3_set']], + ) + song.save()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/mp3/models.py Sat Mar 24 15:14:40 2012 -0500 @@ -0,0 +1,39 @@ +""" +Models for the mp3 application. + +""" +from django.db import models + + +class Collection(models.Model): + """ + This model represents a collection of songs. + + """ + title = models.CharField(max_length=64) + description = models.TextField() + date_added = models.DateTimeField() + + class Meta: + ordering = ['-date_added'] + + def __unicode__(self): + return self.title + + +class Song(models.Model): + """ + This model represents an uploaded song file. + + """ + title = models.CharField(max_length=64) + description = models.CharField(max_length=255, blank=True) + file = models.FileField(upload_to='mp3s/%Y/%m/%d/') + collection = models.ForeignKey(Collection) + + class Meta: + ordering = ['title'] + + def __unicode__(self): + return self.title +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/mp3/urls.py Sat Mar 24 15:14:40 2012 -0500 @@ -0,0 +1,18 @@ +""" +Urls for the mp3 application. + +""" +from django.conf.urls.defaults import patterns, url +from django.views.generic import ListView + +from mp3.models import Collection + + +urlpatterns = patterns('', + url(r'^$', + ListView.as_view( + model=Collection, + paginate_by=10, + context_object_name='collection_list'), + name='mp3-index'), +)
--- a/madeira/settings/base.py Tue Mar 20 19:47:20 2012 -0500 +++ b/madeira/settings/base.py Sat Mar 24 15:14:40 2012 -0500 @@ -108,6 +108,7 @@ 'articles', 'band', 'gigs', + 'mp3', 'news', 'photologue', ]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/templates/mp3/collection_list.html Sat Mar 24 15:14:40 2012 -0500 @@ -0,0 +1,23 @@ +{% extends 'band/base.html' %} +{% block title %}The Madeira | Songs{% endblock %} +{% block content %} +<h1>Madeira Songs</h1> +{% if collection_list %} + <p>Check out some Madeira song downloads!</p> + {% for collection in collection_list %} + <h2>{{ collection.title }}</h2> + <div> + {{ collection.description|safe }} + </div> + <ul> + {% for song in collection.song_set.all %} + <li><a href="{{ song.file.url }}">{{ song.title }}</a> + ({{ song.file.size|filesizeformat }}){% if song.description %} - {{ song.description }}{% endif %}</li> + {% endfor %} + </ul> + {% endfor %} + {% include 'pagination.html' %} +{% else %} +No downloads available at this time. +{% endif %} +{% endblock %}
--- a/madeira/urls.py Tue Mar 20 19:47:20 2012 -0500 +++ b/madeira/urls.py Sat Mar 24 15:14:40 2012 -0500 @@ -10,6 +10,7 @@ (r'^gigs/', include('gigs.urls')), (r'^news/', include('news.urls')), (r'^press/', include('articles.urls')), + (r'^songs/', include('mp3.urls')), (r'^admin/doc/', include('django.contrib.admindocs.urls')), (r'^admin/', include(admin.site.urls)), (r'^photologue/', include('photologue.urls')),