split the interface code into multiple files
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
import humanize, datetime
|
||||
from helpers import spawnDialog, spawnSimplePopup, spawnYesNoPopup, HButtonSelect, errorTxt, isValidUsername
|
||||
|
||||
from prompt_toolkit.application.current import get_app
|
||||
from prompt_toolkit.buffer import Buffer
|
||||
from prompt_toolkit.layout import FormattedTextControl
|
||||
from prompt_toolkit.layout.containers import HSplit, Window
|
||||
from prompt_toolkit.layout.dimension import Dimension as D
|
||||
from prompt_toolkit.validation import Validator
|
||||
from prompt_toolkit.widgets import Button, Dialog, Label, TextArea, RadioList, ValidationToolbar
|
||||
|
||||
class AdminPanel:
|
||||
|
||||
def __init__(self,floats,server):
|
||||
self.floats = floats
|
||||
self.server = server
|
||||
|
||||
self.el = HButtonSelect([("Ban user",self.banuser),("Unban user",self.unbanuser)])
|
||||
|
||||
async def banuserconfirm(self,name,duration,motive):
|
||||
res = await self.server.getuserinfo(name)
|
||||
if res["type"]=="error":
|
||||
return await spawnSimplePopup("Error",f"{errorTxt(res)}\nCould not get information about the user.",self.floats)
|
||||
info = res["response"]
|
||||
create_time = datetime.datetime.fromtimestamp(info["created"])
|
||||
motivestr = f"\"{motive}\"" if len(motive)>0 else "N/A"
|
||||
conf = await spawnYesNoPopup("Confirm Ban?",Window(FormattedTextControl([
|
||||
("","You are about to ban "),
|
||||
("bold",info["name"]),
|
||||
(""," for "),
|
||||
("bold",({0: "an indefinite amount of time",24*60*60: "24 hours",3*24*60*60: "3 days",7*24*60*60: "7 days"})[duration]),
|
||||
("",".\n\nMotive: "),("bold",motivestr),("","\n"),
|
||||
("","\nUser information:\n"),
|
||||
("","- Internal ID: "),("bold",str(info["id"])),("","\n"),
|
||||
("","- Created: "),("bold",humanize.naturaldate(create_time)),("",f" ({create_time})\n")
|
||||
],focusable=False),dont_extend_width=True,dont_extend_height=True),self.floats)
|
||||
if conf:
|
||||
res = await self.server.banuser(info["id"],duration,motive)
|
||||
if res["type"]=="error":
|
||||
await spawnSimplePopup("Error",f"{errorTxt(res)}\nCould not ban this user.",self.floats)
|
||||
|
||||
def banuser(self):
|
||||
def dlgclose():
|
||||
despawn()
|
||||
get_app().create_background_task(self.banuserconfirm(usertextfield.text,timeselect.current_value,motivetextfield.text))
|
||||
|
||||
def nameaccept(buf: Buffer):
|
||||
get_app().layout.focus(motivetextfield)
|
||||
return True
|
||||
|
||||
def motiveaccept(buf: Buffer):
|
||||
get_app().layout.focus(timeselect)
|
||||
return True
|
||||
|
||||
usernameValidator = Validator.from_callable(isValidUsername, error_message='Invalid username')
|
||||
|
||||
ok_button = Button(text="OK", handler=dlgclose, width=16)
|
||||
cancel_button = Button(text="Cancel", handler=lambda: despawn(), width=16)
|
||||
|
||||
usertextfield = TextArea(
|
||||
text="",
|
||||
multiline=False,
|
||||
password=False,
|
||||
completer=None,
|
||||
validator=usernameValidator,
|
||||
accept_handler=nameaccept,
|
||||
)
|
||||
|
||||
motivetextfield = TextArea(
|
||||
text="",
|
||||
multiline=False,
|
||||
password=False,
|
||||
completer=None,
|
||||
validator=None,
|
||||
accept_handler=motiveaccept,
|
||||
)
|
||||
|
||||
timeselect = RadioList([(24*60*60,"24 hours"),(3*24*60*60,"3 days"),(7*24*60*60,"7 days"),(0,"Permanent")],default=24*60*60)
|
||||
|
||||
dlg = Dialog(
|
||||
title="Ban user",
|
||||
body=HSplit(
|
||||
[
|
||||
Label(text="Username:", dont_extend_height=True),
|
||||
usertextfield,
|
||||
Label(text="Motive:", dont_extend_height=True),
|
||||
motivetextfield,
|
||||
Label(text="Time:", dont_extend_height=True),
|
||||
timeselect,
|
||||
ValidationToolbar(),
|
||||
],
|
||||
padding=D(preferred=1, max=1),
|
||||
width=70
|
||||
),
|
||||
buttons=[ok_button, cancel_button],
|
||||
with_background=True,
|
||||
)
|
||||
|
||||
despawn = spawnDialog(dlg,self.floats)
|
||||
|
||||
async def unbanuserconfirm(self,name):
|
||||
res = await self.server.getuserinfo(name)
|
||||
if res["type"]=="error":
|
||||
return await spawnSimplePopup("Error",f"{errorTxt(res)}\nCould not get information about the user.",self.floats)
|
||||
info = res["response"]
|
||||
if info["banned"]==False:
|
||||
return await spawnSimplePopup("Error",f"The user {name} is not banned.",self.floats)
|
||||
create_time = datetime.datetime.fromtimestamp(info["created"])
|
||||
ban_date = datetime.datetime.fromtimestamp(info["ban_details"]["ban_date"])
|
||||
expire_date = datetime.datetime.fromtimestamp(info["ban_details"]["expire_date"]) if info["ban_details"]["expire_date"]>0 else "Indefinite"
|
||||
motive = f"\"{info["ban_details"]["motive"]}\"" if ("motive" in info["ban_details"]) and (len(info["ban_details"]["motive"])>0) else "N/A"
|
||||
conf = await spawnYesNoPopup("Confirm Ban?",Window(FormattedTextControl([
|
||||
("","You are about to unban "),
|
||||
("bold",info["name"]),
|
||||
("",".\n\nUser information:\n"),
|
||||
("","- Internal ID: "),("bold",str(info["id"])),("","\n"),
|
||||
("","- Created: "),("bold",humanize.naturaldate(create_time)),("",f" ({create_time})\n"),
|
||||
("","\nBan information:\n"),
|
||||
("","- Motive: "),("bold",motive),("","\n"),
|
||||
("","- Banned on: "),("bold",humanize.naturaldate(ban_date)),("",f" ({ban_date})\n"),
|
||||
("","- Ban expires on: "),("bold",humanize.naturaldate(expire_date)),("",f" ({expire_date})\n"),
|
||||
],focusable=False),dont_extend_width=True,dont_extend_height=True),self.floats)
|
||||
if conf:
|
||||
res = await self.server.unbanuser(info["id"])
|
||||
if res["type"]=="error":
|
||||
await spawnSimplePopup("Error",f"{errorTxt(res)}\nCould not unban this user.",self.floats)
|
||||
|
||||
def unbanuser(self):
|
||||
def dlgclose():
|
||||
despawn()
|
||||
get_app().create_background_task(self.unbanuserconfirm(usertextfield.text))
|
||||
|
||||
def nameaccept(buf: Buffer):
|
||||
dlgclose()
|
||||
return True
|
||||
|
||||
usernameValidator = Validator.from_callable(isValidUsername, error_message='Invalid username')
|
||||
|
||||
ok_button = Button(text="OK", handler=dlgclose, width=16)
|
||||
cancel_button = Button(text="Cancel", handler=lambda: despawn(), width=16)
|
||||
|
||||
usertextfield = TextArea(
|
||||
text="",
|
||||
multiline=False,
|
||||
password=False,
|
||||
completer=None,
|
||||
validator=usernameValidator,
|
||||
accept_handler=nameaccept,
|
||||
)
|
||||
|
||||
dlg = Dialog(
|
||||
title="Unban user",
|
||||
body=HSplit(
|
||||
[
|
||||
Label(text="Username:"),
|
||||
usertextfield,
|
||||
ValidationToolbar(),
|
||||
],
|
||||
padding=D(preferred=1, max=1),
|
||||
),
|
||||
buttons=[ok_button, cancel_button],
|
||||
with_background=True,
|
||||
)
|
||||
|
||||
despawn = spawnDialog(dlg,self.floats)
|
||||
Reference in New Issue
Block a user