Skip to content

Commit

Permalink
Improve EventIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
Merlin Buczek committed Jun 13, 2019
1 parent 7a114c8 commit ebdbcf8
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 93 deletions.
18 changes: 3 additions & 15 deletions mpicms/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from wagtail.snippets.models import register_snippet

from mpicms.news.mixins import NewsMixin
from mpicms.events.models import Event, EventIndex
# from mpicms.events.models import Event, EventIndex
from mpicms.events.mixins import EventMixin


Page.show_in_menus_default = True
Expand Down Expand Up @@ -74,7 +75,7 @@ class Meta: # noqa
abstract = True


class HomePage(NewsMixin, Page):
class HomePage(EventMixin, NewsMixin, Page):
banner = models.ForeignKey(
'Banner',
null=True,
Expand All @@ -94,19 +95,6 @@ class HomePage(NewsMixin, Page):
def categories(self):
return self.get_children().type(CategoryPage).live()

def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)

# Events
events = []
for event in Event.objects.live():
events.append(event.get_dict(request))

context["events"] = json.dumps(events)
context['event_index'] = EventIndex.objects.get()

return context

class Meta: # noqa
verbose_name = _("homepage")
verbose_name_plural = _("homepages")
Expand Down
13 changes: 13 additions & 0 deletions mpicms/events/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db import models

from .models import EventIndex


class EventMixin(models.Model):

@property
def event_index(self):
return self.get_children().type(EventIndex).first()

class Meta: # noqa
abstract = True
21 changes: 10 additions & 11 deletions mpicms/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def end(self):
return datetime.combine(self.end_date, self.end_time)
return self.end_date

def get_dict(self, request):
def get_dict(self, request=None):
return {
'title': self.title,
'start': self.start.isoformat(),
Expand Down Expand Up @@ -86,16 +86,15 @@ class EventIndex(Page):

content_panels = Page.content_panels

def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)

events = []
for child in self.get_children().type(Event).live().specific():
events.append(child.get_dict(request))

context["events"] = json.dumps(events)

return context
@property
def events(self):
if self.depth <= 3:
return Event.objects.live().specific()
return self.get_children().stype(Event).live().specific()

def get_json_events(self, request=None):
event_dicts = [event.get_dict(request) for event in self.events]
return json.dumps(event_dicts)

def clean(self): # Prevent more than one event index
model = self.__class__
Expand Down
9 changes: 9 additions & 0 deletions mpicms/events/templatetags/event_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import template


register = template.Library()


@register.simple_tag(takes_context=True)
def get_json_events(context, event_index):
return event_index.get_json_events(request=context['request'])
64 changes: 0 additions & 64 deletions mpicms/events/templatetags/wagtail_events_tags.py

This file was deleted.

7 changes: 4 additions & 3 deletions mpicms/templates/base/home_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@
<div class="column">
{% include 'news/components/news_preview.html' with news=page.news %}
</div>
{% if page.event_index %}
<div class="column calendar-list">
<h3 class="title is-3">{% trans 'Upcoming events' %}</h3>
{% include 'events/components/event_list.html' %}
{% include 'events/components/event_list.html' with event_index=page.event_index %}
<div class="section-button" >
<a href="{% pageurl event_index %}"><button class="button">{% trans 'All events' %}&nbsp&nbsp<i class="fas fa-chevron-right"></i></button></a>
<a href="{% pageurl page.event_index %}"><button class="button">{% trans 'All events' %}&nbsp&nbsp<i class="fas fa-chevron-right"></i></button></a>
</div>
</div>
{% endif %}
</div>


<h3 class="title is-3">{% trans 'Content' %}</h3>
{% include 'base/components/content_preview.html' with pages=page.categories %}

<!-- </section> -->
{% endblock %}
4 changes: 4 additions & 0 deletions mpicms/templates/events/components/event_list.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{% load event_tags %}

<div id="event_list"></div>


{% get_json_events event_index.specific as events %}
<script>

document.addEventListener('DOMContentLoaded', function() {
Expand Down
2 changes: 2 additions & 0 deletions mpicms/templates/events/event_index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends 'base/page.html' %}
{% load event_tags %}

{% block content %}
<div class="section">
Expand All @@ -9,6 +10,7 @@ <h2 class="title is-2">{{ page.title }}</h2>

</div>

{% get_json_events page as events %}
<script>

document.addEventListener('DOMContentLoaded', function() {
Expand Down

0 comments on commit ebdbcf8

Please sign in to comment.