Skip to content

Commit

Permalink
Merge pull request #1 from geoadmin/0.3.6
Browse files Browse the repository at this point in the history
0.4.0
  • Loading branch information
benoitregamey authored Sep 25, 2024
2 parents 19fcd20 + 7e870a8 commit bbfa13b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 77 deletions.
9 changes: 0 additions & 9 deletions bin/geocat_backup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import os
import colorama
from geopycat.GeocatBackup import GeocatBackup

Expand All @@ -13,18 +12,10 @@
parser.add_argument("-u", "--users", action="store_false")
parser.add_argument("-g", "--groups", action="store_false")
parser.add_argument("-s", "--subtpl", action="store_false")
parser.add_argument("-db-user")
parser.add_argument("-db-password")

args = parser.parse_args()

if __name__ == "__main__":

if args.db_user is not None:
os.environ["DB_USERNAME"] = args.db_user

if args.db_password is not None:
os.environ["DB_PASSWORD"] = args.db_password

GeocatBackup(env=args.env, backup_dir=args.output_folder, catalogue=args.metadata,
users=args.users, groups=args.groups, subtemplates=args.subtpl)
26 changes: 4 additions & 22 deletions geopycat/GeocatBackup/backup_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import json
import psycopg2
import pandas as pd
import xml.etree.ElementTree as ET
from datetime import datetime
Expand Down Expand Up @@ -380,30 +379,13 @@ def __backup_harvesting_settings(self):
harvesting = dict()

try:
connection = self.db_connect()
res = self.session.get(f"{self.env}/geonetwork/srv/fre/admin.harvester.list?_content_type=json")

with connection.cursor() as cursor:

cursor.execute("SELECT name,value FROM public.harvestersettings " \
"WHERE value != '' ORDER BY id ASC")

for row in cursor:
if row[0] == "name":
name = row[1]
harvesting[name] = {}
else:
if "name" in locals():
harvesting[name][row[0]] = row[1]

except (Exception, psycopg2.Error) as error:
print("Error while fetching data from PostgreSQL", error)
except:
print("Error while fetching harvesting infomrations")

else:
with open(os.path.join(self.backup_dir, "harvesting_settings.json"), "w") as file:
json.dump(harvesting, file, indent=4)

finally:
if connection:
connection.close()
json.dump(res.json(), file, indent=4)

print(f"Backup harvesting settings {utils.okgreen('Done')}")
92 changes: 47 additions & 45 deletions geopycat/geocat.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,59 +337,61 @@ def get_ro_uuids(self, valid_only: bool = False, published_only: bool = False,
Dict with the 3 kinds of RO : {"contact": list,"extent": list,"format": list}
"""

if not self.check_admin():
print(utils.warningred("You need to be admin to use this method"))
return
subtemplate_types = {
"contact": "che:CHE_CI_ResponsibleParty",
"extent" : "gmd:EX_Extent",
"format": "gmd:MD_Format"
}

body = copy.deepcopy(settings.SEARCH_UUID_API_BODY)

uuids_contact = list()
uuids_extent = list()
uuids_format = list()
body["query"] = {
"bool": {
"must": []
}
}

query_string = str()

if valid_only:
query_string = query_string + "(valid:\"1\") AND"

if published_only:
query_string = query_string + "(isPublishedToAll:\"true\") AND"

if with_template:
template = " or istemplate = 't'"
body["query"]["bool"]["must"].append({"terms": {"isTemplate": ["s", "t"]}})
else:
template = str()

try:
connection = self.db_connect()

with connection.cursor() as cursor:
body["query"]["bool"]["must"].append({"terms": {"isTemplate": ["s"]}})

if valid_only and not published_only:
cursor.execute(f"SELECT UUID,data FROM public.metadata WHERE (istemplate='s' {template}) " \
"AND id IN (SELECT metadataid FROM public.validation WHERE status=1 AND required=true)")

elif published_only and not valid_only:
cursor.execute(f"SELECT UUID,data FROM public.metadata WHERE (istemplate='s' {template}) " \
"AND id IN (SELECT metadataid FROM public.operationallowed WHERE groupid=1 AND operationid=0)")
elif valid_only and published_only:
cursor.execute(f"SELECT UUID,data FROM public.metadata WHERE (istemplate='s' {template}) " \
"AND id IN (SELECT metadataid FROM public.validation WHERE status=1 AND required=true) " \
"AND id IN (SELECT metadataid FROM public.operationallowed WHERE groupid=1 AND operationid=0)")
else:
cursor.execute(f"SELECT UUID,data FROM public.metadata WHERE (istemplate='s' {template})")

for row in cursor:
if row[1].startswith("<che:CHE_CI_ResponsibleParty"):
uuids_contact.append(row[0])
elif row[1].startswith("<gmd:EX_Extent"):
uuids_extent.append(row[0])
elif row[1].startswith("<gmd:MD_Format"):
uuids_format.append(row[0])
if len(query_string) > 0:
query_string = query_string[:-4]
body["query"]["bool"]["must"].insert(
0,
{"query_string": {"query": query_string,
"default_operator": "AND"}}
)

except (Exception, psycopg2.Error) as error:
print("Error while fetching data from PostgreSQL", error)
output = {}

else:
return {
"contact": uuids_contact,
"extent": uuids_extent,
"format": uuids_format,
}
for type in subtemplate_types:

body["query"]["bool"]["must"].append(
{
"terms": {
"root": [
subtemplate_types[type]
]
}
}
)

indexes = self.es_deep_search(body=body)
output[type] = [i["_source"]["uuid"] for i in indexes]

finally:
if connection:
connection.close()
body["query"]["bool"]["must"].pop()

return output

def get_metadata_from_mef(self, uuid: str) -> bytes:
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="geopycat",
version="0.3.9",
version="0.4.0",
author="Benoit G. Regamey",
author_email="[email protected]",
description="Manage metadata and data of geocat.ch - a geonetwork instance for Switzerland",
Expand Down

0 comments on commit bbfa13b

Please sign in to comment.