Skip to content

Add admin log #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/admin/admin.py

This file was deleted.

50 changes: 50 additions & 0 deletions src/admin/crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from datetime import datetime
import sqlalchemy
from admin.models import AdminLog
from sqlalchemy.ext.asyncio import AsyncSession

from permission.types import WebsiteAdmin

async def task_insert_admin_log(
db_session: AsyncSession,
computing_id: str,
description: str
) -> bool:
"""
Returns False if the log was not inserted
"""
# TODO: do we even need this check?
if not WebsiteAdmin.has_permission(db_session, computing_id):
_logger.warning(f"Tried to create admin log for non-admin user {computing_id}")
return False

new_log = AdminLog(
computing_id = computing_id,
log_time = datetime.now(),
log_description = description,
)

db_session.add(new_log)
await db_session.commit()

return True


async def read_admin_log(
db_session: AsyncSession,
page_size: int,
page_number: int,
) -> list[AdminLog]:
"""
This function may only be called by admins
"""
# TODO: order by log_id?
query = (
sqlalchemy
.select(AdminLog)
.limit(page_size)
.offset(page_number * page_size)
)

return list(db_session.scalars(query))

21 changes: 21 additions & 0 deletions src/admin/tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from datetime import datetime

from constants import COMPUTING_ID_LEN, SESSION_ID_LEN
from database import Base
from sqlalchemy import Column, DateTime, ForeignKey, String
from sqlalchemy.orm import relationship

class AdminLog(Base):
# The admin log stores any admin-level changes that have been made & by who.
# It is very much like the discord feature for server moderation.
__tablename__ = "admin_log"

log_id = Column(Integer, primary_key=True, autoincrement=True)

computing_id = Column(String(COMPUTING_ID_LEN), nullable=False)

log_time = Column(DateTime, nullable=False)
log_description = Column(
Text, nullable=False
)

Loading