From 6821e490c4e96e03643f7bdca20693f6b4d23703 Mon Sep 17 00:00:00 2001 From: Jamil Atta Junior Date: Mon, 20 Jun 2022 10:39:19 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20a=20aplica=C3=A7=C3=A3o=20do=20blog.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog/__init__.py | 0 blog/admin.py | 3 + blog/apps.py | 6 ++ blog/migrations/0001_initial.py | 75 ++++++++++++++++++++++ blog/migrations/__init__.py | 0 blog/models.py | 106 ++++++++++++++++++++++++++++++++ blog/tests.py | 3 + blog/views.py | 3 + config/settings/base.py | 1 + 9 files changed, 197 insertions(+) create mode 100644 blog/__init__.py create mode 100644 blog/admin.py create mode 100644 blog/apps.py create mode 100644 blog/migrations/0001_initial.py create mode 100644 blog/migrations/__init__.py create mode 100644 blog/models.py create mode 100644 blog/tests.py create mode 100644 blog/views.py diff --git a/blog/__init__.py b/blog/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/blog/admin.py b/blog/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/blog/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/blog/apps.py b/blog/apps.py new file mode 100644 index 00000000..94788a5e --- /dev/null +++ b/blog/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BlogConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'blog' diff --git a/blog/migrations/0001_initial.py b/blog/migrations/0001_initial.py new file mode 100644 index 00000000..dc048b60 --- /dev/null +++ b/blog/migrations/0001_initial.py @@ -0,0 +1,75 @@ +# Generated by Django 3.2.12 on 2022-06-20 13:05 + +from django.db import migrations, models +import django.db.models.deletion +import modelcluster.contrib.taggit +import modelcluster.fields +import wagtail.core.fields + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('taggit', '0004_alter_taggeditem_content_type_alter_taggeditem_tag'), + ('wagtailimages', '0023_add_choose_permissions'), + ('wagtailcore', '0066_collection_management_permissions'), + ] + + operations = [ + migrations.CreateModel( + name='BlogIndexPage', + fields=[ + ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.page')), + ('intro', wagtail.core.fields.RichTextField(blank=True)), + ], + options={ + 'abstract': False, + }, + bases=('wagtailcore.page',), + ), + migrations.CreateModel( + name='BlogPage', + fields=[ + ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.page')), + ('date', models.DateField(verbose_name='Post date')), + ('intro', models.CharField(max_length=250)), + ('body', wagtail.core.fields.RichTextField(blank=True)), + ], + options={ + 'abstract': False, + }, + bases=('wagtailcore.page',), + ), + migrations.CreateModel( + name='BlogPageTag', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content_object', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='tagged_items', to='blog.blogpage')), + ('tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_blogpagetag_items', to='taggit.tag')), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='BlogPageGalleryImage', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('sort_order', models.IntegerField(blank=True, editable=False, null=True)), + ('caption', models.CharField(blank=True, max_length=250)), + ('image', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='wagtailimages.image')), + ('page', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='gallery_images', to='blog.blogpage')), + ], + options={ + 'ordering': ['sort_order'], + 'abstract': False, + }, + ), + migrations.AddField( + model_name='blogpage', + name='tags', + field=modelcluster.contrib.taggit.ClusterTaggableManager(blank=True, help_text='A comma-separated list of tags.', through='blog.BlogPageTag', to='taggit.Tag', verbose_name='Tags'), + ), + ] diff --git a/blog/migrations/__init__.py b/blog/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/blog/models.py b/blog/models.py new file mode 100644 index 00000000..2673b77d --- /dev/null +++ b/blog/models.py @@ -0,0 +1,106 @@ +from django.db import models +from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator +from django.conf import settings + +from wagtail.core.models import Page, Orderable + +from wagtail.core.fields import RichTextField +from wagtail.admin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel + +from modelcluster.fields import ParentalKey +from modelcluster.contrib.taggit import ClusterTaggableManager +from taggit.models import TaggedItemBase + +from wagtail.images.edit_handlers import ImageChooserPanel +from wagtail.search import index + + +class BlogIndexPage(Page): + intro = RichTextField(blank=True) + + def get_context(self, request): + context = super().get_context(request) + all_posts = self.get_children().live().order_by('-blogpage__date') + all_tags = set() + + if request.GET.get('tag'): + all_posts = all_posts.filter(blogpage__tags__slug=request.GET.get('tag')) + + for post in all_posts.exclude(blogpage__tags=None)[0:100]: + for tag in post.specific.tags.all(): + all_tags.add(tag) + + # Paginate all posts by 2 per page + paginator = Paginator(all_posts, settings.PAGINATION_PER_PAGE) + # import pdb; pdb.set_trace() + # Try to get the ?page=x value + page = request.GET.get("page") + try: + # If the page exists and the ?page=x is an int + posts = paginator.page(page) + except PageNotAnInteger: + # If the ?page=x is not an int; show the first page + posts = paginator.page(1) + except EmptyPage: + # If the ?page=x is out of range (too high most likely) + # Then return the last page + posts = paginator.page(paginator.num_pages) + + context['posts'] = posts + context['tags'] = all_tags + + return context + + content_panels = Page.content_panels + [ + FieldPanel('intro', classname="full") + ] + + +class BlogPageTag(TaggedItemBase): + content_object = ParentalKey( + 'BlogPage', + related_name='tagged_items', + on_delete=models.CASCADE + ) + + +class BlogPage(Page): + date = models.DateField("Post date") + intro = models.CharField(max_length=250) + body = RichTextField(blank=True) + tags = ClusterTaggableManager(through=BlogPageTag, blank=True) + + def main_image(self): + gallery_item = self.gallery_images.first() + if gallery_item: + return gallery_item.image + else: + return None + + search_fields = Page.search_fields + [ + index.SearchField('intro'), + index.SearchField('body'), + ] + + content_panels = Page.content_panels + [ + MultiFieldPanel([ + FieldPanel('date'), + FieldPanel('tags'), + ], heading="Blog information"), + FieldPanel('intro'), + FieldPanel('body'), + InlinePanel('gallery_images', label="Gallery images"), + ] + + +class BlogPageGalleryImage(Orderable): + page = ParentalKey(BlogPage, on_delete=models.CASCADE, related_name='gallery_images') + image = models.ForeignKey( + 'wagtailimages.Image', on_delete=models.CASCADE, related_name='+' + ) + caption = models.CharField(blank=True, max_length=250) + + panels = [ + ImageChooserPanel('image'), + FieldPanel('caption'), + ] diff --git a/blog/tests.py b/blog/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/blog/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/blog/views.py b/blog/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/blog/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/config/settings/base.py b/config/settings/base.py index f003abd3..a2036a30 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -103,6 +103,7 @@ "wagtailcaptcha", "wagtailmenus", "rest_framework", + "blog", ] LOCAL_APPS = [