Nicer references.
authorRadek Czajka <rczajka@rczajka.pl>
Thu, 15 Jun 2023 12:40:03 +0000 (14:40 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Thu, 15 Jun 2023 12:40:03 +0000 (14:40 +0200)
requirements/requirements.txt
src/catalogue/models/book.py
src/references/admin.py
src/references/migrations/0002_remove_reference_first_section_occurence.py [new file with mode: 0644]
src/references/models.py
src/references/templates/references/map.html
src/references/templates/references/popup.html

index 86afdfc..7dcdbb3 100644 (file)
@@ -40,7 +40,7 @@ mutagen==1.45.1
 sorl-thumbnail==12.8.0
 
 # home-brewed & dependencies
-librarian==2.4.12
+librarian==2.5
 
 # celery tasks
 celery[redis]==5.2.7
index bcbefea..3eb6023 100644 (file)
@@ -20,6 +20,7 @@ from fnpdjango.storage import BofhFileSystemStorage
 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
@@ -713,45 +714,42 @@ class Book(models.Model):
         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):
index 191d0e6..cba8b88 100644 (file)
@@ -2,4 +2,7 @@ from django.contrib import admin
 from . import models
 
 
-admin.site.register(models.Entity)
+@admin.register(models.Entity)
+class EntityAdmin(admin.ModelAdmin):
+    list_display = ['uri', 'label']
+    search_fields = ['uri', 'label']
diff --git a/src/references/migrations/0002_remove_reference_first_section_occurence.py b/src/references/migrations/0002_remove_reference_first_section_occurence.py
new file mode 100644 (file)
index 0000000..0c1e077
--- /dev/null
@@ -0,0 +1,30 @@
+# 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',),
+            },
+        ),
+    ]
index 9246ee3..d4733ff 100644 (file)
@@ -76,8 +76,15 @@ class Entity(models.Model):
 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',)
index 1ea6508..242d92f 100644 (file)
        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: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);
index 1708273..f4ac1f0 100644 (file)
@@ -1,11 +1,38 @@
 {% 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 %}