fix bug for schedules set to "one week or two"; sections.List: avoid to call self.get_object_list if possible + add prepare_object_list
This commit is contained in:
		@ -238,7 +238,6 @@ class List(Section):
 | 
			
		||||
    * truncate: number of words to keep in content (0 = full content)
 | 
			
		||||
    """
 | 
			
		||||
    template_name = 'aircox/cms/list.html'
 | 
			
		||||
    base_template = 'aircox/cms/section.html'
 | 
			
		||||
 | 
			
		||||
    object_list = None
 | 
			
		||||
    url = None
 | 
			
		||||
@ -265,19 +264,45 @@ class List(Section):
 | 
			
		||||
    def get_object_list(self):
 | 
			
		||||
        return self.object_list
 | 
			
		||||
 | 
			
		||||
    def get_context_data(self, request, object=None, *args, **kwargs):
 | 
			
		||||
    def prepare_object_list(self, object_list):
 | 
			
		||||
        """
 | 
			
		||||
        Prepare objects before context is sent to the template renderer.
 | 
			
		||||
        Return the object_list that is prepared.
 | 
			
		||||
 | 
			
		||||
        Remember: since we are in a rendering process, the items should
 | 
			
		||||
        not be saved.
 | 
			
		||||
        """
 | 
			
		||||
        return object_list
 | 
			
		||||
 | 
			
		||||
    def get_context_data(self, request, object=None, object_list=None,
 | 
			
		||||
                            *args, **kwargs):
 | 
			
		||||
        """
 | 
			
		||||
        Return a context that is passed to the template at rendering, with
 | 
			
		||||
        the following values:
 | 
			
		||||
        - `list`: a reference to self, that contain values used for rendering
 | 
			
		||||
        - `object_list`: a list of object that can be rendered, either
 | 
			
		||||
            instances of Post or ListItem.
 | 
			
		||||
 | 
			
		||||
        If object_list is not given, call `get_object_list` to retrieve it.
 | 
			
		||||
        Prepare the object_list using `self.prepare_object_list`.
 | 
			
		||||
 | 
			
		||||
        Set `request`, `object`, `object_list` and `kwargs` in self.
 | 
			
		||||
        """
 | 
			
		||||
        if request: self.request = request
 | 
			
		||||
        if object: self.object = object
 | 
			
		||||
        if kwargs: self.kwargs = kwargs
 | 
			
		||||
 | 
			
		||||
        object_list = self.object_list or self.get_object_list()
 | 
			
		||||
        if not object_list and not self.message_empty:
 | 
			
		||||
            return
 | 
			
		||||
        if object_list is None:
 | 
			
		||||
            object_list = self.object_list or self.get_object_list()
 | 
			
		||||
            if not object_list and not self.message_empty:
 | 
			
		||||
                return
 | 
			
		||||
        self.object_list = object_list
 | 
			
		||||
 | 
			
		||||
        if object_list:
 | 
			
		||||
            object_list = self.prepare_object_list(object_list)
 | 
			
		||||
 | 
			
		||||
        context = super().get_context_data(request, object, *args, **kwargs)
 | 
			
		||||
        context.update({
 | 
			
		||||
            'base_template': self.base_template,
 | 
			
		||||
            'list': self,
 | 
			
		||||
            'object_list': object_list[:self.paginate_by]
 | 
			
		||||
                           if object_list and self.paginate_by else
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								cms/views.py
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								cms/views.py
									
									
									
									
									
								
							@ -42,7 +42,7 @@ class BaseView:
 | 
			
		||||
            self.sections = sections
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def __is_single(self):
 | 
			
		||||
    def __section_is_single(self):
 | 
			
		||||
        return not issubclass(type(self.sections), list)
 | 
			
		||||
 | 
			
		||||
    def add_css_class(self, css_class):
 | 
			
		||||
@ -64,10 +64,13 @@ class BaseView:
 | 
			
		||||
 | 
			
		||||
        # update from sections
 | 
			
		||||
        if self.sections:
 | 
			
		||||
            if self.__is_single():
 | 
			
		||||
            if self.__section_is_single():
 | 
			
		||||
                self.template_name = self.sections.template_name
 | 
			
		||||
                context.update(self.sections.get_context_data(
 | 
			
		||||
                    self.request, **self.kwargs
 | 
			
		||||
                    self.request,
 | 
			
		||||
                    object_list = hasattr(self, 'object_list') and \
 | 
			
		||||
                                    self.object_list,
 | 
			
		||||
                    **self.kwargs
 | 
			
		||||
                ) or {})
 | 
			
		||||
            else:
 | 
			
		||||
                if not self.template_name:
 | 
			
		||||
@ -143,6 +146,7 @@ class PostListView(BaseView, ListView):
 | 
			
		||||
        return super().dispatch(request, *args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def get_queryset(self):
 | 
			
		||||
        print('get_query_set')
 | 
			
		||||
        if self.route:
 | 
			
		||||
            qs = self.route.get_queryset(self.model, self.request,
 | 
			
		||||
                                         **self.kwargs)
 | 
			
		||||
@ -183,13 +187,13 @@ class PostListView(BaseView, ListView):
 | 
			
		||||
        self.add_css_class('list')
 | 
			
		||||
 | 
			
		||||
        context = super().get_context_data(**kwargs)
 | 
			
		||||
        # context.update(BaseView.get_context_data(self, **kwargs))
 | 
			
		||||
 | 
			
		||||
        if not context.get('title') and self.route:
 | 
			
		||||
            context['title'] = self.route.get_title(
 | 
			
		||||
                self.model, self.request, **self.kwargs
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        print([post.title for post in context.get('object_list')])
 | 
			
		||||
        context['list'] = self.list
 | 
			
		||||
        return context
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								notes.md
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								notes.md
									
									
									
									
									
								
							@ -27,24 +27,26 @@
 | 
			
		||||
        - cms.script
 | 
			
		||||
    - routes
 | 
			
		||||
        - customized header depending on the list (e.g. thread -> link to thread parent)
 | 
			
		||||
        - by tags
 | 
			
		||||
        - different models
 | 
			
		||||
        - different models combinaison
 | 
			
		||||
    - admin cms
 | 
			
		||||
    - content management -> do we use a markup language?
 | 
			
		||||
    - sections:
 | 
			
		||||
        - article list with the focus
 | 
			
		||||
        - related articles
 | 
			
		||||
        - similar articles (using tags)
 | 
			
		||||
        - calendar
 | 
			
		||||
 | 
			
		||||
- website:
 | 
			
		||||
    - diffusions:
 | 
			
		||||
        - filter sounds for undiffused diffusions
 | 
			
		||||
        - print sounds of diffusions
 | 
			
		||||
        - print program's name in lists
 | 
			
		||||
    - player:
 | 
			
		||||
        - "listen" + "favorite" buttons made easy + automated
 | 
			
		||||
        - single mode / play next auto
 | 
			
		||||
        - mixcloud
 | 
			
		||||
        - seek bar
 | 
			
		||||
    - schedule as calendar?
 | 
			
		||||
    - section for schedule as calendar
 | 
			
		||||
    - load complete week for a schedule?
 | 
			
		||||
    - finish that fucking website
 | 
			
		||||
    - list of played diffusions and tracks when non-stop;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -21,7 +21,17 @@ import aircox.programs.settings as settings
 | 
			
		||||
logger = logging.getLogger('aircox.core')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def date_or_default(date, date_only = False):
 | 
			
		||||
def as_date(date, as_datetime = True):
 | 
			
		||||
    """
 | 
			
		||||
    If as_datetime, return the date with time info set to 0; else, return
 | 
			
		||||
    a date with date informations of the given date/time.
 | 
			
		||||
    """
 | 
			
		||||
    import datetime
 | 
			
		||||
    if as_datetime:
 | 
			
		||||
        return date.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
 | 
			
		||||
    return datetime.date(date.year, date.month, date.day)
 | 
			
		||||
 | 
			
		||||
def date_or_default(date, no_time = False):
 | 
			
		||||
    """
 | 
			
		||||
    Return date or default value (now) if not defined, and remove time info
 | 
			
		||||
    if date_only is True
 | 
			
		||||
@ -29,8 +39,8 @@ def date_or_default(date, date_only = False):
 | 
			
		||||
    date = date or tz.datetime.today()
 | 
			
		||||
    if not tz.is_aware(date):
 | 
			
		||||
        date = tz.make_aware(date)
 | 
			
		||||
    if date_only:
 | 
			
		||||
        return date.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
 | 
			
		||||
    if no_time:
 | 
			
		||||
        return as_date(date)
 | 
			
		||||
    return date
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -160,7 +170,6 @@ class Sound(Nameable):
 | 
			
		||||
        # path = self._meta.get_field('path').path
 | 
			
		||||
        path = self.path.replace(main_settings.MEDIA_ROOT, '', 1)
 | 
			
		||||
        #path = self.path.replace(path, '', 1)
 | 
			
		||||
        # print(path, self._meta.get_field('path').path)
 | 
			
		||||
        return path
 | 
			
		||||
 | 
			
		||||
    def file_exists(self):
 | 
			
		||||
@ -352,7 +361,8 @@ class Schedule(models.Model):
 | 
			
		||||
            # NOTE previous algorithm was based on the week number, but this
 | 
			
		||||
            # approach is wrong because number of weeks in a year can be
 | 
			
		||||
            # 52 or 53. This also clashes with the first week of the year.
 | 
			
		||||
            if not (date - self.date).days % 14:
 | 
			
		||||
            diff = as_date(date, False) - as_date(self.date, False)
 | 
			
		||||
            if not diff.days % 14:
 | 
			
		||||
                date += tz.timedelta(days = 7)
 | 
			
		||||
 | 
			
		||||
            while date.month == month:
 | 
			
		||||
 | 
			
		||||
@ -71,8 +71,8 @@ class Diffusion (RelatedPost):
 | 
			
		||||
        if self.thread:
 | 
			
		||||
            if not self.title:
 | 
			
		||||
                self.title = _('{name} // {first_diff}').format(
 | 
			
		||||
                    self.related.program.name,
 | 
			
		||||
                    self.related.start.strftime('%A %d %B')
 | 
			
		||||
                    name = self.related.program.name,
 | 
			
		||||
                    first_diff = self.related.start.strftime('%A %d %B')
 | 
			
		||||
                )
 | 
			
		||||
            if not self.content:
 | 
			
		||||
                self.content = self.thread.content
 | 
			
		||||
 | 
			
		||||
@ -99,6 +99,20 @@ class Diffusions(sections.List):
 | 
			
		||||
        #                .order_by('-start')[:self.prev_count])
 | 
			
		||||
        #return r
 | 
			
		||||
 | 
			
		||||
    def prepare_object_list(self, object_list):
 | 
			
		||||
        """
 | 
			
		||||
        This function just prepare the list of object, in order to have a good
 | 
			
		||||
        title
 | 
			
		||||
        """
 | 
			
		||||
        for post in object_list:
 | 
			
		||||
            if not hasattr(post, 'related') or \
 | 
			
		||||
                    not hasattr(post.related , 'program'):
 | 
			
		||||
                continue
 | 
			
		||||
            name = post.related.program.name
 | 
			
		||||
            if name not in post.title:
 | 
			
		||||
                post.title = '{}: {}'.format(name, post.title)
 | 
			
		||||
        return object_list
 | 
			
		||||
 | 
			
		||||
    def get_object_list(self):
 | 
			
		||||
        diffs = self.get_diffs()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user