|
1 | 1 | import re |
2 | 2 |
|
3 | | -from django import template |
4 | 3 | from django.template.defaultfilters import stringfilter |
| 4 | +from django.template.defaulttags import register |
5 | 5 | from django.utils.html import conditional_escape |
6 | 6 | from django.utils.safestring import mark_safe |
7 | | -from urllib.parse import urlencode |
8 | | - |
9 | | -register = template.Library() |
10 | 7 |
|
11 | 8 |
|
12 | 9 | @register.filter |
@@ -116,169 +113,3 @@ def esc(x): |
116 | 113 | ] |
117 | 114 | ) |
118 | 115 | ) |
119 | | - |
120 | | - |
121 | | -def _get_item_value(obj, key, default=None): |
122 | | - """Get value from Solr object or dictionary. |
123 | | - For Solr objects, tries clean field name first, then falls back to |
124 | | - raw Solr field name with _s suffix if aliasing isn't working. |
125 | | - """ |
126 | | - if isinstance(obj, dict): |
127 | | - return obj.get(key, default) |
128 | | - else: |
129 | | - # Try clean field name first (from aliasing) |
130 | | - value = getattr(obj, key, None) |
131 | | - if value is not None: |
132 | | - return value |
133 | | - |
134 | | - # Fall back to raw Solr field name with _s suffix |
135 | | - raw_field_name = f"{key}_s" |
136 | | - return getattr(obj, raw_field_name, default) |
137 | | - |
138 | | - |
139 | | -def _generate_absolute_url(context, item): |
140 | | - """Generate absolute URL for the item from Solr result.""" |
141 | | - from django.urls import reverse |
142 | | - |
143 | | - source_id = _get_item_value(item, "source_id") |
144 | | - first_page = _get_item_value(item, "first_page") |
145 | | - |
146 | | - if first_page: |
147 | | - detail_url = reverse( |
148 | | - "archive:detail", |
149 | | - kwargs={"source_id": source_id, "start_page": first_page}, |
150 | | - ) |
151 | | - else: |
152 | | - detail_url = reverse("archive:detail", kwargs={"source_id": source_id}) |
153 | | - return context["request"].build_absolute_uri(detail_url) |
154 | | - |
155 | | - |
156 | | -def _add_common_fields(item): |
157 | | - """Return common metadata fields from Solr object.""" |
158 | | - return { |
159 | | - "title": _get_item_value(item, "title"), |
160 | | - "au": _get_item_value(item, "author"), |
161 | | - "date": _get_item_value(item, "pub_date"), |
162 | | - "pub": _get_item_value(item, "publisher"), |
163 | | - "pub_place": _get_item_value(item, "pub_place"), |
164 | | - } |
165 | | - |
166 | | - |
167 | | -def _add_full_work_fields(item, existing_data): |
168 | | - """Return fields specific to full works.""" |
169 | | - return { |
170 | | - "rft_val_fmt": "info:ofi/fmt:kev:mtx:book", |
171 | | - "genre": "book", |
172 | | - "title": existing_data.get("title"), |
173 | | - "place": _get_item_value(item, "pub_place"), |
174 | | - } |
175 | | - |
176 | | - |
177 | | -def _add_excerpt_fields(item, existing_data): |
178 | | - """Return fields specific to excerpts.""" |
179 | | - fields = { |
180 | | - "rft_val_fmt": "info:ofi/fmt:kev:mtx:book", |
181 | | - "genre": "bookitem", |
182 | | - "atitle": existing_data.get("title"), |
183 | | - "btitle": _get_item_value(item, "book_journal"), |
184 | | - "place": _get_item_value(item, "pub_place"), |
185 | | - } |
186 | | - |
187 | | - # Add page information for excerpts if available |
188 | | - first_page = _get_item_value(item, "first_page") |
189 | | - if first_page: |
190 | | - last_page = _get_item_value(item, "last_page") |
191 | | - fields.update( |
192 | | - { |
193 | | - "spage": first_page, |
194 | | - "epage": last_page or first_page, |
195 | | - "pages": f"{first_page}-{last_page}" |
196 | | - if last_page and last_page != first_page |
197 | | - else first_page, |
198 | | - } |
199 | | - ) |
200 | | - |
201 | | - return fields |
202 | | - |
203 | | - |
204 | | -def _add_article_fields(item, existing_data): |
205 | | - """Return fields specific to articles.""" |
206 | | - fields = { |
207 | | - "rft_val_fmt": "info:ofi/fmt:kev:mtx:journal", |
208 | | - "genre": "article", |
209 | | - "atitle": existing_data.get("title"), |
210 | | - "jtitle": _get_item_value(item, "book_journal"), |
211 | | - "volume": _get_item_value(item, "enumcron"), |
212 | | - } |
213 | | - |
214 | | - # Add page information for articles if available |
215 | | - first_page = _get_item_value(item, "first_page") |
216 | | - if first_page: |
217 | | - last_page = _get_item_value(item, "last_page") |
218 | | - fields.update( |
219 | | - { |
220 | | - "spage": first_page, |
221 | | - "epage": last_page or first_page, |
222 | | - "pages": f"{first_page}-{last_page}" |
223 | | - if last_page and last_page != first_page |
224 | | - else first_page, |
225 | | - } |
226 | | - ) |
227 | | - |
228 | | - return fields |
229 | | - |
230 | | - |
231 | | -def _add_work_type_specific_fields(item, existing_data, work_type_str): |
232 | | - """Return fields based on work type.""" |
233 | | - if work_type_str == "full-work": |
234 | | - return _add_full_work_fields(item, existing_data) |
235 | | - elif work_type_str == "excerpt": |
236 | | - return _add_excerpt_fields(item, existing_data) |
237 | | - elif work_type_str == "article": |
238 | | - return _add_article_fields(item, existing_data) |
239 | | - return {} |
240 | | - |
241 | | - |
242 | | -@register.simple_tag(takes_context=True) |
243 | | -def coins_data(context, item): |
244 | | - """Generate COinS metadata dictionary for Zotero from Solr results.""" |
245 | | - work_type_str = _get_item_value(item, "work_type", "full-work") |
246 | | - absolute_url = _generate_absolute_url(context, item) |
247 | | - |
248 | | - data = { |
249 | | - "ctx_ver": "Z39.88-2004", |
250 | | - "rft_id": absolute_url, |
251 | | - } |
252 | | - |
253 | | - # Add common fields first |
254 | | - common_fields = _add_common_fields(item) |
255 | | - data.update(common_fields) |
256 | | - |
257 | | - # Add work-type-specific fields |
258 | | - work_type_fields = _add_work_type_specific_fields(item, data, work_type_str) |
259 | | - data.update(work_type_fields) |
260 | | - |
261 | | - # Add rft. prefix to fields (except those already prefixed) |
262 | | - coins_fields = { |
263 | | - f"rft.{k}" if not k.startswith(("rft", "ctx_ver")) else k: v |
264 | | - for k, v in data.items() |
265 | | - if v is not None # Skip None values |
266 | | - } |
267 | | - |
268 | | - return coins_fields |
269 | | - |
270 | | - |
271 | | -@register.filter |
272 | | -def coins_encode(coins_data): |
273 | | - """Convert COinS metadata dictionary to encoded HTML span element.""" |
274 | | - if not coins_data: |
275 | | - return "" |
276 | | - |
277 | | - title_parts = [] |
278 | | - for key, value in coins_data.items(): |
279 | | - if value: |
280 | | - encoded_value = urlencode({"": str(value)})[1:] |
281 | | - title_parts.append(f"{key}={encoded_value}") |
282 | | - |
283 | | - title_attr = "&".join(str(part) for part in title_parts) |
284 | | - return mark_safe(f'<span class="Z3988" title="{title_attr}"></span>') |
0 commit comments