subclass ManyToManyField to update path after adding authors

This commit is contained in:
j 2009-08-29 15:00:33 +02:00
parent d2df21d8d2
commit 534f8a0df1
2 changed files with 13 additions and 6 deletions

View file

@ -11,6 +11,8 @@ class TextAdmin(admin.ModelAdmin):
search_fields = ['title', 'authors__name', 'languages__name'] search_fields = ['title', 'authors__name', 'languages__name']
list_display = ('title', 'version', 'file') list_display = ('title', 'version', 'file')
list_filter = ('authors', ) list_filter = ('authors', )
filter_horizontal = ('authors', )
admin.site.register(models.Text, TextAdmin) admin.site.register(models.Text, TextAdmin)

View file

@ -32,10 +32,16 @@ class Author(models.Model):
if not self.name_sort: if not self.name_sort:
self.name_sort = oxlib.normalize.canonicalName(self.name) self.name_sort = oxlib.normalize.canonicalName(self.name)
super(Author, self).save(*args, **kwargs) super(Author, self).save(*args, **kwargs)
#update path in related texts #update path in related texts
for text in self.texts.all(): for text in self.texts.all():
text.save() text.save()
class AuthorField(models.ManyToManyField):
def save_form_data(self, instance, data):
super(AuthorField, self).save_form_data(instance, data)
#save instance after adding authors, file gets updated here
instance.save()
class Language(models.Model): class Language(models.Model):
name = models.CharField(blank=True, max_length=1000) name = models.CharField(blank=True, max_length=1000)
class Meta: class Meta:
@ -49,8 +55,8 @@ class Text(models.Model):
title = models.CharField(blank=True, max_length=1000) title = models.CharField(blank=True, max_length=1000)
version = models.CharField(blank=True, max_length=1000) version = models.CharField(blank=True, max_length=1000)
file = models.FileField(upload_to='Incoming', blank=True, max_length=2000) file = models.FileField(upload_to='tmp', blank=True, max_length=2000)
authors = models.ManyToManyField(Author, related_name='texts', blank=True) authors = AuthorField(Author, related_name='texts', blank=True)
languages = models.ManyToManyField(Language, related_name='texts', blank=True) languages = models.ManyToManyField(Language, related_name='texts', blank=True)
compilation = models.BooleanField(default=False) compilation = models.BooleanField(default=False)
@ -117,6 +123,7 @@ class Text(models.Model):
super(Text, self).save(*args, **kwargs) super(Text, self).save(*args, **kwargs)
if rename and self.file: if rename and self.file:
path = self.getComputedPath() path = self.getComputedPath()
print path
if path != self.file.path: if path != self.file.path:
#FIXME: make sure path is not used by other test in system #FIXME: make sure path is not used by other test in system
if not os.path.exists(os.path.dirname(path)): if not os.path.exists(os.path.dirname(path)):
@ -125,10 +132,8 @@ class Text(models.Model):
#print 'old', self.file.path #print 'old', self.file.path
#print 'new', path #print 'new', path
os.rename(self.file.path, path) os.rename(self.file.path, path)
#FIXME: remove old dir if its empy
if not glob('%s/*' % old_dir): if not glob('%s/*' % old_dir):
os.rmdir(old_dir) os.rmdir(old_dir)
self.file.name = path[len(settings.MEDIA_ROOT) + 1:] self.file.name = path[len(settings.MEDIA_ROOT) + 1:]
#this could be recursive! self.save(rename=False)
self.save()