Skip to content

Commit

Permalink
filtering using hydroshare resources and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
romer8 committed Apr 18, 2024
1 parent 9e36396 commit 05076c7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
22 changes: 22 additions & 0 deletions hydrolearn_modules_app/migrations/0002_auto_20240417_1753.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
2 changes: 1 addition & 1 deletion hydrolearn_modules_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion hydrolearn_modules_app/templates/hydrolearn-modules.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<script>
// Define the data you want to send. Example data shown here:
const requestData = {
organization: "{{ instance.organization | safe }}",
tag_filter: "{{ instance.tag_filter | safe }}",
};
const url = "{% url 'hydrolearn-modules-api' %}";
</script>
Expand Down
41 changes: 30 additions & 11 deletions hydrolearn_modules_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import json
import requests
from hs_restclient import HydroShare

logger = logging.getLogger(__name__)

Expand All @@ -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.
Expand All @@ -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 = []

Expand All @@ -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 = {
Expand All @@ -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)

0 comments on commit 05076c7

Please sign in to comment.