Пример #1
0
 def api_post_set_attachment(self, request, id):
     o = self.get_object_or_404(KBEntry, id=id)
     attach = KBEntryAttachment(
         kb_entry=o, name="uploaded_file1", description="", file=request.FILES["file"]
     )
     attach.save()
     return self.response({"result": "Upload succesful"}, status=self.OK)
Пример #2
0
 def convert_page(self, page):
     # Convert (hex) sequences to unicode
     def convert_hexseq(m):
         seq = m.group(1)
         r = []
         while seq:
             c = seq[:2]
             seq = seq[2:]
             r += chr(int(c, 16))
         r = "".join(r)
         return unicode(r, self.encoding)
     root = os.path.join(self.pages, page)
     name = rx_hexseq.sub(convert_hexseq, page)
     self.out("Converting %s (%s)..." % (page, name))
     # Find current revisions
     current_path = os.path.join(root, "current")
     if not os.path.exists(current_path):
         return  # Return on incomplete pages
     with open(current_path) as f:
         current = f.read().split()  # noqa
     # Write all revisions
     kbe = None
     revisions = sorted(os.listdir(os.path.join(root, "revisions")))
     for rev in revisions:
         rev_path = os.path.join(root, "revisions", rev)
         with open(rev_path) as f:
             body = self.convert_body(unicode(f.read(), self.encoding))
         mtime = datetime.datetime.fromtimestamp(os.stat(rev_path)[stat.ST_MTIME])  # Revision time
         if kbe is None:
             kbe = KBEntry(subject=name, body=body, language=self.language, markup_language="Creole")
             kbe.save(user=self.user, timestamp=mtime)  # Revision history will be populated automatically
             if self.tags:
                 kbe.tags = self.tags
                 kbe.save(user=self.user, timestamp=mtime)
         else:
             kbe.body = body
             kbe.save(user=self.user, timestamp=mtime)  # Revision history will be populated automatically
     self.out("... %d revisions\n" % len(revisions))
     if kbe is None:
         return  # Return when no revisions found
     # Write all attachments
     attachments_root = os.path.join(root, "attachments")
     if os.path.isdir(attachments_root):
         for a in os.listdir(attachments_root):
             self.out("     %s..." % a)
             a_path = os.path.join(attachments_root, a)
             mtime = datetime.datetime.fromtimestamp(os.stat(a_path)[stat.ST_MTIME])  # Attach modification time
             with open(a_path) as f:
                 dbs_path = "/kb/%d/%s" % (kbe.id, a)
                 database_storage.save(dbs_path, f)
                 # Correct mtime
                 database_storage.set_mtime(dbs_path, mtime)
             KBEntryAttachment(kb_entry=kbe, name=a, file=dbs_path).save()
             self.out("...done\n")
Пример #3
0
 def update_file(self, files, o, attrs=None):
     left = {}  # name -> data
     for f in files:
         left[f] = files[f]
     # errors = {}
     # failed = False
     for name in left:
         f = left[name]
         attr = attrs.get(name[4:], {})
         attach = KBEntryAttachment.objects.filter(kb_entry=o, name=name)
         # @todo update attributes
         if not attach:
             attach = KBEntryAttachment(
                 kb_entry=o,
                 name=f.name,
                 description=attr.get("description", ""),
                 is_hidden=attr.get("is_hidden") == "true",
                 file=f,
             )
             attach.save()
     return True