Skip to content

Commit

Permalink
Reorganized before and after request functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanmaster53 committed May 17, 2024
1 parent 2cee175 commit d382196
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
21 changes: 20 additions & 1 deletion pwnedhub/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, Blueprint
from flask import Flask, request, render_template, session, Blueprint, __version__
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from pwnedhub.utils import get_current_utc_time
Expand Down Expand Up @@ -46,6 +46,25 @@ def markdown_filter(data):
from markdown import markdown
return Markup(markdown(data or '', extensions=app.config['MARKDOWN_EXTENSIONS']))

@app.before_request
def render_mobile():
if any(x in request.user_agent.string.lower() for x in ['android', 'iphone', 'ipad']):
if not request.endpoint.startswith('static'):
return render_template('mobile.html')

@app.after_request
def add_header(response):
response.headers['X-Powered-By'] = 'Flask/{}'.format(__version__)
response.headers['X-XSS-Protection'] = '1; mode=block'
return response

@app.after_app_request
def restrict_flashes(response):
flashes = session.get('_flashes')
if flashes and len(flashes) > 5:
del session['_flashes'][0]
return response

StaticBlueprint = Blueprint('common', __name__, static_url_path='/static/common', static_folder='../common/static')
app.register_blueprint(StaticBlueprint)

Expand Down
8 changes: 7 additions & 1 deletion pwnedhub/views/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Blueprint, current_app, request, session, redirect, url_for, render_template, flash
from flask import Blueprint, current_app, request, g, session, redirect, url_for, render_template, flash
from pwnedhub import db
from pwnedhub.constants import QUESTIONS
from pwnedhub.decorators import validate
Expand All @@ -14,6 +14,12 @@

blp = Blueprint('auth', __name__)

@blp.before_app_request
def load_user():
g.user = None
if session.get('user_id'):
g.user = User.query.get(session.get('user_id'))

def create_welcome_message(user):
sender = User.query.get(1)
receiver = user
Expand Down
27 changes: 1 addition & 26 deletions pwnedhub/views/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Blueprint, current_app, request, session, g, redirect, url_for, render_template, flash, jsonify, Response, send_file, abort, __version__
from flask import Blueprint, current_app, request, session, g, redirect, url_for, render_template, flash, jsonify, Response, send_file, abort
from pwnedhub import db
from pwnedhub.constants import QUESTIONS, DEFAULT_NOTE, ADMIN_RESPONSE
from pwnedhub.decorators import login_required, roles_required, validate, csrf_protect
Expand All @@ -14,31 +14,6 @@

blp = Blueprint('core', __name__)

@blp.before_app_request
def render_mobile():
if any(x in request.user_agent.string.lower() for x in ['android', 'iphone', 'ipad']):
if not request.endpoint.startswith('static'):
return render_template('mobile.html')

@blp.before_app_request
def load_user():
g.user = None
if session.get('user_id'):
g.user = User.query.get(session.get('user_id'))

@blp.after_app_request
def add_header(response):
response.headers['X-Powered-By'] = 'Flask/{}'.format(__version__)
response.headers['X-XSS-Protection'] = '1; mode=block'
return response

@blp.after_app_request
def restrict_flashes(response):
flashes = session.get('_flashes')
if flashes and len(flashes) > 5:
del session['_flashes'][0]
return response

# general controllers

@blp.route('/')
Expand Down

0 comments on commit d382196

Please sign in to comment.