-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve municipalities search (unaccent, word part)
Resolve #531.
- Loading branch information
Showing
3 changed files
with
81 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,75 @@ | ||
# -*- coding:utf-8 -*- | ||
|
||
import ast | ||
|
||
from flask import current_app | ||
from sqlalchemy import distinct | ||
from sqlalchemy.sql import text | ||
from sqlalchemy.sql.expression import func | ||
|
||
from atlas.modeles.entities.vmCommunes import VmCommunes | ||
|
||
|
||
def getAllCommunes(session): | ||
req = session.query(distinct(VmCommunes.commune_maj), VmCommunes.insee).all() | ||
communeList = list() | ||
for r in req: | ||
temp = {"label": r[0], "value": r[1]} | ||
communeList.append(temp) | ||
return communeList | ||
|
||
|
||
def getCommunesSearch(session, search, limit=50): | ||
req = session.query( | ||
distinct(VmCommunes.commune_maj), VmCommunes.insee, func.length(VmCommunes.commune_maj) | ||
).filter(VmCommunes.commune_maj.ilike("%" + search + "%")) | ||
|
||
req = req.order_by(VmCommunes.commune_maj) | ||
|
||
req = req.limit(limit).all() | ||
|
||
communeList = list() | ||
for r in req: | ||
temp = {"label": r[0], "value": r[1]} | ||
communeList.append(temp) | ||
return communeList | ||
|
||
|
||
def getCommuneFromInsee(connection, insee): | ||
sql = """ | ||
SELECT c.commune_maj, | ||
c.insee, | ||
c.commune_geojson | ||
FROM atlas.vm_communes c | ||
WHERE c.insee = :thisInsee | ||
""" | ||
req = connection.execute(text(sql), thisInsee=insee) | ||
communeObj = dict() | ||
for r in req: | ||
communeObj = { | ||
"areaName": r.commune_maj, | ||
"areaCode": str(r.insee), | ||
"areaGeoJson": ast.literal_eval(r.commune_geojson), | ||
} | ||
return communeObj | ||
|
||
|
||
def getCommunesObservationsChilds(connection, cd_ref): | ||
sql = """ | ||
SELECT DISTINCT (com.insee) AS insee, com.commune_maj | ||
FROM atlas.vm_communes com | ||
JOIN atlas.vm_observations obs | ||
ON obs.insee = com.insee | ||
WHERE obs.cd_ref IN ( | ||
SELECT * FROM atlas.find_all_taxons_childs(:thiscdref) | ||
) | ||
OR obs.cd_ref = :thiscdref | ||
ORDER BY com.commune_maj ASC | ||
""" | ||
req = connection.execute(text(sql), thiscdref=cd_ref) | ||
listCommunes = list() | ||
for r in req: | ||
temp = {"insee": r.insee, "commune_maj": r.commune_maj} | ||
listCommunes.append(temp) | ||
return listCommunes | ||
# -*- coding:utf-8 -*- | ||
|
||
import ast | ||
|
||
from sqlalchemy import distinct | ||
from sqlalchemy.sql import text | ||
from sqlalchemy.sql.expression import func | ||
|
||
from atlas.modeles.entities.vmCommunes import VmCommunes | ||
|
||
|
||
def getAllCommunes(session): | ||
req = session.query(distinct(VmCommunes.commune_maj), VmCommunes.insee).all() | ||
communeList = list() | ||
for r in req: | ||
temp = {"label": r[0], "value": r[1]} | ||
communeList.append(temp) | ||
return communeList | ||
|
||
|
||
def searchMunicipalities(session, search, limit=50): | ||
like_search = "%" + search.replace(" ", "%") + "%" | ||
|
||
query = ( | ||
session.query( | ||
distinct(VmCommunes.commune_maj), | ||
VmCommunes.insee, | ||
func.length(VmCommunes.commune_maj), | ||
) | ||
.filter(func.unaccent(VmCommunes.commune_maj).ilike(func.unaccent(like_search))) | ||
.order_by(VmCommunes.commune_maj) | ||
.limit(limit) | ||
) | ||
results = query.all() | ||
|
||
return [{"label": r[0], "value": r[1]} for r in results] | ||
|
||
|
||
def getCommuneFromInsee(connection, insee): | ||
sql = """ | ||
SELECT c.commune_maj, | ||
c.insee, | ||
c.commune_geojson | ||
FROM atlas.vm_communes c | ||
WHERE c.insee = :thisInsee | ||
""" | ||
req = connection.execute(text(sql), thisInsee=insee) | ||
communeObj = dict() | ||
for r in req: | ||
communeObj = { | ||
"areaName": r.commune_maj, | ||
"areaCode": str(r.insee), | ||
"areaGeoJson": ast.literal_eval(r.commune_geojson), | ||
} | ||
return communeObj | ||
|
||
|
||
def getCommunesObservationsChilds(connection, cd_ref): | ||
sql = """ | ||
SELECT DISTINCT (com.insee) AS insee, com.commune_maj | ||
FROM atlas.vm_communes com | ||
JOIN atlas.vm_observations obs | ||
ON obs.insee = com.insee | ||
WHERE obs.cd_ref IN ( | ||
SELECT * FROM atlas.find_all_taxons_childs(:thiscdref) | ||
) | ||
OR obs.cd_ref = :thiscdref | ||
ORDER BY com.commune_maj ASC | ||
""" | ||
req = connection.execute(text(sql), thiscdref=cd_ref) | ||
listCommunes = list() | ||
for r in req: | ||
temp = {"insee": r.insee, "commune_maj": r.commune_maj} | ||
listCommunes.append(temp) | ||
return listCommunes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters