def handle(self, *args, **options): if not hasattr(settings, 'BOOKTYPE_CONFIG'): raise CommandError('Does not have BOOKTYPE_CONFIG in settings.py file.') if len(args) != 2: raise CommandError("You must specify variable name and value.") key = args[0] value = args[1] if options['integer']: try: value = int(value) except ValueError: raise CommandError("I don't think this %s is a number!" % value) if options['float']: try: value = float(value) except ValueError: raise CommandError("I don't think this %s is a number!" % value) if options['as_json']: try: value = json.loads(value) except ValueError: raise CommandError("Not a valid JSON string.") if options['append']: # ovo neshto ne radi sa as_jsonom lst = config.getConfiguration(key, []) if type(lst) == type([]): lst.append(value) config.setConfiguration(key, lst) else: raise CommandError("Can not append to something that is not a list") elif options['remove']: lst = config.getConfiguration(key, []) if type(lst) == type([]): try: lst.remove(value) except ValueError: raise CommandError("I can't see it!") config.setConfiguration(key, lst) else: raise CommandError("Can not append to something that is not a list") else: config.setConfiguration(key, value) try: config.saveConfiguration() except config.ConfigurationError: raise CommandError("Could not save the file.")
def parseJSON(js): try: return json.loads(js) except Exception: return {}
def importBookFromFile(user, zname, createTOC=False, **extraOptions): """Create a new book from a bookizip filename""" from booki.utils.log import logChapterHistory # unzip it zf = zipfile.ZipFile(zname) # load info.json info = json.loads(zf.read("info.json")) logWarning("Loaded json file %r" % info) metadata = info["metadata"] manifest = info["manifest"] TOC = info["TOC"] if extraOptions.get("book_title", None): bookTitle = extraOptions["book_title"] else: bookTitle = get_metadata(metadata, "title", ns=DC)[0] bookTitle = makeTitleUnique(bookTitle) logWarning("Chose unique book title %r" % bookTitle) if extraOptions.get("book_url", None): bookURL = extraOptions["book_url"] else: bookURL = None book = createBook(user, bookTitle, status="new", bookURL=bookURL) if extraOptions.get("hidden"): book.hidden = True book.save() # this is for Table of Contents p = re.compile('\ssrc="(.*)"') # what if it does not have status "new" stat = models.BookStatus.objects.filter(book=book, name="new")[0] chapters = getChaptersFromTOC(TOC) n = len(chapters) + 1 # is +1 necessary? now = datetime.datetime.now() for chapterName, chapterFile, is_section in chapters: urlName = bookiSlugify(chapterName) if is_section: # create section if createTOC: c = models.BookToc(book=book, version=book.version, name=chapterName, chapter=None, weight=n, typeof=2) c.save() n -= 1 else: # create chapter # check if i can open this file at all content = zf.read(chapterFile) # content = p.sub(r' src="../\1"', content) chapter = models.Chapter( book=book, version=book.version, url_title=urlName, title=chapterName, status=stat, content=content, created=now, modified=now, ) chapter.save() history = logChapterHistory( chapter=chapter, content=content, user=user, comment="", revision=chapter.revision ) if createTOC: c = models.BookToc( book=book, version=book.version, name=chapterName, chapter=chapter, weight=n, typeof=1 ) c.save() n -= 1 stat = models.BookStatus.objects.filter(book=book, name="new")[0] from django.core.files import File for item in manifest.values(): if item["mimetype"] != "text/html": attachmentName = item["url"] if attachmentName.startswith("static/"): att = models.Attachment(book=book, version=book.version, status=stat) s = zf.read(attachmentName) f = StringIO(s) f2 = File(f) f2.size = len(s) att.attachment.save(os.path.basename(attachmentName), f2, save=False) att.save() f.close() # metadata for namespace in metadata: # namespace is something like "http://purl.org/dc/elements/1.1/" or "" # in the former case, preepend it to the name, in {}. ns = "{%s}" % namespace if namespace else "" for keyword, schemes in metadata[namespace].iteritems(): for scheme, values in schemes.iteritems(): # schema, if it is set, describes the value's format. # for example, an identifier might be an ISBN. sc = "{%s}" % scheme if scheme else "" key = "%s%s%s" % (ns, keyword, sc) for v in values: if not v: continue try: info = models.Info(book=book, name=key) if len(v) >= 2500: info.value_text = v info.kind = 2 else: info.value_string = v info.kind = 0 info.save() except: # For now just ignore any kind of error here. # Considering we don't handle metadata as we # should it is not such a problem. pass zf.close() return book
def importBookFromFile(user, zname, createTOC=False, **extraOptions): """Create a new book from a bookizip filename""" from booki.utils.log import logChapterHistory # unzip it zf = zipfile.ZipFile(zname) # load info.json info = json.loads(zf.read('info.json')) logWarning("Loaded json file %r" % info) metadata = info['metadata'] manifest = info['manifest'] TOC = info['TOC'] if extraOptions.get('book_title', None): bookTitle = extraOptions['book_title'] else: bookTitle = get_metadata(metadata, 'title', ns=DC)[0] bookTitle = makeTitleUnique(bookTitle) logWarning("Chose unique book title %r" % bookTitle) if extraOptions.get('book_url', None): bookURL = extraOptions['book_url'] else: bookURL = None book = createBook(user, bookTitle, status="new", bookURL=bookURL) if extraOptions.get("hidden"): book.hidden = True book.save() # this is for Table of Contents p = re.compile('\ssrc="(.*)"') # what if it does not have status "new" stat = models.BookStatus.objects.filter(book=book, name="new")[0] chapters = getChaptersFromTOC(TOC) n = len(chapters) + 1 #is +1 necessary? now = datetime.datetime.now() for chapterName, chapterFile, is_section in chapters: urlName = bookiSlugify(chapterName) if is_section: # create section if createTOC: c = models.BookToc(book=book, version=book.version, name=chapterName, chapter=None, weight=n, typeof=2) c.save() n -= 1 else: # create chapter # check if i can open this file at all content = zf.read(chapterFile) #content = p.sub(r' src="../\1"', content) chapter = models.Chapter(book=book, version=book.version, url_title=urlName, title=chapterName, status=stat, content=content, created=now, modified=now) chapter.save() history = logChapterHistory(chapter=chapter, content=content, user=user, comment="", revision=chapter.revision) if createTOC: c = models.BookToc(book=book, version=book.version, name=chapterName, chapter=chapter, weight=n, typeof=1) c.save() n -= 1 stat = models.BookStatus.objects.filter(book=book, name="new")[0] from django.core.files import File for item in manifest.values(): if item["mimetype"] != 'text/html': attachmentName = item['url'] if attachmentName.startswith("static/"): att = models.Attachment(book=book, version=book.version, status=stat) s = zf.read(attachmentName) f = StringIO(s) f2 = File(f) f2.size = len(s) att.attachment.save(os.path.basename(attachmentName), f2, save=False) att.save() f.close() # metadata for namespace in metadata: # namespace is something like "http://purl.org/dc/elements/1.1/" or "" # in the former case, preepend it to the name, in {}. ns = ('{%s}' % namespace if namespace else '') for keyword, schemes in metadata[namespace].iteritems(): for scheme, values in schemes.iteritems(): #schema, if it is set, describes the value's format. #for example, an identifier might be an ISBN. sc = ('{%s}' % scheme if scheme else '') key = "%s%s%s" % (ns, keyword, sc) for v in values: if not v: continue try: info = models.Info(book=book, name=key) if len(v) >= 2500: info.value_text = v info.kind = 2 else: info.value_string = v info.kind = 0 info.save() except: # For now just ignore any kind of error here. # Considering we don't handle metadata as we # should it is not such a problem. pass zf.close() return book
def importBookFromFile(user, zname, createTOC=False, **extraOptions): """Create a new book from a bookizip filename""" # unzip it zf = zipfile.ZipFile(zname) # load info.json info = json.loads(zf.read('info.json')) logWarning("Loaded json file %r" % info) metadata = info['metadata'] manifest = info['manifest'] TOC = info['TOC'] if extraOptions.get('book_title', None): bookTitle = extraOptions['book_title'] else: bookTitle = get_metadata(metadata, 'title', ns=DC)[0] bookTitle = makeTitleUnique(bookTitle) if extraOptions.get('book_url', None): bookURL = extraOptions['book_url'] else: bookURL = None book = createBook(user, bookTitle, status = "imported", bookURL = bookURL) # this is for Table of Contents p = re.compile('\ssrc="(.*)"') # what if it does not have status "imported" stat = models.BookStatus.objects.filter(book=book, name="imported")[0] chapters = getChaptersFromTOC(TOC) n = len(chapters) + 1 #is +1 necessary? now = datetime.datetime.now() for chapterName, chapterFile, is_section in chapters: urlName = slugify(chapterName) if is_section: # create section if createTOC: c = models.BookToc(book = book, version = book.version, name = chapterName, chapter = None, weight = n, typeof = 2) c.save() n -= 1 else: # create chapter # check if i can open this file at all content = zf.read(chapterFile) #content = p.sub(r' src="../\1"', content) chapter = models.Chapter(book = book, version = book.version, url_title = urlName, title = chapterName, status = stat, content = content, created = now, modified = now) chapter.save() if createTOC: c = models.BookToc(book = book, version = book.version, name = chapterName, chapter = chapter, weight = n, typeof = 1) c.save() n -= 1 stat = models.BookStatus.objects.filter(book=book, name="imported")[0] from django.core.files import File for item in manifest.values(): if item["mimetype"] != 'text/html': attachmentName = item['url'] if attachmentName.startswith("static/"): att = models.Attachment(book = book, version = book.version, status = stat) s = zf.read(attachmentName) f = StringIO(s) f2 = File(f) f2.size = len(s) att.attachment.save(os.path.basename(attachmentName), f2, save=False) att.save() f.close() # metadata for namespace in metadata: # namespace is something like "http://purl.org/dc/elements/1.1/" or "" # in the former case, preepend it to the name, in {}. ns = ('{%s}' % namespace if namespace else '') for keyword, schemes in metadata[namespace].iteritems(): for scheme, values in schemes.iteritems(): #schema, if it is set, describes the value's format. #for example, an identifier might be an ISBN. sc = ('{%s}' % scheme if scheme else '') key = "%s%s%s" % (ns, keyword, sc) for v in values: info = models.Info(book=book, name=key) if len(v) >= 2500: info.value_text = v info.kind = 2 else: info.value_string = v info.kind = 0 info.save() zf.close()
def handle(self, *args, **options): if not hasattr(settings, 'BOOKTYPE_CONFIG'): raise CommandError( 'Does not have BOOKTYPE_CONFIG in settings.py file.') if len(args) != 2: raise CommandError("You must specify variable name and value.") key = args[0] value = args[1] if options['integer']: try: value = int(value) except ValueError: raise CommandError("I don't think this %s is a number!" % value) if options['float']: try: value = float(value) except ValueError: raise CommandError("I don't think this %s is a number!" % value) if options['as_json']: try: value = json.loads(value) except ValueError: raise CommandError("Not a valid JSON string.") if options['append']: # ovo neshto ne radi sa as_jsonom lst = config.getConfiguration(key, []) if type(lst) == type([]): lst.append(value) config.setConfiguration(key, lst) else: raise CommandError( "Can not append to something that is not a list") elif options['remove']: lst = config.getConfiguration(key, []) if type(lst) == type([]): try: lst.remove(value) except ValueError: raise CommandError("I can't see it!") config.setConfiguration(key, lst) else: raise CommandError( "Can not append to something that is not a list") else: config.setConfiguration(key, value) try: config.saveConfiguration() except config.ConfigurationError: raise CommandError("Could not save the file.")