Skip to content

Commit

Permalink
Merge pull request #311 from PnX-SI/dev
Browse files Browse the repository at this point in the history
v0.99.4-dev
  • Loading branch information
lpofredc authored Oct 5, 2021
2 parents 4c486ac + df56257 commit 2f937ff
Show file tree
Hide file tree
Showing 71 changed files with 5,052 additions and 3,986 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.99.3-dev
0.99.4-dev
22 changes: 0 additions & 22 deletions backend/dev-requirements.txt

This file was deleted.

26 changes: 16 additions & 10 deletions backend/gncitizen/core/badges/routes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from flask import Flask, request, Blueprint, Response, jsonify, current_app
from gncitizen.utils.sqlalchemy import json_resp
from gncitizen.core.observations.models import ObservationModel
from calendar import monthrange
from datetime import datetime, timedelta

from flask import Blueprint, Flask, Response, current_app, jsonify, request
from sqlalchemy.sql.expression import func
from utils_flask_sqla.response import json_resp

from gncitizen.core.commons.models import ProgramsModel
from gncitizen.core.users.models import UserModel
from gncitizen.core.observations.models import ObservationModel
from gncitizen.core.taxonomy.models import Taxref
from sqlalchemy.sql.expression import func
from datetime import date, datetime, timedelta
from calendar import monthrange
from gncitizen.core.users.models import UserModel
from server import db

badges_api = Blueprint("badges", __name__)
Expand Down Expand Up @@ -35,7 +37,8 @@ def get_rewards(id):
total_obs = total_obs + item.nb_obs
taxon_classe_query = (
db.session.query(
Taxref.classe.label("classe"), func.count(Taxref.famille).label("nb_obs")
Taxref.classe.label("classe"),
func.count(Taxref.famille).label("nb_obs"),
)
.join(ObservationModel, Taxref.cd_nom == ObservationModel.cd_nom)
.filter(ObservationModel.id_role == id)
Expand All @@ -46,7 +49,8 @@ def get_rewards(id):

taxon_famille_query = (
db.session.query(
Taxref.famille.label("famille"), func.count(Taxref.famille).label("nb_obs")
Taxref.famille.label("famille"),
func.count(Taxref.famille).label("nb_obs"),
)
.join(ObservationModel, Taxref.cd_nom == ObservationModel.cd_nom)
.filter(ObservationModel.id_role == id)
Expand All @@ -59,7 +63,9 @@ def get_rewards(id):
user = UserModel.query.filter(UserModel.id_user == id).one()
result = user.as_secured_dict(True)
user_date_create = result["timestamp_create"]
user_date_create = datetime.strptime(user_date_create, "%Y-%m-%dT%H:%M:%S.%f")
user_date_create = datetime.strptime(
user_date_create, "%Y-%m-%dT%H:%M:%S.%f"
)

for reward in rewards:

Expand Down
77 changes: 34 additions & 43 deletions backend/gncitizen/core/commons/admin.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import json
import urllib.parse

import requests

from flask import Blueprint, current_app, request, flash
from flask_admin.contrib.geoa import ModelView
from flask_admin.form import SecureForm
from flask import current_app, flash
from flask_admin.contrib.sqla.view import ModelView
from flask_admin.form.upload import FileUploadField
from flask_ckeditor import CKEditorField

from geoalchemy2.shape import from_shape
from geojson import FeatureCollection
from shapely.geometry import MultiPolygon, asShape
from flask_admin.model.form import InlineFormAdmin
from flask_ckeditor import CKEditorField
from wtforms import SelectField

import json
from flask_admin.contrib.sqla.view import ModelView
from jinja2 import Markup

from gncitizen.core.users.models import UserModel
from gncitizen.core.sites.models import CorProgramSiteTypeModel
from gncitizen.utils.env import admin, MEDIA_DIR
from gncitizen.utils.errors import GeonatureApiError
from gncitizen.utils.sqlalchemy import json_resp
from server import db

from .models import ProgramsModel
from gncitizen.core.taxonomy.models import BibListes
import os
from gncitizen.utils.admin import (
CustomJSONField,
CustomTileView,
json_formatter,
)
from gncitizen.utils.env import MEDIA_DIR


def json_formatter(view, context, model, name):
value = getattr(model, name)
json_value = json.dumps(value, ensure_ascii=False, indent=2)
return Markup("<pre>{}</pre>".format(json_value))
logger = current_app.logger


def taxonomy_lists():
Expand Down Expand Up @@ -64,30 +45,31 @@ def taxonomy_lists():
return taxonomy_lists


from flask_admin.model.form import InlineFormAdmin


class CorProgramSiteTypeModelInlineForm(InlineFormAdmin):
form_columns = ("site_type",)


class ProjectView(ModelView):
form_overrides = {"long_desc": CKEditorField}
create_template = "edit.html"
create_template = "edit.html"
edit_template = "edit.html"
form_excluded_columns = ["timestamp_create", "timestamp_update"]
column_exclude_list = ['long_desc','short_desc']
column_exclude_list = ["long_desc", "short_desc"]


class ProgramView(ModelView):
# form_base_class = SecureForm
form_overrides = {"long_desc": CKEditorField, "taxonomy_list": SelectField}
form_args = {"taxonomy_list": {"choices": taxonomy_lists(), "coerce": int}}
create_template = "edit.html"
edit_template = "edit.html"
form_excluded_columns = ["timestamp_create", "timestamp_update"]
column_exclude_list = ['long_desc','form_message','short_desc','image','logo']
column_exclude_list = [
"long_desc",
"form_message",
"short_desc",
"image",
"logo",
]
inline_models = [
(
CorProgramSiteTypeModel,
Expand All @@ -100,22 +82,27 @@ class ProgramView(ModelView):


class CustomFormView(ModelView):
form_overrides = {"json_schema": CustomJSONField}
column_formatters = {
"json_schema": json_formatter,
}


class UserView(ModelView):
column_exclude_list = ["password"]
form_excluded_columns = ["timestamp_create", "timestamp_update", "password"]
form_excluded_columns = [
"timestamp_create",
"timestamp_update",
"password",
]


def get_geom_file_path(obj, file_data):
return "geometries/{}".format(file_data.filename)


class GeometryView(ModelView):
column_exclude_list = ["geom"]
class GeometryView(CustomTileView):
# column_exclude_list = ["geom"]
form_excluded_columns = ["timestamp_create", "timestamp_update"]
form_overrides = dict(geom_file=FileUploadField)
form_args = dict(
Expand All @@ -124,7 +111,7 @@ class GeometryView(ModelView):
description="""
Le fichier contenant la géométrie de la zone doit être au format geojson ou kml.<br>
Seules les types Polygon et MultiPolygon (ou MultiGeometry pour kml) sont acceptées.<br>
Les fichiers GeoJson fournis devront être en projection WGS84 (donc SRID 4326)
Les fichiers GeoJson fournis devront être en projection WGS84 (donc SRID 4326)
et respecter le format "FeatureCollection" tel que présenté ici :
https://tools.ietf.org/html/rfc7946#section-1.5.
""",
Expand All @@ -135,9 +122,13 @@ class GeometryView(ModelView):
)

def on_model_change(self, form, model, is_created):
model.set_geom_from_geom_file()
logger.debug(f"data {form.data}")
logger.debug(f"geom_file {form.geom_file}")
logger.debug(f"model {dir(model)}")
if form.data["geom_file"]:
model.set_geom_from_geom_file()

def handle_view_exception(self, exc):
flash("Une erreur s'est produite ({})".format(exc), "error")
logger.critical(exc)
return True

49 changes: 31 additions & 18 deletions backend/gncitizen/core/commons/models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from datetime import datetime
import json
import os
import uuid
import xml.etree.ElementTree as ET
from datetime import datetime

from geoalchemy2 import Geometry
from geoalchemy2.functions import (
ST_GeomFromGeoJSON,
ST_GeomFromKML,
ST_SetSRID,
)
from sqlalchemy import ForeignKey
from sqlalchemy.sql import expression
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.sql import expression
from utils_flask_sqla_geo.serializers import geoserializable, serializable

from gncitizen.core.taxonomy.models import BibListes
from gncitizen.utils.env import db, MEDIA_DIR
from gncitizen.utils.sqlalchemy import serializable, geoserializable
import os
from gncitizen.utils.env import MEDIA_DIR, db


class TimestampMixinModel(object):
Expand Down Expand Up @@ -65,12 +72,9 @@ def __repr__(self):
return self.name


from geoalchemy2.functions import ST_GeomFromKML, ST_GeomFromGeoJSON, ST_SetSRID
import json
import xml.etree.ElementTree as ET


@serializable
@geoserializable
class GeometryModel(TimestampMixinModel, db.Model):
"""Table des géométries associées aux programmes"""

Expand Down Expand Up @@ -104,7 +108,9 @@ def set_geom_from_geom_file(self):
if abs(x) > 180 or abs(y) > 180:
raise Exception("Mauvais système de projection")
# Convert Geo
self.geom = ST_SetSRID(ST_GeomFromGeoJSON(json.dumps(json_geom)), 4326)
self.geom = ST_SetSRID(
ST_GeomFromGeoJSON(json.dumps(json_geom)), 4326
)
elif ext == ".kml":
kml_root = ET.fromstring(geo_data)
kml_geom_elt = None
Expand All @@ -119,7 +125,9 @@ def set_geom_from_geom_file(self):
raise Exception(gnc_invalid_err_message)
if kml_geom_elt is None:
raise Exception(gnc_invalid_err_message)
kml_geom = ET.tostring(kml_geom_elt, encoding="unicode", method="xml")
kml_geom = ET.tostring(
kml_geom_elt, encoding="unicode", method="xml"
)
self.geom = ST_GeomFromKML(kml_geom) # KML is always 4326 srid

def __repr__(self):
Expand Down Expand Up @@ -169,11 +177,16 @@ class ProgramsModel(TimestampMixinModel, db.Model):
image = db.Column(db.String(250))
logo = db.Column(db.String(250))
id_module = db.Column(
db.Integer, ForeignKey(TModules.id_module), nullable=False, default=1,
db.Integer,
ForeignKey(TModules.id_module),
nullable=False,
default=1,
)
module = relationship("TModules")
taxonomy_list = db.Column(db.Integer, nullable=True)
is_active = db.Column(db.Boolean(), server_default=expression.true(), default=True)
is_active = db.Column(
db.Boolean(), server_default=expression.true(), default=True
)
id_geom = db.Column(
db.Integer, db.ForeignKey(GeometryModel.id_geom), nullable=False
)
Expand All @@ -187,7 +200,9 @@ class ProgramsModel(TimestampMixinModel, db.Model):
def get_geofeature(self, recursif=True, columns=None):
geometry = to_shape(self.geometry.geom)
feature = Feature(
id=self.id_program, geometry=geometry, properties=self.as_dict(True),
id=self.id_program,
geometry=geometry,
properties=self.as_dict(True),
)
return feature

Expand All @@ -196,10 +211,8 @@ def __repr__(self):


@serializable
@geoserializable
class MediaModel(TimestampMixinModel, db.Model):
"""Table des Programmes de GeoNature-citizen
"""
"""Table des Programmes de GeoNature-citizen"""

__tablename__ = "t_medias"
__table_args__ = {"schema": "gnc_core"}
Expand Down
Loading

0 comments on commit 2f937ff

Please sign in to comment.