Skip to content

Commit

Permalink
Createing site model views
Browse files Browse the repository at this point in the history
  • Loading branch information
hypsug0 committed Mar 28, 2024
1 parent 975ee1b commit e6d7617
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
60 changes: 60 additions & 0 deletions backend/gncitizen/core/sites/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
from flask_admin.actions import action
from flask_admin.contrib.sqla.view import ModelView
from gncitizen.utils.admin import CustomJSONField, CustomTileView, json_formatter
from server import db
from wtforms import SelectField

from .models import SiteModel, VisitModel


def enum_formatter(view, context, model, name):
enum_attr = getattr(model, name)
return enum_attr.value


class SiteView(CustomTileView):
can_export = True
inline_models = [
VisitModel,
]
# column_exclude_list = ["geom"]
column_list = [
"id_site",
"name",
"visits",
"site_type.type",
"id_program",
"program.title",
"geom",
]
column_filters = (
"name",
"site_type.type",
"id_program",
"program.title",
"program.unique_id_program",
)
column_searchable_list = (
"name",
"site_type.type",
"id_program",
"program.title",
"program.unique_id_program",
)
can_create = False


class VisitView(CustomTileView):
can_export = True
form_overrides = {"json_data": CustomJSONField}
column_formatters = {
"json_data": json_formatter,
}
# column_exclude_list = ["geom"]
column_filters = (
"date",
"site.site_type.type",
"site.id_program",
"site.program.title",
"site.program.unique_id_program",
)
can_create = False


class SiteTypeView(ModelView):
Expand Down
13 changes: 6 additions & 7 deletions backend/gncitizen/core/sites/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
# import enum

from geoalchemy2 import Geometry
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import relationship
from utils_flask_sqla_geo.serializers import geoserializable, serializable

from gncitizen.core.commons.models import (
CustomFormModel,
MediaModel,
Expand All @@ -16,6 +12,9 @@
from gncitizen.core.observations.models import ObservationModel
from gncitizen.core.users.models import ObserverMixinModel
from server import db
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import relationship
from utils_flask_sqla_geo.serializers import geoserializable, serializable


def create_schema(db):
Expand Down Expand Up @@ -69,7 +68,7 @@ class SiteModel(TimestampMixinModel, ObserverMixinModel, db.Model):
)

def __repr__(self):
return "<Site {0}>".format(self.id_site)
return f"Site #{self.id_site} - {self.name}"


@serializable
Expand Down Expand Up @@ -97,12 +96,12 @@ class VisitModel(TimestampMixinModel, ObserverMixinModel, db.Model):
id_site = db.Column(
db.Integer, db.ForeignKey(SiteModel.id_site, ondelete="CASCADE"), index=True
)
site = relationship("SiteModel")
site = db.relationship("SiteModel", backref=db.backref("visits"))
date = db.Column(db.Date)
json_data = db.Column(JSONB, nullable=True)

def __repr__(self):
return "<Visit {0}>".format(self.id_visit)
return f"Visit #{self.id_visit}"


class MediaOnVisitModel(TimestampMixinModel, db.Model):
Expand Down
14 changes: 9 additions & 5 deletions backend/gncitizen/core/sites/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
from flask_jwt_extended import jwt_required
from geoalchemy2.shape import from_shape
from geojson import FeatureCollection
from shapely.geometry import Point, asShape
from sqlalchemy import or_
from utils_flask_sqla.response import json_resp
from utils_flask_sqla_geo.generic import get_geojson_feature

from gncitizen.core.commons.models import MediaModel, ProgramsModel
from gncitizen.core.users.models import UserModel
from gncitizen.utils.env import admin
from gncitizen.utils.errors import GeonatureApiError
from gncitizen.utils.jwt import get_id_role_if_exists, get_user_if_exists
from gncitizen.utils.media import save_upload_files
from server import db
from shapely.geometry import Point, asShape
from sqlalchemy import or_
from utils_flask_sqla.response import json_resp
from utils_flask_sqla_geo.generic import get_geojson_feature

from .admin import SiteView, VisitView
from .models import MediaOnVisitModel, SiteModel, SiteTypeModel, VisitModel

sites_api = Blueprint("sites", __name__)

admin.add_view(SiteView(SiteModel, db.session, "Sites", category="Sites"))
admin.add_view(VisitView(VisitModel, db.session, "Visites", category="Sites"))


@sites_api.route("/types", methods=["GET"])
@json_resp
Expand Down

0 comments on commit e6d7617

Please sign in to comment.