From 9d0a4f124c6b0dca121206c8b4f12826c510906a Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Mon, 23 May 2022 11:42:52 +0200 Subject: [PATCH 1/1] Add first alerts. --- src/alerts/__init__.py | 0 src/alerts/admin.py | 5 +++ src/alerts/apps.py | 6 ++++ src/alerts/migrations/0001_initial.py | 24 ++++++++++++++ src/alerts/migrations/__init__.py | 0 src/alerts/models.py | 32 +++++++++++++++++++ src/alerts/rules.py | 35 +++++++++++++++++++++ src/alerts/templatetags/alerts.py | 12 +++++++ src/alerts/tests.py | 3 ++ src/alerts/views.py | 3 ++ src/documents/templates/documents/base.html | 21 ++++++++++++- src/redakcja/settings/__init__.py | 1 + 12 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/alerts/__init__.py create mode 100644 src/alerts/admin.py create mode 100644 src/alerts/apps.py create mode 100644 src/alerts/migrations/0001_initial.py create mode 100644 src/alerts/migrations/__init__.py create mode 100644 src/alerts/models.py create mode 100644 src/alerts/rules.py create mode 100644 src/alerts/templatetags/alerts.py create mode 100644 src/alerts/tests.py create mode 100644 src/alerts/views.py diff --git a/src/alerts/__init__.py b/src/alerts/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/alerts/admin.py b/src/alerts/admin.py new file mode 100644 index 00000000..3eda708d --- /dev/null +++ b/src/alerts/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Alert + + +admin.site.register(Alert) diff --git a/src/alerts/apps.py b/src/alerts/apps.py new file mode 100644 index 00000000..0a64765e --- /dev/null +++ b/src/alerts/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AlertsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'alerts' diff --git a/src/alerts/migrations/0001_initial.py b/src/alerts/migrations/0001_initial.py new file mode 100644 index 00000000..2e1230c3 --- /dev/null +++ b/src/alerts/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2.12 on 2022-05-23 11:14 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('documents', '0008_book_legimi_id'), + ] + + operations = [ + migrations.CreateModel( + name='Alert', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('tag', models.CharField(max_length=32)), + ('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='documents.book')), + ], + ), + ] diff --git a/src/alerts/migrations/__init__.py b/src/alerts/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/alerts/models.py b/src/alerts/models.py new file mode 100644 index 00000000..268089ab --- /dev/null +++ b/src/alerts/models.py @@ -0,0 +1,32 @@ +from django.db import models +from django.dispatch import receiver +from dvcs.models import post_commit +from .rules import rules, rules_by_tag + + +class Alert(models.Model): + book = models.ForeignKey('documents.Book', models.CASCADE) + tag = models.CharField(max_length=32) + + @property + def rule(self): + return rules_by_tag[self.tag] + + @classmethod + def validate_book(cls, book): + cls.objects.filter(book=book).delete() + try: + wlbook = book.wldocument(publishable=False, librarian2=True) + except: + cls.objects.create(book=book, tag='parse') + return + + for rule in rules: + if rule.check_meta(wlbook.meta): + print(rule.tag, book) + cls.objects.create(book=book, tag=rule.tag) + + +@receiver(post_commit) +def validate_post_commit(sender, **kwargs): + Alert.validate_book(sender.tree.book) diff --git a/src/alerts/rules.py b/src/alerts/rules.py new file mode 100644 index 00000000..c7903f1a --- /dev/null +++ b/src/alerts/rules.py @@ -0,0 +1,35 @@ +import re +from django.utils.translation import gettext_lazy as _ + + +class Check: + def check_meta(self, meta): + return False + + +class CheckParse(Check): + tag = 'parse' + description = _('Book parse error.') + + +class CheckCoverLocal(Check): + tag = 'cover-local' + description = _('Cover is not local') + + def check_meta(self, meta): + print(meta) + if meta.cover_source is None: + print('no cover_source') + return False + return not re.match(r'https?://redakcja.wolnelektury.pl/cover/image/', meta.cover_source) + + +rules = [ + CheckParse(), + CheckCoverLocal(), +] + +rules_by_tag = { + r.tag: r + for r in rules +} diff --git a/src/alerts/templatetags/alerts.py b/src/alerts/templatetags/alerts.py new file mode 100644 index 00000000..cbab9de2 --- /dev/null +++ b/src/alerts/templatetags/alerts.py @@ -0,0 +1,12 @@ +from django.template import Library +from ..models import Alert + + +register = Library() + +@register.simple_tag +def get_alerts(): + return { + 'count': Alert.objects.all().count(), + 'items': Alert.objects.all()[:20], + } diff --git a/src/alerts/tests.py b/src/alerts/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/src/alerts/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/src/alerts/views.py b/src/alerts/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/src/alerts/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/src/documents/templates/documents/base.html b/src/documents/templates/documents/base.html index 81af8933..2d5fe8ea 100644 --- a/src/documents/templates/documents/base.html +++ b/src/documents/templates/documents/base.html @@ -2,6 +2,7 @@ {% load pipeline i18n %} {% load static %} {% load documents %} +{% load alerts %} @@ -26,7 +27,25 @@ diff --git a/src/redakcja/settings/__init__.py b/src/redakcja/settings/__init__.py index 01fb6cee..1325743e 100644 --- a/src/redakcja/settings/__init__.py +++ b/src/redakcja/settings/__init__.py @@ -102,6 +102,7 @@ INSTALLED_APPS = ( 'apiclient', 'email_mangler', 'wlxml.apps.WlxmlConfig', + 'alerts', ) if DEBUG: -- 2.20.1