Implement project deletion

And fix all of the cascades on the database relationships

Fuck SQLAlchemy
L
This commit is contained in:
Drew DeVault 2020-04-02 13:14:35 -04:00
parent 2ec9808304
commit af43379a80
7 changed files with 42 additions and 23 deletions

View file

@ -122,3 +122,12 @@ def config_POST(owner, project_name):
return redirect(url_for("projects.summary_GET",
owner=current_user.canonical_name,
project_name=project.name))
@projects.route("/<owner>/<project_name>/delete", methods=["POST"])
@loginrequired
def delete_POST(owner, project_name):
owner, project = get_project(owner, project_name, ProjectAccess.write)
project.summary_repo_id = None
db.session.delete(project)
db.session.commit()
return redirect(url_for("public.index"))

View file

@ -186,7 +186,13 @@
</div>
</div>
<div class="event">
<form
class="event"
method="POST"
action="{{url_for("projects.delete_POST",
owner=owner.canonical_name, project_name=project.name)}}"
>
{{csrf_token()}}
<h3>Delete project</h3>
<p>
This will permanently delete your project,
@ -194,16 +200,12 @@
</p>
<div class="row">
<div class="col-md-3">
<a
href="#"
class="btn btn-danger btn-block"
>
<button class="btn btn-danger btn-block" type="submit">
Delete project {{icon('caret-right')}}
</a>
</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>

View file

@ -14,8 +14,11 @@ class Event(Base):
id = sa.Column(sa.Integer, primary_key=True)
created = sa.Column(sa.DateTime, nullable=False)
project_id = sa.Column(sa.Integer, sa.ForeignKey("project.id"), nullable=False)
project = sa.orm.relationship("Project", backref=sa.orm.backref("events"))
project_id = sa.Column(sa.Integer,
sa.ForeignKey("project.id", ondelete="CASCADE"),
nullable=False)
project = sa.orm.relationship("Project",
backref=sa.orm.backref("events", cascade="all, delete"))
"""The project implicated in this event"""
user_id = sa.Column(sa.Integer,

View file

@ -12,14 +12,14 @@ class MailingList(Base):
updated = sa.Column(sa.DateTime, nullable=False)
project_id = sa.Column(sa.Integer,
sa.ForeignKey("project.id"), nullable=False)
sa.ForeignKey("project.id", ondelete="CASCADE"), nullable=False)
project = sa.orm.relationship("Project",
backref=sa.orm.backref("mailing_lists"),
backref=sa.orm.backref("mailing_lists", cascade="all, delete"),
foreign_keys=[project_id])
# Note: in theory this may eventually be different from the project owner(?)
owner_id = sa.Column(sa.Integer,
sa.ForeignKey("user.id"), nullable=False)
sa.ForeignKey("user.id", ondelete="CASCADE"), nullable=False)
owner = sa.orm.relationship("User")
name = sa.Column(sa.Unicode(128), nullable=False)

View file

@ -9,8 +9,11 @@ class Project(Base):
created = sa.Column(sa.DateTime, nullable=False)
updated = sa.Column(sa.DateTime, nullable=False)
owner_id = sa.Column(sa.Integer, sa.ForeignKey("user.id"))
owner = sa.orm.relationship("User", backref=sa.orm.backref("projects"))
owner_id = sa.Column(sa.Integer,
sa.ForeignKey("user.id", ondelete="CASCADE"),
nullable=False)
owner = sa.orm.relationship("User",
backref=sa.orm.backref("projects", cascade="all, delete"))
name = sa.Column(sa.Unicode(128), nullable=False)
description = sa.Column(sa.Unicode(512), nullable=False)
@ -21,6 +24,8 @@ class Project(Base):
checklist_complete = sa.Column(sa.Boolean,
nullable=False, server_default='f')
summary_repo_id = sa.Column(sa.Integer, sa.ForeignKey("source_repo.id"))
summary_repo_id = sa.Column(sa.Integer,
sa.ForeignKey("source_repo.id", ondelete="CASCADE"))
summary_repo = sa.orm.relationship("SourceRepo",
foreign_keys=[summary_repo_id])
foreign_keys=[summary_repo_id],
cascade="all, delete")

View file

@ -19,14 +19,14 @@ class SourceRepo(Base):
updated = sa.Column(sa.DateTime, nullable=False)
project_id = sa.Column(sa.Integer,
sa.ForeignKey("project.id"), nullable=False)
sa.ForeignKey("project.id", ondelete="CASCADE"), nullable=False)
project = sa.orm.relationship("Project",
backref=sa.orm.backref("source_repos"),
backref=sa.orm.backref("source_repos", cascade="all, delete"),
foreign_keys=[project_id])
# Note: in theory this may eventually be different from the project owner(?)
owner_id = sa.Column(sa.Integer,
sa.ForeignKey("user.id"), nullable=False)
sa.ForeignKey("user.id", ondelete="CASCADE"), nullable=False)
owner = sa.orm.relationship("User")
name = sa.Column(sa.Unicode(128), nullable=False)

View file

@ -12,14 +12,14 @@ class Tracker(Base):
updated = sa.Column(sa.DateTime, nullable=False)
project_id = sa.Column(sa.Integer,
sa.ForeignKey("project.id"), nullable=False)
sa.ForeignKey("project.id", ondelete="CASCADE"), nullable=False)
project = sa.orm.relationship("Project",
backref=sa.orm.backref("trackers"),
backref=sa.orm.backref("trackers", cascade="all, delete"),
foreign_keys=[project_id])
# Note: in theory this may eventually be different from the project owner(?)
owner_id = sa.Column(sa.Integer,
sa.ForeignKey("user.id"), nullable=False)
sa.ForeignKey("user.id", ondelete="CASCADE"), nullable=False)
owner = sa.orm.relationship("User")
name = sa.Column(sa.Unicode(128), nullable=False)