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
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from json import dumps
|
||||||
|
|
||||||
from fastapi import Depends, FastAPI, HTTPException, status, responses
|
from fastapi import Depends, FastAPI, HTTPException, status, responses
|
||||||
from sqlalchemy.orm import selectinload
|
from sqlalchemy.orm import selectinload
|
||||||
from sqlmodel import select
|
from sqlmodel import select
|
||||||
|
import pandas as pd
|
||||||
|
import geopandas as gpd
|
||||||
|
|
||||||
from gisaf.config import conf
|
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.authentication import User
|
||||||
from gisaf.models.dashboard import (
|
from gisaf.models.dashboard import (
|
||||||
DashboardPage, DashboardPageSection,
|
DashboardPage, DashboardPageSection,
|
||||||
|
@ -17,6 +22,17 @@ from gisaf.security import get_current_active_user
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
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(
|
api = FastAPI(
|
||||||
default_response_class=responses.ORJSONResponse,
|
default_response_class=responses.ORJSONResponse,
|
||||||
|
@ -30,23 +46,16 @@ async def get_groups(
|
||||||
data = await db_session.exec(query)
|
data = await db_session.exec(query)
|
||||||
groups: dict[str, DashboardPage_] = {}
|
groups: dict[str, DashboardPage_] = {}
|
||||||
for page in data.all():
|
for page in data.all():
|
||||||
page_field = DashboardPage_(
|
page_field = DashboardPage_(name=page.name, group=page.group,
|
||||||
name=page.name,
|
description=page.description)
|
||||||
group=page.group,
|
|
||||||
description=page.description,
|
|
||||||
)
|
|
||||||
group = groups.get(page.group)
|
group = groups.get(page.group)
|
||||||
if group is None:
|
if group is None:
|
||||||
group = DashboardGroup(
|
group = DashboardGroup(name=page.group, pages=[page_field])
|
||||||
name=page.group,
|
|
||||||
pages=[page_field]
|
|
||||||
)
|
|
||||||
groups[page.group] = group
|
groups[page.group] = group
|
||||||
else:
|
else:
|
||||||
group.pages.append(page_field)
|
group.pages.append(page_field)
|
||||||
return groups.values()
|
return groups.values()
|
||||||
|
|
||||||
|
|
||||||
@api.get('/home')
|
@api.get('/home')
|
||||||
async def get_home() -> DashboardHome:
|
async def get_home() -> DashboardHome:
|
||||||
content_path = Path(conf.gisaf.dashboard_home.content_file).expanduser()
|
content_path = Path(conf.gisaf.dashboard_home.content_file).expanduser()
|
||||||
|
@ -54,25 +63,24 @@ async def get_home() -> DashboardHome:
|
||||||
if content_path.is_file():
|
if content_path.is_file():
|
||||||
content = content_path.read_text()
|
content = content_path.read_text()
|
||||||
else:
|
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():
|
if footer_path.is_file():
|
||||||
footer = footer_path.read_text()
|
footer = footer_path.read_text()
|
||||||
else:
|
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(
|
return DashboardHome(
|
||||||
title=conf.gisaf.dashboard_home.title,
|
title=conf.gisaf.dashboard_home.title,
|
||||||
content=content,
|
content=content,
|
||||||
footer=footer,
|
footer=footer,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@api.get('/page/{group}/{name}')
|
@api.get('/page/{group}/{name}')
|
||||||
async def get_dashboard_page(group: str, name: str,
|
async def get_dashboard_page(group: str, name: str,
|
||||||
db_session: db_session,
|
db_session: db_session,
|
||||||
user: User = Depends(get_current_active_user),
|
user: User = Depends(get_current_active_user),
|
||||||
) -> DashboardPage_:
|
) -> DashboardPage_:
|
||||||
query1 = select(DashboardPage).where((DashboardPage.name==name)
|
query1 = select(DashboardPage).where((DashboardPage.name==name)
|
||||||
& (DashboardPage.group==group))
|
& (DashboardPage.group==group))
|
||||||
data1 = await db_session.exec(query1)
|
data1 = await db_session.exec(query1)
|
||||||
page = data1.one_or_none()
|
page = data1.one_or_none()
|
||||||
if not page:
|
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"
|
username = user.username if user is not None else "Anonymous"
|
||||||
logger.info(f'{username} tried to access dashboard page {name}')
|
logger.info(f'{username} tried to access dashboard page {name}')
|
||||||
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
|
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
|
||||||
dashboard_page = DashboardPage_(
|
dp = DashboardPage_(
|
||||||
name=page.name,
|
name=page.name,
|
||||||
group=page.group,
|
group=page.group,
|
||||||
description=page.description,
|
description=page.description,
|
||||||
|
@ -116,11 +124,12 @@ async def get_dashboard_page(group: str, name: str,
|
||||||
if isinstance(df, gpd.GeoDataFrame):
|
if isinstance(df, gpd.GeoDataFrame):
|
||||||
gdf = pd.DataFrame(df.drop(columns=['geometry']))
|
gdf = pd.DataFrame(df.drop(columns=['geometry']))
|
||||||
df = gdf
|
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:
|
except NotADataframeError:
|
||||||
logger.warning(f'Dashboard: cannot read dataframe for page {page.name}')
|
logger.warning(f'Dashboard: cannot read dataframe for page {page.name}')
|
||||||
except Exception as err:
|
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)
|
logger.exception(err)
|
||||||
if page.plot:
|
if page.plot:
|
||||||
try:
|
try:
|
||||||
|
@ -130,8 +139,9 @@ async def get_dashboard_page(group: str, name: str,
|
||||||
'layout': plot.layout.to_plotly_json(),
|
'layout': plot.layout.to_plotly_json(),
|
||||||
}
|
}
|
||||||
except Exception as err:
|
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)
|
logger.exception(err)
|
||||||
else:
|
else:
|
||||||
dashboard_page.plotData = dumps(plotData, cls=NumpyEncoder)
|
dp.plotData = dumps(plotData, cls=NumpyEncoder)
|
||||||
return dashboard_page
|
return dp
|
Loading…
Add table
Add a link
Reference in a new issue