add basic user support

This commit is contained in:
John Burwell 2023-04-30 14:44:35 -05:00
parent 8c4c15c904
commit 4e75ea61b5
5 changed files with 95 additions and 5 deletions

View File

@ -27,3 +27,4 @@ from .controller import Controller
from .logger import Logger from .logger import Logger
from .parser import Parser from .parser import Parser
from .pluginloader import PluginLoader from .pluginloader import PluginLoader
from .user import User

View File

@ -20,16 +20,18 @@ import logging
import sys import sys
import rsbbs import rsbbs
from rsbbs import Config, Controller, Parser, PluginLoader from rsbbs import Config, Controller
from rsbbs.models import User
# Main UI console class # Main UI console class
class Console(): class Console():
def __init__(self, config: Config, controller: Controller): def __init__(self, config: Config, controller: Controller, user: User):
self.config = config self.config = config
self.controller = controller self.controller = controller
self.user = user
self.parser = rsbbs.Parser() self.parser = rsbbs.Parser()
@ -93,7 +95,9 @@ class Console():
f"{self.config.callsign} ") f"{self.config.callsign} ")
greeting.append(f"Welcome to {self.config.bbs_name}, " greeting.append(f"Welcome to {self.config.bbs_name}, "
f"{self.config.calling_station}") f"{self.user.callsign}")
greeting.append(f"Last login: {self.user.login_last}")
greeting.append(self.config.banner_message) greeting.append(self.config.banner_message)

View File

@ -18,7 +18,7 @@
from datetime import datetime, timezone from datetime import datetime, timezone
from sqlalchemy import Boolean, DateTime, String from sqlalchemy import Boolean, DateTime, String, Integer
from sqlalchemy.orm import DeclarativeBase, Mapped from sqlalchemy.orm import DeclarativeBase, Mapped
from sqlalchemy.orm import mapped_column from sqlalchemy.orm import mapped_column
@ -38,3 +38,14 @@ class Message(Base):
datetime: Mapped[DateTime] = mapped_column( datetime: Mapped[DateTime] = mapped_column(
DateTime, default=datetime.now(timezone.utc)) DateTime, default=datetime.now(timezone.utc))
is_private: Mapped[bool] = mapped_column(Boolean) is_private: Mapped[bool] = mapped_column(Boolean)
class User(Base):
__tablename__ = 'user'
id: Mapped[int] = mapped_column(primary_key=True)
callsign: Mapped[str] = mapped_column(String)
given_name: Mapped[str] = mapped_column(String, nullable=True)
family_name: Mapped[str] = mapped_column(String, nullable=True)
login_count: Mapped[int] = mapped_column(Integer)
login_last: Mapped[DateTime] = mapped_column(
DateTime, default=datetime.now(timezone.utc))

View File

@ -23,6 +23,8 @@ from rsbbs import Config, Console, Controller, Logger
from rsbbs.args import parse_args from rsbbs.args import parse_args
from rsbbs.user import User
def main(): def main():
@ -41,8 +43,11 @@ def main():
# Init the controller # Init the controller
controller = Controller(config) controller = Controller(config)
# Init the user:
user = User(controller, args.calling_station.upper())
# Init the UI console # Init the UI console
console = Console(config, controller) console = Console(config, controller, user)
# Start the app # Start the app
console.run() console.run()

69
rsbbs/user.py Normal file
View File

@ -0,0 +1,69 @@
#!/usr/bin/env python
#
# Really Simple BBS - a really simple BBS for ax.25 packet radio.
# Copyright (C) 2023 John Burwell <john@atatdotdot.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
from typing import Any
import sqlalchemy
from datetime import datetime, timezone
from rsbbs import __version__
from rsbbs import Controller
from rsbbs.models import User as SAUser
class User():
def __init__(self, controller: Controller, callsign: str):
self.controller = controller
self.callsign = callsign
self.user = self.get_or_create_user()
def __getattr__(self, __name: str) -> Any:
return getattr(self.user, __name)
def get_or_create_user(self):
with self.controller.session() as session:
session.expire_on_commit = False
try:
statement = sqlalchemy.select(SAUser).where(
SAUser.callsign == self.callsign.upper())
exopts = {"prebuffer_rows": True}
result = session.execute(statement,
execution_options=exopts)
result = result.one_or_none()
if result:
user = result[0]
logging.info(f"User {result[0].callsign} found.")
self.login_last = user.login_last
user.login_count = user.login_count + 1
user.login_last = datetime.now(timezone.utc)
session.commit()
else:
logging.info(f"User not found.")
user = SAUser(
callsign=self.callsign.upper(),
login_count=1,
)
session.add(user)
session.commit()
logging.info(f"User added.")
return user
except Exception as e:
logging.error(e)
raise