fix: admin and emulator users now optionnal types

This commit is contained in:
Laureηt 2021-11-02 18:53:34 +01:00
parent 0985367de8
commit a09befe4a3
No known key found for this signature in database
GPG key ID: D88C6B294FD40994

View file

@ -3,7 +3,7 @@ import json
import logging import logging
import time import time
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any from typing import Any, Optional
import websockets import websockets
@ -46,8 +46,8 @@ class User:
class Users(set): class Users(set):
"""Store `User`s connected to the server.""" """Store `User`s connected to the server."""
emulator: User = User(None) emulator: Optional[User] = None
admin: User = User(None) admin: Optional[User] = None
def register(self, user: User): def register(self, user: User):
"""Register a user in the set. """Register a user in the set.
@ -110,7 +110,8 @@ class Votes(dict):
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
PASSWORD: str = "password" PASSWORD_ADMIN: str = "password"
PASSWORD_EMU: str = "password"
VOTES: Votes = Votes() VOTES: Votes = Votes()
USERS: Users = Users() USERS: Users = Users()
@ -120,16 +121,16 @@ async def parse_message(user: User, msg: dict[str, str]):
Args: Args:
user (User): the sender of the message. user (User): the sender of the message.
msg (dict[str, str]): the message received through websocket. msg (dict[str, str]): the data received (through the websocket).
""" """
if "auth" in msg: if "auth" in msg:
data = msg["auth"] data = msg["auth"]
if data == PASSWORD: if USERS.emulator is not None and data == PASSWORD_EMU:
USERS.emulator = user USERS.emulator = user
logging.debug(f"emulator authenticated: {user}") logging.debug(f"emulator authenticated: {user}")
# elif not USERS.admin and data == PASSWORD: elif USERS.admin is not None and data == PASSWORD_ADMIN:
# USERS.admin = user USERS.admin = user
# logging.debug(f"admin authenticated: {user}") logging.debug(f"admin authenticated: {user}")
if "action" in msg: if "action" in msg:
data = msg["action"] data = msg["action"]
@ -142,7 +143,7 @@ async def parse_message(user: User, msg: dict[str, str]):
if "admin" in msg: if "admin" in msg:
data = msg["admin"] data = msg["admin"]
if user == USERS.admin: if USERS.emulator is not None and user == USERS.admin:
if data == "save": if data == "save":
await USERS.emulator.send('{"admin":"save"}') await USERS.emulator.send('{"admin":"save"}')
elif data == "load": elif data == "load":