forked from django-cms/django-cms-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding lazy loading to publications plugin 1/2
- Loading branch information
romer8
committed
Mar 18, 2024
1 parent
8e28ab9
commit 5db0614
Showing
26 changed files
with
807 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
backend/migrations/0030_zoterobibliographyresource_is_saving.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.