Skip to content

Commit

Permalink
Remove recent links to make the app stateless
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasha Radchenko committed Jan 19, 2022
1 parent 2bc7235 commit 2ecea21
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 70 deletions.
64 changes: 9 additions & 55 deletions autopsy_app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def page_forbidden(e) -> render_template
"""
import datetime
from flask import abort, flash
from flask import redirect, render_template, request, session, url_for
from flask import redirect, render_template, request, url_for
from flask_login import login_user, logout_user, current_user, login_required
from autopsy_app import app
from autopsy_app.admin import adm
Expand All @@ -40,8 +40,6 @@ def dashboard():
"""
Show a Autopsy dashboard with Recently added and Random Postmortems
"""
# Getting recent urls
urls = session['recent_urls']
# Getting recent postmortems
recent_mortems = Mortem.query.order_by(Mortem.mortem_created.desc()).\
limit(3).all()
Expand All @@ -55,8 +53,7 @@ def dashboard():
rnd_mortem = None
return render_template('dashboard.html',
mortems=recent_mortems,
rnd_mortem=rnd_mortem,
recent_urls=urls)
rnd_mortem=rnd_mortem)


@app.route('/reset', methods=['GET', 'POST'])
Expand Down Expand Up @@ -114,8 +111,6 @@ def login():
if current_user.is_authenticated:
return redirect("/")
form = LoginForm()
# Get recent URLs
session['recent_urls'] = []
if request.method == "POST":
if form.validate_on_submit():
user = User.query.filter_by(user_email=form.email.data).first()
Expand All @@ -132,28 +127,6 @@ def login():
return render_template('login.html', form=form, now=now)


@app.before_request
def get_visted_urls():
urls = session['recent_urls']

# URLs filtering
# Only postmortems are passed
if '/postmortems/' not in request.path:
return
# Exclude service's endpoints
known_urls = [f"/postmortems/{x}" for x in ['add', 'update']]
if request.path in known_urls:
return
# Do not update duplicates
if True in list(map(lambda i: request.path in i.keys(), urls)):
return
recent_page = {request.path: request.url}
urls.append(recent_page)
if len(urls) > 3:
urls.pop(0)
session.modified = True


@app.route('/register', methods=['GET', 'POST'])
def register():
"""
Expand Down Expand Up @@ -194,8 +167,6 @@ def profile():
"""
Show profile page to change name, password, avatar
"""
# Getting recent urls
urls = session['recent_urls']
form = ProfileForm()
if form.validate_on_submit():
hashed_pw = generate_password(form.password.data)
Expand All @@ -206,7 +177,7 @@ def profile():
db.session.commit()
flash('An account has been updated', 'success')
return redirect(url_for('profile'))
return render_template('profile.html', form=form, recent_urls=urls)
return render_template('profile.html', form=form)


@app.route('/postmortems')
Expand All @@ -215,17 +186,14 @@ def postmortems():
"""
Show postmortems list
"""
# Getting recent urls
urls = session['recent_urls']
MORTEMS_PER_PAGE = 7
page = request.args.get('page', type=int, default=1)
mortems = Mortem.query.order_by(
Mortem.id.desc()).paginate(page=page,
per_page=MORTEMS_PER_PAGE,
error_out=False)
return render_template('postmortems.html',
mortems=mortems,
recent_urls=urls)
mortems=mortems)


@app.route('/postmortems/add', methods=['GET', 'POST'])
Expand All @@ -234,8 +202,6 @@ def add_postmortem():
"""
Add new postmortem
"""
# Getting recent urls
urls = session['recent_urls']
form = PostmortemForm()
if form.validate_on_submit():
title = form.title.data
Expand All @@ -254,7 +220,7 @@ def add_postmortem():
db.session.commit()
flash('The mortem has been added', 'success')
return redirect(url_for('add_postmortem'))
return render_template('add_postmortem.html', form=form, recent_urls=urls)
return render_template('add_postmortem.html', form=form)


@app.route('/postmortems/<url>')
Expand All @@ -263,13 +229,10 @@ def get_postmortem(url):
"""
Show paricular postmortem details
"""
# Getting recent urls
urls = session['recent_urls']
mortem = Mortem.query.filter_by(mortem_url=url).first()
mortem.mortem_tags = get_tags(mortem.mortem_tags)
return render_template('get_postmortem.html',
mortem=mortem,
recent_urls=urls)
mortem=mortem)


@app.route('/postmortems/<url>/update', methods=['GET', 'POST'])
Expand All @@ -278,8 +241,6 @@ def update_postmortem(url):
"""
Update paricular postmortem details
"""
# Getting recent urls
urls = session['recent_urls']
mortem = Mortem.query.filter_by(mortem_url=url).first()
if mortem.author != current_user:
abort(403)
Expand All @@ -301,8 +262,7 @@ def update_postmortem(url):
form.mortem.data = mortem.mortem_content
return render_template('update_postmortem.html',
mortem=mortem,
form=form,
recent_urls=urls)
form=form)


@app.route('/search', methods=['GET', 'POST'])
Expand All @@ -311,9 +271,6 @@ def search():
"""
Perform search per Postmortems
"""
# Getting recent urls
urls = session['recent_urls']

def _get_results(q):
MORTEMS_PER_PAGE = 3
page = request.args.get('page', type=int, default=1)
Expand All @@ -335,8 +292,7 @@ def _get_results(q):
mortems = None
return render_template('search.html',
mortems=mortems,
query=query,
recent_urls=urls)
query=query)


@app.route('/support', methods=['GET', 'POST'])
Expand All @@ -345,8 +301,6 @@ def support():
"""
Create a support case (bug,error,issue)
"""
# Getting recent urls
urls = session['recent_urls']
form = SupportForm()
if form.validate_on_submit():
subject = form.subject.data
Expand All @@ -368,7 +322,7 @@ def support():
db.session.commit()
flash('The Support Case has been created', 'warning')
return redirect(url_for('support'))
return render_template('support.html', form=form, recent_urls=urls)
return render_template('support.html', form=form)


@app.route('/healthz')
Expand Down
15 changes: 0 additions & 15 deletions autopsy_app/templates/base/top_layer.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,6 @@ <h6 class="sidebar-heading d-flex justify-content-between align-items-center mt-
</li>
</ul>

<h6 class="sidebar-heading d-flex justify-content-between align-items-center mt-4 mb-1 text-muted">
<span>Recent pages</span>
</h6>
<ul class="nav flex-column mb-2">
{% for urls in recent_urls %}
{% for page, url in urls.items() %}
<li class="nav-item">
<a class="nav-link" href="{{ url }}">
<span data-feather="compass"></span>
<span> {{ page | truncate(30, True) }} </span>
</a>
</li>
{% endfor %}
{% endfor %}
</ul>
</div>
</nav>
{% endblock menu %}
Expand Down

0 comments on commit 2ecea21

Please sign in to comment.