Skip to content

Commit

Permalink
Merge pull request #492 from PnX-SI/feat/lint
Browse files Browse the repository at this point in the history
Feat/lint
  • Loading branch information
joelclems authored Sep 29, 2023
2 parents 461a8b3 + 84a8dc9 commit 386c4f9
Show file tree
Hide file tree
Showing 36 changed files with 408 additions and 328 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# blackify everything!
c8028f48134dbf07b632c589db900bef1ba6109f
15 changes: 15 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Lint

on: [push, pull_request]

jobs:
backend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Backend code formatting check (Black)
uses: psf/black@stable
with:
src: "setup.py ./atlas"
25 changes: 20 additions & 5 deletions atlas/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,35 @@

from atlas.configuration.config_parser import valid_config_from_dict
from atlas.configuration.config_schema import AtlasConfig, SecretSchemaConf
from atlas.env import atlas_static_folder, atlas_template_folder, atlas_config_file_path, db, cache
from atlas.env import (
atlas_static_folder,
atlas_template_folder,
atlas_config_file_path,
db,
cache,
)

compress = Compress()


def create_app():
"""
renvoie une instance de l'app Flask
"""

app = Flask(__name__, template_folder=atlas_template_folder, static_folder=atlas_static_folder)
app = Flask(
__name__,
template_folder=atlas_template_folder,
static_folder=atlas_static_folder,
)
# push the config in app config at 'PUBLIC' key
app.config.from_pyfile(str(atlas_config_file_path))

app.config.from_prefixed_env(prefix="ATLAS")
config_valid=valid_config_from_dict(copy.copy(app.config), AtlasConfig)
config_secret_valid=valid_config_from_dict(copy.copy(app.config), SecretSchemaConf)
config_valid = valid_config_from_dict(copy.copy(app.config), AtlasConfig)
config_secret_valid = valid_config_from_dict(
copy.copy(app.config), SecretSchemaConf
)

app.config.update(config_valid)
app.config.update(config_secret_valid)
Expand All @@ -46,7 +59,9 @@ def get_locale():
from atlas.atlasRoutes import main as main_blueprint

if app.config["MULTILINGUAL"]:
app.register_blueprint(main_blueprint, url_prefix="/<lang_code>", name="multi_lg")
app.register_blueprint(
main_blueprint, url_prefix="/<lang_code>", name="multi_lg"
)
app.register_blueprint(main_blueprint)

from atlas.atlasAPI import api
Expand Down
48 changes: 29 additions & 19 deletions atlas/atlasAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

api = Blueprint("api", __name__)


@api.route("/searchTaxon", methods=["GET"])
def searchTaxonAPI():
session = db.session
Expand All @@ -33,13 +34,15 @@ def searchCommuneAPI():
session.close()
return jsonify(results)

if not current_app.config['AFFICHAGE_MAILLE']:

if not current_app.config["AFFICHAGE_MAILLE"]:

@api.route("/observationsMailleAndPoint/<int:cd_ref>", methods=["GET"])
def getObservationsMailleAndPointAPI(cd_ref):
"""
Retourne les observations d'un taxon en point et en maille
Retourne les observations d'un taxon en point et en maille
:returns: dict ({'point:<GeoJson>', 'maille': 'GeoJson})
:returns: dict ({'point:<GeoJson>', 'maille': 'GeoJson})
"""
session = db.session
observations = {
Expand All @@ -55,9 +58,9 @@ def getObservationsMailleAndPointAPI(cd_ref):
@api.route("/observationsMaille/<int:cd_ref>", methods=["GET"])
def getObservationsMailleAPI(cd_ref, year_min=None, year_max=None):
"""
Retourne les observations d'un taxon par maille (et le nombre d'observation par maille)
Retourne les observations d'un taxon par maille (et le nombre d'observation par maille)
:returns: GeoJson
:returns: GeoJson
"""
session = db.session
observations = vmObservationsMaillesRepository.getObservationsMaillesChilds(
Expand All @@ -70,18 +73,18 @@ def getObservationsMailleAPI(cd_ref, year_min=None, year_max=None):
return jsonify(observations)


if not current_app.config["AFFICHAGE_MAILLE"]:


if not current_app.config['AFFICHAGE_MAILLE']:
@api.route("/observationsPoint/<int:cd_ref>", methods=["GET"])
def getObservationsPointAPI(cd_ref):
session = db.session
observations = vmObservationsRepository.searchObservationsChilds(session, cd_ref)
observations = vmObservationsRepository.searchObservationsChilds(
session, cd_ref
)
session.close()
return jsonify(observations)



@api.route("/observations/<int:cd_ref>", methods=["GET"])
def getObservationsGenericApi(cd_ref: int):
"""[summary]
Expand All @@ -93,17 +96,22 @@ def getObservationsGenericApi(cd_ref: int):
[type]: [description]
"""
session = db.session
observations = vmObservationsMaillesRepository.getObservationsMaillesChilds(
session,
cd_ref,
year_min=request.args.get("year_min"),
year_max=request.args.get("year_max"),
) if current_app.config['AFFICHAGE_MAILLE'] else vmObservationsRepository.searchObservationsChilds(session, cd_ref)
observations = (
vmObservationsMaillesRepository.getObservationsMaillesChilds(
session,
cd_ref,
year_min=request.args.get("year_min"),
year_max=request.args.get("year_max"),
)
if current_app.config["AFFICHAGE_MAILLE"]
else vmObservationsRepository.searchObservationsChilds(session, cd_ref)
)
session.close()
return jsonify(observations)


if not current_app.config['AFFICHAGE_MAILLE']:

if not current_app.config["AFFICHAGE_MAILLE"]:

@api.route("/observations/<insee>/<int:cd_ref>", methods=["GET"])
def getObservationsCommuneTaxonAPI(insee, cd_ref):
connection = db.engine.connect()
Expand Down Expand Up @@ -148,18 +156,20 @@ def getPhotosGallery():
connection.close()
return jsonify(photos)


@api.route("/main_stat", methods=["GET"])
@cache.cached()
def main_stat():
connection = db.engine.connect()
return vmObservationsRepository.statIndex(connection)


@api.route("/rank_stat", methods=["GET"])
@cache.cached()
def rank_stat():
connection = db.engine.connect()
return jsonify(
vmObservationsRepository.genericStat(
vmObservationsRepository.genericStat(
connection, current_app.config["RANG_STAT"]
)
)
)
22 changes: 15 additions & 7 deletions atlas/atlasRoutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@

@main.url_defaults
def add_language_code(endpoint, values):
if 'lang_code' in values or not g.get('lang_code', None):
if "lang_code" in values or not g.get("lang_code", None):
return
# If endpoint expects lang_code, send it forward
if current_app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
values['lang_code'] = g.lang_code
if current_app.url_map.is_endpoint_expecting(endpoint, "lang_code"):
values["lang_code"] = g.lang_code

@main.url_value_preprocessor
def pull_lang_code(endpoint, values):
if values is not None:
# If no language code has been set, get the best language from the browser settings
default_lang = request.accept_languages.best_match(current_app.config['LANGUAGES'])
g.lang_code = values.pop('lang_code', default_lang)
default_lang = request.accept_languages.best_match(
current_app.config["LANGUAGES"]
)
g.lang_code = values.pop("lang_code", default_lang)

@main.before_request
def redirect_default_language():
Expand All @@ -60,7 +62,8 @@ def redirect_default_language():
default_lang_code = session["language"]
else:
default_lang_code = request.accept_languages.best_match(
current_app.config["AVAILABLE_LANGUAGES"].keys(), current_app.config["DEFAULT_LANGUAGE"]
current_app.config["AVAILABLE_LANGUAGES"].keys(),
current_app.config["DEFAULT_LANGUAGE"],
)
view_args = request.view_args
view_args["lang_code"] = default_lang_code
Expand Down Expand Up @@ -401,10 +404,15 @@ def photos():
connection.close()
return render_template("templates/photoGalery/_main.html", groups=groups)


if current_app.config["AFFICHAGE_RECHERCHE_AVANCEE"]:

@main.route("/recherche", methods=["GET"])
def advanced_search():
return render_template("templates/core/advanced_search.html", )
return render_template(
"templates/core/advanced_search.html",
)


@main.route("/<page>", methods=["GET", "POST"])
def get_staticpages(page):
Expand Down
6 changes: 3 additions & 3 deletions atlas/configuration/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from importlib.machinery import SourceFileLoader



def get_config_module(atlas_config_file_path):
# imports the module from the given path
# imports the module from the given path
atlas_config = SourceFileLoader("config", atlas_config_file_path).load_module()
return atlas_config


def read_config_file(config_module):
return {
var: getattr(config_module, var) for var in remove_reserved_word(config_module)
Expand All @@ -22,4 +22,4 @@ def remove_reserved_word(config_module):


def valid_config_from_dict(config_dict, config_schema):
return config_schema().load(config_dict)
return config_schema().load(config_dict)
3 changes: 1 addition & 2 deletions atlas/configuration/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class Meta:
CACHE_TIMEOUT = fields.Number(load_default=3600)



class MapConfig(Schema):
LAT_LONG = fields.List(fields.Float(), load_default=[44.7952, 6.2287])
MIN_ZOOM = fields.Integer(load_default=1)
Expand Down Expand Up @@ -207,7 +206,7 @@ class Meta:
SPLIT_NOM_VERN = fields.Boolean(load_default=True)
INTERACTIVE_MAP_LIST = fields.Boolean(load_default=True)
AVAILABLE_LANGUAGES = fields.Dict(load_default=LANGUAGES)
# Flask parameter enabling auto reload of templates
# Flask parameter enabling auto reload of templates
# (no need to restart the atlas service when updating templates)
# Defaults to False to have the best performance in production
TEMPLATES_AUTO_RELOAD = fields.Boolean(load_default=False)
Expand Down
22 changes: 14 additions & 8 deletions atlas/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@

babel = Babel()

cache = Cache(config={
'CACHE_TYPE': 'SimpleCache',
})
cache = Cache(
config={
"CACHE_TYPE": "SimpleCache",
}
)

db = SQLAlchemy()

default_atlas_config_file_path=Path(__file__).parent / 'configuration/config.py'
default_atlas_static_folder=Path(__file__).parent / 'static'
default_atlas_template_folder=Path(__file__).parent
default_atlas_config_file_path = Path(__file__).parent / "configuration/config.py"
default_atlas_static_folder = Path(__file__).parent / "static"
default_atlas_template_folder = Path(__file__).parent


atlas_config_file_path=os.environ.get("ATLAS_SETTINGS", default_atlas_config_file_path)
atlas_config_file_path = os.environ.get(
"ATLAS_SETTINGS", default_atlas_config_file_path
)
atlas_static_folder = os.environ.get("ATLAS_STATIC_FOLDER", default_atlas_static_folder)
atlas_template_folder = os.environ.get("ATLAS_TEMPLATE_FOLDER", default_atlas_template_folder)
atlas_template_folder = os.environ.get(
"ATLAS_TEMPLATE_FOLDER", default_atlas_template_folder
)
11 changes: 6 additions & 5 deletions atlas/modeles/entities/tBibTaxrefRang.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

class TBibTaxrefRang(Base):
__table__ = Table(
'bib_taxref_rangs', metadata,
Column('id_rang', String(4), nullable=False, primary_key=True),
Column('nom_rang', String(20), nullable=False),
Column('tri_rang', Integer),
schema='atlas'
"bib_taxref_rangs",
metadata,
Column("id_rang", String(4), nullable=False, primary_key=True),
Column("nom_rang", String(20), nullable=False),
Column("tri_rang", Integer),
schema="atlas",
)
4 changes: 2 additions & 2 deletions atlas/modeles/entities/tCommunes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@


class LCommune(Base):
__tablename__ = 'l_communes'
__table_args__ = {u'schema': 'layers'}
__tablename__ = "l_communes"
__table_args__ = {"schema": "layers"}

insee = Column(String(5), primary_key=True)
commune_maj = Column(String(50))
Expand Down
2 changes: 1 addition & 1 deletion atlas/modeles/entities/vmAreas.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class VmAreas(Base):
Column("area_code", String(50)),
Column("area_name", String(50)),
Column("id_type", Integer(), ForeignKey("atlas.vm_bib_areas_types.id_type")),
Column("the_geom", Geometry(u"MULTIPOLYGON", 4326), index=True),
Column("the_geom", Geometry("MULTIPOLYGON", 4326), index=True),
Column("area_geojson", Text()),
schema="atlas",
autoload=True,
Expand Down
13 changes: 8 additions & 5 deletions atlas/modeles/entities/vmCommunes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

class VmCommunes(Base):
__table__ = Table(
'vm_communes', metadata,
Column('insee', String(5), primary_key=True, unique=True),
Column('commune_maj', String(50)),
"vm_communes",
metadata,
Column("insee", String(5), primary_key=True, unique=True),
Column("commune_maj", String(50)),
# Column('commune_min', String(50)),
Column('the_geom', Geometry(u'MULTIPOLYGON'), index=True),
schema='atlas', autoload=True, autoload_with=db.engine
Column("the_geom", Geometry("MULTIPOLYGON"), index=True),
schema="atlas",
autoload=True,
autoload_with=db.engine,
)
Loading

0 comments on commit 386c4f9

Please sign in to comment.