4 from django.apps import apps
5 from django.conf import settings
6 from django.db import models
7 from django.urls import reverse
8 from django.utils.timezone import now
9 from django.utils.translation import gettext_lazy as _
10 from . import conversion
11 from . import document
15 class Source(models.Model):
16 name = models.CharField(_('name'), max_length=1024)
17 notes = models.TextField(blank=True, help_text=_('private'))
18 wikisource = models.CharField(max_length=1024, blank=True)
19 modified_at = models.DateTimeField(null=True, blank=True)
20 processed_at = models.DateTimeField(null=True, blank=True)
25 def get_absolute_url(self):
26 return reverse('source', args=[self.pk])
29 self.modified_at = now()
30 self.save(update_fields=['modified_at'])
32 def get_upload_directory(self):
33 return f'sources/upload/{self.pk}/'
35 def get_view_directory(self):
36 return f'sources/view/{self.pk}/'
38 def get_ocr_directory(self):
39 return f'sources/ocr/{self.pk}/'
41 def get_view_files(self):
42 d = self.get_view_directory()
45 for name in sorted(os.listdir(
46 os.path.join(settings.MEDIA_ROOT, d)
50 def get_ocr_files(self):
51 d = os.path.join(settings.MEDIA_ROOT, self.get_ocr_directory())
54 for name in sorted(os.listdir(d))
60 self.get_upload_directory()
62 view_dir = os.path.join(
64 self.get_view_directory()
66 ocr_dir = os.path.join(
68 self.get_ocr_directory()
70 with utils.replace_dir(view_dir) as d:
71 self.build_view_directory(updir, d)
72 with utils.replace_dir(ocr_dir) as d:
73 self.build_ocr_directory(updir, d)
74 self.processed_at = now()
75 self.save(update_fields=['processed_at'])
77 def build_view_directory(self, srcpath, targetpath):
78 for source_file_name in os.listdir(srcpath):
79 print(source_file_name)
80 src = os.path.join(srcpath, source_file_name)
81 ext = source_file_name.rsplit('.', 1)[-1].lower()
82 if ext in ('png', 'jpg', 'jpeg'):
83 conversion.resize_image(src, targetpath)
86 elif ext in ('tiff', 'tif'):
87 conversion.convert_image(src, targetpath)
89 conversion.convert_pdf(src, targetpath)
91 conversion.convert_djvu(src, targetpath)
95 def build_ocr_directory(self, srcpath, targetpath):
96 for source_file_name in os.listdir(srcpath):
97 print(source_file_name)
100 os.path.join(srcpath, source_file_name),
101 os.path.join(targetpath, source_file_name),
106 class BookSource(models.Model):
107 book = models.ForeignKey('catalogue.Book', models.CASCADE)
108 source = models.ForeignKey(Source, models.CASCADE)
109 page_start = models.IntegerField(null=True, blank=True)
110 page_end = models.IntegerField(null=True, blank=True)
113 ordering = ('page_start',)
116 return f'{self.source} -> {self.book}'
118 def get_absolute_url(self):
119 return reverse('source_book_prepare', args=[self.pk])
121 def get_view_files(self):
122 # TODO: won't work for PDFs.
123 files = self.source.get_view_files()
125 files = files[:self.page_end]
127 files = files[self.page_start - 1:]
130 def get_ocr_files(self):
131 # TODO: won't work for PDFs.
132 files = self.source.get_ocr_files()
134 files = files[:self.page_end]
136 files = files[self.page_start - 1:]
139 def get_document(self):
140 return self.book.document_books.first()
142 def prepare_document(self, user=None):
143 DBook = apps.get_model('documents', 'Book')
144 texts = document.build_document_texts(self)
146 dbook = self.get_document()
148 dbook = DBook.create(
150 title=self.book.title,
151 slug=str(uuid.uuid4()),
154 dbook[0].commit(text=texts[0], description='OCR', author=user)
155 for text in texts[1:]:
156 dbook[0].commit(text=text, description='OCR', author=user)
158 dbook[0].head.set_publishable(True)