def ebook(self, my_file, md5): elib = EbookLib() save = Save() edit = Edit() mime = Mimetype() self.tempid = str(random.randint(1, 1000)) ext = my_file.filename.split('.')[-1] self.filename = 'books/temp/' + self.tempid + "." + ext self.info["content_type"] = str(my_file.content_type) m = mime.get_type(my_file.file.read()) self.info["ct"] = str(m) if m == 'application/epub+zip': my_file.file.seek(0) self.write(my_file) elib.epub(self.filename) elib.res["md5"] = md5 edit.new(self.filename, elib.res) save.move(self.filename, Book(edit.lastid)) elib.epub_image(Book(edit.lastid)) self.info["lastid"] = edit.lastid self.info["book"] = elib.res self.info["status"] = "Success" else: self.info["status"] = "Unsupported format"
def epub(self, path): self.info = {} checksum = Checksum() edit = Edit() elib = EbookLib() mime = Mimetype() save = Save() self.info['path'] = path for item in os.listdir(path): #mime = mimetypes.guess_type(item) #print(item) try: with open(path + item, "rb") as my_file: data = my_file.read() m = mime.get_type(data) if m == 'application/epub+zip': self.info[item] = item md5 = hashlib.md5(data).hexdigest() if checksum.check(md5): elib.epub(path + item) elib.res["md5"] = md5 edit.new(path + item, elib.res) save.copy(path + item, Book(edit.lastid)) elib.epub_image(Book(edit.lastid)) #print (elib.res) #print (md5) else: pass #print (m) my_file.close() except Exception as e: self.info[item + " error"] = str(e)
def info(self, message): self.book = {} self.books = [] self.message = message db = Database() query = db.query("SELECT * FROM `book`", "").fetchall() # add items from db to dict for json output for item in query: book = Book(item['id']) self.book = { 'id': item['id'], 'title': item['title'], 'author': item['author'], 'date': item['date'], 'ISBN': item['ISBN'], 'ext': item['ext'], 'language': item['language'], 'image': book.image, 'md5': item["md5"] } self.books.append(self.book) self.json_prepared['books'] = self.books self.json_prepared['books'] = self.books return self.json_prepared
def add_book(module_short_title, book_isbn): """ Link a module and a book """ from lib.ModuleBook import ModuleBook module_ref = Module.get_by_key_name(module_short_title) book_ref = Book.get_by_key_name(book_isbn) new_module_book = ModuleBook(module=module_ref, book=book_ref) new_module_book.put()
def add_book(user_email, book_isbn, book_price, book_condition): """ Add a book to the datastore """ from lib.UserBook import UserBook user_ref = User.get_by_key_name(user_email) book_ref = Book.get_by_key_name(book_isbn) new_user_book = UserBook(user=user_ref, book=book_ref, price=book_price, condition=book_condition) new_user_book.put()
def remove_book(module_short_title, book_isbn): """ Remove a book from a module """ module_ref = Module.get_by_key_name(module_short_title) book_ref = Book.get_by_key_name(book_isbn) module_book_ref = db.GqlQuery("SELECT * FROM ModuleBook WHERE " +\ "module = :1 AND " +\ "book = :2", module_ref, book_ref) db.delete(module_book_ref)
def list_books(module_short_title): """ List all books associated with a module """ books = [] module_ref = Module.get_by_key_name(module_short_title) module_book_ref = db.GqlQuery("SELECT * FROM ModuleBook WHERE " +\ "module = :1", module_ref) for book_ref in module_book_ref.run(): book_key = ModuleBook.book.get_value_for_datastore(book_ref) book_res = Book.get(book_key) books.append(book_res) return books
def remove_book(user_email, book_isbn, book_price, book_condition): """ Remove a book from the datastore """ user_ref = User.get_by_key_name(user_email) book_ref = Book.get_by_key_name(book_isbn) user_book_ref = db.GqlQuery("SELECT * FROM UserBook WHERE " +\ "user = :1 AND " +\ "book = :2 AND " +\ "price = :3 AND " +\ "condition = :4", user_ref, book_ref, book_price, book_condition) db.delete(user_book_ref)
def book(self, id): db = Database() book = Book(id) try: os.remove(book.full_path) os.remove(book.image) db.query("DELETE FROM book WHERE id=:id", {"id": id}).fetchone() db.commit() self.error = "Success" except FileNotFoundError as e: self.error = str(e) + " Deleting from db anyways" db.query("DELETE FROM book WHERE id=:id", {"id": id}).fetchone() db.commit() try: os.rmdir(book.dir) except: pass
def image(self, id): book = Book(id) cherrypy.response.headers['Content-Type'] = "image/png" with open(book.image, "rb") as f: return f.read()
def download(self, id): book = Book(id) cherrypy.response.headers[ 'Content-Disposition'] = 'attachment;filename="' + book.real_name + '"' with open(book.full_path, "rb") as f: return f.read()