Skip to content

WebHost: allow deleting Rooms and Seeds, as well as their associated data #3071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion WebHostLib/autolauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import threading
import time
import typing
from uuid import UUID
from datetime import timedelta, datetime

from pony.orm import db_session, select, commit
Expand Down Expand Up @@ -62,6 +63,16 @@ def autohost(config: dict):
def keep_running():
try:
with Locker("autohost"):
# delete unowned user-content
with db_session:
# >>> bool(uuid.UUID(int=0))
# True
rooms = Room.select(lambda room: room.owner == UUID(int=0)).delete(bulk=True)
seeds = Seed.select(lambda seed: seed.owner == UUID(int=0) and not seed.rooms).delete(bulk=True)
slots = Slot.select(lambda slot: not slot.seed).delete(bulk=True)
# Command gets deleted by ponyorm Cascade Delete, as Room is Required
if rooms or seeds or slots:
logging.info(f"{rooms} Rooms, {seeds} Seeds and {slots} Slots have been deleted.")
run_guardian()
while 1:
time.sleep(0.1)
Expand Down Expand Up @@ -191,6 +202,6 @@ def guard():
guardian = threading.Thread(name="Guardian", target=guard)


from .models import Room, Generation, STATE_QUEUED, STATE_STARTED, STATE_ERROR, db, Seed
from .models import Room, Generation, STATE_QUEUED, STATE_STARTED, STATE_ERROR, db, Seed, Slot
from .customserver import run_server_process, get_static_server_data
from .generate import gen_game
4 changes: 4 additions & 0 deletions WebHostLib/templates/userContent.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h2>Your Rooms</h2>
<th class="center">Players</th>
<th>Created (UTC)</th>
<th>Last Activity (UTC)</th>
<th>Mark for deletion</th>
</tr>
</thead>
<tbody>
Expand All @@ -35,6 +36,7 @@ <h2>Your Rooms</h2>
<td>{{ room.seed.slots|length }}</td>
<td>{{ room.creation_time.strftime("%Y-%m-%d %H:%M") }}</td>
<td>{{ room.last_activity.strftime("%Y-%m-%d %H:%M") }}</td>
<td><a href="{{ url_for("disown_room", room=room.id) }}">Delete next maintenance.</td>
</tr>
{% endfor %}
</tbody>
Expand All @@ -51,6 +53,7 @@ <h2>Your Seeds</h2>
<th>Seed</th>
<th class="center">Players</th>
<th>Created (UTC)</th>
<th>Mark for deletion</th>
</tr>
</thead>
<tbody>
Expand All @@ -60,6 +63,7 @@ <h2>Your Seeds</h2>
<td>{% if seed.multidata %}{{ seed.slots|length }}{% else %}1{% endif %}
</td>
<td>{{ seed.creation_time.strftime("%Y-%m-%d %H:%M") }}</td>
<td><a href="{{ url_for("disown_seed", seed=seed.id) }}">Delete next maintenance.</td>
</tr>
{% endfor %}
</tbody>
Expand Down
28 changes: 27 additions & 1 deletion WebHostLib/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import zlib

from io import BytesIO
from flask import request, flash, redirect, url_for, session, render_template
from flask import request, flash, redirect, url_for, session, render_template, abort
from markupsafe import Markup
from pony.orm import commit, flush, select, rollback
from pony.orm.core import TransactionIntegrityError
Expand Down Expand Up @@ -219,3 +219,29 @@ def user_content():
rooms = select(room for room in Room if room.owner == session["_id"])
seeds = select(seed for seed in Seed if seed.owner == session["_id"])
return render_template("userContent.html", rooms=rooms, seeds=seeds)


@app.route("/disown_seed/<suuid:seed>", methods=["GET"])
def disown_seed(seed):
seed = Seed.get(id=seed)
if not seed:
return abort(404)
if seed.owner != session["_id"]:
return abort(403)

seed.owner = 0

return redirect(url_for("user_content"))


@app.route("/disown_room/<suuid:room>", methods=["GET"])
def disown_room(room):
room = Room.get(id=room)
if not room:
return abort(404)
if room.owner != session["_id"]:
return abort(403)

room.owner = 0

return redirect(url_for("user_content"))