first push

This commit is contained in:
bkfox
2015-06-11 18:25:24 +02:00
parent 83885addeb
commit 55665a919b
15 changed files with 1598 additions and 0 deletions

0
programs/__init__.py Executable file
View File

119
programs/admin.py Executable file
View File

@ -0,0 +1,119 @@
import copy
from django.contrib import admin
import programs.models as models
#
# Inlines
#
# TODO: inherits from the corresponding admin view
class SoundFileInline (admin.TabularInline):
model = models.SoundFile
extra = 1
class EpisodeInline (admin.StackedInline):
model = models.Episode
extra = 0
class ScheduleInline (admin.TabularInline):
model = models.Schedule
extra = 0
class EventInline (admin.StackedInline):
model = models.Event
extra = 0
#
# Parents
#
class MetadataAdmin (admin.ModelAdmin):
fieldsets = [
( None, {
'fields': [ 'title', 'tags' ]
}),
( 'metadata', {
'fields': [ 'date' ],
'classes': ['collapse']
}),
]
def save_model(self, request, obj, form, change):
if not obj.author:
obj.author = request.user
obj.save()
class PublicationAdmin (MetadataAdmin):
fieldsets = copy.deepcopy(MetadataAdmin.fieldsets)
list_display = ('id', 'title', 'date', 'status')
list_filter = ['date', 'status']
search_fields = ['title', 'content']
def __init__ (self, *args, **kwargs):
self.fieldsets[0][1]['fields'].insert(1, 'subtitle')
self.fieldsets[0][1]['fields'] += [ 'img', 'content' ]
self.fieldsets[1][1]['fields'] += [ 'parent', 'status', 'enable_comments', 'meta' ],
return super(PublicationAdmin, self).__init__(*args, **kwargs)
#
# ModelAdmin list
#
#class TrackAdmin (MetadataAdmin):
# fieldsets = [
# (None, { 'fields': [ 'title', 'artist', 'version', 'tags'] } )
# ]
class SoundFileAdmin (MetadataAdmin):
fieldsets = [
(None, { 'fields': ['title', 'tags', 'file', 'embed' ] } ),
('metadata', { 'fields': ['duration', 'date', 'podcastable', 'fragment' ] } )
]
#inlines = [ EpisodeInline ]
class ArticleAdmin (PublicationAdmin):
fieldsets = copy.deepcopy(PublicationAdmin.fieldsets)
fieldsets[1][1]['fields'] += ['static_page']
class ProgramAdmin (PublicationAdmin):
fieldsets = copy.deepcopy(PublicationAdmin.fieldsets)
prepopulated_fields = { 'tag': ('title',) }
inlines = [ EpisodeInline, ScheduleInline ]
fieldsets[1][1]['fields'] += ['email', 'url', 'tag']
class EpisodeAdmin (PublicationAdmin):
fieldsets = copy.deepcopy(PublicationAdmin.fieldsets)
inlines = [ EventInline, SoundFileInline ]
list_filter = ['parent'] + PublicationAdmin.list_filter
fieldsets[0][1]['fields'] += ['tracks']
admin.site.register(models.Track)
admin.site.register(models.SoundFile, SoundFileAdmin)
admin.site.register(models.Schedule)
admin.site.register(models.Article, ArticleAdmin)
admin.site.register(models.Program, ProgramAdmin)
admin.site.register(models.Episode, EpisodeAdmin)
admin.site.register(models.Event)

Binary file not shown.

View File

@ -0,0 +1,237 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-09-02 17:05+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: models.py:51
msgid "ponctual"
msgstr ""
#: models.py:52
msgid "every week"
msgstr "toutes les semaines"
#: models.py:53
msgid "first week"
msgstr "1<sup>ère</sup> semaine du mois"
#: models.py:54
msgid "second week"
msgstr "2<sup>ème</sup> semaine du mois"
#: models.py:55
msgid "third week"
msgstr "3<sup>ème</sup> semaine du mois"
#: models.py:56
msgid "fourth week"
msgstr "4<sup>ème</sup> semaine du mois"
#: models.py:57
msgid "first and third"
msgstr "la 1<sup>ère</sup> et 2<sup>ème</sup> semaine du mois"
#: models.py:58
msgid "second and fourth"
msgstr "la 2<sup>ème</sup> et 4<sup>ème</sup> semaine du mois"
#: models.py:59
msgid "one week on two"
msgstr "une semaine sur deux"
#: models.py:74
msgid "author"
msgstr "auteur"
#: models.py:78 models.py:568
msgid "date"
msgstr "date"
#: models.py:81 models.py:332
msgid "title"
msgstr "titre"
#: models.py:89 models.py:565
msgid "meta"
msgstr "metadonnées"
#: models.py:93
msgid "tags"
msgstr "mots-clés"
#: models.py:125
msgid "subtitle"
msgstr "sous-titre"
#: models.py:129
msgid "image"
msgstr "image"
#: models.py:133
msgid "content"
msgstr "contenu"
#: models.py:136 models.py:575
msgid "status"
msgstr "statut"
#: models.py:140
msgid "enable comments"
msgstr "activer les commentaires"
#: models.py:328
msgid "artist"
msgstr "artiste"
#: models.py:335
msgid "version"
msgstr "version"
#: models.py:338
msgid "additional informations on that track"
msgstr "informations supplémentaires sur cette piste"
#: models.py:343
msgid "by"
msgstr "par"
#: models.py:348
msgid "track"
msgstr "piste"
#: models.py:349
msgid "tracks"
msgstr "pistes"
#: models.py:356
msgid "file"
msgstr "fichier"
#: models.py:360 models.py:380 models.py:570
msgid "duration"
msgstr "durée"
#: models.py:364
msgid "podcastable"
msgstr "peut être podcasté"
#: models.py:366
msgid "if checked, the file can be podcasted"
msgstr "si coché, le fichier peut être podcasté"
#: models.py:368
msgid "incomplete sound"
msgstr "son incomplet"
#: models.py:370
msgid "the file has been cut"
msgstr "le fichier a été monté"
#: models.py:378
msgid "schedule"
msgstr "horaire"
#: models.py:379
msgid "frequency"
msgstr "fréquence"
#: models.py:381 models.py:452
msgid "rerun"
msgstr "rediffusion"
#: models.py:464 models.py:485 models.py:524
msgid "parent"
msgstr "parent"
#: models.py:468
msgid "static page"
msgstr "page statique"
#: models.py:471
msgid "article is focus"
msgstr "l'article est épinglé"
#: models.py:477
msgid "Article"
msgstr "Article"
#: models.py:478
msgid "Articles"
msgstr "Articles"
#: models.py:490
msgid "email"
msgstr "email"
#: models.py:495
msgid "website"
msgstr "site"
#: models.py:499
msgid "tag"
msgstr "mot-clé"
#: models.py:501
msgid "used in articles to refer to it"
msgstr "utilisé par les articles pour y faire référence"
#: models.py:510
msgid "Emission"
msgstr "Émission"
#: models.py:511
msgid "Emissions"
msgstr "Émissions"
#: models.py:529
msgid "podcast file"
msgstr "fichier de podcast"
#: models.py:534
msgid "playlist"
msgstr "playlist"
#: models.py:543
#, python-format
msgid "An unknown name for this episode of %s"
msgstr "Cette épisode de %s n'a pas encore de nom"
#: models.py:551
msgid "Episode"
msgstr "Épisode"
#: models.py:552
msgid "Episodes"
msgstr "Épisodes"
#: models.py:561
msgid "episode"
msgstr "épisode"
#: models.py:573
msgid "this is just indicative"
msgstr "juste indicatif"
#: models.py:578
msgid "canceled"
msgstr "annulé"
#~ msgid "third %s of the month"
#~ msgstr "le troisième %s du mois"
#~ msgid "fourth %s of the month"
#~ msgstr "le quatrième %s du mois"

View File

View File

View File

View File

@ -0,0 +1,29 @@
import os
from django.core.management.base import BaseCommand, CommandError
import programs.models as models
import programs.settings
class Command(BaseCommand):
help= "Take a look at the programs directory to check on new podcasts"
def handle(self, *args, **options):
programs = models.Program.objects.filter(schedule__isnull = True)
for program in programs:
self.scan(program, program.path + '/public', public = True)
self.scan(program, program.path + '/podcasts', embed = True)
self.scan(program, program.path + '/private')
def scan (self, program, path, public = False, embed = False):
try:
for filename in os.listdir(path):
long_filename = path + '/' + filename
except:
pass

513
programs/models.py Executable file
View File

@ -0,0 +1,513 @@
import datetime
# django
from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.http import HttpResponse, Http404
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext as _, ugettext_lazy
from django.utils import timezone
from django.utils.html import strip_tags
from django.conf import settings
# extensions
from taggit.managers import TaggableManager
import programs.settings as settings
AStatus = {
'private': 0,
'public': 1,
# 'canceled': 2,
# 'finished': 3,
}
Status = [ (y, ugettext_lazy(x)) for x,y in AStatus.items() ]
RStatus = { y: x for x,y in AStatus.items() }
AFrequency = {
'ponctual': 0x000000,
'every week': 0b001111,
'first week': 0x000001,
'second week': 0x000010,
'third week': 0x000100,
'fourth week': 0x001000,
'first and third': 0x000101,
'second and fourth': 0x001010,
'one week on two': 0x010010,
#'uneven week': 0x100000,
# TODO 'every day': 0x110000
}
# Translators: html safe values
ugettext_lazy('ponctual')
ugettext_lazy('every week')
ugettext_lazy('first week')
ugettext_lazy('second week')
ugettext_lazy('third week')
ugettext_lazy('fourth week')
ugettext_lazy('first and third')
ugettext_lazy('second and fourth')
ugettext_lazy('one week on two')
Frequency = [ (y, x) for x,y in AFrequency.items() ]
RFrequency = { y: x for x,y in AFrequency.items() }
Frequency.sort(key = lambda e: e[0])
class Model (models.Model):
@classmethod
def type (cl):
"""
Return a string with the type of the model (class name lowered)
"""
name = cl.__name__.lower()
return name
@classmethod
def type_plural (cl):
"""
Return a string with the name in plural of the model (cf. name())
"""
return cl.type() + 's'
@classmethod
def name (cl, plural = False):
"""
Return the name of the model using meta.verbose_name
"""
if plural:
return cl._meta.verbose_name_plural.title()
return cl._meta.verbose_name.title()
class Meta:
abstract = True
class Metadata (Model):
"""
meta is used to extend a model for future needs
"""
author = models.ForeignKey (
User,
verbose_name = _('author'),
blank = True,
null = True )
date = models.DateTimeField(
_('date'),
default = datetime.datetime.now )
title = models.CharField(
_('title'),
max_length = 128 )
meta = models.TextField(
_('meta'),
blank = True,
null = True )
tags = TaggableManager(
_('tags'),
blank = True )
class Meta:
abstract = True
class Publication (Metadata):
def get_slug_name (self):
return slugify(self.title)
def __str__ (self):
return self.title + ' (' + str(self.id) + ')'
subtitle = models.CharField(
_('subtitle'),
max_length = 128,
blank = True )
img = models.ImageField(
_('image'),
upload_to = "images",
blank = True )
content = models.TextField(
_('content'),
blank = True )
status = models.SmallIntegerField(
_('status'),
choices = Status,
default = AStatus['public'] )
enable_comments = models.BooleanField(
_('enable comments'),
default = True,
help_text = 'select to enable comments')
#
# Class methods
#
@staticmethod
def _exclude_args (allow_unpublished = False, prefix = ''):
if allow_unpublished:
return {}
res = {}
res[prefix + 'status'] = AStatus['private']
res[prefix + 'date__gt'] = timezone.now()
return res
@classmethod
def get_available (cl, first = False, **kwargs):
"""
Return the result of filter(kargs) if the resulting publications
is published and public
Otherwise, return None
"""
kwargs['status'] = AStatus['public']
kwargs['date__lte'] = timezone.now()
e = cl.objects.filter(**kwargs)
if first:
return (e and e[0]) or None
return e or None
#
# Instance's methods
#
def is_private (self):
return self.status == AStatus['private']
def get_parent (self, raise_404 = False ):
if not parent and raise_404:
raise Http404
return parent
def get_parents ( self, order_by = "desc", include_fields = None ):
"""
Return an array of the parents of the item.
If include_fields is an array of files to include.
"""
# TODO: fields included
# FIXME: parameter name + container
parents = [ self ]
while parents[-1].parent:
parent = parents[-1].parent
if parent not in parents:
# avoid cycles
parents.append(parent)
parents = parents[1:]
if order_by == 'desc':
return reversed(parents)
return parents
class Meta:
abstract = True
#
# Final models
#
class Track (Model):
artist = models.CharField(
_('artist'),
max_length = 128,
blank = True)
title = models.CharField(
_('title'),
max_length = 128 )
version = models.CharField(
_('version'),
max_length = 128,
blank = True,
help_text = _('additional informations on that track'))
tags = TaggableManager( blank = True )
def __str__(self):
return ' '.join([self.title, _('by'), self.artist,
(self.version and ('(' + self.version + ')') or '') ])
class Meta:
verbose_name = _('Track')
verbose_name_plural = _('Tracks')
class SoundFile (Metadata):
parent = models.ForeignKey(
'Episode',
verbose_name = _('episode'),
blank = True,
null = True )
file = models.FileField(
_('file'),
upload_to = "data/tracks",
blank = True )
duration = models.TimeField(
_('duration'),
blank = True,
null = True )
podcastable = models.BooleanField(
_('podcastable'),
default = False,
help_text = _('if checked, the file can be podcasted from this server'))
fragment = models.BooleanField(
_('incomplete sound'),
default = False,
help_text = _("the file has been cut"))
embed = models.TextField (
_('embed HTML code from external website'),
blank = True,
null = True,
help_text = _('if set, consider the sound podcastable from there')
)
def __str__ (self):
return str(self.id) + ': ' + self.file.name
class Meta:
verbose_name = _('Sound')
verbose_name_plural = _('Sounds')
class Schedule (Model):
parent = models.ForeignKey( 'Program', blank = True, null = True )
date = models.DateTimeField(_('schedule'))
frequency = models.SmallIntegerField(_('frequency'), choices = Frequency)
duration = models.TimeField(_('duration'))
rerun = models.BooleanField(_('rerun'), default = False)
def match_week (self, at = datetime.date.today()):
"""
Return True if the given week number matches the schedule, False
otherwise.
If the schedule is ponctual, return None.
"""
if self.frequency == AFrequency['ponctual']:
return None
if self.frequency == AFrequency['one week on two']:
week = at.isocalendar()[1]
return (week % 2) == (self.date.isocalendar()[1] % 2)
first_of_month = datetime.date(at.year, at.month, 1)
week = at.isocalendar()[1] - first_of_month.isocalendar()[1]
# weeks of month
if week == 4:
# fifth week: return if for every week
return self.frequency == 0b1111
return (self.frequency & (0b0001 << week) > 0)
def next_date (self, at = datetime.date.today()):
if self.frequency == AFrequency['ponctual']:
return None
print('#####')
# first day of the week
date = at - datetime.timedelta( days = at.weekday() )
# for the next five week, we look for a matching week.
# when found, add the number of day since de start of the
# we need to test if the result is >= at
for i in range(0,5):
if self.match_week(date):
date_ = date + datetime.timedelta( days = self.date.weekday() )
if date_ >= at:
# we don't want past events
return datetime.datetime(date_.year, date_.month, date_.day,
self.date.hour, self.date.minute)
date += datetime.timedelta( days = 7 )
else:
return None
def next_dates (self, at = datetime.date.today(), n = 52):
# we could have optimized this function, but since it should not
# be use too often, we keep a more readable and easier to debug
# solution
if self.frequency == 0b000000:
return None
res = []
for i in range(n):
e = self.next_date(at)
if not e:
break
res.append(e)
at = res[-1] + datetime.timedelta(days = 1)
return res
def to_string(self):
s = ugettext_lazy( RFrequency[self.frequency] )
if self.rerun:
return s + ' (' + _('rerun') + ')'
return s
def __str__ (self):
return self.parent.title + ': ' + RFrequency[self.frequency]
class Meta:
verbose_name = _('Schedule')
verbose_name_plural = _('Schedules')
class Article (Publication):
parent = models.ForeignKey(
'self',
verbose_name = _('parent'),
blank = True,
null = True )
static_page = models.BooleanField(
_('static page'),
default = False )
focus = models.BooleanField(
_('article is focus'),
blank = True,
default = False )
class Meta:
verbose_name = _('Article')
verbose_name_plural = _('Articles')
class Program (Publication):
parent = models.ForeignKey(
Article
, verbose_name = _('parent')
, blank = True
, null = True
)
email = models.EmailField(
_('email')
, max_length = 128
, null = True
, blank = True
)
url = models.URLField(
_('website')
, blank = True
, null = True
)
tag = models.CharField(
_('tag')
, max_length = 64
, help_text = _('used in articles to refer to it')
)
@property
def path(self):
return settings.AIRCOX_PROGRAMS_DATA + slugify(self.title + '_' + self.id)
class Meta:
verbose_name = _('Program')
verbose_name_plural = _('Programs')
class Episode (Publication):
# Note:
# We do not especially need a duration here, because even if an
# emussion's schedule can have specified durations, in practice this
# duration may vary. Furthermore, we want the users have to enter a
# minimum of values.
# Duration can be retrieved from the sound file if there is one.
#
parent = models.ForeignKey(
Program,
verbose_name = _('parent'),
blank = True,
null = True )
tracks = models.ManyToManyField(
Track,
verbose_name = _('playlist'),
blank = True )
class Meta:
verbose_name = _('Episode')
verbose_name_plural = _('Episodes')
class Event (Model):
"""
"""
parent = models.ForeignKey (
Episode,
verbose_name = _('episode'),
blank = True,
null = True )
meta = models.TextField (
_('meta'),
blank = True,
null = True )
date = models.DateTimeField( _('date') )
duration = models.TimeField(
_('duration'),
blank = True,
null = True,
help_text = _('this is just indicative'))
status = models.SmallIntegerField(
_('status'),
choices = Status,
default = AStatus['public'] )
canceled = models.BooleanField( _('canceled'), default = False )
def testify (self):
parent = self.parent
self.parent.testify()
self.parent.date = self.date
return self.parent
class Meta:
verbose_name = _('Event')
verbose_name_plural = _('Events')

View File

@ -0,0 +1,4 @@
Django>=1.9.0
taggit

10
programs/settings.py Executable file
View File

@ -0,0 +1,10 @@
from django.conf import settings
def ensure (key, default):
globals()[key] = getattr(settings, key, default)
ensure('AIRCOX_PROGRAMS_DATA', settings.MEDIA_ROOT + '/programs')

3
programs/tests.py Executable file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

2
programs/views.py Executable file
View File

@ -0,0 +1,2 @@
from django.shortcuts import render