Skip to content

Commit 0e0b723

Browse files
committed
Consolidates work identifier logic
Uses a consistent method to determine the work identifier (DOI or ID) for generating URLs and API responses. This change ensures that permalinks, sitemap entries, and API data consistently use the DOI if available, falling back to the internal ID otherwise. It also centralizes this logic in a `get_identifier` method on the `Work` model.
1 parent f440e3e commit 0e0b723

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

optimap/sitemaps.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ def items(self):
1717

1818
def location(self, item):
1919
"""Return the URL path for a work (without domain)."""
20-
# Return only the path, not the full URL (Django's sitemap adds domain)
21-
if item.doi:
22-
return reverse("optimap:work-landing", args=[item.doi])
23-
else:
24-
return f"/work/{item.id}/"
20+
return reverse("optimap:work-landing", args=[item.get_identifier()])
2521

2622
def lastmod(self, item):
2723
"""Return the last modification date of the work."""

works/models.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,26 @@ class Meta:
138138
def __str__(self):
139139
return self.title
140140

141+
def get_identifier(self) -> str:
142+
"""
143+
Return the most suitable identifier for this work.
144+
Prefers DOI if available, otherwise returns internal ID as string.
145+
146+
This identifier can be used in URLs, API responses, and anywhere
147+
a unique work identifier is needed.
148+
149+
Returns:
150+
str: DOI (if available) or internal ID (as string)
151+
"""
152+
return self.doi if self.doi else str(self.id)
153+
141154
def permalink(self) -> str | None:
142155
"""
143-
Return the absolute OPTIMAP permalink (/work/<doi>) if a DOI exists; otherwise None.
156+
Return the absolute OPTIMAP permalink (/work/<identifier>).
157+
Uses DOI if available, otherwise falls back to internal ID.
144158
"""
145-
if not getattr(self, "doi", None):
146-
return None
147159
base = settings.BASE_URL.rstrip("/")
148-
rel = reverse("optimap:work-landing", args=[self.doi])
160+
rel = reverse("optimap:work-landing", args=[self.get_identifier()])
149161
return f"{base}{rel}"
150162
permalink.short_description = "Permalink"
151163

works/sitemaps.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ def items(self):
1717

1818
def location(self, item):
1919
"""Return the URL path for a work (without domain)."""
20-
# Return only the path, not the full URL (Django's sitemap adds domain)
21-
if item.doi:
22-
return reverse("optimap:work-landing", args=[item.doi])
23-
else:
24-
return f"/work/{item.id}/"
20+
return reverse("optimap:work-landing", args=[item.get_identifier()])
2521

2622
def lastmod(self, item):
2723
"""Return the last modification date of the work."""

works/static/js/map-interaction.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ class MapInteractionManager {
368368
showPaginatedPopup(overlapping, latlng) {
369369
console.log('showPaginatedPopup called with latlng:', latlng);
370370

371-
// Close existing popup FIRST (before setting new location)
371+
// Close all existing popups FIRST (both paginated and individual)
372+
this.map.closePopup();
372373
this.closePaginatedPopup();
373374

374375
// Now set new state
@@ -555,6 +556,10 @@ class MapInteractionManager {
555556
* Show publication popup for single feature
556557
*/
557558
showPublicationPopup(featureData, latlng) {
559+
// Close all existing popups first (both individual and paginated)
560+
this.map.closePopup();
561+
this.closePaginatedPopup();
562+
558563
// Use layer's built-in popup if it exists
559564
if (featureData.layer && featureData.layer.getPopup()) {
560565
featureData.layer.openPopup();

works/views.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,13 +832,9 @@ def works_list(request):
832832
"doi": work.doi,
833833
"authors": work.authors or [],
834834
"source": work.source.name if work.source else None,
835+
"href": reverse("optimap:work-landing", args=[work.get_identifier()]),
835836
}
836837

837-
if work.doi:
838-
work_data["href"] = reverse("optimap:work-landing", args=[work.doi])
839-
elif work.url:
840-
work_data["href"] = work.url
841-
842838
# Add status info for admin users
843839
if is_admin:
844840
work_data["status"] = work.get_status_display()

works/views/work_views.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,9 @@ def works_list(request):
170170
"doi": work.doi,
171171
"authors": work.authors or [],
172172
"source": work.source.name if work.source else None,
173+
"href": reverse("optimap:work-landing", args=[work.get_identifier()]),
173174
}
174175

175-
if work.doi:
176-
work_data["href"] = reverse("optimap:work-landing", args=[work.doi])
177-
elif work.url:
178-
work_data["href"] = work.url
179-
180176
# Add status info for admin users
181177
if is_admin:
182178
work_data["status"] = work.get_status_display()

0 commit comments

Comments
 (0)