diff --git a/bin/geocat_backup.py b/bin/geocat_backup.py index e5ea725..20a719e 100644 --- a/bin/geocat_backup.py +++ b/bin/geocat_backup.py @@ -1,5 +1,4 @@ import argparse -import os import colorama from geopycat.GeocatBackup import GeocatBackup @@ -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) diff --git a/geopycat/GeocatBackup/backup_generator.py b/geopycat/GeocatBackup/backup_generator.py index c9a627d..cd86c6d 100644 --- a/geopycat/GeocatBackup/backup_generator.py +++ b/geopycat/GeocatBackup/backup_generator.py @@ -1,6 +1,5 @@ import os import json -import psycopg2 import pandas as pd import xml.etree.ElementTree as ET from datetime import datetime @@ -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')}") diff --git a/geopycat/geocat.py b/geopycat/geocat.py index 89cade5..a8ffef3 100644 --- a/geopycat/geocat.py +++ b/geopycat/geocat.py @@ -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(" 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: """ diff --git a/setup.py b/setup.py index 2e86d11..d1c9eae 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="geopycat", - version="0.3.9", + version="0.4.0", author="Benoit G. Regamey", author_email="benoit.regamey@swisstopo.ch", description="Manage metadata and data of geocat.ch - a geonetwork instance for Switzerland",