Code cosmetic
This commit is contained in:
parent
581598c208
commit
e43c88d0ab
1 changed files with 31 additions and 21 deletions
|
@ -1,11 +1,16 @@
|
|||
import logging
|
||||
from pathlib import Path
|
||||
from json import dumps
|
||||
|
||||
from fastapi import Depends, FastAPI, HTTPException, status, responses
|
||||
from sqlalchemy.orm import selectinload
|
||||
from sqlmodel import select
|
||||
import pandas as pd
|
||||
import geopandas as gpd
|
||||
|
||||
from gisaf.config import conf
|
||||
from gisaf.database import pandas_query, fastapi_db_session as db_session
|
||||
from gisaf.utils import NumpyEncoder
|
||||
from gisaf.database import fastapi_db_session as db_session
|
||||
from gisaf.models.authentication import User
|
||||
from gisaf.models.dashboard import (
|
||||
DashboardPage, DashboardPageSection,
|
||||
|
@ -17,6 +22,17 @@ from gisaf.security import get_current_active_user
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
default_footer = '''
|
||||
<a rel="license" href="https://www.gnu.org/licenses/gpl.html">
|
||||
<img alt="GNU GPL v3 license"style="border-width:0"
|
||||
src="/static/icons/gplv3-88x31.png" title="GPL Open Source license"/>
|
||||
</a>
|
||||
'''
|
||||
|
||||
default_content = '''
|
||||
Gisaf is free, open source software for geomatics and GIS:
|
||||
<a href="http://redmine.auroville.org.in/projects/gisaf">Gisaf</a>.
|
||||
'''
|
||||
|
||||
api = FastAPI(
|
||||
default_response_class=responses.ORJSONResponse,
|
||||
|
@ -30,23 +46,16 @@ async def get_groups(
|
|||
data = await db_session.exec(query)
|
||||
groups: dict[str, DashboardPage_] = {}
|
||||
for page in data.all():
|
||||
page_field = DashboardPage_(
|
||||
name=page.name,
|
||||
group=page.group,
|
||||
description=page.description,
|
||||
)
|
||||
page_field = DashboardPage_(name=page.name, group=page.group,
|
||||
description=page.description)
|
||||
group = groups.get(page.group)
|
||||
if group is None:
|
||||
group = DashboardGroup(
|
||||
name=page.group,
|
||||
pages=[page_field]
|
||||
)
|
||||
group = DashboardGroup(name=page.group, pages=[page_field])
|
||||
groups[page.group] = group
|
||||
else:
|
||||
group.pages.append(page_field)
|
||||
return groups.values()
|
||||
|
||||
|
||||
@api.get('/home')
|
||||
async def get_home() -> DashboardHome:
|
||||
content_path = Path(conf.gisaf.dashboard_home.content_file).expanduser()
|
||||
|
@ -54,25 +63,24 @@ async def get_home() -> DashboardHome:
|
|||
if content_path.is_file():
|
||||
content = content_path.read_text()
|
||||
else:
|
||||
content = 'Gisaf is free, open source software for geomatics and GIS: <a href="http://redmine.auroville.org.in/projects/gisaf">Gisaf</a>.'
|
||||
content = default_content
|
||||
if footer_path.is_file():
|
||||
footer = footer_path.read_text()
|
||||
else:
|
||||
footer = '<a rel="license" href="https://www.gnu.org/licenses/gpl.html"><img alt="GNU GPL v3 license"style="border-width:0" src="/static/icons/gplv3-88x31.png" title="GPL Open Source license"/></a>'
|
||||
footer = default_footer
|
||||
return DashboardHome(
|
||||
title=conf.gisaf.dashboard_home.title,
|
||||
content=content,
|
||||
footer=footer,
|
||||
)
|
||||
|
||||
|
||||
@api.get('/page/{group}/{name}')
|
||||
async def get_dashboard_page(group: str, name: str,
|
||||
db_session: db_session,
|
||||
user: User = Depends(get_current_active_user),
|
||||
) -> DashboardPage_:
|
||||
query1 = select(DashboardPage).where((DashboardPage.name==name)
|
||||
& (DashboardPage.group==group))
|
||||
& (DashboardPage.group==group))
|
||||
data1 = await db_session.exec(query1)
|
||||
page = data1.one_or_none()
|
||||
if not page:
|
||||
|
@ -88,7 +96,7 @@ async def get_dashboard_page(group: str, name: str,
|
|||
username = user.username if user is not None else "Anonymous"
|
||||
logger.info(f'{username} tried to access dashboard page {name}')
|
||||
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
|
||||
dashboard_page = DashboardPage_(
|
||||
dp = DashboardPage_(
|
||||
name=page.name,
|
||||
group=page.group,
|
||||
description=page.description,
|
||||
|
@ -116,11 +124,12 @@ async def get_dashboard_page(group: str, name: str,
|
|||
if isinstance(df, gpd.GeoDataFrame):
|
||||
gdf = pd.DataFrame(df.drop(columns=['geometry']))
|
||||
df = gdf
|
||||
dashboard_page.dfData = df.to_json(orient='table', double_precision=2)
|
||||
dp.dfData = df.to_json(orient='table', double_precision=2)
|
||||
except NotADataframeError:
|
||||
logger.warning(f'Dashboard: cannot read dataframe for page {page.name}')
|
||||
except Exception as err:
|
||||
logger.warning(f'Dashboard: cannot add dataframe for page {page.name}, see debug message')
|
||||
logger.warning(f'Dashboard: cannot add dataframe for page {page.name}, '
|
||||
'see debug message')
|
||||
logger.exception(err)
|
||||
if page.plot:
|
||||
try:
|
||||
|
@ -130,8 +139,9 @@ async def get_dashboard_page(group: str, name: str,
|
|||
'layout': plot.layout.to_plotly_json(),
|
||||
}
|
||||
except Exception as err:
|
||||
logger.warning(f'Dashboard: cannot add plot for page {page.name}, see debug message')
|
||||
logger.warning(f'Dashboard: cannot add plot for page {page.name}, '
|
||||
'see debug message')
|
||||
logger.exception(err)
|
||||
else:
|
||||
dashboard_page.plotData = dumps(plotData, cls=NumpyEncoder)
|
||||
return dashboard_page
|
||||
dp.plotData = dumps(plotData, cls=NumpyEncoder)
|
||||
return dp
|
Loading…
Add table
Add a link
Reference in a new issue