示例#1
0
 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})
示例#2
0
 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})
示例#3
0
    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})
示例#4
0
 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})
示例#5
0
 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
示例#6
0
 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
     })
示例#7
0
 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})
示例#8
0
 def index(self):
     session = cherrypy.request.sa
     books = Book.list(session, enabled=True)
     return self.wrap_template_params({
         "books": books
     })