Repository: UTNkar/moore

Issue:
News block
<!-- Do you want to ask a question? Are you looking for support? The system administrator can help you: admin@utn.se -->

### Prerequisites

* [ ] Put an X between the brackets on this line if you have done all of the
following:
    * Reproduced the problem with clear cache.
    * (If running the application locally:) Made sure your running the newest version on the development branch
    * Checked that your issue isn't already filed: https://github.com/UTNkar/moore/issues

### Description

[Description of the issue]

### Steps to Reproduce

1. [First Step]
2. [Second Step]
3. [and so on...]

<!-- Please select the appropriate "topic category"/blue and "issue type"/yellow label -->


Discussion:

Resolving patch:
diff --git a/website/home/migrations/0005_auto_20170628_2222.py b/website/home/migrations/0005_auto_20170628_2222.py
new file mode 100644
--- /dev/null
+++ b/website/home/migrations/0005_auto_20170628_2222.py
@@ -0,0 +1,38 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.2 on 2017-06-28 20:22
+from __future__ import unicode_literals
+
+from django.db import migrations
+import wagtail.wagtailcore.blocks
+import wagtail.wagtailcore.fields
+import wagtail.wagtailimages.blocks
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('home', '0004_webpage'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='homepage',
+            name='body_en',
+            field=wagtail.wagtailcore.fields.StreamField((('heading', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock(required=True)), ('subtitle', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('paragraph', wagtail.wagtailcore.blocks.RichTextBlock()), ('image_description', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock()), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('image_alignment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('left', 'Left'), ('right', 'Right')])), ('icons', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailcore.blocks.StructBlock((('icon', wagtail.wagtailcore.blocks.CharBlock(help_text='Material icon font icon text, as found on: https://material.io/icons')), ('title', wagtail.wagtailcore.blocks.CharBlock()), ('description', wagtail.wagtailcore.blocks.CharBlock()))))), ('hide_on_med', wagtail.wagtailcore.blocks.BooleanBlock(required=False))))), ('image_icons', wagtail.wagtailcore.blocks.StructBlock((('description', wagtail.wagtailcore.blocks.RichTextBlock()), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('image_alignment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('left', 'Left'), ('right', 'Right')])), ('hide_on_med', wagtail.wagtailcore.blocks.BooleanBlock(required=False))))), ('overlay', wagtail.wagtailcore.blocks.StructBlock((('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('title', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('description', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('link', wagtail.wagtailcore.blocks.URLBlock(required=False)), ('button', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('logos', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailimages.blocks.ImageChooserBlock(), icon='fa-pied-piper', label='Logos', template='blocks/logos.html')), ('counters', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock()), ('counters', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailcore.blocks.StructBlock((('icon', wagtail.wagtailcore.blocks.CharBlock(help_text='Material icon font icon text, as found on: https://material.io/icons')), ('value', wagtail.wagtailcore.blocks.CharBlock()), ('description', wagtail.wagtailcore.blocks.CharBlock(required=False)))))), ('style', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('light', 'Light'), ('dark', 'Dark')]))))), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock(template='blocks/image.html')), ('news', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('subtitle', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('index', wagtail.wagtailcore.blocks.PageChooserBlock(target_model=['news.NewsIndexPage'])), ('items', wagtail.wagtailcore.blocks.IntegerBlock()))))), blank=True),
+        ),
+        migrations.AlterField(
+            model_name='homepage',
+            name='body_sv',
+            field=wagtail.wagtailcore.fields.StreamField((('heading', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock(required=True)), ('subtitle', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('paragraph', wagtail.wagtailcore.blocks.RichTextBlock()), ('image_description', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock()), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('image_alignment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('left', 'Left'), ('right', 'Right')])), ('icons', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailcore.blocks.StructBlock((('icon', wagtail.wagtailcore.blocks.CharBlock(help_text='Material icon font icon text, as found on: https://material.io/icons')), ('title', wagtail.wagtailcore.blocks.CharBlock()), ('description', wagtail.wagtailcore.blocks.CharBlock()))))), ('hide_on_med', wagtail.wagtailcore.blocks.BooleanBlock(required=False))))), ('image_icons', wagtail.wagtailcore.blocks.StructBlock((('description', wagtail.wagtailcore.blocks.RichTextBlock()), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('image_alignment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('left', 'Left'), ('right', 'Right')])), ('hide_on_med', wagtail.wagtailcore.blocks.BooleanBlock(required=False))))), ('overlay', wagtail.wagtailcore.blocks.StructBlock((('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('title', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('description', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('link', wagtail.wagtailcore.blocks.URLBlock(required=False)), ('button', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('logos', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailimages.blocks.ImageChooserBlock(), icon='fa-pied-piper', label='Logos', template='blocks/logos.html')), ('counters', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock()), ('counters', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailcore.blocks.StructBlock((('icon', wagtail.wagtailcore.blocks.CharBlock(help_text='Material icon font icon text, as found on: https://material.io/icons')), ('value', wagtail.wagtailcore.blocks.CharBlock()), ('description', wagtail.wagtailcore.blocks.CharBlock(required=False)))))), ('style', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('light', 'Light'), ('dark', 'Dark')]))))), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock(template='blocks/image.html')), ('news', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('subtitle', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('index', wagtail.wagtailcore.blocks.PageChooserBlock(target_model=['news.NewsIndexPage'])), ('items', wagtail.wagtailcore.blocks.IntegerBlock()))))), blank=True),
+        ),
+        migrations.AlterField(
+            model_name='webpage',
+            name='body_en',
+            field=wagtail.wagtailcore.fields.StreamField((('heading', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock(required=True)), ('subtitle', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('paragraph', wagtail.wagtailcore.blocks.RichTextBlock()), ('image_description', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock()), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('image_alignment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('left', 'Left'), ('right', 'Right')])), ('icons', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailcore.blocks.StructBlock((('icon', wagtail.wagtailcore.blocks.CharBlock(help_text='Material icon font icon text, as found on: https://material.io/icons')), ('title', wagtail.wagtailcore.blocks.CharBlock()), ('description', wagtail.wagtailcore.blocks.CharBlock()))))), ('hide_on_med', wagtail.wagtailcore.blocks.BooleanBlock(required=False))))), ('image_icons', wagtail.wagtailcore.blocks.StructBlock((('description', wagtail.wagtailcore.blocks.RichTextBlock()), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('image_alignment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('left', 'Left'), ('right', 'Right')])), ('hide_on_med', wagtail.wagtailcore.blocks.BooleanBlock(required=False))))), ('overlay', wagtail.wagtailcore.blocks.StructBlock((('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('title', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('description', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('link', wagtail.wagtailcore.blocks.URLBlock(required=False)), ('button', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('logos', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailimages.blocks.ImageChooserBlock(), icon='fa-pied-piper', label='Logos', template='blocks/logos.html')), ('counters', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock()), ('counters', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailcore.blocks.StructBlock((('icon', wagtail.wagtailcore.blocks.CharBlock(help_text='Material icon font icon text, as found on: https://material.io/icons')), ('value', wagtail.wagtailcore.blocks.CharBlock()), ('description', wagtail.wagtailcore.blocks.CharBlock(required=False)))))), ('style', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('light', 'Light'), ('dark', 'Dark')]))))), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock(template='blocks/image.html')), ('google_form', wagtail.wagtailcore.blocks.StructBlock((('form_id', wagtail.wagtailcore.blocks.CharBlock()), ('height', wagtail.wagtailcore.blocks.IntegerBlock())))), ('google_drive', wagtail.wagtailcore.blocks.StructBlock((('folder_id', wagtail.wagtailcore.blocks.CharBlock()), ('view', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('list', 'List'), ('grid', 'Grid')])), ('height', wagtail.wagtailcore.blocks.IntegerBlock())))), ('news', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('subtitle', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('index', wagtail.wagtailcore.blocks.PageChooserBlock(target_model=['news.NewsIndexPage'])), ('items', wagtail.wagtailcore.blocks.IntegerBlock()))))), blank=True),
+        ),
+        migrations.AlterField(
+            model_name='webpage',
+            name='body_sv',
+            field=wagtail.wagtailcore.fields.StreamField((('heading', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock(required=True)), ('subtitle', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('paragraph', wagtail.wagtailcore.blocks.RichTextBlock()), ('image_description', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock()), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('image_alignment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('left', 'Left'), ('right', 'Right')])), ('icons', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailcore.blocks.StructBlock((('icon', wagtail.wagtailcore.blocks.CharBlock(help_text='Material icon font icon text, as found on: https://material.io/icons')), ('title', wagtail.wagtailcore.blocks.CharBlock()), ('description', wagtail.wagtailcore.blocks.CharBlock()))))), ('hide_on_med', wagtail.wagtailcore.blocks.BooleanBlock(required=False))))), ('image_icons', wagtail.wagtailcore.blocks.StructBlock((('description', wagtail.wagtailcore.blocks.RichTextBlock()), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('image_alignment', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('left', 'Left'), ('right', 'Right')])), ('hide_on_med', wagtail.wagtailcore.blocks.BooleanBlock(required=False))))), ('overlay', wagtail.wagtailcore.blocks.StructBlock((('image', wagtail.wagtailimages.blocks.ImageChooserBlock()), ('title', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('description', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('link', wagtail.wagtailcore.blocks.URLBlock(required=False)), ('button', wagtail.wagtailcore.blocks.CharBlock(required=False))))), ('logos', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailimages.blocks.ImageChooserBlock(), icon='fa-pied-piper', label='Logos', template='blocks/logos.html')), ('counters', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock()), ('counters', wagtail.wagtailcore.blocks.ListBlock(wagtail.wagtailcore.blocks.StructBlock((('icon', wagtail.wagtailcore.blocks.CharBlock(help_text='Material icon font icon text, as found on: https://material.io/icons')), ('value', wagtail.wagtailcore.blocks.CharBlock()), ('description', wagtail.wagtailcore.blocks.CharBlock(required=False)))))), ('style', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('light', 'Light'), ('dark', 'Dark')]))))), ('image', wagtail.wagtailimages.blocks.ImageChooserBlock(template='blocks/image.html')), ('google_form', wagtail.wagtailcore.blocks.StructBlock((('form_id', wagtail.wagtailcore.blocks.CharBlock()), ('height', wagtail.wagtailcore.blocks.IntegerBlock())))), ('google_drive', wagtail.wagtailcore.blocks.StructBlock((('folder_id', wagtail.wagtailcore.blocks.CharBlock()), ('view', wagtail.wagtailcore.blocks.ChoiceBlock(choices=[('list', 'List'), ('grid', 'Grid')])), ('height', wagtail.wagtailcore.blocks.IntegerBlock())))), ('news', wagtail.wagtailcore.blocks.StructBlock((('title', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('subtitle', wagtail.wagtailcore.blocks.CharBlock(required=False)), ('index', wagtail.wagtailcore.blocks.PageChooserBlock(target_model=['news.NewsIndexPage'])), ('items', wagtail.wagtailcore.blocks.IntegerBlock()))))), blank=True),
+        ),
+    ]
diff --git a/website/home/models.py b/website/home/models.py
--- a/website/home/models.py
+++ b/website/home/models.py
@@ -11,6 +11,7 @@
 
 from blocks.models import WAGTAIL_STATIC_BLOCKTYPES
 from google.models import GoogleFormBlock, GoogleDriveBlock
+from news.models import LatestNewsBlock
 from utils.translation import TranslatedField
 
 
@@ -20,11 +21,13 @@ class HomePage(Page):
     translated_title = TranslatedField('title', 'title_sv')
 
     body_en = StreamField(
-        WAGTAIL_STATIC_BLOCKTYPES,
+        WAGTAIL_STATIC_BLOCKTYPES
+        + [('news', LatestNewsBlock())],
         blank=True,
     )
     body_sv = StreamField(
-        WAGTAIL_STATIC_BLOCKTYPES,
+        WAGTAIL_STATIC_BLOCKTYPES
+        + [('news', LatestNewsBlock())],
         blank=True,
     )
     body = TranslatedField('body_en', 'body_sv')
@@ -139,6 +142,7 @@ class WebPage(Page):
         WAGTAIL_STATIC_BLOCKTYPES + [
             ('google_form', GoogleFormBlock()),
             ('google_drive', GoogleDriveBlock()),
+            ('news', LatestNewsBlock()),
         ],
         blank=True,
     )
@@ -146,6 +150,7 @@ class WebPage(Page):
         WAGTAIL_STATIC_BLOCKTYPES + [
             ('google_form', GoogleFormBlock()),
             ('google_drive', GoogleDriveBlock()),
+            ('news', LatestNewsBlock()),
         ],
         blank=True,
     )
diff --git a/website/news/models.py b/website/news/models.py
--- a/website/news/models.py
+++ b/website/news/models.py
@@ -2,6 +2,7 @@
 from django.utils.translation import ugettext_lazy as _
 from wagtail.wagtailadmin.edit_handlers import FieldPanel, TabbedInterface, \
     ObjectList
+from wagtail.wagtailcore import blocks
 from wagtail.wagtailcore.fields import RichTextField
 from wagtail.wagtailcore.models import Page
 from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
@@ -10,6 +11,26 @@
 from utils.translation import TranslatedField
 
 
+class LatestNewsBlock(blocks.StructBlock):
+    title = blocks.CharBlock(required=False)
+    subtitle = blocks.CharBlock(required=False)
+    index = blocks.PageChooserBlock(target_model='news.NewsIndexPage')
+    items = blocks.IntegerBlock()
+
+    def get_context(self, value, parent_context=None):
+        context = super(LatestNewsBlock, self).get_context(
+            value, parent_context=parent_context
+        )
+        context['news_items'] = NewsPage.objects.child_of(value['index'])\
+            .live().order_by('-created')[:value['items']]
+        return context
+
+    class Meta:
+        label = _('Latest News')
+        icon = 'fa-newspaper-o'
+        template = 'news/latest_news.html'
+
+
 class NewsIndexPage(Page):
     title_sv = models.CharField(max_length=255)
     translated_title = TranslatedField('title', 'title_sv')
@@ -24,9 +45,8 @@ class NewsIndexPage(Page):
 
     def get_context(self, request, **kwargs):
         context = super(NewsIndexPage, self).get_context(request, **kwargs)
-
-        # Add extra variables and return the updated context
-        context['news_items'] = NewsPage.objects.child_of(self).live()
+        context['news_items'] = NewsPage.objects.child_of(self).live() \
+            .order_by('-created')
         return context
 
 


Test patch: