sorl-thumbnail==12.8.0
# home-brewed & dependencies
-librarian==2.4.12
+librarian==2.5
# celery tasks
celery[redis]==5.2.7
from lxml import html
from librarian.cover import WLCover
from librarian.html import transform_abstrakt
+from librarian.builders import builders
from newtagging import managers
from catalogue import constants
from catalogue import fields
cls.published.send(sender=cls, instance=book)
return book
- def get_master(self):
- master_tags = [
- 'opowiadanie',
- 'powiesc',
- 'dramat_wierszowany_l',
- 'dramat_wierszowany_lp',
- 'dramat_wspolczesny', 'liryka_l', 'liryka_lp',
- 'wywiad',
- ]
- from librarian.parser import WLDocument
- wld = WLDocument.from_file(self.xml_file.path, parse_dublincore=False)
- root = wld.edoc.getroot()
- for master in root.iter():
- if master.tag in master_tags:
- return master
-
def update_references(self):
- from references.models import Entity, Reference
- master = self.get_master()
- if master is None:
- master = []
- found = set()
- for i, sec in enumerate(master):
- for ref in sec.findall('.//ref'):
- href = ref.attrib.get('href', '')
- if not href or href in found:
- continue
- found.add(href)
- entity, created = Entity.objects.get_or_create(
- uri=href
- )
- ref, created = Reference.objects.get_or_create(
- book=self,
- entity=entity
- )
- ref.first_section = 'sec%d' % (i + 1)
- entity.populate()
- entity.save()
- Reference.objects.filter(book=self).exclude(entity__uri__in=found).delete()
+ Entity = apps.get_model('references', 'Entity')
+ doc = self.wldocument2()
+ doc._compat_assign_section_ids()
+ doc._compat_assign_ordered_ids()
+ refs = {}
+ for ref_elem in doc.references():
+ uri = ref_elem.attrib.get('href', '')
+ if not uri:
+ continue
+ if uri in refs:
+ ref = refs[uri]
+ else:
+ entity, entity_created = Entity.objects.get_or_create(uri=uri)
+ if entity_created:
+ entity.populate()
+ entity.save()
+ ref, ref_created = entity.reference_set.get_or_create(book=self)
+ refs[uri] = ref
+ if not ref_created:
+ ref.occurence_set.all().delete()
+ sec = ref_elem.get_link()
+ m = re.match(r'sec(\d+)', sec)
+ assert m is not None
+ sec = int(m.group(1))
+ snippet = ref_elem.get_snippet()
+ b = builders['html-snippet']()
+ for s in snippet:
+ s.html_build(b)
+ html = b.output().get_bytes().decode('utf-8')
+
+ ref.occurence_set.create(
+ section=sec,
+ html=html
+ )
+ self.reference_set.exclude(entity__uri__in=refs).delete()
@property
def references(self):
from . import models
-admin.site.register(models.Entity)
+@admin.register(models.Entity)
+class EntityAdmin(admin.ModelAdmin):
+ list_display = ['uri', 'label']
+ search_fields = ['uri', 'label']
--- /dev/null
+# Generated by Django 4.0.8 on 2023-06-15 11:12
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('references', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='reference',
+ name='first_section',
+ ),
+ migrations.CreateModel(
+ name='Occurence',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('section', models.IntegerField()),
+ ('html', models.TextField()),
+ ('reference', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='references.reference')),
+ ],
+ options={
+ 'ordering': ('section',),
+ },
+ ),
+ ]
class Reference(models.Model):
book = models.ForeignKey('catalogue.Book', models.CASCADE)
entity = models.ForeignKey(Entity, models.CASCADE)
- first_section = models.CharField(max_length=255)
class Meta:
unique_together = (('book', 'entity'),)
+
+class Occurence(models.Model):
+ reference = models.ForeignKey(Reference, models.CASCADE)
+ section = models.IntegerField()
+ html = models.TextField()
+
+ class Meta:
+ ordering = ('section',)
display: flex;
flex-wrap: wrap;
gap: 5px;
+ max-height: 350px;
+ overflow-y: scroll;
}
.l-popup-books img {
display: block;
}
+ .l-popup-books .c-search-result-fragment {
+ padding: 0 0 20px 0;
+ border-width: 0;
+ border-radius: 0;
+ }
+ .l-popup-book-header {
+ display: flex;
+ gap: 16px;
+ }
</style>
{% endblock %}
{% block global-content %}
- <div id="map" style="height: calc(100vh - 68px"></div>
+ <div id="map" style="height: calc(100vh - 68px)"></div>
{% endblock %}
{% block extrabody %}
<script>
- var map = L.map('map').setView([51.505, -0.09], 8);
+ var map = L.map('map').setView([52, 20], 7);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
{% load thumbnail %}
-<h2 class="header">{{ entity.label }}</h2>
+<h2 class="header">
+ <a href="{{ entity.wikipedia_link }}" target="_blank">
+ {{ entity.label }}
+ </a>
+</h2>
<div class="l-popup-books">
{% for ref in entity.reference_set.all %}
- <a href='/katalog/lektura/{{ ref.book.slug }}.html#{{ ref.first_section }}' target="_blank">
- {% thumbnail ref.book.cover_clean '150x150' as th %}
- <img src="{{ th.url }}" alt="{{ ref.book.pretty_title }}" title="{{ ref.book.pretty_title }}">
- {% endthumbnail %}
- </a>
+ <!-- a href='/katalog/lektura/{{ ref.book.slug }}.html#{{ ref.first_section }}' target="_blank"-->
+ <div class="c-search-result-fragment">
+ <div class="l-popup-book-header">
+ <div>
+ {% thumbnail ref.book.cover_clean '150x150' as th %}
+ <a href="{{ ref.book.get_absolute_url }}" target="_blank">
+ <img src="{{ th.url }}" alt="{{ ref.book.pretty_title }}" title="{{ ref.book.pretty_title }}">
+ {% endthumbnail %}
+ </div>
+ <div>
+ {% for author in ref.book.authors %}
+ <a class="c-search-result-fragment-author" href="{{ author.get_absolute_url }}" target="_blank">
+ {{ author.name }}
+ </a>
+ {% endfor %}
+ <a class="c-search-result-fragment-title" href="{{ ref.book.get_absolute_url }}" target="_blank">
+ {{ ref.book.title }}
+ </a>
+ </div>
+ </div>
+
+ {% for occ in ref.occurence_set.all %}
+ <a target="_blank" href="/katalog/lektura/{{ ref.book.slug }}.html#sec{{ occ.section }}" class="c-search-result-fragment-text">
+ {{ occ.html|safe }}
+ </a>
+ {% endfor %}
+ </a>
+ </div>
{% endfor %}