Admins basket details

This commit is contained in:
phil 2024-02-13 19:08:06 +05:30
parent c1f229f805
commit b00bf1f9f9
3 changed files with 94 additions and 47 deletions

View file

@ -1,8 +1,8 @@
import logging
from fastapi import Depends, FastAPI, HTTPException, status, responses
from gisaf.models.admin import Basket, BasketNameOnly
from gisaf.models.admin import AdminBasket, BasketNameOnly
from gisaf.models.authentication import User
from gisaf.security import get_current_active_user
from gisaf.admin import manager
@ -21,3 +21,20 @@ async def get_baskets(
BasketNameOnly(name=name)
for name, basket in (await manager.baskets_for_role(user)).items()
]
@api.get('/basket/{name}')
async def get_basket(
name: str,
user: User = Depends(get_current_active_user),
) -> AdminBasket:
basket = manager.baskets[name]
if basket.role and not user.has_role(basket.role):
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
return AdminBasket(
name=name,
files=await basket.get_files(),
columns=basket.columns,
uploadFields=basket.upload_fields,
## TODO: Fix projects
# projects=getattr(basket, 'projects', None)
)

View file

@ -9,14 +9,16 @@ from typing import ClassVar
# from aiohttp.multipart import MultipartReader
# from aiohttp.web import HTTPUnauthorized, HTTPForbidden
from sqlmodel import select
from gisaf.config import conf
from gisaf.models.admin import FileImport
from gisaf.models.authentication import User
# from gisaf.models.graphql import AdminBasketFile, BasketImportResult
from gisaf.models.survey import Surveyor, Accuracy, Equipment, AccuracyEquimentSurveyorMapping
from gisaf.models.project import Project
from gisaf.importers import RawSurveyImporter, GeoDataImporter, LineWorkImporter, ImportError
from gisaf.utils import ToMigrate
from gisaf.database import db_session
from gisaf.importers import RawSurveyImporter, GeoDataImporter, LineWorkImporter, ImportError
from gisaf.models.admin import FileImport, AdminBasketFile, BasketImportResult
from gisaf.models.authentication import User
from gisaf.models.survey import Surveyor, Equipment
from gisaf.models.project import Project
logger = logging.getLogger(__name__)
@ -58,41 +60,46 @@ class Basket:
else:
return False
async def get_files(self, convert_path=False):
"""
Get a dataframe of FileImport items in the basket.
"""
where = FileImport.basket==self.name
## First, get the list of files in the base_dir, then associate with the FileImport instance
df = await FileImport.get_df(
where=where,
with_related=True
#with_only_columns=['id', 'path', 'time', 'status', 'table'],
)
df.rename(columns={
'gisaf_admin_project_name': 'project',
'gisaf_survey_surveyor_name': 'surveyor',
'gisaf_survey_equipment_name': 'equipment',
}, inplace=True)
## Sanity check
df.dropna(subset=['name'], inplace=True)
df['dir'] = df.dir.fillna('.')
df.reset_index(drop=True, inplace=True)
async def get_files(self) -> list[FileImport]:
async with db_session() as session:
data = await session.exec(select(FileImport).where(FileImport.basket==self.name))
return data.all()
## TODO: After the old admin is completely off and all is clean and nice, remove below and just:
# return df
# async def get_files_df(self, convert_path=False):
# """
# Get a dataframe of FileImport items in the basket.
# """
# where = FileImport.basket==self.name
# ## First, get the list of files in the base_dir, then associate with the FileImport instance
# df = await FileImport.get_df(
# where=where,
# with_related=True
# #with_only_columns=['id', 'path', 'time', 'status', 'table'],
# )
# df.rename(columns={
# 'gisaf_admin_project_name': 'project',
# 'gisaf_survey_surveyor_name': 'surveyor',
# 'gisaf_survey_equipment_name': 'equipment',
# }, inplace=True)
# ## Sanity check
# df.dropna(subset=['name'], inplace=True)
# df['dir'] = df.dir.fillna('.')
# df.reset_index(drop=True, inplace=True)
## Until the compatibility with old admin is required and we're sure nothing is destroyed:
## Get files on the file system
if len(df) == 0:
return df
if convert_path:
df['path'] = df.apply(lambda fi: Path(fi['dir'])/fi['name'], axis=1)
#if check_fs:
# files = set(self.base_dir.glob('**/*'))
# df['exists'] = df.apply(lambda fi: self.base_dir/fi['dir']/fi['name'] in files, axis=1)
else:
return df
# ## TODO: After the old admin is completely off and all is clean and nice, remove below and just:
# # return df
# ## Until the compatibility with old admin is required and we're sure nothing is destroyed:
# ## Get files on the file system
# if len(df) == 0:
# return df
# if convert_path:
# df['path'] = df.apply(lambda fi: Path(fi['dir'])/fi['name'], axis=1)
# #if check_fs:
# # files = set(self.base_dir.glob('**/*'))
# # df['exists'] = df.apply(lambda fi: self.base_dir/fi['dir']/fi['name'] in files, axis=1)
# else:
# return df
async def get_file(self, id):
df = await FileImport.get_df(

View file

@ -8,7 +8,7 @@ import pandas as pd
from gisaf.models.models_base import Model
from gisaf.models.survey import Surveyor, Equipment
from gisaf.models.project import Project
from gisaf.models.metadata import gisaf_admin
from gisaf.models.metadata import gisaf_admin, gisaf_survey
re_file_import_record_date_expr = '^(\S+)-(\d\d\d\d)-(\d\d)-(\d\d).*$'
@ -38,7 +38,7 @@ def get_file_import_date(record) -> date:
year=int(fname_search.group(2)))
class FileImport(Model):
class FileImport(Model, table=True):
"""
Files to import or imported in the DB.
Give either url or path.
@ -58,13 +58,13 @@ class FileImport(Model):
status: str
store: str
basket: str
project_id: int = Field(foreign_key='project.id')
project_id: int = Field(foreign_key=gisaf_admin.table('project.id'))
project: Project = Relationship()
# ALTER TABLE gisaf_admin.file_import add column project_id INT REFERENCES gisaf_admin.project;
surveyor_id: int = Field(foreign_key='surveyor.id')
surveyor_id: int = Field(foreign_key=gisaf_survey.table('surveyor.id'))
surveyor: Surveyor = Relationship()
# ALTER TABLE gisaf_admin.file_import add column surveyor_id INT REFERENCES gisaf_survey.surveyor;
equipment_id: int = Field(foreign_key='equipment.id')
equipment_id: int = Field(foreign_key=gisaf_survey.table('equipment.id'))
equipment: Equipment = Relationship()
# ALTER TABLE gisaf_admin.file_import add column equipment_id INT REFERENCES gisaf_survey.equipment;
@ -112,7 +112,7 @@ class FileImport(Model):
# return os_path.exists(self.get_absolute_path())
class FeatureImportData(Model):
class FeatureImportData(Model, table=True):
"""
Keep track of imported data, typically from shapefiles
"""
@ -159,4 +159,27 @@ class Basket(BasketNameOnly):
class BasketImportResult(BaseModel):
time: datetime
message: str
details: str
details: str
class AdminBasketFile(BaseModel):
id: int
dir: str
name: str
url: str
md5: str
time: datetime
comment: str
status: str
store: str
project: str
surveyor: str
equipment: str
import_result: str
class AdminBasket(BaseModel):
name: str
files: list[FileImport]
columns: list[str]
uploadFields: list[str]
projects: list[str] = []