Refactor pk;switch move, specify UTC everywhere

This commit is contained in:
Ske 2018-12-18 18:30:10 +01:00
parent 97b972e38a
commit e8d1c5bf90
5 changed files with 19 additions and 18 deletions

View File

@ -3,7 +3,6 @@ from typing import List
import dateparser
import pluralkit.utils
from pluralkit.bot.commands import *
from pluralkit.member import Member
from pluralkit.utils import display_relative
@ -108,29 +107,31 @@ async def switch_move(ctx: CommandContext):
# Make sure it all runs in a big transaction for atomicity
async with ctx.conn.transaction():
# Get the last two switches to make sure the switch to move isn't before the second-last switch
last_two_switches = await pluralkit.utils.get_front_history(ctx.conn, system.id, count=2)
last_two_switches = await system.get_switches(ctx.conn, 2)
if len(last_two_switches) == 0:
raise CommandError("There are no registered switches for this system.")
last_timestamp, last_fronters = last_two_switches[0]
last_switch = last_two_switches[0]
if len(last_two_switches) > 1:
second_last_timestamp, _ = last_two_switches[1]
second_last_switch = last_two_switches[1]
if new_time < second_last_timestamp:
time_str = display_relative(second_last_timestamp)
if new_time < second_last_switch.timestamp:
time_str = display_relative(second_last_switch.timestamp)
raise CommandError(
"Can't move switch to before last switch time ({} ago), as it would cause conflicts.".format(time_str))
# Display the confirmation message w/ humanized times
last_fronters = await last_switch.fetch_members(ctx.conn)
members = ", ".join([member.name for member in last_fronters]) or "nobody"
last_absolute = last_timestamp.isoformat(sep=" ", timespec="seconds")
last_relative = display_relative(last_timestamp)
last_absolute = last_switch.timestamp.isoformat(sep=" ", timespec="seconds")
last_relative = display_relative(last_switch.timestamp)
new_absolute = new_time.isoformat(sep=" ", timespec="seconds")
new_relative = display_relative(new_time)
# Confirm with user
switch_confirm_message = await ctx.reply(
"This will move the latest switch ({}) from {} ({} ago) to {} ({} ago). Is this OK?".format(members,
"This will move the latest switch ({}) from {} UTC ({} ago) to {} UTC ({} ago). Is this OK?".format(members,
last_absolute,
last_relative,
new_absolute,
@ -139,9 +140,6 @@ async def switch_move(ctx: CommandContext):
if not await ctx.confirm_react(ctx.message.author, switch_confirm_message):
raise CommandError("Switch move cancelled.")
# DB requires the actual switch ID which our utility method above doesn't return, do this manually
switch_id = (await db.front_history(ctx.conn, system.id, count=1))[0]["id"]
# Change the switch in the DB
await db.move_last_switch(ctx.conn, system.id, switch_id, new_time)
# Actually move the switch
await last_switch.move(ctx.conn, new_time)
await ctx.reply_ok("Switch moved.")

View File

@ -203,7 +203,7 @@ async def system_fronthistory(ctx: CommandContext, system: System):
if i > 0:
last_switch_time = front_history[i - 1][0]
delta_text = ", for {}".format(display_relative(timestamp - last_switch_time))
lines.append("**{}** ({}, {} ago{})".format(name, time_text, rel_text, delta_text))
lines.append("**{}** ({} UTC, {} ago{})".format(name, time_text, rel_text, delta_text))
embed = embeds.status("\n".join(lines) or "(none)")
embed.title = "Past switches"
@ -302,6 +302,6 @@ async def system_frontpercent(ctx: CommandContext, system: System):
embed.add_field(name=member.name if member else "(no fronter)",
value="{}% ({})".format(percent, humanize.naturaldelta(front_time)))
embed.set_footer(text="Since {} ({} ago)".format(span_start.isoformat(sep=" ", timespec="seconds"),
embed.set_footer(text="Since {} UTC ({} ago)".format(span_start.isoformat(sep=" ", timespec="seconds"),
display_relative(span_start)))
await ctx.reply(embed=embed)

View File

@ -186,7 +186,7 @@ async def front_status(switch: Switch, conn) -> discord.Embed:
if switch.timestamp:
embed.add_field(name="Since",
value="{} ({})".format(switch.timestamp.isoformat(sep=" ", timespec="seconds"),
value="{} UTC ({})".format(switch.timestamp.isoformat(sep=" ", timespec="seconds"),
display_relative(switch.timestamp)))
else:
embed = error("No switches logged.")

View File

@ -285,7 +285,7 @@ async def add_switch(conn, system_id: int):
return res["id"]
@db_wrap
async def move_last_switch(conn, system_id: int, switch_id: int, new_time: datetime):
async def move_switch(conn, system_id: int, switch_id: int, new_time: datetime):
logger.debug("Moving latest switch (system={}, id={}, new_time={})".format(system_id, switch_id, new_time))
await conn.execute("update switches set timestamp = $1 where system = $2 and id = $3", new_time, system_id, switch_id)

View File

@ -18,6 +18,9 @@ class Switch(namedtuple("Switch", ["id", "system", "timestamp", "members"])):
async def delete(self, conn):
await db.delete_switch(conn, self.id)
async def move(self, conn, new_timestamp):
await db.move_switch(conn, self.system, self.id, new_timestamp)
async def to_json(self, conn):
return {
"timestamp": self.timestamp.isoformat(),