fix errors with exposures and calendar
This commit is contained in:
		@ -11,6 +11,8 @@ class Exposure:
 | 
			
		||||
    """
 | 
			
		||||
    Define an exposure. Look at @expose decorator.
 | 
			
		||||
    """
 | 
			
		||||
    __uuid = 0
 | 
			
		||||
 | 
			
		||||
    name = None
 | 
			
		||||
    """generated view name"""
 | 
			
		||||
    pattern = None
 | 
			
		||||
@ -30,7 +32,12 @@ class Exposure:
 | 
			
		||||
        self.__dict__.update(kwargs)
 | 
			
		||||
 | 
			
		||||
    @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
 | 
			
		||||
        them. This is done at this place in order to allow sub-classing
 | 
			
		||||
@ -46,7 +53,8 @@ class Exposure:
 | 
			
		||||
                    raise Http404()
 | 
			
		||||
 | 
			
		||||
            exp = fn._exposure
 | 
			
		||||
            res = fn(request, *args, **kwargs)
 | 
			
		||||
            # kwargs['request'] = request
 | 
			
		||||
            res = fn(cl, *args, **kwargs)
 | 
			
		||||
            if res and exp.template_name:
 | 
			
		||||
                ctx = res or {}
 | 
			
		||||
                ctx.update({
 | 
			
		||||
@ -57,13 +65,12 @@ class Exposure:
 | 
			
		||||
                                       ctx, request = request)
 | 
			
		||||
            return HttpResponse(res or '')
 | 
			
		||||
 | 
			
		||||
        # id = str(uuid.uuid1())
 | 
			
		||||
        uuid = Exposure.new_id()
 | 
			
		||||
        exp = cl._exposure
 | 
			
		||||
        exp.pattern = '{name}/{id}'.format(name = exp.name, id = id(cl))
 | 
			
		||||
        exp.name = 'exps.{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 = uuid)
 | 
			
		||||
 | 
			
		||||
        urls = []
 | 
			
		||||
 | 
			
		||||
        for name, fn in inspect.getmembers(cl):
 | 
			
		||||
            if name.startswith('__') or not hasattr(fn, '_exposure'):
 | 
			
		||||
                continue
 | 
			
		||||
@ -78,7 +85,7 @@ class Exposure:
 | 
			
		||||
 | 
			
		||||
            urls.append(url(
 | 
			
		||||
                pattern, name = exp.name, view = view,
 | 
			
		||||
                kwargs = { 'fn': fn }
 | 
			
		||||
                kwargs = { 'fn': fn, 'website': website }
 | 
			
		||||
            ))
 | 
			
		||||
 | 
			
		||||
        urls.append(url(
 | 
			
		||||
 | 
			
		||||
@ -181,22 +181,23 @@ class Section(Viewable, View):
 | 
			
		||||
            'embed': True,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def prepare(self, view, **kwargs):
 | 
			
		||||
    def prepare(self, view = None, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        initialize the object with valuable informations.
 | 
			
		||||
        """
 | 
			
		||||
        self.view = view
 | 
			
		||||
        self.request = view.request
 | 
			
		||||
        self.kwargs = view.kwargs
 | 
			
		||||
        if hasattr(view, 'object'):
 | 
			
		||||
            self.object = view.object
 | 
			
		||||
        self.kwargs = kwargs
 | 
			
		||||
        if view:
 | 
			
		||||
            self.view = view
 | 
			
		||||
            self.request = view.request
 | 
			
		||||
            if hasattr(view, 'object'):
 | 
			
		||||
                self.object = view.object
 | 
			
		||||
 | 
			
		||||
    def render(self, *args, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Render the section as a string. Use *args and **kwargs to prepare
 | 
			
		||||
        the section, then get_context_data and render.
 | 
			
		||||
        """
 | 
			
		||||
        if args and not self.view:
 | 
			
		||||
        if args or kwargs:
 | 
			
		||||
            self.prepare(*args, **kwargs)
 | 
			
		||||
        context = self.get_context_data()
 | 
			
		||||
 | 
			
		||||
@ -594,10 +595,15 @@ class Calendar(Section):
 | 
			
		||||
    model = None
 | 
			
		||||
    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 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()
 | 
			
		||||
        if year:
 | 
			
		||||
@ -610,8 +616,9 @@ class Calendar(Section):
 | 
			
		||||
        def make_date(date, day):
 | 
			
		||||
            date += tz.timedelta(days=day)
 | 
			
		||||
            return (
 | 
			
		||||
                date, self.model.reverse(
 | 
			
		||||
                    routes.DateRoute, year = date.year,
 | 
			
		||||
                date, self.website.reverse(
 | 
			
		||||
                    model = None,
 | 
			
		||||
                    route = routes.DateRoute, year = date.year,
 | 
			
		||||
                    month = date.month, day = date.day
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
@ -630,7 +637,8 @@ class Calendar(Section):
 | 
			
		||||
    def render_exp(cl, *args, year, month, **kwargs):
 | 
			
		||||
        year = int(year)
 | 
			
		||||
        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.pattern = '(?P<year>[0-9]{4})/(?P<month>[0-1]?[0-9])'
 | 
			
		||||
 | 
			
		||||
@ -64,7 +64,7 @@ class BaseView:
 | 
			
		||||
        if self.sections:
 | 
			
		||||
            if issubclass(type(self.sections), sections.Section):
 | 
			
		||||
                self.template_name = self.sections.template_name
 | 
			
		||||
                self.sections.prepare(self)
 | 
			
		||||
                self.sections.prepare(self, **self.kwargs)
 | 
			
		||||
                context.update(self.sections.get_context_data())
 | 
			
		||||
            else:
 | 
			
		||||
                if not self.template_name:
 | 
			
		||||
@ -142,9 +142,9 @@ class PostListView(BaseView, ListView):
 | 
			
		||||
 | 
			
		||||
    def get_queryset(self):
 | 
			
		||||
        default = self.prepare_list()
 | 
			
		||||
        if default:
 | 
			
		||||
        if not default:
 | 
			
		||||
            qs = self.list.get_object_list()
 | 
			
		||||
            if qs:
 | 
			
		||||
            if qs is not None:
 | 
			
		||||
                return qs
 | 
			
		||||
 | 
			
		||||
        if self.route:
 | 
			
		||||
@ -181,7 +181,7 @@ class PostListView(BaseView, ListView):
 | 
			
		||||
            self.css_class = self.list.css_class
 | 
			
		||||
            default = False
 | 
			
		||||
 | 
			
		||||
        self.list.prepare(self)
 | 
			
		||||
        self.list.prepare(self, **self.kwargs)
 | 
			
		||||
 | 
			
		||||
        if self.request.GET.get('fields'):
 | 
			
		||||
            self.list.fields = [
 | 
			
		||||
 | 
			
		||||
@ -96,8 +96,8 @@ class Website:
 | 
			
		||||
        for section in sections:
 | 
			
		||||
            if not hasattr(section, '_exposure'):
 | 
			
		||||
                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):
 | 
			
		||||
        # route can be a tuple
 | 
			
		||||
 | 
			
		||||
@ -282,6 +282,10 @@ class ListByDate(sections.List):
 | 
			
		||||
    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):
 | 
			
		||||
        """
 | 
			
		||||
        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)
 | 
			
		||||
 | 
			
		||||
        context.update({
 | 
			
		||||
            'date': date,
 | 
			
		||||
            'dates': dates,
 | 
			
		||||
            'next_week': next_week,
 | 
			
		||||
            'prev_week': prev_week,
 | 
			
		||||
            'nav': {
 | 
			
		||||
                'date': date,
 | 
			
		||||
                'dates': dates,
 | 
			
		||||
                'next': next_week,
 | 
			
		||||
                'prev': prev_week,
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
@ -368,7 +374,6 @@ class Logs(ListByDate):
 | 
			
		||||
    """
 | 
			
		||||
    Return a list of played stream sounds and diffusions.
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def make_item(log):
 | 
			
		||||
        """
 | 
			
		||||
@ -396,7 +401,8 @@ class Logs(ListByDate):
 | 
			
		||||
        return post
 | 
			
		||||
 | 
			
		||||
    def get_object_list(self):
 | 
			
		||||
        station = self.view._website.station
 | 
			
		||||
        return []
 | 
			
		||||
        station = self.view.website.station
 | 
			
		||||
        qs = station.get_played(
 | 
			
		||||
            models = [ programs.Diffusion, programs.Track ],
 | 
			
		||||
        ).filter(
 | 
			
		||||
@ -404,10 +410,9 @@ class Logs(ListByDate):
 | 
			
		||||
            date__day = int(day)
 | 
			
		||||
        )
 | 
			
		||||
        # TODO for each, exclude if there is a diffusion (that has not been logged)
 | 
			
		||||
 | 
			
		||||
        return [ cl.make_item(log) for log in qs ]
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def get_date_url(date):
 | 
			
		||||
        pass
 | 
			
		||||
        return 'TODO'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user