basic text management

This commit is contained in:
j 2009-08-28 11:02:20 +02:00
commit e55e78ba2d
19 changed files with 499 additions and 0 deletions

0
text/__init__.py Normal file
View file

27
text/admin.py Normal file
View file

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
# Written 2009 by j@mailb.org
from django.contrib import admin
import models
class TextAdmin(admin.ModelAdmin):
search_fields = ['title', 'authors__name']
list_display = ('title', 'version', 'file')
list_filter = ('authors', )
admin.site.register(models.Text, TextAdmin)
class AuthorAdmin(admin.ModelAdmin):
search_fields = ['name']
list_display = ('name', )
admin.site.register(models.Author, AuthorAdmin)
class LanguageAdmin(admin.ModelAdmin):
search_fields = ['name']
list_display = ('name', )
admin.site.register(models.Language, LanguageAdmin)

View file

View file

View file

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
# Written 2009 by j@mailb.org
import os
from glob import glob
import oxlib
import oxlib.normalize
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from texts.text import models
class Command(BaseCommand):
"""
import texts from media/text.
"""
help = 'import texts that are not in db.'
args = ''
def handle(self, **options):
for f in glob('%s/*/*/*' % settings.MEDIA_ROOT):
if os.path.isfile(f):
name = f[len(settings.MEDIA_ROOT)+1:]
name = name.decode('utf-8')
q = models.Text.objects.filter(file=name)
if q.count() == 0:
print 'adding', name
text = models.Text()
text.file.name = name
text.parsePath()

130
text/models.py Normal file
View file

@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
# Written 2009 by j@mailb.org
import os
import re
from glob import glob
import oxlib
import oxlib.normalize
from django.db import models
from django.conf import settings
def getAuthor(name, name_sort=''):
try:
a = Author.objects.get(name=name)
except Author.DoesNotExist:
a = Author(name=name, name_sort=name_sort)
a.save()
return a
class Author(models.Model):
name = models.CharField(blank=True, max_length=1000, unique=True)
name_sort = models.CharField('Sort Name', blank=True, max_length=1000)
class Meta:
ordering = ('name_sort', )
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
if not self.name_sort:
self.name_sort = oxlib.normalize.canonicalName(self.name)
super(Author, self).save(*args, **kwargs)
class Language(models.Model):
name = models.CharField(blank=True, max_length=1000)
class Meta:
ordering = ('name', )
def __unicode__(self):
return self.name
class Text(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
title = models.CharField(blank=True, max_length=1000)
version = models.CharField(blank=True, max_length=1000)
file = models.FileField(upload_to='Incoming', blank=True)
authors = models.ManyToManyField(Author, related_name='texts', blank=True)
languages = models.ManyToManyField(Language, related_name='texts', blank=True)
compilation = models.BooleanField(default=False)
class Meta:
ordering = ('title', )
def __unicode__(self):
return self.title
def getComputedPath(self):
authors = [a.name_sort for a in self.authors.all()]
author = "; ".join(authors)
if not author:
author = "Unknown Author"
if self.compilation:
author += " (Ed.)"
elif author.endswith("."):
author = author[:-1] + "_"
title = self.title
if self.version:
title += ".%s" % self.version
extension = os.path.splitext(self.file.path)[1].lower()
path = u"%s/%s/%s%s" % (author[0].upper(), author, title, extension)
return os.path.join(settings.MEDIA_ROOT, path).encode('utf-8')
def parsePath(self):
if self.file:
match = re.compile('./(.*)/(.*)').findall(self.file.name)
title = os.path.splitext(match[0][1])[0]
compilation = False
#FIXME: parse version
version = re.compile("\.(\w+)$").findall(title)
if version:
version = version[0]
title = title[:-(len(version) + 1)]
else:
version = ''
authors = match[0][0]
if authors.endswith("_"):
authors = authors[:-1] + "."
if authors.endswith(' (Ed.)'):
authors = authors[:-len(' (Ed.)')]
compilation = True
if authors != 'Unknown Author':
authors = [(oxlib.normalize.normalizeName(a), a) for a in authors.split("; ")]
else:
authors = []
self.title = title
self.version = version
self.compilation = compilation
self.save(rename=False)
for a in authors:
author = getAuthor(a[0], a[1])
self.authors.add(author)
self.save()
def save(self, *args, **kwargs):
rename = True
if "rename" in kwargs:
rename = kwargs['rename']
del kwargs['rename']
super(Text, self).save(*args, **kwargs)
if rename and self.file:
path = self.getComputedPath()
if path != self.file.path:
#FIXME: make sure path is not used by other test in system
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
old_dir = os.path.dirname(self.file.path)
print 'old', self.file.path
print 'new', path
os.rename(self.file.path, path)
#FIXME: remove old dir if its empy
if not glob('%s/*' % old_dir):
os.rmdir(old_dir)
self.file.name = path[len(settings.MEDIA_ROOT) + 1:]
#this could be recursive!
self.save()

23
text/tests.py Normal file
View file

@ -0,0 +1,23 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

1
text/views.py Normal file
View file

@ -0,0 +1 @@
# Create your views here.