def _is_isbn(self, text): """ Determine whether the user's input is a valid isbn number. """ try: utils.validate_isbn(text) except Exception: # not a isbn return False else: return True
def post(self): """ Handling requests for editing data. """ email = auth.get_email_from_cookies(self.request.cookies) self.user = auth.user.User.get_by_email(email) if not self.user: self.redirect('/login') return self.isbn = self.request.path.split('/book/')[1] try: utils.validate_isbn(self.isbn) except Exception: msg = "Invalid ISBN: " + self.isbn params = {'msg': msg} self.redirect('/error?' + urllib.urlencode(params)) return if not self.user.is_douban_connected(): self.redirect('/auth/douban') return edit_type = self.request.get('type') # the booklists this book was previously in from_lists = [bl for bl in BookList.get_all_booklists(self.user) if self.isbn in bl.isbns()] if edit_type == 'booklist': self._edit_booklist(from_lists) self._finish_editing() return self.edited = False if edit_type == 'rating': self._edit_rating() elif edit_type == 'comment': self._edit_comment() elif edit_type == 'tags': self._edit_tags() elif edit_type == 'tongji': self._edit_tongji() if self.edited: if from_lists: # already in some lists, now edited, sync to douban, edit self._sync_edit("PUT") else: # previously not in any list, now edited, add it to Done List done_list = BookList.get_or_create(self.user, books.booklist.LIST_DONE) done_list.add_isbn(self.isbn, front=True) # sync to douban, add self._sync_edit("POST") self._finish_editing()
def get(self): email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) if not user: self.redirect('/login') return isbn = self.request.path.split('/book/')[1] try: utils.validate_isbn(isbn) except Exception: msg = "Invalid ISBN: " + isbn params = {'msg': msg} self.redirect('/error?' + urllib.urlencode(params)) return template = utils.get_jinja_env().get_template('onebook.html') context = { 'user': user } reload = self.request.get('reload') == 'True' if reload: # force to reload from douban.. try: full = self._load(user, isbn, reload=True, tjlib=True) except Exception as err: params = {'msg': err} self.redirect('/error?%s' % urllib.urlencode(params)) return else: full = self._load(user, isbn, reload=False) if full.is_empty(): # no such data in local datastore, try fetch try: full = self._load(user, isbn, reload=True, tjlib=True) except Exception as err: params = {'msg': err} self.redirect('/error?%s' % urllib.urlencode(params)) return context['title'] = full.book.title context['book'] = full.book context['booklist_name'] = full.booklist_name context['rating'] = full.rating context['tags'] = full.tags context['comment'] = full.comment self.response.out.write(template.render(context)) return
def isbn(request): from utils import search_by_isbn, validate_isbn, convert_to_13 from search.models import get_offers, get_auctions if request.method == "POST": isbn = request.POST.get("search_input","0") isbn = isbn.lstrip() isbn = isbn.rstrip() if validate_isbn(isbn=isbn): isbn = re.sub("[^0-9Xx]", "", isbn) isbn = convert_to_13(isbn=isbn) result = search_by_isbn(query=isbn) if result["books"]: result["query"] = isbn result["search_length"] = len(result["books"]) for book in result["books"]: offer = sorted(get_offers(book["isbn"]), key=(lambda x:x["buy_price"])) if offer: book["min_offer"] = offer[0]["buy_price"] auction = sorted(get_auctions(book["isbn"]), key=(lambda y:y["current_price"])) if auction: book["min_auction"] = auction[0]["current_price"] return render(request, 'search_results.html', result) else: return render(request, 'search_empty.html', {"query": isbn}) else: return render(request, 'search_empty.html', {"query": isbn}) return render(request, 'error_page.html')
def isbn(request): from utils import search_by_isbn, validate_isbn, convert_to_13 from search.models import get_offers, get_auctions if request.method == "POST": isbn = request.POST.get("search_input", "0") isbn = isbn.lstrip() isbn = isbn.rstrip() if validate_isbn(isbn=isbn): isbn = re.sub("[^0-9Xx]", "", isbn) isbn = convert_to_13(isbn=isbn) result = search_by_isbn(query=isbn) if result["books"]: result["query"] = isbn result["search_length"] = len(result["books"]) for book in result["books"]: offer = sorted(get_offers(book["isbn"]), key=(lambda x: x["buy_price"])) if offer: book["min_offer"] = offer[0]["buy_price"] auction = sorted(get_auctions(book["isbn"]), key=(lambda y: y["current_price"])) if auction: book["min_auction"] = auction[0]["current_price"] return render(request, 'search_results.html', result) else: return render(request, 'search_empty.html', {"query": isbn}) else: return render(request, 'search_empty.html', {"query": isbn}) return render(request, 'error_page.html')