Skip to content

Commit

Permalink
#258 Clean QuickSearchManager and tweak SettingsManager
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSAMPERE committed Mar 25, 2024
1 parent d8117b3 commit 789d143
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 182 deletions.
194 changes: 17 additions & 177 deletions modules/quick_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(self, search_form_manager: object = None):

def api_base_url_setter(self, value: str):
self.api_base_url = value
self.customize_settings_mng()
self.customize_settings_manager()

def customize_settings_manager(self):
settings_mng.api_base_url = self.api_base_url
Expand All @@ -102,26 +102,6 @@ def fetch_params(self):

def write_params(self, search_name: str = "_default", search_kind: str = "Default"):
"""Write a new element in the json file when a search is saved."""
# Open the saved_search file as a dict. Each key is a search name,
# each value is a dict containing the parameters for this search name
# saved_searches = self.load_file()
# # If the name already exists, ask for a new one. ================ TO DO
# if search_kind == "Last":
# params = saved_searches.get(
# "_current", "{}/resources/search?&_limit=0".format(self.api_base_url)
# )
# if search_name == "Last search" and "Dernière recherche" in saved_searches:
# del saved_searches["Dernière recherche"]
# elif search_name == "Dernière recherche" and "Last search" in saved_searches:
# del saved_searches["Last search"]
# else:
# pass
# else:
# params = self.fetch_params()

# saved_searches[search_name] = params
# # writing file
# self.dump_file(saved_searches)

saved_searches = settings_mng.load_quicksearch()
# If the name already exists, ask for a new one. ================ TO DO
Expand All @@ -140,8 +120,7 @@ def write_params(self, search_name: str = "_default", search_kind: str = "Defaul

saved_searches[search_name] = params
# writing file
settings_mng.dump_json_file(settings_mng.quicksearch_json_path, saved_searches)
settings_mng.update_quicksearch_qsettings(saved_searches)
settings_mng.update_quicksearch(saved_searches)

# Log and messages
logger.info(
Expand Down Expand Up @@ -193,15 +172,6 @@ def save(self, i):
"""Call the write_params() function and refresh the combobox."""
# retrieve quicksearch given name and store it
if i == QMessageBox.Yes:
# search_name = self.dlg_new.txt_quicksearch_name.text()
# self.dlg_new.txt_quicksearch_name.setText("")
# self.write_params(search_name, search_kind="Quicksearch")
# # load all saved quicksearches and populate drop-down (combobox)
# saved_searches = self.load_file()
# search_list = list(saved_searches.keys())
# # updating quick search widgets
# self.form_mng.pop_qs_cbbs(items_list=search_list)

search_name = self.dlg_new.txt_quicksearch_name.text()
self.dlg_new.txt_quicksearch_name.setText("")
self.write_params(search_name, search_kind="Quicksearch")
Expand All @@ -218,17 +188,6 @@ def save(self, i):
def rename(self, i):
"""Modify the json file in order to rename a search."""
if i == QMessageBox.Yes:
# old_name = self.form_mng.cbb_quicksearch_edit.currentText()
# new_name = self.dlg_rename.txt_quicksearch_rename.text()
# self.dlg_rename.txt_quicksearch_rename.setText("")

# saved_searches = self.load_file()
# saved_searches[new_name] = saved_searches[old_name]
# saved_searches.pop(old_name)
# search_list = list(saved_searches.keys())
# self.form_mng.pop_qs_cbbs(items_list=search_list)
# # Update JSON file
# self.dump_file(content=saved_searches)

old_name = self.form_mng.cbb_quicksearch_edit.currentText()
new_name = self.dlg_rename.txt_quicksearch_rename.text()
Expand All @@ -241,8 +200,7 @@ def rename(self, i):
# Update quick search widgets
self.form_mng.pop_qs_cbbs(items_list=search_list)
# Update JSON file
settings_mng.dump_json_file(settings_mng.quicksearch_json_path, saved_searches)
settings_mng.update_quicksearch_qsettings(saved_searches)
settings_mng.update_quicksearch(saved_searches)

# inform user
msgBar.pushMessage(
Expand All @@ -261,14 +219,6 @@ def rename(self, i):

def remove(self):
"""Modify the json file in order to delete a search."""
# to_remove = self.form_mng.cbb_quicksearch_edit.currentText()
# saved_searches = self.load_file()
# saved_searches.pop(to_remove)
# search_list = list(saved_searches.keys())
# # updating quick search widgets
# self.form_mng.pop_qs_cbbs(items_list=search_list)
# # Update JSON file
# self.dump_file(content=saved_searches)

to_remove = self.form_mng.cbb_quicksearch_edit.currentText()
saved_searches = settings_mng.load_quicksearch()
Expand All @@ -277,8 +227,7 @@ def remove(self):
# Update quick search widgets
self.form_mng.pop_qs_cbbs(items_list=search_list)
# Update JSON file
settings_mng.dump_json_file(settings_mng.quicksearch_json_path, saved_searches)
settings_mng.update_quicksearch_qsettings(saved_searches)
settings_mng.update_quicksearch(saved_searches)

# inform user
msgBar.pushMessage(
Expand All @@ -291,141 +240,32 @@ def remove(self):
logger.debug("'{}' quicksearch removed from JSON file.".format(to_remove))
return

def get_default_file_content(self):
"""Return quicksearch.json file default content. useful to reset JSON file content"""
default_content = {
"_default": {
"contacts": None,
"datatype": "type:dataset",
"favorite": None,
"formats": None,
"geofilter": None,
"inspire": None,
"groupTheme": None,
"lang": "fr",
"licenses": None,
"ob": "relevance",
"od": "desc",
"operation": "intersects",
"owners": None,
"page": 1,
"show": True,
"srs": None,
"text": "",
"url": "{}/resources/search?q=type:dataset&ob=relevance&od=desc&_include=serviceLayers,layers,limitations&_limit=10&_offset=0&_lang=fr".format(
self.api_base_url
),
}
}
return default_content

def load_file(self):

# try:
# with open(self.json_path, "r") as json_content:
# saved_searches = json.load(json_content)

# if not isinstance(saved_searches, dict):
# logger.warning(
# "_user/quicksearches.json file content is not correctly formatted : {}.".format(
# saved_searches
# )
# )
# logger.warning("Let's replace it with the default content.")
# saved_searches = self.get_default_file_content()
# self.dump_file(saved_searches)
# elif "_default" not in saved_searches:
# logger.warning(
# "Missing '_default' quicksearch in _user/quicksearches.json file content : {}.".format(
# saved_searches
# )
# )
# logger.warning("Let's add the default one.")
# # if default search is missing, let's adding it to JSON file content
# saved_searches["_default"] = self.get_default_file_content().get("_default")
# self.dump_file(saved_searches)
# else:
# for trad in ["Last search", "Dernière recherche"]:
# if trad in saved_searches and self.tr("Last search") != trad:
# saved_searches[self.tr("Last search")] = saved_searches.get(trad)
# del saved_searches[trad]
# else:
# pass

# for quicksearch in saved_searches:
# quicksearch_url = saved_searches.get(quicksearch).get("url")
# if self.api_base_url not in quicksearch_url:
# default_search_parameters = quicksearch_url.split("/resources/search?")[1]
# base_search_url = self.api_base_url + "/resources/search?"

# saved_searches[quicksearch]["url"] = (
# base_search_url + default_search_parameters
# )
# logger.warning(
# "'{}' quicksearch : URL {} replaced with {}.".format(
# quicksearch,
# quicksearch_url,
# base_search_url + default_search_parameters,
# )
# )
# else:
# pass
# self.dump_file(saved_searches)

# except Exception as e:
# if not self.json_path.exists() or not self.json_path.is_file():
# logger.warning(
# "_user/quicksearches.json file can't be used : {} doesn't exist or is not a file : {}".format(
# str(self.json_path), str(e)
# )
# )
# logger.warning("Let's create one with default values: {}.".format(self.json_path))
# saved_searches = self.get_default_file_content()
# self.dump_file(saved_searches)
# else:
# logger.error("_user/quicksearches.json file can't be read : {}.".format(str(e)))
# saved_searches = 0

saved_searches = settings_mng.load_quicksearch()

logger.debug(
"{} quicksearch(es) found : {}".format(
len(saved_searches), list(saved_searches.keys())
)
)

return saved_searches

def dump_file(self, content: dict):
with open(self.json_path, "w") as outfile:
json.dump(content, outfile, sort_keys=True, indent=4)
return

def reset_default_search(self):

# search_name = "_default"
# # fetch current JSON file content
# saved_searches = self.load_file()
# # fetch default search default params
# params = self.get_default_file_content().get(search_name)
# # update JSON file content
# saved_searches[search_name] = params
# self.dump_file(saved_searches)

search_name = "_default"
# fetch current JSON file content
saved_searches = settings_mng.load_quicksearch()
# fetch default search default params
params = settings_mng.get_default_quicksearch_content().get(search_name)
# update JSON file content
saved_searches[search_name] = params
settings_mng.dump_json_file(settings_mng.quicksearch_json_path, saved_searches)
settings_mng.update_quicksearch_qsettings(saved_searches)
settings_mng.update_quicksearch(saved_searches)

# Log and messages
logger.info("Default search successfully reset.")
msgBar.pushMessage(
self.tr("Default search successfully reset.", context=__class__.__name__),
duration=3,
)
return

def load_file(self):

saved_searches = settings_mng.load_quicksearch()

logger.debug(
"{} quicksearch(es) found : {}".format(
len(saved_searches), list(saved_searches.keys())
)
)

return saved_searches
20 changes: 15 additions & 5 deletions modules/settings_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def load_quicksearch_from_json(self):
)
logger.warning("Let's replace it with the default content.")
json_content = self.get_default_quicksearch_content()
self.dump_json_file(self.quicksearch_json_path, json_content)
self.update_quicksearch_json(json_content)
return json_content
else:
if "_default" not in json_content:
Expand Down Expand Up @@ -200,7 +200,7 @@ def load_quicksearch_from_json(self):
)
else:
pass
self.dump_json_file(self.quicksearch_json_path, json_content)
self.update_quicksearch_json(json_content)
return json_content

def load_quicksearch_from_qsettings(self):
Expand Down Expand Up @@ -258,12 +258,15 @@ def merge_quicksearch(self, json_content, qsettings_content):
else:
pass

logger.debug("*=====* {}".format(json_content == qsettings_content))

self.dump_json_file(self.quicksearch_json_path, json_content)
self.update_quicksearch_json(json_content)

return json_content

def update_quicksearch_json(self, content: dict):

self.dump_json_file(self.quicksearch_json_path, content)
return

def update_quicksearch_qsettings(self, content: dict):

self.remove(self.quicksearch_prefix[:-1])
Expand All @@ -282,6 +285,13 @@ def update_quicksearch_qsettings(self, content: dict):
self.setValue(qsetting_key, qsetting_value)
return

def update_quicksearch(self, content):

self.update_quicksearch_json(content)
self.update_quicksearch_qsettings(content)

return

def save_quicksearch(self):
return

Expand Down

0 comments on commit 789d143

Please sign in to comment.