Skip to content

Commit 4d5ba38

Browse files
committed
Keep row in the database when plan deleted
By doing this we ensure that the id is not reused.
1 parent cd173d3 commit 4d5ba38

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

app/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import os
44

5-
from flask import Flask, jsonify, redirect, render_template, session, url_for
5+
from flask import Flask, abort, jsonify, redirect, render_template, session, url_for
66
from flask_migrate import Migrate
77
from flask_sqlalchemy import SQLAlchemy
88
from flask_wtf import FlaskForm
@@ -110,18 +110,23 @@ def save(json=False):
110110
def plan():
111111
return redirect(url_for("plan_error"))
112112

113+
def get_active_plan_or_404(id):
114+
plan = Plan.query.get_or_404(id, description="This plan doesn't exist.")
115+
if plan.plan is None and plan.sql is None:
116+
abort(404, description="This plan has been deleted.")
117+
return plan
113118

114119
@app.route("/plan/<id>", methods=["GET"])
115120
def plan_from_db(id):
116-
plan = Plan.query.get_or_404(id, description="This plan doesn't exist.")
121+
plan = get_active_plan_or_404(id)
117122
if plan.password_hash is not None:
118123
return render_template("locked.html")
119124
return render_template("plan.html", plan=plan)
120125

121126

122127
@app.route("/plan/<id>", methods=["POST"])
123128
def plan_from_db_with_password(id):
124-
plan = Plan.query.get_or_404(id, description="This plan doesn't exist.")
129+
plan = get_active_plan_or_404(id)
125130
form = PasswordForm()
126131
form.validate_on_submit()
127132
if plan.password_hash is None or check_password_hash(
@@ -135,8 +140,11 @@ def plan_from_db_with_password(id):
135140
def delete(id, key):
136141
plan = Plan.query.get_or_404(id, description="This plan doesn't exist.")
137142
if plan.delete_key == key:
143+
plan.title = None
144+
plan.plan = None
145+
plan.sql = None
146+
plan.is_public = False
138147
session["deleted"] = id
139-
db.session.delete(plan)
140148
db.session.commit()
141149
return ("", 204)
142150

0 commit comments

Comments
 (0)