ptrad
[redakcja.git] / src / sources / models.py
index 165cb67..ed2775e 100644 (file)
@@ -67,6 +67,7 @@ class Source(models.Model):
         return os.path.isdir(d) and os.listdir(d)
 
     def process(self):
         return os.path.isdir(d) and os.listdir(d)
 
     def process(self):
+        processed_at = now()
         updir = os.path.join(
             settings.MEDIA_ROOT,
             self.get_upload_directory()
         updir = os.path.join(
             settings.MEDIA_ROOT,
             self.get_upload_directory()
@@ -83,7 +84,7 @@ class Source(models.Model):
             self.build_view_directory(updir, d)
         with utils.replace_dir(ocr_dir) as d:
             self.build_ocr_directory(updir, d)
             self.build_view_directory(updir, d)
         with utils.replace_dir(ocr_dir) as d:
             self.build_ocr_directory(updir, d)
-        self.processed_at = now()
+        self.processed_at = processed_at
         self.save(update_fields=['processed_at'])
     
     def build_view_directory(self, srcpath, targetpath):
         self.save(update_fields=['processed_at'])
     
     def build_view_directory(self, srcpath, targetpath):
@@ -118,17 +119,18 @@ class Source(models.Model):
 class BookSource(models.Model):
     book = models.ForeignKey('catalogue.Book', models.CASCADE)
     source = models.ForeignKey(Source, models.CASCADE)
 class BookSource(models.Model):
     book = models.ForeignKey('catalogue.Book', models.CASCADE)
     source = models.ForeignKey(Source, models.CASCADE)
+    ordering = models.IntegerField(default=1)
     page_start = models.IntegerField(null=True, blank=True)
     page_end = models.IntegerField(null=True, blank=True)
         
     class Meta:
     page_start = models.IntegerField(null=True, blank=True)
     page_end = models.IntegerField(null=True, blank=True)
         
     class Meta:
-        ordering = ('page_start',)
+        ordering = ('ordering', 'page_start',)
 
     def __str__(self):
         return f'{self.source} -> {self.book}'
 
     def get_absolute_url(self):
 
     def __str__(self):
         return f'{self.source} -> {self.book}'
 
     def get_absolute_url(self):
-        return reverse('source_book_prepare', args=[self.pk])
+        return reverse('source_book_prepare', args=[self.book.pk])
 
     def get_view_files(self):
         # TODO: won't work for PDFs.
 
     def get_view_files(self):
         # TODO: won't work for PDFs.
@@ -150,16 +152,18 @@ class BookSource(models.Model):
 
     def get_document(self):
         return self.book.document_books.first()
 
     def get_document(self):
         return self.book.document_books.first()
-        
-    def prepare_document(self, user=None):
+
+    @classmethod
+    def prepare_document(cls, book, user=None):
         DBook = apps.get_model('documents', 'Book')
         DBook = apps.get_model('documents', 'Book')
-        texts = document.build_document_texts(self)
+        texts = document.build_document_texts(book)
 
 
-        dbook = self.get_document()
+        dbook = book.document_books.first()
         if dbook is None:
             dbook = DBook.create(
                 user, texts[0],
         if dbook is None:
             dbook = DBook.create(
                 user, texts[0],
-                title=self.book.title,
+                catalogue_book=book,
+                title=book.title,
                 slug=str(uuid.uuid4()),
             )
         else:
                 slug=str(uuid.uuid4()),
             )
         else: