def save_text(self, text, timestamp): path = "%s/%s_%s.txt" % (self.document.get_root_path(), self.document.slug, self.page) text = unicode(text).encode("utf-8") fss.write_content(text, path) fss.close_file(path) path_ts = "%s/%s_%s-%s.txt" % (self.document.get_root_path(), self.document.slug, self.page, timestamp) fss.copy_file(path, path_ts)
def process_file(self): src = os.path.join(settings.MEDIA_ROOT, self.docfile.name) dst = "%s/%s.%s" % (self.get_root_path(), self.slug, self.docfile_basename.split(".")[-1].lower()) fss.copy_file(src, dst) task = task_generate_document.apply_async(args=[self.pk], countdown=5) self = Document.objects.get(id=self.id) self.task_id = task.task_id self.save()
def generate(self): # concatenate all text files ts = datetime.now() all_txt = "%s/%s.txt" % (self.get_root_path(), self.slug) self.page_count = 0 self.pages_set.all().delete() pages = {} for f in fss.listdir(self.get_root_path())[1]: # [0]: dirs, [1]: files if f[-4:] == ".txt" and f != "%s.txt" % self.slug: m = RE_PAGE.match(f) if m: k = int(m.group(1)) pages[k] = f mod_pags_orig = {} mod_pags_curr = {} for k in pages: f = pages[k] self.page_count += 1 tmp_txt = "%s/%s" % (self.get_root_path(), f) fss.write_file(tmp_txt, all_txt) page = Page(document=self, page=RE_PAGE.match(f).group(1), modified=ts) page.save() filepath_orig = "%s/%s_%s-%s.txt" % (self.document.get_root_path(), self.document.slug, k, zeros) fss.copy_file(tmp_txt, filepath_orig) text_url = self.document.text_page_url mod_pags_orig[self.page_count] = text_url.replace("%(page)s", "{}-{}".format(self.page_count, zeros)) mod_pags_curr[self.page_count] = text_url.replace("%(page)s", "{}".format(self.page_count)) Edition.objects.create( document=self.document, author=self.document.document.owner, comment="Original version", modified_pages=mod_pags_orig, date_string=zeros, ) Edition.objects.create( document=self.document, author=self.document.document.owner, comment="Current version", modified_pages=mod_pags_curr, date_string=nines, ) fss.close_file(all_txt)