real-estate / project /admin.py
brestok's picture
init
d0e0a14
from sqladmin import ModelView
from sqladmin import Admin
from fastapi import Request, Response
from fastapi.responses import RedirectResponse
from fastapi.templating import Jinja2Templates
import logging
from project.bot.models import Folder, AudioRecord, ImageRecord, Report
from project.users.models import User
template = Jinja2Templates(directory='templates')
logger = logging.getLogger(__name__)
class FolderAdmin(ModelView, model=Folder):
category = 'Data'
column_list = [Folder.id, Folder.name, Folder.owner]
is_async = True
icon = "fa-solid fa-folder"
name = "Folder"
name_plural = "Folders"
column_searchable_list = [Folder.user_id]
column_default_sort = ('id', True)
# column_formatters = {
# ChatMessage.transcription: lambda m, a: m.transcription[:50] + '...' if len(m.transcription) > 50
# else m.transcription}
class AudioRecordsAdmin(ModelView, model=AudioRecord):
category = 'Data'
column_list = [AudioRecord.id, AudioRecord.transcription]
is_async = True
icon = "fa-solid fa-microphone"
name = "Audio"
name_plural = "Audio"
column_searchable_list = [AudioRecord.transcription, AudioRecord.id]
column_default_sort = ('id', True)
column_formatters = {
AudioRecord.transcription: lambda m, a: m.transcription[:400] + '...' if len(m.transcription) > 50
else m.transcription}
can_edit = False
details_template = 'details.html'
class ImageRecordsAdmin(ModelView, model=ImageRecord):
category = 'Data'
column_list = [ImageRecord.id, ImageRecord.transcription]
is_async = True
icon = "fa-solid fa-images"
name = "Image"
name_plural = "Images"
column_searchable_list = [ImageRecord.transcription, ImageRecord.id]
column_default_sort = ('id', True)
column_formatters = {
ImageRecord.transcription: lambda m, a: m.transcription[:400] + '...' if len(m.transcription) > 50
else m.transcription}
details_template = 'details.html'
can_edit = False
class ReportAdmin(ModelView, model=Report):
category = 'Data'
column_list = [Report.id, Report.user, Report.content]
is_async = True
icon = "fa-solid fa-rocket"
name = "Report"
name_plural = "Reports"
column_searchable_list = [Report.id, Report.content]
column_default_sort = ('id', True)
column_formatters = {
Report.content: lambda m, a: m.content[:400] + '...' if len(m.content) > 400
else m.content}
can_edit = False
class UserAdmin(ModelView, model=User):
column_list = column_sortable_list = [User.id, User.username, User.email]
is_async = True
icon = "fa-solid fa-user"
category = "Account"
name = "User"
name_plural = "Users"
column_searchable_list = [User.username, User.email]
column_details_exclude_list = [User.hashed_password, 'is_active', 'is_verified']
form_excluded_columns = ['is_active', 'is_verified', 'is_superuser']
column_export_list = ['id', 'email', 'username', 'is_superuser']
class AdminCustom(Admin):
async def login(self, request: Request) -> Response:
assert self.authentication_backend is not None
context = {"request": request, "error": ""}
if request.method == "GET":
return template.TemplateResponse("login_admin.html", context)
ok = await self.authentication_backend.login(request)
if not ok:
context["error"] = "Invalid credentials."
return template.TemplateResponse(
"login_admin.html", context, status_code=400
)
return RedirectResponse(request.url_for("admin:index"), status_code=302)