Skip to content

Commit

Permalink
adding bib refresh stuff for ca to make it more like ant
Browse files Browse the repository at this point in the history
  • Loading branch information
acholyn committed Dec 12, 2024
1 parent 97f54d6 commit f7a6f93
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 6 deletions.
34 changes: 34 additions & 0 deletions src/rard/research/models/citing_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,40 @@ def save(self, *args, **kwargs):
self.order_name = self.name
super().save(*args, **kwargs)

def refresh_bibliography_items_from_mentions(self):
from rard.research.models import AnonymousFragment, Fragment, Testimonium

"""Author bibliography should be derived from bibliography
items mentioned in:
- the author's introduction
- the introduction to works by that author, and introductions
to any citing works
- commentaries belonging to any fragments, testimonia, or
apposita linked to that author
"""
self.bibliography_items.clear() # Start with a blank slate
# Link bib mentions from introduction
self.introduction.link_bibliography_mentions_in_content()
# Now loop through linked items
for fr in Fragment.objects.filter(
original_texts__citing_work__author=self
).distinct():
fr.commentary.link_bibliography_mentions_in_content()

for tt in Testimonium.objects.filter(
original_texts__citing_work__author=self
).distinct():
tt.commentary.link_bibliography_mentions_in_content()

for an in AnonymousFragment.objects.filter(
original_texts__citing_work__author=self
).distinct():
an.commentary.link_bibliography_mentions_in_content()

for work in CitingWork.objects.filter(author=self).distinct():
if work.introduction:
work.introduction.link_bibliography_mentions_in_content()


CitingAuthor.init_text_object_fields()

Expand Down
10 changes: 10 additions & 0 deletions src/rard/research/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,16 @@
views.CitingAuthorUpdateIntroductionView.as_view(),
name="update_introduction",
),
path(
"<pk>/bibliography/",
views.BibliographySectionView.as_view(),
name="bibliography",
),
path(
"<pk>/refresh_bibliography/",
views.ca_refresh_bibliography_from_mentions,
name="refresh_bibliography",
),
path(
"<pk>/work/create/",
views.CitingAuthorCreateWorkView.as_view(),
Expand Down
2 changes: 2 additions & 0 deletions src/rard/research/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
CitingWorkIntroductionView,
CitingWorkUpdateIntroductionView,
CitingWorkUpdateView,
ca_refresh_bibliography_from_mentions,
)
from .comments import CommentDeleteView, TextObjectFieldCommentListView
from .concordance import (
Expand Down Expand Up @@ -280,6 +281,7 @@
"WorkListView",
"WorkUpdateView",
"refresh_bibliography_from_mentions",
"ca_refresh_bibliography_from_mentions",
"WorkUpdateIntroductionView",
"WorkIntroductionView",
"fetch_books",
Expand Down
3 changes: 3 additions & 0 deletions src/rard/research/views/bibliography.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class BibliographySectionView(LoginRequiredMixin, PermissionRequiredMixin, ListV
context_object_name = "bibliography_items"
permission_required = ("research.view_bibliographyitem",)

# todo: update this to dynamically work with authors and ants
def get_queryset(self) -> QuerySet[Any]:
if self.model is not None:
queryset = self.model._default_manager.all()
Expand All @@ -173,6 +174,8 @@ def get_queryset(self) -> QuerySet[Any]:

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if self.ant_pk:
context["antiquarian"] = Antiquarian.objects.get(id=self.ant_pk)
if self.ant_pk:
context["antiquarian"] = Antiquarian.objects.get(id=self.ant_pk)
return context
25 changes: 23 additions & 2 deletions src/rard/research/views/citing_work.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404
from django.urls import reverse, reverse_lazy
from django.utils.decorators import method_decorator
from django.views.decorators.http import require_POST
from django.views.decorators.http import require_GET, require_POST
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, DeleteView, UpdateView
Expand Down Expand Up @@ -280,7 +282,6 @@ class CitingWorkUpdateIntroductionView(TextObjectFieldUpdateMixin, UpdateView):
textobject_field = "introduction"

def create_intro_if_does_not_exist(self, *args, **kwargs):
print("creating intro since it doesn't exist")
# If a TOF is not created for the introduction an error will be
# thrown when trying to save as it will try to save something that
# does not exist
Expand All @@ -298,3 +299,23 @@ class CitingWorkIntroductionView(TextObjectFieldViewMixin):
model = CitingWork
permission_required = ("research.view_citingwork",)
textobject_field = "introduction"


@require_GET
@login_required
@permission_required("research.change_citingauthor")
def ca_refresh_bibliography_from_mentions(request, pk):
"""Given the pk of a Citing Author object, call its
refresh_bibliography_items_from_mentions method to
parse DynamicTextFields for mentions of bibliography
items and link these directly."""
try:
author = CitingAuthor.objects.get(pk=pk)
except CitingAuthor.DoesNotExist:
raise Http404("No Citing Authors found matching the query")
author.refresh_bibliography_items_from_mentions()
response = HttpResponse(
status=204,
)
response.headers["HX-Trigger"] = "refreshed-bibliography"
return response
14 changes: 10 additions & 4 deletions src/rard/templates/research/citingauthor_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,29 @@ <h5>{% trans 'Linked Material' %}</h5>

<hr>

<section>
<section id="bibliography">
<div class='d-flex justify-content-between mb-3'>
<div>
<h5>{% trans 'Bibliography' %}</h5>
</div>
<div>
<div class="d-flex flex-column">
{% if perms.research.change_citingauthor and has_object_lock %}
<a href='{% url "citingauthor:update" object.pk %}'>{% trans 'Add existing' %}</a><p></p>
<a href='{% url "bibliography:create" %}'>{% trans 'Add new' %}</a>
<button type="button" class='btn text-primary text-right d-block' hx-get="{% url "citingauthor:refresh_bibliography" pk=citingauthor.pk %}" hx-trigger="click">{% trans 'Refresh bibliography from mentions' %}</button>

{% endif %}
</div>
</div>
<div id="bib-list"
hx-trigger="refreshed-bibliography from:body, intro-updated from:body"
hx-get="{% url 'citingauthor:bibliography' pk=citingauthor.pk %}"
hx-swap="outerHTML">

{% for item in object.bibliography_items.all %}

{% include 'research/partials/bibliography_list_item.html' with can_edit=perms.research.change_citingauthor has_object_lock=has_object_lock %}

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

{% endwith %}
Expand Down

0 comments on commit f7a6f93

Please sign in to comment.