def index(self): """ Show users"s books """ books = Book.list(cherrypy.request.sa, self.get_logged_in_user().user_id) return self.wrap_template_params({"books": books})
def pages(self, book_id): """ Pages of given book :param book_id: :return: """ book = Book.get(cherrypy.request.sa, book_id) if book_id else None return self.wrap_template_params({"book": book})
def book(self, book_id=0, **params): """ CRUD for Book model """ account = self.get_logged_in_user() session = cherrypy.request.sa if book_id: book = Book.get(session, book_id) if book.user_id != account.user_id: raise cherrypy.HTTPError(401, "Unauthorized") else: book = Book(account.user_id) if cherrypy.request.method == "POST" and "cmd_delete" not in params: # Create or update the Book book.title = params["title"] book.author = params["author"] book.description = params["description"] book.enabled = ("enabled" in params) if not book.book_id: session.add(book) session.flush() cherrypy.engine.publish("elastic-save", book) raise cherrypy.HTTPRedirect("?book_id=%s" % book.book_id) if cherrypy.request.method == "POST" and "cmd_delete" in params: # delete the Book # TODO Delete parts, images etc. session.delete(book) cherrypy.engine.publish("elastic-drop", book) raise cherrypy.HTTPRedirect(".") return self.wrap_template_params({"book": book})
def page(self, page_id): """ Фрагменты страницы :param page_id: :return: """ session = cherrypy.request.sa page = Page.get(session, page_id) book = Book.get(session, page.book_id) return self.wrap_template_params({"book": book, "page": page})
def GET(self, book_id, has_to_be_enabled=True, **params): r = {} if book_id: if has_to_be_enabled: # load only enabled data book = Book.get(cherrypy.request.sa, book_id) if book.enabled: r = ModelSerializer.dict(book) if "load_complete" in params and book.pages: r["pages"] = [] for i in book.pages: if i.enabled: r["pages"].append(ModelSerializer.dict(i)) else: # load everything book = Book.get(cherrypy.request.sa, book_id) r = ModelSerializer.dict(book) if "load_complete" in params and book.pages: r["pages"] = [] for i in book.pages: r["pages"].append(ModelSerializer.dict(i)) return r
def book(self, book_id, page_id=0): """ Обрабатываем и @book и @page, т.к. см. выше """ session = cherrypy.request.sa book = Book.get(session, book_id) page = Page.get(session, page_id) if page_id else None # parts = Part.get(session, page_id) if page else None # parts передаются через REST, поскольку координаты, являющиеся частью # этих записей, зависимы от мастаба окна клиента. return self.wrap_template_params({ "book": book, "page": page })
def upload(self, book_id): book = Book.get(cherrypy.request.sa, book_id) if book_id else None return self.wrap_template_params({"book": book})
def index(self): session = cherrypy.request.sa books = Book.list(session, enabled=True) return self.wrap_template_params({ "books": books })