fix errors with exposures and calendar

This commit is contained in:
bkfox 2016-07-18 04:07:43 +02:00
parent 4e5d90fb1d
commit d3e05211c3
5 changed files with 53 additions and 33 deletions

View File

@ -11,6 +11,8 @@ class Exposure:
""" """
Define an exposure. Look at @expose decorator. Define an exposure. Look at @expose decorator.
""" """
__uuid = 0
name = None name = None
"""generated view name""" """generated view name"""
pattern = None pattern = None
@ -30,7 +32,12 @@ class Exposure:
self.__dict__.update(kwargs) self.__dict__.update(kwargs)
@staticmethod @staticmethod
def gather(cl): def new_id():
Exposure.__uuid += 1
return Exposure.__uuid
@staticmethod
def gather(cl, website):
""" """
Prepare all exposure declared in self.cl, create urls and return Prepare all exposure declared in self.cl, create urls and return
them. This is done at this place in order to allow sub-classing them. This is done at this place in order to allow sub-classing
@ -46,7 +53,8 @@ class Exposure:
raise Http404() raise Http404()
exp = fn._exposure exp = fn._exposure
res = fn(request, *args, **kwargs) # kwargs['request'] = request
res = fn(cl, *args, **kwargs)
if res and exp.template_name: if res and exp.template_name:
ctx = res or {} ctx = res or {}
ctx.update({ ctx.update({
@ -57,13 +65,12 @@ class Exposure:
ctx, request = request) ctx, request = request)
return HttpResponse(res or '') return HttpResponse(res or '')
# id = str(uuid.uuid1()) uuid = Exposure.new_id()
exp = cl._exposure exp = cl._exposure
exp.pattern = '{name}/{id}'.format(name = exp.name, id = id(cl)) exp.pattern = '{name}/{id}'.format(name = exp.name, id = uuid)
exp.name = 'exps.{name}.{id}'.format(name = exp.name, id = id(cl)) exp.name = 'exps.{name}.{id}'.format(name = exp.name, id = uuid)
urls = [] urls = []
for name, fn in inspect.getmembers(cl): for name, fn in inspect.getmembers(cl):
if name.startswith('__') or not hasattr(fn, '_exposure'): if name.startswith('__') or not hasattr(fn, '_exposure'):
continue continue
@ -78,7 +85,7 @@ class Exposure:
urls.append(url( urls.append(url(
pattern, name = exp.name, view = view, pattern, name = exp.name, view = view,
kwargs = { 'fn': fn } kwargs = { 'fn': fn, 'website': website }
)) ))
urls.append(url( urls.append(url(

View File

@ -181,22 +181,23 @@ class Section(Viewable, View):
'embed': True, 'embed': True,
} }
def prepare(self, view, **kwargs): def prepare(self, view = None, **kwargs):
""" """
initialize the object with valuable informations. initialize the object with valuable informations.
""" """
self.view = view self.kwargs = kwargs
self.request = view.request if view:
self.kwargs = view.kwargs self.view = view
if hasattr(view, 'object'): self.request = view.request
self.object = view.object if hasattr(view, 'object'):
self.object = view.object
def render(self, *args, **kwargs): def render(self, *args, **kwargs):
""" """
Render the section as a string. Use *args and **kwargs to prepare Render the section as a string. Use *args and **kwargs to prepare
the section, then get_context_data and render. the section, then get_context_data and render.
""" """
if args and not self.view: if args or kwargs:
self.prepare(*args, **kwargs) self.prepare(*args, **kwargs)
context = self.get_context_data() context = self.get_context_data()
@ -594,10 +595,15 @@ class Calendar(Section):
model = None model = None
template_name = "aircox/cms/calendar.html" template_name = "aircox/cms/calendar.html"
def get_context_data(self, year = None, month = None, *args, **kwargs): def get_context_data(self):
import calendar import calendar
import aircox.cms.routes as routes import aircox.cms.routes as routes
context = super().get_context_data(*args, **kwargs) context = super().get_context_data()
if self.kwargs:
year, month = self.kwargs.get('year'), self.kwargs.get('month')
else:
year, month = None, None
date = datetime.date.today() date = datetime.date.today()
if year: if year:
@ -610,8 +616,9 @@ class Calendar(Section):
def make_date(date, day): def make_date(date, day):
date += tz.timedelta(days=day) date += tz.timedelta(days=day)
return ( return (
date, self.model.reverse( date, self.website.reverse(
routes.DateRoute, year = date.year, model = None,
route = routes.DateRoute, year = date.year,
month = date.month, day = date.day month = date.month, day = date.day
) )
) )
@ -630,7 +637,8 @@ class Calendar(Section):
def render_exp(cl, *args, year, month, **kwargs): def render_exp(cl, *args, year, month, **kwargs):
year = int(year) year = int(year)
month = int(month) month = int(month)
return cl.render(*args, year = year, month = month, **kwargs) calendar = cl(website = cl.website)
return calendar.render(year = year, month = month, **kwargs)
render_exp._exposure.name = 'render' render_exp._exposure.name = 'render'
render_exp._exposure.pattern = '(?P<year>[0-9]{4})/(?P<month>[0-1]?[0-9])' render_exp._exposure.pattern = '(?P<year>[0-9]{4})/(?P<month>[0-1]?[0-9])'

View File

@ -64,7 +64,7 @@ class BaseView:
if self.sections: if self.sections:
if issubclass(type(self.sections), sections.Section): if issubclass(type(self.sections), sections.Section):
self.template_name = self.sections.template_name self.template_name = self.sections.template_name
self.sections.prepare(self) self.sections.prepare(self, **self.kwargs)
context.update(self.sections.get_context_data()) context.update(self.sections.get_context_data())
else: else:
if not self.template_name: if not self.template_name:
@ -142,9 +142,9 @@ class PostListView(BaseView, ListView):
def get_queryset(self): def get_queryset(self):
default = self.prepare_list() default = self.prepare_list()
if default: if not default:
qs = self.list.get_object_list() qs = self.list.get_object_list()
if qs: if qs is not None:
return qs return qs
if self.route: if self.route:
@ -181,7 +181,7 @@ class PostListView(BaseView, ListView):
self.css_class = self.list.css_class self.css_class = self.list.css_class
default = False default = False
self.list.prepare(self) self.list.prepare(self, **self.kwargs)
if self.request.GET.get('fields'): if self.request.GET.get('fields'):
self.list.fields = [ self.list.fields = [

View File

@ -96,8 +96,8 @@ class Website:
for section in sections: for section in sections:
if not hasattr(section, '_exposure'): if not hasattr(section, '_exposure'):
continue continue
self.exposures += section._exposure.gather(section) self.exposures += section._exposure.gather(section, website = self)
section.website = self
def __route_to_url(self, name, route, view, sections, kwargs): def __route_to_url(self, name, route, view, sections, kwargs):
# route can be a tuple # route can be a tuple

View File

@ -282,6 +282,10 @@ class ListByDate(sections.List):
if true, print days in header by week if true, print days in header by week
""" """
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.add_css_class('list_by_date')
def nav_dates(self, date): def nav_dates(self, date):
""" """
Return a list of dates of the week of the given date. Return a list of dates of the week of the given date.
@ -317,10 +321,12 @@ class ListByDate(sections.List):
prev_week = self.get_date_url(prev_week) prev_week = self.get_date_url(prev_week)
context.update({ context.update({
'date': date, 'nav': {
'dates': dates, 'date': date,
'next_week': next_week, 'dates': dates,
'prev_week': prev_week, 'next': next_week,
'prev': prev_week,
}
}) })
return context return context
@ -368,7 +374,6 @@ class Logs(ListByDate):
""" """
Return a list of played stream sounds and diffusions. Return a list of played stream sounds and diffusions.
""" """
@staticmethod @staticmethod
def make_item(log): def make_item(log):
""" """
@ -396,7 +401,8 @@ class Logs(ListByDate):
return post return post
def get_object_list(self): def get_object_list(self):
station = self.view._website.station return []
station = self.view.website.station
qs = station.get_played( qs = station.get_played(
models = [ programs.Diffusion, programs.Track ], models = [ programs.Diffusion, programs.Track ],
).filter( ).filter(
@ -404,10 +410,9 @@ class Logs(ListByDate):
date__day = int(day) date__day = int(day)
) )
# TODO for each, exclude if there is a diffusion (that has not been logged) # TODO for each, exclude if there is a diffusion (that has not been logged)
return [ cl.make_item(log) for log in qs ] return [ cl.make_item(log) for log in qs ]
@staticmethod @staticmethod
def get_date_url(date): def get_date_url(date):
pass return 'TODO'