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 template = utils.get_jinja_env().get_template('tags.html') context = {'user': user} helper = books.TagHelper(user) tag_name = self.request.get('t') if tag_name: # a specific tag to display isbns = helper.isbns(tag_name) if isbns is None: params = { 'msg': 'Invalid tag name: "' + codecs.encode(tag_name, 'utf-8') + '"' } self.redirect('/error?' + urllib.urlencode(params)) return self._render_tag_books(user, tag_name, helper, context) else: self._render_tags_info(user, helper, context) self.response.out.write(template.render(context)) return
def get(self): email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) template = utils.get_jinja_env().get_template('404.html') context = { 'user': user } self.response.out.write(template.render(context))
def get(self): email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) msg = self.request.get("msg") template = utils.get_jinja_env().get_template("error.html") context = { 'msg': msg, 'user': user } self.response.out.write(template.render(context))
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 post(self): """ Currently, only disconnecting from douban. """ email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) if not user: self.redirect('/login') return if user.is_douban_connected(): user.disconnect_from_douban() self.redirect('/me') return
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 template = utils.get_jinja_env().get_template('guess.html') context = {'user': user} self._fill(context, user) self.response.out.write(template.render(context)) return
def get(self): action = self.request.get('action') if action == 'clear': # clear all data in db msg = self._clear() user = None elif action == 'load': email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) if not user: self.redirect('/login') return # load books from douban # these books are for defense only.. ids = ["20381804", "10555435", "1432683"] for douban_id in ids: if Book.get_by_douban_id(douban_id) is None: # load it! b = douban.get_book_all_by_id(douban_id, user) b.merge_into_datastore(user) msg = "DONE" else: # default case email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) if not user: self.redirect('/login') return msg = self._test(user) template = utils.get_jinja_env().get_template('test.html') context = { 'user': user, 'msg': msg } self.response.out.write(template.render(context)) return
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 get(self): email = auth.get_email_from_cookies(self.request.cookies) self.user = auth.User.get_by_email(email) if not self.user: self.redirect('/login') return book_id = utils.random_book_id() b = books.book.Book.get_by_douban_id(book_id) if b: self.redirect('/book/%s' % b.isbn) return if self.user.is_douban_connected(): self._try_fetch_render(book_id) else: self.redirect('/auth/douban') return
def post(self): """ Post method is used when user wants to import from douban or to refresh Tongji Library info. """ email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) if not user: self.redirect('/login') return if not user.is_douban_connected(): # goto the oauth2 of douban self.redirect('/auth/douban') return action = self.request.get('type') if action == 'import': # import from douban self._import_async(user) params = {'import_started': True} self.redirect(self.request.path + '?' + urllib.urlencode(params)) elif action == 'douban': # refresh each book's public information from douban bl = booklist.BookList.get_or_create(user, self.list_type) for isbn in bl.isbns(): t = taskqueue.Task(url='/workers/douban', params={'isbn': isbn}) t.add(queue_name="douban") self.redirect(self.request.path) elif action == 'tongji': # refresh each book's status in tj library bl = booklist.BookList.get_or_create(user, self.list_type) for isbn in bl.isbns(): t = taskqueue.Task(url='/workers/tongji', params={'isbn': isbn}) t.add(queue_name="tongji") self.redirect(self.request.path) else: self.redirect(self.request.path) return
def post(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 if not user.is_douban_connected(): self.redirect('/auth/douban') return data = codecs.decode(self.request.get('file'), 'utf-8') # get a list of (douban_id, tags) id_tags = local.parse(data) deferred.defer(_upload_worker, user.key(), id_tags) params = { 'msg': 'Upload finished, parsing & syncing now.' } self.redirect('/upload?' + urllib.urlencode(params)) return
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 template = utils.get_jinja_env().get_template('whatsnext.html') ctx = {'user': user} bl = booklist.BookList.get_or_create(user, booklist.LIST_INTERESTED) if bl.size() == 0: # no books in interested list ctx['no_books'] = True else: # recommend next ctx['no_books'] = False self._prepare_next(ctx, user, bl) self.response.out.write(template.render(ctx)) return
def get(self): email = auth.get_email_from_cookies(self.request.cookies) user = User.get_by_email(email) if not user: # needs to be logged in first! self.redirect('/login') return auth_code = self.request.get('code') auth_error = self.request.get('error') if auth_code: # douban user has agreed to authenticate, authorization_code provided. base_url, params = self._prepare_access_token_url(auth_code) try: page = urllib2.urlopen(base_url, urllib.urlencode(params)) except urllib2.HTTPError: msg = "HTTP error when trying to get the access token from douban, auth_code: %s" % auth_code self.redirect('/error?' + urllib.urlencode({'msg': msg})) else: obj = json.loads(page.read()) user.douban_access_token = obj.get('access_token') user.douban_refresh_token = obj.get('refresh_token') user.douban_id = obj.get('douban_user_id') obj = get_my_info(user.douban_access_token) user.add_info_from_douban(obj) user.put() self.redirect('/me') elif auth_error: # douban user disagreed to authenticate, error message provided. msg = "Please click Agree to bind your Douban account! Auth error: %s" % auth_error self.redirect('/error?' + urllib.urlencode({'msg': msg})) else: # To start OAuth2 authentication or has fully finished. if user.is_douban_connected(): self.redirect('/me') else: self.redirect(self._prepare_authorization_code_url())
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 keywords = self.request.get('q') if self._is_isbn(keywords): # show that book self.redirect('/book/' + keywords) return if self._is_tag(keywords, user): # show the books of that tag self.redirect('/tags?t=' + keywords) return msg = "Invalid search words: " + keywords params = {'msg': msg} self.redirect('/error?' + urllib.urlencode(params)) return
def get(self): """ Get method: Ask for data for a particular booklist. """ email = auth.get_email_from_cookies(self.request.cookies) user = auth.user.User.get_by_email(email) if not user: self.redirect('/login') return template = utils.get_jinja_env().get_template("booklist.html") bl = booklist.BookList.get_or_create(user, self.list_type) context = { 'user': user, 'title': self.title, 'active_nav': self.active_nav, 'booklist': bl } import_started = self.request.get('import_started') if import_started: # an async Task has just been added to import from douban context['import_started'] = True start_str = self.request.get('start') if start_str: try: start = int(start_str) - 1 if start < 0: start = 0 except Exception: start = 0 else: start = 0 # there are 4 types now: # time, rating, voted, pages sort_by = self.request.get('sortby') if not sort_by: sort_by = 'time' context['sortby'] = sort_by if sort_by == 'time': bookbriefs = self._prepare_by_time(user, bl, start) elif sort_by == 'public_rating': bookbriefs = self._prepare_by_public_rating(user, bl, start) elif sort_by == 'user_rating': bookbriefs = self._prepare_by_user_rating(user, bl, start) elif sort_by == 'rated_amount': bookbriefs = self._prepare_by_rated_amount(user, bl, start) elif sort_by == 'pages': bookbriefs = self._prepare_by_pages(user, bl, start) else: bookbriefs = None if bookbriefs: context['bookbriefs'] = bookbriefs context['start'] = start + 1 context['end'] = len(bookbriefs) + start - 1 + 1 context['prev_url'] = self._prepare_prev_url(start, sort_by) context['next_url'] = self._prepare_next_url(start, bl.size(), sort_by) self.response.out.write(template.render(context)) return