diff --git a/text/admin.py b/text/admin.py index 4fcfd8e..47fb640 100644 --- a/text/admin.py +++ b/text/admin.py @@ -11,6 +11,8 @@ class TextAdmin(admin.ModelAdmin): search_fields = ['title', 'authors__name', 'languages__name'] list_display = ('title', 'version', 'file') list_filter = ('authors', ) + filter_horizontal = ('authors', ) + admin.site.register(models.Text, TextAdmin) diff --git a/text/models.py b/text/models.py index f15cc98..509dcf3 100644 --- a/text/models.py +++ b/text/models.py @@ -32,10 +32,16 @@ class Author(models.Model): if not self.name_sort: self.name_sort = oxlib.normalize.canonicalName(self.name) super(Author, self).save(*args, **kwargs) - #update path in related texts + #update path in related texts for text in self.texts.all(): 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): name = models.CharField(blank=True, max_length=1000) class Meta: @@ -49,8 +55,8 @@ class Text(models.Model): title = 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) - authors = models.ManyToManyField(Author, related_name='texts', blank=True) + file = models.FileField(upload_to='tmp', blank=True, max_length=2000) + authors = AuthorField(Author, related_name='texts', blank=True) languages = models.ManyToManyField(Language, related_name='texts', blank=True) compilation = models.BooleanField(default=False) @@ -117,6 +123,7 @@ class Text(models.Model): super(Text, self).save(*args, **kwargs) if rename and self.file: path = self.getComputedPath() + print path 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)): @@ -125,10 +132,8 @@ class Text(models.Model): #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() + self.save(rename=False)