From 05076c71f160f8dd10f006d29e973b76da8b85d9 Mon Sep 17 00:00:00 2001 From: romer8 Date: Wed, 17 Apr 2024 18:04:07 -0600 Subject: [PATCH] filtering using hydroshare resources and tags --- .../migrations/0002_auto_20240417_1753.py | 22 ++++++++++ hydrolearn_modules_app/models.py | 2 +- .../templates/hydrolearn-modules.html | 2 +- hydrolearn_modules_app/views.py | 41 ++++++++++++++----- 4 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 hydrolearn_modules_app/migrations/0002_auto_20240417_1753.py diff --git a/hydrolearn_modules_app/migrations/0002_auto_20240417_1753.py b/hydrolearn_modules_app/migrations/0002_auto_20240417_1753.py new file mode 100644 index 0000000..acd14bb --- /dev/null +++ b/hydrolearn_modules_app/migrations/0002_auto_20240417_1753.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2 on 2024-04-17 17:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('hydrolearn_modules_app', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='hydrolearnmoduleslist', + name='organization', + ), + migrations.AddField( + model_name='hydrolearnmoduleslist', + name='tag_filter', + field=models.CharField(default='nwm_portal_module', max_length=200), + ), + ] diff --git a/hydrolearn_modules_app/models.py b/hydrolearn_modules_app/models.py index 012f372..9724fba 100644 --- a/hydrolearn_modules_app/models.py +++ b/hydrolearn_modules_app/models.py @@ -6,7 +6,7 @@ class HydroLearnModulesList(CMSPlugin): - organization = models.CharField(max_length=200, default="", blank=True) + tag_filter = models.CharField(max_length=200, default="nwm_portal_module") placeholder_image = models.CharField( max_length=200, default="https://placehold.co/200" ) diff --git a/hydrolearn_modules_app/templates/hydrolearn-modules.html b/hydrolearn_modules_app/templates/hydrolearn-modules.html index ab8a610..c61848b 100644 --- a/hydrolearn_modules_app/templates/hydrolearn-modules.html +++ b/hydrolearn_modules_app/templates/hydrolearn-modules.html @@ -13,7 +13,7 @@ diff --git a/hydrolearn_modules_app/views.py b/hydrolearn_modules_app/views.py index 1f79e61..8d9aa14 100644 --- a/hydrolearn_modules_app/views.py +++ b/hydrolearn_modules_app/views.py @@ -3,6 +3,7 @@ import logging import json import requests +from hs_restclient import HydroShare logger = logging.getLogger(__name__) @@ -13,6 +14,26 @@ def base_view(request): return render(request, "hydrolearn-modules-base.html", context) +## let's filter the modules using the HydroShare API +def filter_modules_view_using_hydroshare(tag): + + hs = HydroShare(prompt_auth=False) + filter_list = [] + try: + # let's call the resources + resources = hs.resources(subject=tag) + for resource in resources: + logger.warning(f"{resource}") + + filter_list.append(resource["resource_title"]) + + except Exception as e: + logger.warning(f"Error fetching HydroLearn modules: {e}") + filter_list = [] + + return filter_list + + def hydrolearn_modules_view(request): # This dictionary can pass variables to the template. @@ -21,7 +42,7 @@ def hydrolearn_modules_view(request): body_unicode = request.body.decode("utf-8") body = json.loads(body_unicode) instance = { - "organization": body["organization"], + "tag_filter": body["tag_filter"], } modules_list = [] @@ -40,12 +61,6 @@ def hydrolearn_modules_view(request): ) courses_list = courses_response.json()["results"] - if instance["organization"]: - - def is_from_organization(course): - return course["data"]["org"] == instance["organization"] - - courses_list = filter(is_from_organization, courses_list) for course in courses_list: course_data = course["data"] course_dict = { @@ -63,15 +78,19 @@ def is_from_organization(course): "short_description", "" ), } - # logger.warning(course_dict) modules_list.append(course_dict) - hl_modules["modules"] = modules_list + filter_hs_list = filter_modules_view_using_hydroshare(instance["tag_filter"]) + filtered_modules_list = [ + module + for module in modules_list + if module["course_title"] in filter_hs_list + ] + + hl_modules["modules"] = filtered_modules_list except Exception as e: logger.warning(f"Error fetching HydroLearn modules: {e}") hl_modules["modules"] = modules_list return JsonResponse(hl_modules) - # context = {instance: {"modules": modules_list}} - # return render(request, "hydrolearn-modules.html", context)