Skip to content

Commit

Permalink
Cause Document object urls to point to /media/filename instead of to …
Browse files Browse the repository at this point in the history
…/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 wagtail/wagtail#4359, wagtail/wagtail#1158, and the knot at wagtail/wagtail#1420.
  • Loading branch information
rjsparks committed Jun 7, 2019
1 parent 94fe542 commit f71a2e7
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 0 deletions.
Empty file added ietf/documents/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions ietf/documents/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions ietf/documents/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class DocumentsConfig(AppConfig):
name = 'documents'
29 changes: 29 additions & 0 deletions ietf/documents/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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',),
),
]
30 changes: 30 additions & 0 deletions ietf/documents/migrations/0002_connect_ietfdocument_to_document.py
Original file line number Diff line number Diff line change
@@ -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)
]
Empty file.
18 changes: 18 additions & 0 deletions ietf/documents/migrations/holdme
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions ietf/documents/models.py
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions ietf/documents/templates/wagtaildocs/documents/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{% load i18n %}
<table class="listing">
<col />
<col />
<col width="16%" />
<thead>
<tr class="table-headers">
<th>
{% if not is_searching %}
<a href="{% url 'wagtaildocs:index' %}{% if not ordering == "title" %}?ordering=title{% endif %}" class="icon icon-arrow-down-after {% if ordering == "title" %}teal{% endif %}">
{% trans "Title" %}
</a>
{% else %}
{% trans "Title" %}
{% endif %}
</th>
<th>{% trans "File" %}</th>
<th>
{% if not is_searching %}
<a href="{% url 'wagtaildocs:index' %}{% if not ordering == "-created_at" %}?ordering=-created_at{% endif %}" class="icon icon-arrow-down-after {% if ordering == "-created_at" %}teal{% endif %}">
{% trans "Uploaded" %}
</a>
{% else %}
{% trans "Uploaded" %}
{% endif %}
</th>
</tr>
</thead>
<tbody>
{% for doc in documents %}
<tr>
<td class="title">
{% if choosing %}
<h2><a href="{% url 'wagtaildocs:document_chosen' doc.id %}" class="document-choice">{{ doc.title }}</a></h2>
{% else %}
<h2><a href="{% url 'wagtaildocs:edit' doc.id %}">{{ doc.title }}</a></h2>
{% endif %}
</td>
<td><a href="{{ doc.url }}" class="nolink">{{ doc.filename }}</a></td>
<td><div class="human-readable-date" title="{{ doc.created_at|date:"d M Y H:i" }}">{% blocktrans with time_period=doc.created_at|timesince %}{{ time_period }} ago{% endblocktrans %}</div></td>
</tr>
{% endfor %}
</tbody>
</table>
3 changes: 3 additions & 0 deletions ietf/documents/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions ietf/documents/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
3 changes: 3 additions & 0 deletions ietf/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'ietf.datatracker',
'ietf.bibliography',
'ietf.images',
'ietf.documents',

'wagtail.contrib.forms',
'wagtail.contrib.redirects',
Expand Down Expand Up @@ -209,3 +210,5 @@
WAGTAIL_SITE_NAME = "ietf"
WAGTAIL_USAGE_COUNT_ENABLED = True
WAGTAILIMAGES_IMAGE_MODEL = 'images.IETFImage'
WAGTAILDOCS_DOCUMENT_MODEL = 'documents.IetfDocument'

0 comments on commit f71a2e7

Please sign in to comment.