from django.db import models import datetime from django.db.models.signals import post_save # import subprocess import os import codecs class Issue(models.Model): name = models.CharField(max_length=255) date = models.DateField() published = models.BooleanField(default=False) notes = models.TextField(blank=True) def __unicode__(self): return self.name class Article(models.Model): authors = models.ManyToManyField("Contributor", blank=True, null=True) title = models.CharField(max_length=255) order = models.IntegerField(blank=True, null=True) notes = models.TextField(blank=True) issue = models.ForeignKey(Issue) def __unicode__(self): return self.title + " - " + self.author class Contributor(models.Model): name = models.CharField(max_length=255) email = models.EmailField(blank=True, null=True) def __unicode__(self): return self.name + ": " + self.email DOC_CATEGORIES = ( ('main', 'Main Article'), ('secondary', 'Secondary Article'), ('feedback', 'Feedback'), ('announce', 'Announcements & Reviews'), ('reference', 'Reference'), ('other', 'Other'), ) class Document(models.Model): file = models.FileField(upload_to='erang/documents/') title = models.CharField(max_length=500) issue = models.ForeignKey(Issue) category = models.CharField(max_length=255, choices=DOC_CATEGORIES, blank=True) notes = models.TextField(blank=True) contributor = models.ForeignKey(Contributor, blank=True, null=True) doc_txt = models.TextField(blank=True) date_added = models.DateTimeField(default=datetime.datetime.now(), editable=False) def txtPath(self): path = self.file.path return path + ".txt" def __unicode__(self): return self.title class Image(models.Model): file = models.FileField(upload_to='erang/images/') title = models.CharField(max_length=255) contributor = models.ForeignKey(Contributor, blank=True, null=True) issue = models.ForeignKey(Issue) notes = models.TextField(blank=True) date_added = models.DateTimeField(default=datetime.datetime.now(), editable=False) def __unicode__(self): return self.title ''' def docToTxt(**kwargs): obj = kwargs['instance'] f = open("/tmp/tmpDocToTxt.txt", "w") s = subprocess.Popen(["antiword", "-f", "-m", "UTF-8", obj.file.path], stdout = f) f.close() f2 = open("/tmp/tmpDocToTxt.txt") txt = f2.read() f2.close() obj.doc_txt = txt obj.save() return ''' def docToTxt(**kwargs): obj = kwargs['instance'] docFilePath = obj.file.path txtPath = obj.txtPath() if not os.path.isfile(txtPath): os.system("antiword -f -m 'UTF-8' '%s' > %s" % (docFilePath, txtPath,)) f = codecs.open(txtPath, encoding='utf-8') txt = f.read() f.close() obj.doc_txt = txt obj.save() return return post_save.connect(docToTxt, sender=Document)