Skip to content

Commit

Permalink
Improve suspension date handling (#1398)
Browse files Browse the repository at this point in the history
- Use native date picker for release date
- Replace use of server-time-zone-dependent
`weasyl.define.convert_unixdate` with consistent UTC 00:00
  • Loading branch information
charmander authored Mar 18, 2024
1 parent 1145144 commit ab64dd9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 48 deletions.
2 changes: 0 additions & 2 deletions assets/scss/mod.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@import 'components/date-picker';

.report-status-review {
color: #e0a000;
}
Expand Down
2 changes: 1 addition & 1 deletion weasyl/controllers/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def modcontrol_suspenduser_get_(request):
@moderator_only
@token_checked
def modcontrol_suspenduser_post_(request):
form = request.web_input(userid="", username="", mode="", reason="", day="", month="", year="", datetype="",
form = request.web_input(userid="", username="", mode="", reason="", datetype="",
duration="", durationunit="")

moderation.setusermode(request.userid, form)
Expand Down
40 changes: 21 additions & 19 deletions weasyl/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import arrow
import sqlalchemy as sa

from libweasyl.legacy import UNIXTIME_OFFSET
from libweasyl.models.content import Submission
from libweasyl import ratings, staff, text

Expand Down Expand Up @@ -417,32 +418,32 @@ def setusermode(userid, form):
form.reason = form.reason.strip()

if form.mode == "s":
today = datetime.datetime.utcnow().date()

if form.datetype == "r":
# Relative date
magnitude = int(form.duration)

if magnitude < 0:
raise WeasylError("releaseInvalid")

basedate = datetime.datetime.now()
if form.durationunit == "y":
basedate += datetime.timedelta(days=magnitude * 365)
duration = datetime.timedelta(days=magnitude * 365)
elif form.durationunit == "m":
basedate += datetime.timedelta(days=magnitude * 30)
duration = datetime.timedelta(days=magnitude * 30)
elif form.durationunit == "w":
basedate += datetime.timedelta(weeks=magnitude)
duration = datetime.timedelta(weeks=magnitude)
else: # Catchall, days
basedate += datetime.timedelta(days=magnitude)
duration = datetime.timedelta(days=magnitude)

form.release = d.convert_unixdate(basedate.day, basedate.month, basedate.year)
release_date = today + duration
else:
# Absolute date
if datetime.date(int(form.year), int(form.month), int(form.day)) < datetime.date.today():
raise WeasylError("releaseInvalid")
try:
release_date = datetime.date.fromisoformat(form["release-date"])
except ValueError as e:
raise WeasylError("releaseInvalid") from e

form.release = d.convert_unixdate(form.day, form.month, form.year)
if release_date <= today:
raise WeasylError("releaseInvalid")
else:
form.release = None
release_date = None

if userid not in staff.MODS:
raise WeasylError("Unexpected")
Expand All @@ -456,13 +457,15 @@ def setusermode(userid, form):
db.execute("INSERT INTO permaban VALUES (%(target)s, %(reason)s)", target=form.userid, reason=form.reason)
elif form.mode == "s":
# Suspend user
if not form.release:
if release_date is None:
raise WeasylError("releaseInvalid")

release_unixtime = arrow.get(release_date).int_timestamp + UNIXTIME_OFFSET

with d.engine.begin() as db:
db.execute("DELETE FROM permaban WHERE userid = %(target)s", target=form.userid)
db.execute("DELETE FROM suspension WHERE userid = %(target)s", target=form.userid)
db.execute("INSERT INTO suspension VALUES (%(target)s, %(reason)s, %(release)s)", target=form.userid, reason=form.reason, release=form.release)
db.execute("INSERT INTO suspension VALUES (%(target)s, %(reason)s, %(release)s)", target=form.userid, reason=form.reason, release=release_unixtime)
elif form.mode == "x":
# Unban/Unsuspend
with d.engine.begin() as db:
Expand All @@ -473,9 +476,8 @@ def setusermode(userid, form):
if action is not None:
isoformat_release = None
message = form.reason
if form.release is not None:
isoformat_release = d.datetime.datetime.fromtimestamp(form.release).isoformat()
message = '#### Release date: %s\n\n%s' % (isoformat_release, message)
if release_date is not None:
message = '#### Release date: %s\n\n%s' % (release_date.isoformat(), message)
d.append_to_log(
'staff.actions',
userid=userid, action=action, target=form.userid, reason=form.reason,
Expand Down
28 changes: 2 additions & 26 deletions weasyl/templates/modcontrol/suspenduser.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,8 @@ <h4>Release User</h4>

<label class="input-checkbox"><input type="radio" id="datetype" name="datetype" value="a" /> Release Date</label>

<div class="form-date clear">
<div class="form-date-day">
<label for="day" class="color-lighter">Day</label>
<input type="number" class="input" id="day" name="day" min="1" max="31" />
</div>
<div class="form-date-month">
<label for="month" class="color-lighter">Month</label>
<select class="input" name="month" id="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</div>
<div class="form-date-year">
<label for="year" class="color-lighter">Year</label>
<input type="number" class="input last-input" id="year" name="year" min="2012" max="2036" />
</div>
<div class="clear">
<input type="date" name="release-date" min="${arrow.utcnow().shift(days=1).date().isoformat()}">
</div>

<button type="submit" class="button" style="float: right;">Ban / Suspend / Release User</button>
Expand Down

0 comments on commit ab64dd9

Please sign in to comment.