|
| 1 | +from django.db import models |
| 2 | + |
| 3 | +from modelcluster.fields import ParentalKey |
| 4 | + |
| 5 | +from wagtail.admin.edit_handlers import ( FieldPanel, |
| 6 | + InlinePanel, |
| 7 | + StreamFieldPanel, |
| 8 | + ) |
| 9 | +from wagtail.images.edit_handlers import ImageChooserPanel |
| 10 | +from wagtail.snippets.edit_handlers import SnippetChooserPanel |
| 11 | +from wagtail.core.fields import StreamField |
| 12 | +from wagtail.core.models import Page, Orderable |
| 13 | +from wagtail.search import index |
| 14 | + |
| 15 | +from home.blocks import BaseStreamBlock |
| 16 | + |
| 17 | +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
| 18 | + |
| 19 | + |
| 20 | +# Create your models here. |
| 21 | +class BlogPage(Page): |
| 22 | + """ |
| 23 | + Blog Details Page |
| 24 | + """ |
| 25 | + banner_image = models.ForeignKey( |
| 26 | + 'wagtailimages.Image', |
| 27 | + blank=True, |
| 28 | + null=True, |
| 29 | + on_delete=models.SET_NULL, |
| 30 | + related_name='+', |
| 31 | + help_text='Banner Image' |
| 32 | + ) |
| 33 | + banner_intro = models.CharField(max_length=50, blank=True) |
| 34 | + introduction = models.TextField(blank=True, |
| 35 | + help_text='Text to Describe the Page' |
| 36 | + ) |
| 37 | + blog_introduction = models.CharField(max_length=500, blank=True) |
| 38 | + blog_title = models.CharField(blank=True, |
| 39 | + max_length=255 |
| 40 | + ) |
| 41 | + blog_image = models.ForeignKey( |
| 42 | + 'wagtailimages.Image', |
| 43 | + null=True, |
| 44 | + blank=True, |
| 45 | + on_delete=models.SET_NULL, |
| 46 | + related_name='+', |
| 47 | + ) |
| 48 | + body = StreamField( |
| 49 | + BaseStreamBlock(), verbose_name='Page Body', |
| 50 | + blank=True |
| 51 | + ) |
| 52 | + date_published = models.DateField("Date Article Published", |
| 53 | + blank=True, null=True) |
| 54 | + |
| 55 | + content_panels = Page.content_panels + [ |
| 56 | + ImageChooserPanel('banner_image'), |
| 57 | + FieldPanel('banner_intro'), |
| 58 | + FieldPanel('introduction', classname='full'), |
| 59 | + FieldPanel('blog_introduction'), |
| 60 | + FieldPanel('blog_title', classname='full'), |
| 61 | + ImageChooserPanel('blog_image'), |
| 62 | + StreamFieldPanel('body'), |
| 63 | + FieldPanel('date_published'), |
| 64 | + # InlinePanel('blog_person_relationship', |
| 65 | + # label='Author', |
| 66 | + # panels=None, |
| 67 | + # min_num=1) |
| 68 | + ] |
| 69 | + |
| 70 | + search_fields = Page.search_fields + [ |
| 71 | + index.SearchField('body'), |
| 72 | + ] |
| 73 | + |
| 74 | + # def author(self): |
| 75 | + # authors = [ |
| 76 | + # n.people for n in self.blog_person_relationship |
| 77 | + # ] |
| 78 | + # return authors |
| 79 | + |
| 80 | + # Specifies parent to BlogPage as being BlogIndexPages |
| 81 | + parent_page_types = ['BlogIndex'] |
| 82 | + |
| 83 | +class BlogIndex(Page): |
| 84 | + banner_image = models.ForeignKey( |
| 85 | + 'wagtailimages.Image', |
| 86 | + blank=True, |
| 87 | + null=True, |
| 88 | + on_delete=models.SET_NULL, |
| 89 | + related_name='+', |
| 90 | + help_text='Banner Image' |
| 91 | + ) |
| 92 | + banner_intro = models.CharField(max_length=50, blank=True) |
| 93 | + introduction = models.CharField( |
| 94 | + max_length=200, |
| 95 | + help_text='Text to Describe the Page', |
| 96 | + blank=True, |
| 97 | + ) |
| 98 | + index_image = models.ForeignKey( |
| 99 | + 'wagtailimages.Image', |
| 100 | + null=True, |
| 101 | + blank=True, |
| 102 | + on_delete=models.SET_NULL, |
| 103 | + related_name='+', |
| 104 | + help_text='Image for Blog' |
| 105 | + ) |
| 106 | + |
| 107 | + content_panels = Page.content_panels + [ |
| 108 | + ImageChooserPanel('banner_image'), |
| 109 | + FieldPanel('banner_intro'), |
| 110 | + FieldPanel('introduction'), |
| 111 | + ImageChooserPanel('index_image'), |
| 112 | + ] |
| 113 | + |
| 114 | + # Speficies that only BlogPage objects can live under this index page |
| 115 | + subpage_types = ['BlogPage'] |
| 116 | + |
| 117 | + def children(self): |
| 118 | + return self.get_children().specific().live() |
| 119 | + |
| 120 | + def get_context(self,request): |
| 121 | + context = super(BlogIndex, self).get_context(request) |
| 122 | + context['posts'] = BlogPage.objects.descendant_of( |
| 123 | + self).live().order_by('-date_published') |
| 124 | + |
| 125 | + all_pages = BlogPage.objects.live() |
| 126 | + paginator = Paginator(all_pages,5) # 5 index per page |
| 127 | + page = request.GET.get('page') |
| 128 | + |
| 129 | + try: |
| 130 | + pages = paginator.page(page) |
| 131 | + except PageNotAnInteger: |
| 132 | + # If page is not an integer, deliver first page. |
| 133 | + pages = paginator.page(1) |
| 134 | + except EmptyPage: |
| 135 | + # If page is out of range (e.g. 9999), deliver last page of results. |
| 136 | + pages = paginator.page(paginator.num_pages) |
| 137 | + |
| 138 | + context['pages'] = pages |
| 139 | + |
| 140 | + return context |
0 commit comments