def ultimos_12_libros(request, params): bm = get_book_manager() cursor = bm.findBooks() if cursor.count() > 12: return { "books" : cursor[cursor.count()-12:], } else: return { "books" : cursor[:cursor.count()], }
def post(self, request, params): um = get_user_manager() bm = get_book_manager() # Comprobamos que el usuario esté logueado username = who_is_logged(self.session) if username is None: return HttpResponseFound(redirect_to="/user/login/") else: user = um.findOneUser({"username":username}) #Comprobamos que la página del libro sea correctas slug = params["book_slug"] book = bm.findOneBook({"slug":slug}) if book is None: raise HttpResponseNotFound("El libro con identificador %s no existe." % slug) if ('text' in request.data and not_empty(request.data["text"].value) and 'stars' in request.data and not_empty(request.data["stars"].value)): review = Review({ "user" : user, "book" : book, "text" : request.data["text"].value, "stars" : request.data["stars"].value, }) user = um.addReview(review) book = bm.addReview(review) return { "book": book, "user_logged": True, "msg": "Comentario guardado correctamente.", } return { "book": book, "user_logged": True, "review_form":review, "msg":"Algunos campos del comentario no son válidos.", }
def get(self, request, params): bm = get_book_manager() slug = params["book_slug"] book = bm.findOneBook({"slug":slug}) user = who_is_logged(self.session) if user is None: user_logged = False else: user_logged = True if book is not None: return { "book" : book, "user_logged" : user_logged, #"count" : self.session["count"], } else: raise HttpResponseNotFound("El libro con identificador %s no existe." % slug)
def post(self, request, params): user = who_is_logged(self.session) if user is None: return HttpResponseFound(redirect_to=("/user/login/")) bm = get_book_manager() if ('title' in request.data and 'author' in request.data and not_empty(request.data["title"].value) and not_empty(request.data["author"].value)): book = bm.createBook({ "title":request.data["title"].value, "author":request.data["author"].value, "slug":slugify(request.data["title"].value), }) # Exist this book? exist_book = bm.findOneBook({"slug":book["slug"]}) if exist_book is None: # Si el libro no existía guardamos la imagen config = ConfigRunner() media_root = config.media_root media_url = config.media_url #Si existe, guardamos imagen. if "cover" in request.data: file_name_server = book["slug"] + "." + request.data["cover"].filename.split(".")[-1] file = open(media_root + file_name_server, "wb") urlfile = media_url + file_name_server for line in request.data["cover"].file: file.write(line) file.close() book["urlfile"] = urlfile id_book = bm.insertBook(book) if id_book is not None: return { "book":book, "msg":"Creado de forma satisfactoria.", } else: return { "msg":"Algo falló, inténtelo de nuevo.", } else: return { "msg":"Ya existe un libro con el título '%s'." % request.data["title"].value, } else: return { "msg":"Falta alguno de los campos.", }