Skip to content

Commit

Permalink
Adiciona a aplicação do blog.
Browse files Browse the repository at this point in the history
  • Loading branch information
gitnnolabs committed Jun 20, 2022
1 parent 87427a6 commit 6821e49
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 0 deletions.
Empty file added blog/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions blog/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.
6 changes: 6 additions & 0 deletions blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'blog'
75 changes: 75 additions & 0 deletions blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
Empty file added blog/migrations/__init__.py
Empty file.
106 changes: 106 additions & 0 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -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'),
]
3 changes: 3 additions & 0 deletions blog/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 blog/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.
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"wagtailcaptcha",
"wagtailmenus",
"rest_framework",
"blog",
]

LOCAL_APPS = [
Expand Down

0 comments on commit 6821e49

Please sign in to comment.