def auth_login_oauth(): # OAuth code code = request.args.get('code') # State identifies provider try: state = b64_to_json(request.args.get('state')) except: return abort(400) provider = state.get('provider') if state.get('target_client', False): auth_key = auth.set_user_oauth(code, provider=provider, client_side=True) if isinstance(auth_key, str): return render_template('client_oauth/success.html', auth_key=auth_key) elif auth_key is None: return render_template('client_oauth/failed.html') else: # In this case auth_key is errors return auth_key else: oauth_errors = auth.set_user_oauth(code, provider=provider, client_side=False) if oauth_errors is not None: return oauth_errors else: return redirect(state.get('redirect', url_for('home')), 303)
def get_user(user_id, name): matched_user = User.query.filter_by(id=user_id).first() if matched_user is None: return abort(404) return render_template('user.html', user=matched_user)
def codepage(encoding): normalized_encoding = codepage_controller.get_normalized_encoding(encoding) if normalized_encoding != encoding and request.args.get('noredirect', '0') != '1': return redirect(url_for('codepage', encoding=normalized_encoding, noredirect=1), code=301) if normalized_encoding == 'utf-8': return redirect('https://en.wikipedia.org/wiki/UTF-8', code=303) if normalized_encoding == 'utf-16': return redirect('https://en.wikipedia.org/wiki/UTF-16', code=303) raw_codepage = codepage_controller.get_codepage(normalized_encoding) if not raw_codepage: return abort(404) # Format into a 2D table mapped_codepage = [[None] * 16 for _ in range(16)] for self_codepoint, unicode_codepoint in raw_codepage.items(): mapped_codepage[self_codepoint // 16][self_codepoint % 16] = (chr(unicode_codepoint), unicode_codepoint) return render_template('codepage.html', encoding=normalized_encoding, codepage=mapped_codepage)
def get_user(user_id, name): matched_user = User.query.filter_by(id=user_id, deleted=False).first() if matched_user is None: return abort(404) # Redirect if name is incorrect. add 'noredirect=1' flag to avoid infinite redirection in # exceptional circumstances if name != matched_user.name and request.args.get('noredirect', '0') != '1': return redirect(url_for('get_user', user_id=user_id, name=matched_user.name, **request.args, noredirect='1'), code=301) stackexchange_login = UserAuthToken.\ query.\ filter_by(user_id=user_id, issuer='stackexchange.com').\ order_by(UserAuthToken.id.desc()).\ first() if matched_user.linked_stackexchange_public else None return render_template('user.html', user=matched_user, stackexchange_login=stackexchange_login)
def get_post(post_id, title=""): # Locate post matched_post = post.get_post(post_id=post_id) if matched_post is None: return abort(404) # Render main post's markdown body = markdown.render_markdown.delay(matched_post.body).wait() if body is None: return abort(500) # Get answers try: page = int(request.args.get('p', 1)) except ValueError: return abort(400) answers = answer_controller.get_answers(post_id=post_id, page=page) leaderboard = Leaderboard(post_id=post_id) # Get respective comments answer_comments = [] for answer in answers.items: answer_comments.append(get_rendered_comments(AnswerComment, max_depth=2, answer_id=answer.id)) post_comments = get_rendered_comments(PostComment, max_depth=2, post_id=post_id) return \ render_template('post/view.html', post_id=post_id, post=matched_post, post_body=body, answers=answers, leaderboard=leaderboard, vote=vote, answer_comments=answer_comments, post_comments=post_comments)
def get_post_preview(id): # Make sure valid preview ID. # These numbers should be in-sync with JS if not match('[0-9a-f]{32}:[0-9a-f]{16}', id): return abort(404) return render_template('post/preview.html', id=id)
def auth_login_oauth(): # OAuth code code = request.args.get('code') # State identifies provider try: state = b64_to_json(request.args.get('state')) except: return abort(400) provider = state.get('provider') auth_opts = state.get('authConfig', {}) is_client_flow = state.get('clientOnly', False) is_append_flow = auth_opts.get('append', False) oauth_result = auth.set_user_oauth(code, provider=provider, client_side=is_client_flow, auth_opts=auth_opts) if state.get('clientOnly', False): auth_key = oauth_result # Seperate behavior for append flow if is_append_flow: if auth_key is None: return render_template('client_oauth/success.html', auth_key=auth_key) else: if isinstance(oauth_result, tuple): response = oauth_result[0] error_message = response.get_json().get('message', '') else: error_message = 'unknown error' return render_template('client_oauth/failed.html', error_message=error_message) if isinstance(auth_key, str): return render_template('client_oauth/success.html', auth_key=auth_key) elif auth_key is None: return render_template('client_oauth/failed.html') else: # In this case auth_key is errors return auth_key else: oauth_errors = oauth_result if oauth_errors is not None: return oauth_errors else: return redirect(state.get('redirect', url_for('home')), 303)
def get_post(post_id, title=None): # Locate post matched_post = post.get_post(post_id=post_id) if matched_post is None: return abort(404) if matched_post.deleted: return render_template('deleted.html'), 410 # Always redirect to canonical url slug = slugify(matched_post.title) # Redirect if slug is incorrect. add 'noredirect=1' flag to avoid infinite redirection in # exceptional circumstances if title != slug and request.args.get('noredirect', '0') != '1': canonical_url = url_for('get_post', p=request.args.get('p'), post_id=post_id, title=slug, noredirect=1) return redirect(canonical_url, code=301) # Render main post's markdown body = markdown.render_markdown.delay(matched_post.body).wait() if body is None: return abort(500) # Get answers try: page = int(request.args.get('p', 1)) except ValueError: return abort(400) answers = answer_controller.get_answers(post_id=post_id, page=page) leaderboard = Leaderboard(post_id=post_id) # Get respective comments answer_comments = [get_rendered_comments(AnswerComment, max_depth=2, answer_id=answer.id) for answer in answers.items] post_comments = get_rendered_comments(PostComment, max_depth=2, post_id=post_id) return \ render_template('post/view.html', post_id=post_id, post=matched_post, post_body=body, answers=answers, leaderboard=leaderboard, vote=vote, answer_comments=answer_comments, post_comments=post_comments)
def get_posts(): try: page = int(request.args.get('p', 1)) except ValueError: return abort(400) posts = post.get_posts(page=page) if len(posts.items) == 0: return abort(404) return render_template('posts.html', posts=posts)
def help_page(path): # Get the human-readable path breadcrumbs = help_center.get_breadcrumbs(path) if breadcrumbs is None: return abort(404) section_name, doc_name = breadcrumbs html, frontmatter_params, headings = help_center.render_doc_path( path, breadcrumbs=breadcrumbs) caption = frontmatter_params.get('caption') return render_template( 'docpage.html', **{ 'doc_structure': help_center.DOC_STRUCTURE, 'path_name': path, 'section_name': section_name, 'doc_name': doc_name, 'headings': headings, 'caption': caption, 'html': html })
def get_post(post_id): # Locate post matched_post = post.get_post(post_id=post_id) if matched_post is None: return abort(404) # Render main post's markdown body = markdown.render_markdown.delay(matched_post.body).wait() if body is None: return abort(500) # Get answers try: page = int(request.args.get('p', 1)) except ValueError: return abort(400) answers = answer.get_answers(post_id=post_id, page=page) return render_template('post/view.html', post=matched_post, post_body=body, answers=answers)
def write_post(): if g.user is None: return redirect(url_for('home')) return render_template('post/write.html')
def admin_homepage(): return render_template('admin/index.html')
def categories(): return render_template('categories.html')
def error_400(e): return render_template('badrequest.html'), 400
def home(): return render_template('index.html')
def error_500(e): return render_template('servererror.html'), 500
def error_404(e): return render_template('notfound.html'), 404
def duplicate_users(): return render_template('admin/duplicate_users.html', users_data=admin_controller.get_duplicate_users())
def help(): return render_template('help.html')
def profile_settings(): if g.user is None: do_redirect() return render_template('settings/profile.html')