def load_user(user_id): if user_id: key = 'user_{}'.format(user_id) r = cache.get(key) if not r: r = UserGateway().get_by_user_id(user_id) cache.set(key, r, timeout=3600) return r return None
def authorize_callback(): '''Handles the authorization response Returns: a redirection to the previous page, if the user logs in otherwise directs them to a signup page ''' headers = ['{}: {}'.format(k, v) for k, v in request.headers.items()] msg = 'received request with\n{}'.format('\n'.join(headers)) current_app.logger.debug(msg) resp = handle_authorize_response() if not resp: msg = 'Did not receive an authorization response' current_app.logger.debug(msg) return redirect(url_for('authbp.authorize_info')) msg = 'Received authorization response' current_app.logger.debug(msg) try: payload = verify_token(resp['access_token']) except BenwaOnlineError as err: msg = 'Error occured during token verification: {}'.format(err) current_app.logger.debug(msg) else: session['access_token'] = resp['access_token'] session['refresh_token'] = resp['refresh_token'] msg = 'Checking if user has signed up before' current_app.logger.debug(msg) user = UserGateway().get_by_user_id(payload['sub']) if not user: msg = 'New user. Redirecting to signup.' current_app.logger.debug(msg) return redirect(url_for('authbp.signup')) cache.set('user_{}'.format(user.user_id), user) login_user(user) msg = 'User {}: logged in'.format(user.user_id) current_app.logger.info(msg) return back.redirect()
def unlike_post(self, post_id, access_token): r = UserGateway().unlike_post(self, post_id, access_token) if r.status_code == 200: self.likes.remove(str(post_id)) key = 'user_{}'.format(self.user_id) s = cache.set(key, self, timeout=3600) return r
def show_posts(tags='all'): ''' Show all posts that match a given tag filter. Shows all posts by default. ''' post_fields = {'posts': ['title', 'created_on', 'preview']} if tags == 'all': posts = cache.get('all_posts') if not posts: posts = PostGateway().get(include=['preview'], fields=post_fields, page_size=0) cache.set('all_posts', posts, timeout=3600) else: tags = tags.split('+') posts = PostGateway().tagged_with(tags, include=['preview'], fields=post_fields, page_size=0) posts.sort(key=lambda post: post.created_on, reverse=True) tags = all_tags() return render_template('gallery.html', posts=posts, tags=tags)