linuxsoftware / ls.joyous

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

Add Context to TemplateResponse #14

Closed Jean-Zombie closed 5 years ago

Jean-Zombie commented 5 years ago

hey there,

with joy I found that serve functions now are returning a TemplateResponse. Nevertheless I struggle to update the context (and am afraid it is because of my inexperience). I tried to add to my model:

def serveMonth(self, request, year=None, month=None):
        cxt = self._getContext()
        cxt.update({'test': "test"})
        return TemplateResponse(request, "joyous/calendar_week.html", cxt)

But that throws me an error: 'EventCalendarPage' object has no attribute '_getContext'

I can’t even find _getContext() anywhere in the code. Am I that far off with this approach?

Just returning an empty dict cxt = {} works. But yeah …

(Btw. is it ok to ask stuff like that here? Or should I use Stack Overflow? Maybe for visibility resons?)

Cheers Ronald

linuxsoftware commented 5 years ago

Hi,

I've only just pushed the change that added _getContext to the CalendarPage model. But sorry I think I have confused you with the name. It does not give all the context you need. It only returns the common fields: self, page, version, themeCSS and today, that all the serve functions use. I should probably rename it _getCommonContext or something like that.

Possibly _getExtraContext would work for you? e.g.

    def _getExtraContext(self, route):
        if route == "month":     # if you only want this for the month view
            return {'test': "test"}
        else:
            return {}

Or, modifying the TemplateResponse is possible. [NB: If you want this code to be called via an URL like /calendar/2019/05/ then your EventCalendarPage class must be concrete not a proxy and the derived serveMonth method must be registered as a RoutablePageMixin. I guess you have already worked this out, but I hadn't tried it before.]

@route(r"^month/$")
@route(r"^{YYYY}/{MM}/$".format(**DatePictures))
def serveMonth(self, request, year=None, month=None):
        response = super().serveMonth(request, year, month)
        response.context_data.update({'test': "test"})
        return response

Yes, this is a good way of raising questions. It's good to get the feedback, thanks. And I'd rather keep on using GitHub issues for this. I will see questions here quicker than on Stack Overflow. Are you able to share the full code for your Wagtail project? No worries if you can't, but if you can it might make it clearer to me what you are trying to do.

Best wishes, David.

Jean-Zombie commented 5 years ago

Hey David,

thanks again for your suggestions. I needed the weekend to read up on the concept of the super()-class; but the solution with RouteablePageMixin totally does it.

What I am actually trying to do (now) is to make a route for categories; i.e. query all events of category "Party" or "Concert". I am pretty confident your code snippet will lead me there. I can share code, so I might be back if I get stuck ;-)

Cheers Ronald