linuxsoftware / ls.joyous

A calendar application for Wagtail
BSD 3-Clause "New" or "Revised" License
74 stars 35 forks source link

Customizing calendar templates #8

Closed Jean-Zombie closed 5 years ago

Jean-Zombie commented 5 years ago

Hey there,

first a big thank your for work in this code. I am a Python newbie and it is the first time I explore Django/Wagtail (and related apps like this) beyond the bare basics. Reading through your documentation and the source code I manged to implement two (seperated) calendars and even make my event models inherit from the joyous ones. But now I am stuck. My question:

Is there a way to alter the HTML used for the rendering of the calendar views such as the list of the upcoming events (calendar_list_upcoming.html)? I know the files live in the modules template folder, but it seems a bit odd to me to change them there.

I managed to set custom templates for the inherited event models by setting the template attribute but can't make it happen for the calendars since they take multiple HTML-files.

cheers.

Jean-Zombie commented 5 years ago

Just answered my own question. Copying Joyous' template files into my Wagtail site's template folder made them editable. Am I breaking things with this approach?

linuxsoftware commented 5 years ago

Hi @Jean-Zombie

Thanks for your feedback. Good to hear Joyous is useful to you.

Yes, you've got it. Copying the templates and changing them in your project is the way to alter the HTML. You only need to copy the templates that require changes. Django will look for a template first in your project and if it doesn't find it there, then look in the ls.joyous app directory.

See https://docs.djangoproject.com/en/2.1/howto/overriding-templates/

ls.joyous hasn't reached 1.0 yet, so the template structure might still change, but I think it is pretty stable, so that is not likely.

Django template inheritance is worth knowing about too. Rather than changing the whole template. It is possible in Django to create a template that extends another. And then only change specific blocks as needed within the template. However looking at calendar_list_upcoming.html I see I've only defined blocks around the calendar options, nothing for the events list. Is that what you wish to change? I will aim to adding some more block definitions there and throughout my templates to make them easier to inherit from.

See https://docs.djangoproject.com/en/2.1/ref/templates/language/#template-inheritance

Hope this helps.

Best wishes for your project.

David.

linuxsoftware commented 5 years ago

Please note also that as I said in Issue #5 that I am planning on changing the CSS and the HTML classes in version 0.9. You might have to change your customisations if you want to upgrade when that happens. I hope that doesn't inconvenience you too much.

Jean-Zombie commented 5 years ago

Hey David

thank you very much for taking the time to answer and explain. I played around with template inheritance before but looking at your code helps a lot.

I am all good with changes to the CSS. I'll adapt ;-)

Regarding the list view of a calendar-page and calendar_list_upcoming.html: I switched to the monthly view now, since I need every occurrence of an event – not only the event itself. But it can't hurt to have more block definitions.

I came across another issue/problem though. How can I pass extra context to a template? I tried to overwrite the get_context method of my calendar page models as described in Wagtail's documentation. But I can't get extra context in.

I tried to write a custom template tag by copying the template tags folder from the Joyous module into my project. But unlike the templates themselves overwriting does not seem to work.

If you find the time to nudge me in the right direction I`d be very thankful. Ronald

linuxsoftware commented 5 years ago

Hi Ronald,

Adding extra context in to the calendar page templates is, right now, pretty difficult. It just wasn't coded with that in mind. I've been thinking for a while about changing from using render to returning a TemplateResponse (you might have seen the comment). Your situation makes it clear why I should do that. Then you'd be able to grab the context and change it.

If you can wait a few days I can make the change. But right now, I think your only option is a cut-n-paste of the Calendar.serveMonth code in to your own class and add your extra variables there.

For the template tags (that is joyous_tags right?) - most of them use there own little templates which are in templates/joyous/tags. Maybe a change there would work for you? Or maybe define your own template tags and use those?

Best wishes, David.

Jean-Zombie commented 5 years ago

Hey David,

much appreciated. But don't stress it. I realized that the context was already there. I just needed to access page.get_children for what I had in mind (get all events for a specific calendar unlike all_upcoming_events). A bit embarrassing to bother you with it.

Nevertheless, I bet at one point down the road having a TemplateResponse for extra context will be very useful. As I see myself using Joyous way more often. And I strongly believe more people will do so ;-). I looked around and for Wagtail it is by far the best calendar solution. F.e. I tried to implement django-recurrence myself but failed miserably.

Thx again Ronald

linuxsoftware commented 5 years ago

I'm glad you found a solution. Thanks for your feedback, it is very helpful for me.

Jean-Zombie commented 5 years ago

I am facing another issue. And I believe it might be useful to others.

Basically I am trying to combine serveUpcomingand serveMonth. I need all occurrences of an event but only those in the future. With serveMonth I get all occurrences but also ones in the past (of that month). With serveUpcoming I get only upcoming events, but not all occurrences.

I am looking for something like

{% for title, event in evod.days_events %}
    {% if event.is_upcoming %}   # this would be the missing link
        {% include "events/event.html" %}
    {% endif %}
{% endwith %}

Do I make sense?

linuxsoftware commented 5 years ago

Two possible ideas

In your serve function use getAllEventsByDay(request, today, endOfMonth, home=self) to get all the events from today until the end of the month that are children of this calendar. (You did only want ones that were children right?).

or,

Use if event._upcoming_datetime_from as the "missing link" in your example code to check if an event is upcoming or not.

linuxsoftware commented 5 years ago

You could also check if evod.date >= today

Jean-Zombie commented 5 years ago

Damn. I did not think about simply reducing the evod's of a month. I was fixated on the event. Check {% if evod.date >= today %} totally did it.