From f71a2e7e05658353a1b7d2c196e2ff3204af1106 Mon Sep 17 00:00:00 2001 From: Robert Sparks Date: Fri, 7 Jun 2019 15:56:52 -0500 Subject: [PATCH] Cause Document object urls to point to /media/filename instead of to /document/doc_id/filename. This is a workaround for a wart in wagtail that insists on serving documents as attachments. It is a mild compromise in that the proported inside wagtail features of counting document references and controlling access to files is circumvented. However, by design, we don't want different access controls to files for different viewers of the IETF website, and we are looking at different ways to gather information like references. See https://github.com/wagtail/wagtail/issues/4359, https://github.com/wagtail/wagtail/issues/1158, and the knot at https://github.com/wagtail/wagtail/issues/1420. --- ietf/documents/__init__.py | 0 ietf/documents/admin.py | 3 ++ ietf/documents/apps.py | 5 +++ ietf/documents/migrations/0001_initial.py | 29 ++++++++++++ .../0002_connect_ietfdocument_to_document.py | 30 +++++++++++++ ietf/documents/migrations/__init__.py | 0 ietf/documents/migrations/holdme | 18 ++++++++ ietf/documents/models.py | 9 ++++ .../templates/wagtaildocs/documents/list.html | 44 +++++++++++++++++++ ietf/documents/tests.py | 3 ++ ietf/documents/views.py | 3 ++ ietf/settings/base.py | 3 ++ 12 files changed, 147 insertions(+) create mode 100644 ietf/documents/__init__.py create mode 100644 ietf/documents/admin.py create mode 100644 ietf/documents/apps.py create mode 100644 ietf/documents/migrations/0001_initial.py create mode 100644 ietf/documents/migrations/0002_connect_ietfdocument_to_document.py create mode 100644 ietf/documents/migrations/__init__.py create mode 100644 ietf/documents/migrations/holdme create mode 100644 ietf/documents/models.py create mode 100644 ietf/documents/templates/wagtaildocs/documents/list.html create mode 100644 ietf/documents/tests.py create mode 100644 ietf/documents/views.py diff --git a/ietf/documents/__init__.py b/ietf/documents/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ietf/documents/admin.py b/ietf/documents/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/ietf/documents/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/ietf/documents/apps.py b/ietf/documents/apps.py new file mode 100644 index 00000000..93ca7550 --- /dev/null +++ b/ietf/documents/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class DocumentsConfig(AppConfig): + name = 'documents' diff --git a/ietf/documents/migrations/0001_initial.py b/ietf/documents/migrations/0001_initial.py new file mode 100644 index 00000000..e23a6b2b --- /dev/null +++ b/ietf/documents/migrations/0001_initial.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.21 on 2019-06-07 16:27 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('wagtaildocs', '0008_document_file_size'), + ] + + operations = [ + migrations.CreateModel( + name='IetfDocument', + fields=[ + ('document_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtaildocs.Document')), + ], + options={ + 'verbose_name': 'document', + 'abstract': False, + }, + bases=('wagtaildocs.document',), + ), + ] diff --git a/ietf/documents/migrations/0002_connect_ietfdocument_to_document.py b/ietf/documents/migrations/0002_connect_ietfdocument_to_document.py new file mode 100644 index 00000000..3f39dc0f --- /dev/null +++ b/ietf/documents/migrations/0002_connect_ietfdocument_to_document.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.21 on 2019-06-07 20:32 +from __future__ import unicode_literals + +from django.db import migrations, connection, transaction + +from wagtail.documents.models import Document + +def forward(apps, schema_editor): + IetfDocument = apps.get_model('documents', 'IetfDocument') + + # See https://stackoverflow.com/questions/4298278/django-using-custom-raw-sql-inserts-with-executemany-and-mysql + cursor = connection.cursor() + query = ' INSERT INTO documents_ietfdocument (document_ptr_id) values (%s) ' + queryList = Document.objects.values_list('pk') + cursor.executemany(query,queryList) +# transaction.commit() + +def reverse(apps, schema_editor): + pass + +class Migration(migrations.Migration): + + dependencies = [ + ('documents', '0001_initial'), + ] + + operations = [ + migrations.RunPython(forward,reverse) + ] diff --git a/ietf/documents/migrations/__init__.py b/ietf/documents/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ietf/documents/migrations/holdme b/ietf/documents/migrations/holdme new file mode 100644 index 00000000..a82e616c --- /dev/null +++ b/ietf/documents/migrations/holdme @@ -0,0 +1,18 @@ +# See https://stackoverflow.com/questions/4298278/django-using-custom-raw-sql-inserts-with-executemany-and-mysql +In [8]: from django.db import connection,transaction + +In [9]: cursor = connection.cursor() + +In [10]: query = ''' INSERT INTO documents_ietfdocument (document_ptr_id) values + ...: (%s) ''' + +In [15]: queryList = Document.objects.values_list('pk') + +In [16]: cursor.executemany(query,queryList) + +In [17]: transaction.commit() + +In [18]: IetfDocument.objects.count() +Out[18]: 286 + +In [19]: quit() diff --git a/ietf/documents/models.py b/ietf/documents/models.py new file mode 100644 index 00000000..b40c1102 --- /dev/null +++ b/ietf/documents/models.py @@ -0,0 +1,9 @@ +from django.db import models + +from wagtail.documents.models import Document + +class IetfDocument(Document): + + @property + def url(self): + return self.file.url diff --git a/ietf/documents/templates/wagtaildocs/documents/list.html b/ietf/documents/templates/wagtaildocs/documents/list.html new file mode 100644 index 00000000..3a0361a6 --- /dev/null +++ b/ietf/documents/templates/wagtaildocs/documents/list.html @@ -0,0 +1,44 @@ +{% load i18n %} + + + + + + + + + + + + + {% for doc in documents %} + + + + + + {% endfor %} + +
+ {% if not is_searching %} + + {% trans "Title" %} + + {% else %} + {% trans "Title" %} + {% endif %} + {% trans "File" %} + {% if not is_searching %} + + {% trans "Uploaded" %} + + {% else %} + {% trans "Uploaded" %} + {% endif %} +
+ {% if choosing %} +

{{ doc.title }}

+ {% else %} +

{{ doc.title }}

+ {% endif %} +
{{ doc.filename }}
{% blocktrans with time_period=doc.created_at|timesince %}{{ time_period }} ago{% endblocktrans %}
diff --git a/ietf/documents/tests.py b/ietf/documents/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/ietf/documents/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/ietf/documents/views.py b/ietf/documents/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/ietf/documents/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/ietf/settings/base.py b/ietf/settings/base.py index 867884ec..70508c90 100644 --- a/ietf/settings/base.py +++ b/ietf/settings/base.py @@ -39,6 +39,7 @@ 'ietf.datatracker', 'ietf.bibliography', 'ietf.images', + 'ietf.documents', 'wagtail.contrib.forms', 'wagtail.contrib.redirects', @@ -209,3 +210,5 @@ WAGTAIL_SITE_NAME = "ietf" WAGTAIL_USAGE_COUNT_ENABLED = True WAGTAILIMAGES_IMAGE_MODEL = 'images.IETFImage' +WAGTAILDOCS_DOCUMENT_MODEL = 'documents.IetfDocument' +