Skip to content

Commit

Permalink
Merge pull request #436 from UCL/feature/shorten-mentions
Browse files Browse the repository at this point in the history
Feature/shorten mentions
  • Loading branch information
acholyn committed Mar 26, 2024
2 parents 6c1c399 + d73680f commit 2961ed2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/rard/research/models/bibliography.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def __str__(self):
def get_absolute_url(self):
return reverse("bibliography:detail", kwargs={"pk": self.pk})

def mention_citation(self):
r = self.author_surnames
if self.year:
r += " [" + self.year + "]"
return r.strip()

class Meta:
ordering = ["author_surnames", "year"]

Expand Down
4 changes: 4 additions & 0 deletions src/rard/research/tests/models/test_bibliography.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def test_display(self):
# the __str__ function should show the name
self.assertEqual(str(self.bibliography), "Hartley [1855]: Fly Fishing")

def test_mention_citation(self):
# the mention_citation function should show the name and year only
self.assertEqual(self.bibliography.mention_citation(), "Hartley [1855]")

def test_creation(self):
self.assertTrue(
BibliographyItem.objects.filter(pk=self.bibliography.pk).exists()
Expand Down
13 changes: 11 additions & 2 deletions src/rard/research/views/mention.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,17 @@ def get(self, request, *args, **kwargs):
if not model_name:
model_name = next(k for k, value in dd.items() if value == o.__class__)
model_name_cache[o.__class__] = model_name

ajax_data.append({"id": o.pk, "target": model_name, "value": str(o)})
citation = (
o.mention_citation() if hasattr(o, "mention_citation") else str(o)
)
ajax_data.append(
{
"id": o.pk,
"target": model_name,
"value": str(o),
"citation": citation,
}
)
return JsonResponse(data=ajax_data, safe=False)

def parse_mention(self, q):
Expand Down
15 changes: 13 additions & 2 deletions src/rard/static/js/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,14 @@ function initRichTextEditor($item) {
$item.hasClass("enable-apparatus-criticus")
) {
let delimiters = [];
let dataAttributes = ["id", "value", "denotationChar", "link", "target"];
let dataAttributes = [
"id",
"value",
"denotationChar",
"link",
"target",
"citation",
];
if ($item.hasClass("enable-mentions")) {
delimiters.push("@");
}
Expand Down Expand Up @@ -401,10 +408,14 @@ function initRichTextEditor($item) {
}
},
renderItem(item, searchTerm) {
// allows you to control how the item is displayed
// allows you to control how the item is displayed in the list
let list_display = item.list_display || item.value;
return `${list_display}`;
},
onSelect(item, insertItem) {
const shortItem = { ...item, value: item.citation };
insertItem(shortItem);
},
};
}

Expand Down
26 changes: 19 additions & 7 deletions src/rard/utils/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ def update_editable_mentions(self, save=True):
linked = model.objects.get(pk=int(pkstr))

if char == "@":
link_text = str(linked)
if hasattr(linked, "mention_citation"):
link_text = linked.mention_citation()
else:
link_text = str(linked)
else:
# it's an apparatus criticus so we need
# show its index and any ordinal relating
Expand Down Expand Up @@ -179,12 +182,21 @@ def render_dynamic_content(self):
linked = model.objects.get(pk=int(pkstr))
# is it something we can link to?
if getattr(linked, "get_absolute_url", False):
replacement = bs4.BeautifulSoup(
'<a href="{}">{}</a>'.format(
linked.get_absolute_url(), str(linked)
),
features="html.parser",
)
if hasattr(linked, "mention_citation"):
replacement = bs4.BeautifulSoup(
'<a href="{}">{}</a>'.format(
linked.get_absolute_url(),
str(linked.mention_citation()),
),
features="html.parser",
)
else:
replacement = bs4.BeautifulSoup(
'<a href="{}">{}</a>'.format(
linked.get_absolute_url(), str(linked)
),
features="html.parser",
)
else:
# else currently that means it's an
# apparatus criticus item
Expand Down

0 comments on commit 2961ed2

Please sign in to comment.