Skip to content

Commit

Permalink
Demos for horizontal list filters
Browse files Browse the repository at this point in the history
  • Loading branch information
darklow committed Feb 11, 2017
1 parent 5797f4d commit 7e010b8
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 41 deletions.
Binary file modified demo/db.sqlite3
Binary file not shown.
85 changes: 45 additions & 40 deletions demo/demo/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib import messages
from django.shortcuts import redirect

from django.utils.encoding import force_text
from suit import apps

from suit.admin_filters import IsNullFieldListFilter
from suit.sortables import SortableTabularInline, SortableModelAdmin, SortableStackedInline
from suit.widgets import AutosizedTextarea
from .models import *
Expand Down Expand Up @@ -52,16 +54,25 @@ class Meta:
}


class PopulationFilter(IsNullFieldListFilter):
notnull_label = 'With population data'
isnull_label = 'Missing population data'
# def __init__(self, *args, **kwargs):
# super(ContinentFilter, self).__init__(*args, **kwargs)
# self.title = 'override filter title'


@admin.register(Country)
class CountryAdmin(admin.ModelAdmin):
form = CountryForm
search_fields = ('name', 'code')
list_display = ('name', 'code', 'continent', 'independence_day')
list_filter = ('continent',)
list_filter = ('continent', 'independence_day', 'code', ('population', PopulationFilter))
suit_list_filter_horizontal = ('code', 'population')
list_select_related = True
# date_hierarchy = 'independence_day'
inlines = (CityInline,)
# date_hierarchy = 'independence_day'

# fields = ('name', 'continent', 'code', 'independence_day')
fieldsets = [
(None, {
'classes': ('suit-tab suit-tab-general',),
Expand Down Expand Up @@ -107,12 +118,6 @@ class CountryAdmin(admin.ModelAdmin):
)


# fields = (('name', 'code', 'continent'), 'independence_day', 'population', 'description')


admin.site.register(Country, CountryAdmin)


# Inlines for ContinentAdmin
class CountryInlineForm(ModelForm):
class Meta:
Expand All @@ -133,6 +138,7 @@ class CountryInline(SortableTabularInline):
show_change_link = True


@admin.register(Continent)
class ContinentAdmin(SortableModelAdmin):
search_fields = ('name',)
list_display = ('name', 'countries')
Expand Down Expand Up @@ -169,27 +175,6 @@ def countries(self, obj):
return len(obj.country_set.all())


admin.site.register(Continent, ContinentAdmin)


class ShowcaseForm(ModelForm):
class Meta:
widgets = {
'textfield': AutosizedTextarea,
}


@staff_member_required
def showcase_custom_view_example(request, pk):
instance = Showcase.objects.get(pk=pk)

# Do something legendary here
messages.success(request, 'Something legendary was done to "%s"' % instance)

return redirect('admin:demo_showcase_change', pk)


# Inlines for Showcase
class BookInline(SortableTabularInline):
model = Book
min_num = 1
Expand Down Expand Up @@ -220,17 +205,28 @@ class MovieInline(SortableStackedInline):
}


class ShowcaseForm(ModelForm):
class Meta:
widgets = {
'textfield': AutosizedTextarea,
}


@admin.register(Showcase)
class ShowcaseAdmin(admin.ModelAdmin):
form = ShowcaseForm
inlines = (BookInline, MovieInline)
search_fields = ['name']
# radio_fields = {"horizontal_choices": admin.HORIZONTAL,
# 'vertical_choices': admin.VERTICAL}
# list_editable = ('boolean',)
# list_filter = ('choices', 'date', CountryFilter)
list_filter = ('horizontal_choices',)
# list_display = ('name', 'help_text', 'choices', 'horizontal_choices', 'boolean')
list_display = ('name', 'help_text')
readonly_fields = ('readonly_field',)
radio_fields = {"horizontal_choices": admin.HORIZONTAL,
'vertical_choices': admin.VERTICAL}

fieldsets = [
(None, {'fields': ['name', 'help_text', 'textfield',
('multiple_in_row', 'multiple2'),
Expand All @@ -239,6 +235,14 @@ class ShowcaseAdmin(admin.ModelAdmin):
'description': 'Original Django admin date/time widgets',
'fields': ['date_and_time', 'date', 'time_only']}),

('Collapsed settings', {
'classes': ('collapse',),
'fields': ['collapsed_param']}),

('Boolean and choices',
{'fields': ['boolean', 'boolean_with_help', 'choices',
'horizontal_choices', 'vertical_choices']}),

# ('Date and time', {
# 'description': 'Improved date/time widgets (SuitDateWidget, '
# 'SuitSplitDateTimeWidget) . Uses original JS.',
Expand All @@ -254,13 +258,7 @@ class ShowcaseAdmin(admin.ModelAdmin):
# 'appended inputs',
# 'fields': ['enclosed1', 'enclosed2']}),
#
# ('Boolean and choices',
# {'fields': ['boolean', 'boolean_with_help', 'choices',
# 'horizontal_choices', 'vertical_choices']}),
#
# ('Collapsed settings', {
# 'classes': ('collapse',),
# 'fields': ['hidden_checkbox', 'hidden_choice']}),

# ('And one more collapsable', {
# 'classes': ('collapse',),
# 'fields': ['hidden_charfield', 'hidden_charfield2']}),
Expand All @@ -286,4 +284,11 @@ def get_urls(self):
return my_urls + urls


admin.site.register(Showcase, ShowcaseAdmin)
@staff_member_required
def showcase_custom_view_example(request, pk):
instance = Showcase.objects.get(pk=pk)

# Do something legendary here
messages.success(request, 'Something legendary was done to "%s"' % instance)

return redirect('admin:demo_showcase_change', pk)
20 changes: 20 additions & 0 deletions demo/demo/migrations/0009_showcase_collapsed_param.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.11 on 2017-02-11 18:32
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('demo', '0008_auto_20160305_1055'),
]

operations = [
migrations.AddField(
model_name='showcase',
name='collapsed_param',
field=models.BooleanField(default=False),
),
]
40 changes: 40 additions & 0 deletions demo/demo/migrations/0010_auto_20170211_2037.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.11 on 2017-02-11 18:37
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('demo', '0009_showcase_collapsed_param'),
]

operations = [
migrations.AddField(
model_name='showcase',
name='boolean',
field=models.BooleanField(default=True),
),
migrations.AddField(
model_name='showcase',
name='boolean_with_help',
field=models.BooleanField(default=False, help_text=b'Boolean field with help text'),
),
migrations.AddField(
model_name='showcase',
name='choices',
field=models.SmallIntegerField(choices=[(1, b'Tall'), (2, b'Normal'), (3, b'Short')], default=3, help_text=b'Help text'),
),
migrations.AddField(
model_name='showcase',
name='horizontal_choices',
field=models.SmallIntegerField(choices=[(1, b'Awesome'), (2, b'Good'), (3, b'Normal'), (4, b'Bad')], default=1, help_text=b'Horizontal choices look like this'),
),
migrations.AddField(
model_name='showcase',
name='vertical_choices',
field=models.SmallIntegerField(choices=[(1, b'Hot'), (2, b'Normal'), (3, b'Cold')], default=2, help_text=b'Some help on vertical choices'),
),
]
13 changes: 13 additions & 0 deletions demo/demo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ class Showcase(models.Model):

date_widget = models.DateField(blank=True, null=True)
datetime_widget = models.DateTimeField(blank=True, null=True)
collapsed_param = models.BooleanField(default=False)

TYPE_CHOICES = ((1, 'Awesome'), (2, 'Good'), (3, 'Normal'), (4, 'Bad'))
TYPE_CHOICES2 = ((1, 'Hot'), (2, 'Normal'), (3, 'Cold'))
TYPE_CHOICES3 = ((1, 'Tall'), (2, 'Normal'), (3, 'Short'))
boolean = models.BooleanField(default=True)
boolean_with_help = models.BooleanField(default=False, help_text="Boolean field with help text")
horizontal_choices = models.SmallIntegerField(
choices=TYPE_CHOICES, default=1, help_text='Horizontal choices look like this')
vertical_choices = models.SmallIntegerField(
choices=TYPE_CHOICES2, default=2, help_text="Some help on vertical choices")
choices = models.SmallIntegerField(
choices=TYPE_CHOICES3, default=3, help_text="Help text")

class Meta:
verbose_name_plural = 'Showcase'
Expand Down
2 changes: 1 addition & 1 deletion demo/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Django==1.9.11
Django==1.10.5

0 comments on commit 7e010b8

Please sign in to comment.