--- /dev/null
+# Generated by Django 3.1.13 on 2022-03-01 14:11
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('documents', '0006_auto_20210706_0130'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='book',
+ name='dc',
+ field=models.JSONField(editable=False, null=True),
+ ),
+ ]
#
from django.apps import apps
from django.contrib.sites.models import Site
-from django.db import models, transaction
+from django.db import connection, models, transaction
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
_on_track = models.IntegerField(null=True, blank=True, db_index=True, editable=False)
dc_cover_image = models.ForeignKey(Image, blank=True, null=True,
db_index=True, on_delete=models.SET_NULL, editable=False)
+ dc = models.JSONField(null=True, editable=False)
catalogue_book = models.ForeignKey(
'catalogue.Book',
models.DO_NOTHING,
qs = qs.filter(public=True)
return qs
+ @staticmethod
+ def q_dc(field, field_plural, value, prefix=''):
+ if connection.features.supports_json_field_contains:
+ return models.Q(**{f'{prefix}dc__{field_plural}__contains': value})
+ else:
+ return models.Q(**{f'{prefix}dc__{field}': value})
+
+
# Representing
# ============
else:
if info.cover_source == image.get_full_url():
update['dc_cover_image'] = image
+ update['dc'] = info.to_dict()
Book.objects.filter(pk=self.pk).update(**update)
def touch(self):
# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
-from re import split
+import re
from django.db.models import Q, Count, F, Max
from django import template
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
-from documents.models import Chunk, Image, Project
+from documents.models import Book, Chunk, Image, Project
register = template.Library()
def search_filter(qs, value, filter_fields):
if not value:
return qs
- q = Q(**{"%s__icontains" % filter_fields[0]: value})
- for field in filter_fields[1:]:
- q |= Q(**{"%s__icontains" % field: value})
- return qs.filter(q)
+
+ for word in value.split():
+ m = re.match(r'(.+):(.+)', word)
+ if m is not None:
+ field = m.group(1)
+ value = m.group(2)
+ q = Book.q_dc(field, field + 's', value, 'book__')
+ else:
+ q = Q(**{"%s__icontains" % filter_fields[0]: value})
+ for field in filter_fields[1:]:
+ q |= Q(**{"%s__icontains" % field: value})
+ qs = qs.filter(q)
+
+ return qs
_states = [