Migrate from dyn_join_with to selectinload

This commit is contained in:
phil 2024-01-03 17:07:11 +05:30
parent be8d7c3175
commit 1e3678fb69
5 changed files with 73 additions and 41 deletions

View file

@ -76,12 +76,8 @@ class FileImport(Model):
return f'<gisaf.misc.FileImport (gisaf_admin.file_import) {self.path}>'
@classmethod
def dyn_join_with(cls):
return {
'project': Project,
'surveyor': Surveyor,
'equipment': Equipment,
}
def selectinload(cls):
return [cls.project, cls.surveyor, cls.equipment]
def set_import_time(self):
self.time = datetime.now()

View file

@ -95,13 +95,21 @@ class BaseSurveyModel(BaseModel):
date: date
@classmethod
def dyn_join_with(cls):
return {
'equipment': Equipment,
'surveyor': Surveyor,
'accuracy': Accuracy,
'project': Project,
}
def selectinload(cls):
return [
cls.equipment,
cls.surveyor,
cls.accuracy,
cls.project,
]
# @classmethod
# def dyn_join_with(cls):
# return {
# 'equipment': Equipment,
# 'surveyor': Surveyor,
# 'accuracy': Accuracy,
# 'project': Project,
# }
#async def get_info(self):
# info = await super(BaseSurveyModel, self).get_info()
@ -313,7 +321,7 @@ class GeoModelNoStatus(Model):
get_gdf_with_related: ClassVar[bool] = False
"""
get_gdf_with_related indicates that get_df (thus, get_geo_df and the geoJson API for
the map online) gets related models (1-n relations, as defined with _join_with and dyn_join_with)
the map online) gets related models (1-n relations, as defined with selectinload)
by default.
It can be overridden with the with_related parameter when calling get_df.
"""
@ -371,12 +379,12 @@ class GeoModelNoStatus(Model):
"""
return {}
@classmethod
def get_join_with(cls):
if hasattr(cls, 'dyn_join_with'):
return cls.dyn_join_with()
else:
return cls._join_with
# @classmethod
# def get_join_with(cls):
# if hasattr(cls, 'dyn_join_with'):
# return cls.dyn_join_with()
# else:
# return cls._join_with
#@classmethod
#def get_style(cls):

View file

@ -1,6 +1,6 @@
from typing import Any
from sqlmodel import Field, String, JSON, Column
from sqlmodel import Field, String, JSON, Relationship
from gisaf.models.models_base import Model
from gisaf.models.metadata import gisaf_map
@ -51,13 +51,21 @@ class BaseMapLayer(Model):
id: int | None = Field(primary_key=True, default=None)
base_map_id: int = Field(foreign_key='base_map.id', index=True)
## Subclasses must include:
# base_map: BaseMap = Relationship()
store: str = Field(sa_type=String(100))
@classmethod
def dyn_join_with(cls):
return {
'base_map': BaseMap,
}
def selectinload(cls):
return [
cls.base_map
]
# @classmethod
# def dyn_join_with(cls):
# return {
# 'base_map': BaseMap,
# }
def __repr__(self):
return f"<models.BaseMapLayer {self.store or '':s}>"

View file

@ -16,13 +16,23 @@ class RawSurveyModel(BaseSurveyModel, GeoPointMModel):
project_id: int | None = Field(foreign_key='project.id')
category: str = Field(foreign_key='category.name')
in_menu: bool = False
# Subclasses must include:
# project: Project = Relationship()
# category_info: Project = Relationship()
@classmethod
def dyn_join_with(cls):
return {
'project': Project.on(cls.project_id == Project.id),
'category_info': Category.on(cls.category == Category.name),
}
def selectinload(cls):
return [
cls.project,
cls.category_info
]
# @classmethod
# def dyn_join_with(cls):
# return {
# 'project': Project.on(cls.project_id == Project.id),
# 'category_info': Category.on(cls.category == Category.name),
# }
#id = db.Column(db.BigInteger, primary_key=True)
## XXX: Can remove the rest since it's is in the GeoPointSurveyModel class?

View file

@ -1,12 +1,12 @@
from enum import Enum
from sqlmodel import Field
from sqlmodel import Field, Relationship
from gisaf.models.models_base import Model
from gisaf.models.metadata import gisaf_survey
class Accuracy(Model):
class Accuracy(Model, table=True):
metadata = gisaf_survey
class Admin:
@ -24,7 +24,7 @@ class Accuracy(Model):
return f'<models.Accuracy {self.name}>'
class Surveyor(Model):
class Surveyor(Model, table=True):
metadata = gisaf_survey
class Admin:
@ -41,7 +41,7 @@ class Surveyor(Model):
return f'<models.Surveyor {self.name}>'
class Equipment(Model):
class Equipment(Model, table=True):
metadata = gisaf_survey
class Admin:
@ -61,7 +61,7 @@ class GeometryType(str, Enum):
point = 'Point'
line_work = 'Line_work'
class AccuracyEquimentSurveyorMapping(Model):
class AccuracyEquimentSurveyorMapping(Model, table=True):
metadata = gisaf_survey
__tablename__ = 'accuracy_equiment_surveyor_mapping'
@ -73,12 +73,22 @@ class AccuracyEquimentSurveyorMapping(Model):
equipment_id: int = Field(foreign_key='equipment.id', index=True)
geometry_type: GeometryType = Field(default='Point', index=True)
accuracy_id: int = Field(foreign_key='accuracy.id')
surveyor: Surveyor = Relationship()
accuracy: Accuracy = Relationship()
equipment: Equipment = Relationship()
@classmethod
def dyn_join_with(cls):
return {
'surveyor': Surveyor,
'equipment': Equipment,
'accuracy': Accuracy,
}
def selectinload(cls):
return [
cls.surveyor,
cls.equipment,
cls.accuracy,
]
# @classmethod
# def dyn_join_with(cls):
# return {
# 'surveyor': Surveyor,
# 'equipment': Equipment,
# 'accuracy': Accuracy,
# }