Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSRC-62: Document block #1638

Merged
merged 29 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9a0d8b7
Created DocumentBlock to add link text and a related file
jamesbiggs Jun 5, 2024
2ccbc1e
Added DocumentBlock to the __init__
jamesbiggs Jun 5, 2024
a7bb1db
Added DocumentBlock to SectionContentBlock
jamesbiggs Jun 5, 2024
7316acd
Formatting
jamesbiggs Jun 5, 2024
9b36828
Checked migration
jamesbiggs Jun 5, 2024
2c8e3b9
Added `GENERIC_RICH_TEXT_FEATURES` for later use to add documents to …
jamesbiggs Jun 13, 2024
267c80f
Deleted migration
jamesbiggs Jun 18, 2024
c451226
Merge branch 'develop' into feature/DSRC-62-documents
jamesbiggs Jun 18, 2024
75885cc
Added DocumentBlock to GeneralPage
jamesbiggs Jun 18, 2024
e833fe6
Document -> Documents
jamesbiggs Jun 18, 2024
e4fabd1
Merge branch 'develop' into feature/DSRC-62-documents
jamesbiggs Jul 2, 2024
1eb7900
Fixed migration
jamesbiggs Jul 2, 2024
97ad4bf
Extended base DocumentModel with CustomDocument to add extent and des…
jamesbiggs Jul 2, 2024
e984300
Updated API representation of the DocumentBlock
jamesbiggs Jul 2, 2024
68b8cfd
Migrations
jamesbiggs Jul 2, 2024
8189421
Formatting
jamesbiggs Jul 2, 2024
ead48b3
More formatting...
jamesbiggs Jul 2, 2024
9f6cc51
Removed GENERIC_RICH_TEXT_FEATURES
jamesbiggs Jul 2, 2024
7b39a3a
Dropped to 1DP
jamesbiggs Jul 2, 2024
4698ce8
Merge branch 'develop' into feature/DSRC-62-documents
jamesbiggs Jul 9, 2024
897e4d4
Merge branch 'develop' into feature/DSRC-62-documents
jamesbiggs Jul 10, 2024
0254156
Removed link_text and updated migrations
jamesbiggs Jul 10, 2024
9e3a7d1
Added pretty_file_size
jamesbiggs Jul 10, 2024
61f99ae
Changed to 1000 base
jamesbiggs Jul 10, 2024
768f8fc
Update etna/core/models/documents.py
jamesbiggs Jul 10, 2024
7577978
Differing decimal places, and strip .0 or .00 endings
jamesbiggs Jul 10, 2024
749a9aa
Formatting
jamesbiggs Jul 10, 2024
e7c2d0b
Further formatting
jamesbiggs Jul 10, 2024
727fd45
Even more formatting...
jamesbiggs Jul 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@
}
}

WAGTAILDOCS_DOCUMENT_MODEL = "core.CustomDocument"

WAGTAILIMAGES_IMAGE_MODEL = "images.CustomImage"

# Custom password template for private pages
Expand Down
2 changes: 2 additions & 0 deletions etna/core/blocks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .base import SectionDepthAwareStructBlock
from .cta import ButtonBlock, CallToActionBlock, LargeCardLinksBlock
from .document import DocumentsBlock
from .featured_content import (
FeaturedCollectionBlock,
FeaturedRecordArticleBlock,
Expand Down Expand Up @@ -27,6 +28,7 @@
"ButtonBlock",
"CallToActionBlock",
"ContentImageBlock",
"DocumentsBlock",
"DoDontListBlock",
"FeaturedRecordArticleBlock",
"FeaturedCollectionBlock",
Expand Down
45 changes: 45 additions & 0 deletions etna/core/blocks/document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from wagtail import blocks
from wagtail.documents.blocks import DocumentChooserBlock


class DocumentBlock(blocks.StructBlock):
"""
A block for embedding a document file in a page.
"""

file = DocumentChooserBlock(required=True)

def get_api_representation(self, value, context=None):
representation = super().get_api_representation(value, context)

file = value.get("file")

if file:
representation["file"] = {
"id": file.id,
"title": file.title,
"description": file.description or None,
"file_size": file.file_size,
"pretty_file_size": file.pretty_file_size,
"type": file.file_extension,
"extent": file.extent,
"url": file.file.url,
}

return representation

class Meta:
icon = "doc-full"
label = "Document"


class DocumentsBlock(blocks.StructBlock):
"""
A block for embedding multiple document files in a page.
"""

documents = blocks.ListBlock(DocumentBlock())

class Meta:
icon = "doc-full"
label = "Documents"
88 changes: 88 additions & 0 deletions etna/core/migrations/0004_customdocument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Generated by Django 5.0.6 on 2024-07-02 15:35

import django.db.models.deletion
import taggit.managers
import wagtail.models.media
import wagtail.search.index
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0003_set_page_uuid"),
(
"taggit",
"0006_rename_taggeditem_content_type_object_id_taggit_tagg_content_8fc721_idx",
),
("wagtailcore", "0093_uploadedfile"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="CustomDocument",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=255, verbose_name="title")),
("file", models.FileField(upload_to="documents", verbose_name="file")),
(
"created_at",
models.DateTimeField(auto_now_add=True, verbose_name="created at"),
),
("file_size", models.PositiveIntegerField(editable=False, null=True)),
(
"file_hash",
models.CharField(blank=True, editable=False, max_length=40),
),
("extent", models.CharField(blank=True, null=True)),
("description", models.TextField(blank=True, null=True)),
(
"collection",
models.ForeignKey(
default=wagtail.models.media.get_root_collection_id,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="wagtailcore.collection",
verbose_name="collection",
),
),
(
"tags",
taggit.managers.TaggableManager(
blank=True,
help_text=None,
through="taggit.TaggedItem",
to="taggit.Tag",
verbose_name="tags",
),
),
(
"uploaded_by_user",
models.ForeignKey(
blank=True,
editable=False,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL,
verbose_name="uploaded by user",
),
),
],
options={
"verbose_name": "document",
"verbose_name_plural": "documents",
"abstract": False,
},
bases=(wagtail.search.index.Indexed, models.Model),
),
]
1 change: 1 addition & 0 deletions etna/core/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .basepage import * # noqa
from .documents import * # noqa
from .forms import * # noqa
from .mixins import * # noqa
from .settings import * # noqa
23 changes: 23 additions & 0 deletions etna/core/models/documents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.db import models

from wagtail.documents.models import AbstractDocument, Document


class CustomDocument(AbstractDocument):
extent = models.CharField(blank=True, null=True)
description = models.TextField(blank=True, null=True)

@property
def pretty_file_size(self):
suffixes = ["B", "KB", "MB", "GB"]
i = 0
pretty_file_size = self.file_size
while pretty_file_size >= 1000 and i < len(suffixes) - 1:
pretty_file_size /= 1000
i += 1
return f"{pretty_file_size:.1f}{suffixes[i]}"

admin_form_fields = Document.admin_form_fields + (
"extent",
"description",
)
2 changes: 2 additions & 0 deletions etna/generic_pages/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ButtonBlock,
CallToActionBlock,
ContentImageBlock,
DocumentsBlock,
DoDontListBlock,
FeaturedRecordArticleBlock,
InsetTextBlock,
Expand All @@ -23,6 +24,7 @@
class SectionContentBlock(blocks.StreamBlock):
button = ButtonBlock()
call_to_action = CallToActionBlock()
document = DocumentsBlock()
do_dont_list = DoDontListBlock()
featured_record_article = FeaturedRecordArticleBlock()
image = ContentImageBlock()
Expand Down
Loading
Loading