from pycen import con_fon view_typprop_x = ''' DROP VIEW IF EXISTS {sch}.{view}; CREATE OR REPLACE VIEW {sch}.{view} AS SELECT p.par_id, p.codcom, p.ccopre, p.ccosec, p.dnupla, p.dparpi, p.dcntpa, p.ccocomm, p.ccoprem, p.ccosecm, p.dnuplam, p.type, p.geom, typprop_lib, typprop, array_agg(dnuper) dnuper, array_agg(ddenom) ddenom FROM "{sch}".parcelles{_cen} p LEFT JOIN "{sch}".d_typprop USING (typprop_id) LEFT JOIN "{sch}".lots{_cen} USING (par_id) LEFT JOIN "{sch}".cadastre{_cen} USING (lot_id) LEFT JOIN "{sch}".cptprop{_cen} USING (dnupro) LEFT JOIN "{sch}".r_prop_cptprop{_cen} USING (dnupro) LEFT JOIN "{sch}".proprios{_cen} USING (dnuper) GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 ''' grant = """ GRANT ALL ON TABLE {sch}.{view} TO grp_admin; GRANT SELECT ON TABLE {sch}.{view} TO grp_consult; """ with con_fon.begin() as cnx: cnx.execute(view_typprop_x.format(sch='cadastre', _cen= '', view='v_prop_parcelles')) cnx.execute(view_typprop_x.format(sch='cadastre', _cen= '_cen', view='v_prop_parcelles_cen')) cnx.execute(grant.format(sch='cadastre', view='v_prop_parcelles')) cnx.execute(grant.format(sch='cadastre', view='v_prop_parcelles_cen')) view_natcult_x = ''' DROP VIEW IF EXISTS {sch}.{view}; CREATE OR REPLACE VIEW {sch}.{view} AS SELECT p.par_id, p.geom, array_agg(d_dsgrpf.natcult_ssgrp) natcult_ssgrp, array_agg(d_cgrnum.natcult_grp) natcult_grp, array_agg(d_cnatsp.natcult_natspe) natcult_natspe FROM "{sch}".parcelles{_cen} p LEFT JOIN "{sch}".lots{_cen} USING (par_id) LEFT JOIN "{sch}".lots_natcult{_cen} USING (lot_id) LEFT JOIN "{sch}".d_dsgrpf USING (dsgrpf) LEFT JOIN "{sch}".d_cgrnum USING (cgrnum) LEFT JOIN "{sch}".d_cnatsp USING (cnatsp) GROUP BY par_id ''' with con_fon.begin() as cnx: cnx.execute(view_natcult_x.format(sch='cadastre', _cen= '', view='v_natcult')) cnx.execute(view_natcult_x.format(sch='cadastre', _cen= '_cen', view='v_natcult_cen')) cnx.execute(grant.format(sch='cadastre', view='v_natcult')) cnx.execute(grant.format(sch='cadastre', view='v_natcult_cen'))