Skip to content

Commit

Permalink
Adding lazy loading to publications plugin 1/2
Browse files Browse the repository at this point in the history
  • Loading branch information
romer8 committed Mar 18, 2024
1 parent 8e28ab9 commit 5db0614
Show file tree
Hide file tree
Showing 26 changed files with 807 additions and 111 deletions.
48 changes: 46 additions & 2 deletions backend/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
ZoteroBibliographyResource,
HydroLearnModulesList,
)
import logging
from hs_restclient import HydroShare, HydroShareAuthBasic
import uuid
import requests
from bs4 import BeautifulSoup
from pyzotero import zotero

# from datetime import datetime

import datetime

import logging

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -65,18 +67,60 @@ def render(self, context, instance, placeholder):
class ZoteroBibliographyResourcePlugin(CMSPluginBase):
model = ZoteroBibliographyResource
name = _("Zotero Citation Resource Plugin")
render_template = "zotero_bibliography.html"
render_template = "zp.html"
cache = False

# This is key in order to call the API every time the page renders
# The instance.save calls the pre_save signal which makes the call of the API
def render(self, context, instance, placeholder):
logging.warning("rendering")

instance.updated_version = instance.updated_version + 1
instance.save(update_fields=["updated_version"])
context = super().render(context, instance, placeholder)
logging.warning("finish rendering")

return context


def create_html_citations(instance):
logger.warning("creating_html_citations ")
params = {
"include": "bib,data",
"style": "apa",
"sort": "date",
"direction": "desc",
"linkwrap": 1,
}
try:
zot = zotero.Zotero(
instance.library_id, instance.library_type, instance.api_key
)
if instance.collection_id:
items = zot.collection_items(instance.collection_id, **params)
else:
items = zot.items(**params)
# Initialize a dictionary to store publications by year
publications_by_year = {}

# Iterate through the data and populate the dictionary
for item in items:
# Extract the year from "parsedDate" (if available)
parsed_date = item.get("meta", {}).get("parsedDate", "")
year = parsed_date.split("-")[0] if parsed_date else "More Publications"

# Add the publication to the corresponding year's list
if year not in publications_by_year:
publications_by_year[year] = []
publications_by_year[year].append(item["bib"])

instance.html = publications_by_year
instance.save(update_fields=["html"])

except Exception as e:
instance.html = {"Error": [f"The following error: {e}"]}


def create_hydroshare_resources(instance):
logger.warning(instance.updated_version)
keywords = []
Expand Down
18 changes: 18 additions & 0 deletions backend/migrations/0030_zoterobibliographyresource_is_saving.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2 on 2024-03-14 17:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('backend', '0029_hydrolearnmoduleslist'),
]

operations = [
migrations.AddField(
model_name='zoterobibliographyresource',
name='is_saving',
field=models.BooleanField(default=False, editable=False),
),
]
7 changes: 6 additions & 1 deletion backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests

# from datetime import datetime
import time

import datetime
import uuid
Expand Down Expand Up @@ -65,10 +66,12 @@ class ZoteroBibliographyResource(CMSPlugin):
unique_identifier = models.UUIDField(default=uuid.uuid4, editable=False)
updated_version = models.IntegerField(default=0, editable=False)
link_of_library_or_collection = models.CharField(max_length=400, default="")
is_saving = models.BooleanField(default=False, editable=False)


@receiver(pre_save, sender=ZoteroBibliographyResource)
# @receiver(post_save, sender=ZoteroBibliographyResource)
def create_html_citations(sender, instance, *args, **kwargs):
logger.warning("creating_html_citations ")
params = {
"include": "bib,data",
"style": "apa",
Expand All @@ -77,6 +80,8 @@ def create_html_citations(sender, instance, *args, **kwargs):
"linkwrap": 1,
}
try:
time.sleep(5)

zot = zotero.Zotero(
instance.library_id, instance.library_type, instance.api_key
)
Expand Down
6 changes: 5 additions & 1 deletion backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"djangocms_frontend.contrib.media",
"djangocms_frontend.contrib.image",
"djangocms_frontend.contrib.tabs",
"djangocms_frontend.contrib.utilities"
"djangocms_frontend.contrib.utilities",
"publications",
]

MIDDLEWARE = [
Expand All @@ -95,6 +96,7 @@
"cms.middleware.page.CurrentPageMiddleware",
"cms.middleware.toolbar.ToolbarMiddleware",
"cms.middleware.language.LanguageCookieMiddleware",
"cms.middleware.utils.ApphookReloadMiddleware",
]

ROOT_URLCONF = "backend.urls"
Expand Down Expand Up @@ -318,3 +320,5 @@
},
]
}

CMS_COLOR_SCHEME_TOGGLE = True
22 changes: 18 additions & 4 deletions backend/templates/zotero_bibliography.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
{% load cms_tags %}

{% addtoblock "js" %}
<script type="text/javascript">
<script defer>
console.log("hey")


</script>

</script>
{% endaddtoblock %}

{% addtoblock "css" %}
<style>

Expand Down Expand Up @@ -37,7 +41,14 @@

</style>
{% endaddtoblock %}
<div class="container-zotero-plugin">

{% block content %}

<p>No publications found</p>


{% endblock %}
<!-- <div class="container-zotero-plugin">
{% for key, values in instance.html.items %}
<div class="wrapper-publication-set">
<h2 class="year-style">{{ key }}</h2>
Expand All @@ -50,4 +61,7 @@ <h2 class="year-style">{{ key }}</h2>
</div>
{% endfor %}
</div>
</div> -->



24 changes: 24 additions & 0 deletions backend/templates/zp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "zotero_bibliography.html" %}




{% block content %}

<div class="container-zotero-plugin">
{% for key, values in instance.html.items %}
<div class="wrapper-publication-set">
<h2 class="year-style">{{ key }}</h2>

{% for citation in values %}

{{ citation | safe }}

{% endfor %}

</div>
{% endfor %}
</div>

{% endblock %}

Empty file.
79 changes: 79 additions & 0 deletions backend/templatetags/loading_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from django import template
from pyzotero import zotero
import logging
import time

logger = logging.getLogger(__name__)
register = template.Library()


@register.simple_tag
def loading_indicator(instance):
# Assuming `instance` has a method or property to check if a field is being saved
logger.warning(instance._state.adding)
pubs = create_html_citations(instance)
# is_saving = getattr(instance, f"{field_name}", False)

# if is_saving:
# Return HTML for the loading indicator
return pubs
# else:
# # Return an empty string or any other placeholder if not saving
# return ""


def create_html_citations(instance):
logger.warning("creating_html_citations ")
params = {
"include": "bib,data",
"style": "apa",
"sort": "date",
"direction": "desc",
"linkwrap": 1,
}
try:
time.sleep(5)
zot = zotero.Zotero(
instance.library_id, instance.library_type, instance.api_key
)
if instance.collection_id:
items = zot.collection_items(instance.collection_id, **params)
else:
items = zot.items(**params)
# Initialize a dictionary to store publications by year
publications_by_year = {}

# Iterate through the data and populate the dictionary
for item in items:
# Extract the year from "parsedDate" (if available)
parsed_date = item.get("meta", {}).get("parsedDate", "")
year = parsed_date.split("-")[0] if parsed_date else "More Publications"

# Add the publication to the corresponding year's list
if year not in publications_by_year:
publications_by_year[year] = []
publications_by_year[year].append(item["bib"])

return publications_by_year
except Exception as e:
return publications_by_year

# {% load static %}

# {% load loading_tag %}

# {% loading_indicator instance as pubs %}
# <div class="container-zotero-plugin">
# {% for key, values in pubs.items %}
# <div class="wrapper-publication-set">
# <h2 class="year-style">{{ key }}</h2>

# {% for citation in values %}

# {{ citation | safe }}

# {% endfor %}

# </div>
# {% endfor %}
# </div>
1 change: 1 addition & 0 deletions backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

urlpatterns = [
path("admin/", admin.site.urls),
path("publications/", include("publications.urls")),
]

# if settings.DEBUG:
Expand Down
6 changes: 4 additions & 2 deletions backend/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.shortcuts import render
import os


def home(request):
home_path = os.environ.get('PREFIX_TO_PATH')
return render(request, 'base.html', {'home_path': home_path})
home_path = os.environ.get("PREFIX_TO_PATH")
return render(request, "base.html", {"home_path": home_path})
Empty file added publications/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions publications/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions publications/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class PublicationsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'publications'
11 changes: 11 additions & 0 deletions publications/cms_apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool


@apphook_pool.register # register the application
class PublicationsApphook(CMSApp):
app_name = "publications"
name = "Zotero Publications Application"

def get_urls(self, page=None, language=None, **kwargs):
return ["publications.urls"]
26 changes: 26 additions & 0 deletions publications/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django.utils.translation import gettext_lazy as _
from .models import ZoteroPublications


import logging

logger = logging.getLogger(__name__)


@plugin_pool.register_plugin
class ZoteroPlugin(CMSPluginBase):
model = ZoteroPublications
name = _("Zotero Publications Plugin")
render_template = "publications/publications.html"
cache = False

# This is key in order to call the API every time the page renders
# The instance.save calls the pre_save signal which makes the call of the API
def render(self, context, instance, placeholder):
logging.warning("init rendering zotero plugin")
context = super().render(context, instance, placeholder)
logging.warning("finish rendering zotero plugin")

return context
Loading

0 comments on commit 5db0614

Please sign in to comment.