Refactor pk;switch move, specify UTC everywhere
This commit is contained in:
parent
97b972e38a
commit
e8d1c5bf90
@ -3,7 +3,6 @@ from typing import List
|
|||||||
|
|
||||||
import dateparser
|
import dateparser
|
||||||
|
|
||||||
import pluralkit.utils
|
|
||||||
from pluralkit.bot.commands import *
|
from pluralkit.bot.commands import *
|
||||||
from pluralkit.member import Member
|
from pluralkit.member import Member
|
||||||
from pluralkit.utils import display_relative
|
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
|
# Make sure it all runs in a big transaction for atomicity
|
||||||
async with ctx.conn.transaction():
|
async with ctx.conn.transaction():
|
||||||
# Get the last two switches to make sure the switch to move isn't before the second-last switch
|
# 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:
|
if len(last_two_switches) == 0:
|
||||||
raise CommandError("There are no registered switches for this system.")
|
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:
|
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:
|
if new_time < second_last_switch.timestamp:
|
||||||
time_str = display_relative(second_last_timestamp)
|
time_str = display_relative(second_last_switch.timestamp)
|
||||||
raise CommandError(
|
raise CommandError(
|
||||||
"Can't move switch to before last switch time ({} ago), as it would cause conflicts.".format(time_str))
|
"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
|
# 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"
|
members = ", ".join([member.name for member in last_fronters]) or "nobody"
|
||||||
last_absolute = last_timestamp.isoformat(sep=" ", timespec="seconds")
|
last_absolute = last_switch.timestamp.isoformat(sep=" ", timespec="seconds")
|
||||||
last_relative = display_relative(last_timestamp)
|
last_relative = display_relative(last_switch.timestamp)
|
||||||
new_absolute = new_time.isoformat(sep=" ", timespec="seconds")
|
new_absolute = new_time.isoformat(sep=" ", timespec="seconds")
|
||||||
new_relative = display_relative(new_time)
|
new_relative = display_relative(new_time)
|
||||||
|
|
||||||
# Confirm with user
|
# Confirm with user
|
||||||
switch_confirm_message = await ctx.reply(
|
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_absolute,
|
||||||
last_relative,
|
last_relative,
|
||||||
new_absolute,
|
new_absolute,
|
||||||
@ -139,9 +140,6 @@ async def switch_move(ctx: CommandContext):
|
|||||||
if not await ctx.confirm_react(ctx.message.author, switch_confirm_message):
|
if not await ctx.confirm_react(ctx.message.author, switch_confirm_message):
|
||||||
raise CommandError("Switch move cancelled.")
|
raise CommandError("Switch move cancelled.")
|
||||||
|
|
||||||
# DB requires the actual switch ID which our utility method above doesn't return, do this manually
|
# Actually move the switch
|
||||||
switch_id = (await db.front_history(ctx.conn, system.id, count=1))[0]["id"]
|
await last_switch.move(ctx.conn, new_time)
|
||||||
|
|
||||||
# Change the switch in the DB
|
|
||||||
await db.move_last_switch(ctx.conn, system.id, switch_id, new_time)
|
|
||||||
await ctx.reply_ok("Switch moved.")
|
await ctx.reply_ok("Switch moved.")
|
||||||
|
@ -203,7 +203,7 @@ async def system_fronthistory(ctx: CommandContext, system: System):
|
|||||||
if i > 0:
|
if i > 0:
|
||||||
last_switch_time = front_history[i - 1][0]
|
last_switch_time = front_history[i - 1][0]
|
||||||
delta_text = ", for {}".format(display_relative(timestamp - last_switch_time))
|
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 = embeds.status("\n".join(lines) or "(none)")
|
||||||
embed.title = "Past switches"
|
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)",
|
embed.add_field(name=member.name if member else "(no fronter)",
|
||||||
value="{}% ({})".format(percent, humanize.naturaldelta(front_time)))
|
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)))
|
display_relative(span_start)))
|
||||||
await ctx.reply(embed=embed)
|
await ctx.reply(embed=embed)
|
||||||
|
@ -186,7 +186,7 @@ async def front_status(switch: Switch, conn) -> discord.Embed:
|
|||||||
|
|
||||||
if switch.timestamp:
|
if switch.timestamp:
|
||||||
embed.add_field(name="Since",
|
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)))
|
display_relative(switch.timestamp)))
|
||||||
else:
|
else:
|
||||||
embed = error("No switches logged.")
|
embed = error("No switches logged.")
|
||||||
|
@ -285,7 +285,7 @@ async def add_switch(conn, system_id: int):
|
|||||||
return res["id"]
|
return res["id"]
|
||||||
|
|
||||||
@db_wrap
|
@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))
|
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)
|
await conn.execute("update switches set timestamp = $1 where system = $2 and id = $3", new_time, system_id, switch_id)
|
||||||
|
|
||||||
|
@ -18,6 +18,9 @@ class Switch(namedtuple("Switch", ["id", "system", "timestamp", "members"])):
|
|||||||
async def delete(self, conn):
|
async def delete(self, conn):
|
||||||
await db.delete_switch(conn, self.id)
|
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):
|
async def to_json(self, conn):
|
||||||
return {
|
return {
|
||||||
"timestamp": self.timestamp.isoformat(),
|
"timestamp": self.timestamp.isoformat(),
|
||||||
|
Loading…
Reference in New Issue
Block a user