#37: Retrouver une émission passée ou à venir #48
							
								
								
									
										32
									
								
								aircox/admin/filters.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								aircox/admin/filters.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
			
		||||
from django.contrib.admin import filters
 | 
			
		||||
from django.utils.translation import gettext_lazy as _
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
__all__ = ('DateFieldFilter',)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DateFieldFilter(filters.FieldListFilter):
 | 
			
		||||
    template = 'admin/aircox/filters/date_filter.html'
 | 
			
		||||
    input_type = 'date'
 | 
			
		||||
 | 
			
		||||
    def __init__(self, field, request, params, model, model_admin, field_path):
 | 
			
		||||
        self.field_generic = '%s__' % field_path
 | 
			
		||||
        self.date_params = {k: v for k, v in params.items()
 | 
			
		||||
                                if k.startswith(self.field_generic)}
 | 
			
		||||
 | 
			
		||||
        self.links = ((_('Exact'), self.field_generic + 'exact'),
 | 
			
		||||
                      (_('Since'), self.field_generic + 'gte'),
 | 
			
		||||
                      (_('Until'), self.field_generic + 'lte'))
 | 
			
		||||
        super().__init__(field, request, params, model, model_admin, field_path)
 | 
			
		||||
 | 
			
		||||
    def expected_parameters(self):
 | 
			
		||||
        return [link[1] for link in self.links]
 | 
			
		||||
 | 
			
		||||
    def choices(self, changelist):
 | 
			
		||||
        for link in self.links:
 | 
			
		||||
            yield {'label': link[0],
 | 
			
		||||
                   'name': link[1],
 | 
			
		||||
                   'value': self.date_params.get(link[1]),
 | 
			
		||||
                   'type': self.input_type,}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -8,9 +8,10 @@ from django.utils.translation import gettext_lazy as _
 | 
			
		||||
from adminsortable2.admin import SortableInlineAdminMixin
 | 
			
		||||
 | 
			
		||||
from ..models import Category, Comment, NavItem, Page, StaticPage
 | 
			
		||||
from .filters import DateFieldFilter
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
__all__ = ['CategoryAdmin', 'PageAdmin', 'NavItemInline']
 | 
			
		||||
__all__ = ('CategoryAdmin', 'PageAdmin', 'NavItemInline')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@admin.register(Category)
 | 
			
		||||
@ -82,7 +83,7 @@ class PageAdmin(BasePageAdmin):
 | 
			
		||||
 | 
			
		||||
    list_display = BasePageAdmin.list_display + ('category',)
 | 
			
		||||
    list_editable = BasePageAdmin.list_editable + ('category',)
 | 
			
		||||
    list_filter = BasePageAdmin.list_editable + ('category',)
 | 
			
		||||
    list_filter = BasePageAdmin.list_filter + ('category', ('pub_date', DateFieldFilter))
 | 
			
		||||
    search_fields = ('category__title',)
 | 
			
		||||
    fieldsets = deepcopy(BasePageAdmin.fieldsets)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -38,7 +38,7 @@ class Category(models.Model):
 | 
			
		||||
        return self.title
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PageQuerySet(InheritanceQuerySet):
 | 
			
		||||
class BasePageQuerySet(InheritanceQuerySet):
 | 
			
		||||
    def draft(self):
 | 
			
		||||
        return self.filter(status=Page.STATUS_DRAFT)
 | 
			
		||||
 | 
			
		||||
@ -86,7 +86,7 @@ class BasePage(models.Model):
 | 
			
		||||
        _('content'), blank=True, null=True,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    objects = PageQuerySet.as_manager()
 | 
			
		||||
    objects = BasePageQuerySet.as_manager()
 | 
			
		||||
 | 
			
		||||
    detail_url_name = None
 | 
			
		||||
    item_template_name = 'aircox/widgets/page_item.html'
 | 
			
		||||
@ -149,6 +149,12 @@ class BasePage(models.Model):
 | 
			
		||||
        return cls(**cls.get_init_kwargs_from(page, **kwargs))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PageQuerySet(BasePageQuerySet):
 | 
			
		||||
    def published(self):
 | 
			
		||||
        return self.filter(status=Page.STATUS_PUBLISHED,
 | 
			
		||||
                           pub_date__lte=tz.now())
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Page(BasePage):
 | 
			
		||||
    """ Base Page model used for articles and other dated content. """
 | 
			
		||||
    category = models.ForeignKey(
 | 
			
		||||
@ -163,6 +169,8 @@ class Page(BasePage):
 | 
			
		||||
        _('allow comments'), default=True,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    objects = PageQuerySet.as_manager()
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        verbose_name = _('Publication')
 | 
			
		||||
        verbose_name_plural = _('Publications')
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										20
									
								
								aircox/templates/admin/aircox/filters/date_filter.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								aircox/templates/admin/aircox/filters/date_filter.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
{% extends "./filter.html" %}
 | 
			
		||||
{% load static %}
 | 
			
		||||
{% block content %}
 | 
			
		||||
{% with choices|first as choice %}
 | 
			
		||||
<form method="GET">
 | 
			
		||||
    <ul>
 | 
			
		||||
        {% for choice in choices %}
 | 
			
		||||
        <li>
 | 
			
		||||
            <label for="{{ choice.name }}">{{ choice.label }}</label>
 | 
			
		||||
            <input type="{{ choice.type }}" name="{{ choice.name }}"
 | 
			
		||||
                value="{{ choice.value }}" {{ choice.extra }} />
 | 
			
		||||
        </li>
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
    </ul>
 | 
			
		||||
    <div style="text-align: right;">
 | 
			
		||||
        <input type="submit" value="Chercher">
 | 
			
		||||
    </div>
 | 
			
		||||
</form>
 | 
			
		||||
{% endwith %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
@ -0,0 +1,7 @@
 | 
			
		||||
{% extends "admin/aircox/filter.html" %}
 | 
			
		||||
{% block content %}
 | 
			
		||||
{% with choices|first as choice %}
 | 
			
		||||
<input type="datetime-local" value="choice.value" />
 | 
			
		||||
{% endwith %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										4
									
								
								aircox/templates/admin/aircox/filters/filter.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								aircox/templates/admin/aircox/filters/filter.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
{% load i18n %}
 | 
			
		||||
<h3>{% blocktranslate with filter_title=title %} By {{ filter_title }} {% endblocktranslate %}</h3>
 | 
			
		||||
{% block content %}{% endblock %}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user