diff options
author | luxagraf <sng@luxagraf.net> | 2017-11-09 11:32:36 -0800 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2017-11-09 11:32:36 -0800 |
commit | ff0316afe40fa8572e787bdc2c5c69fd9c7ace65 (patch) | |
tree | dac03bdcc20b7349d8bfac916ae31377f07b9b5b /app | |
parent | e392dc34c836dc529a72b603c5ee5c9b917c2f37 (diff) |
fixed bird admin. maybe.
Diffstat (limited to 'app')
-rw-r--r-- | app/birds/admin.py | 8 | ||||
-rw-r--r-- | app/utils/views.py | 1 | ||||
-rw-r--r-- | app/utils/widgets.py | 32 |
3 files changed, 39 insertions, 2 deletions
diff --git a/app/birds/admin.py b/app/birds/admin.py index b5c6a7e..a8e3a0b 100644 --- a/app/birds/admin.py +++ b/app/birds/admin.py @@ -4,6 +4,7 @@ from birds.models import BirdSighting, BirdAudio, BirdClass, Bird from photos.forms import GalleryForm from utils.util import get_latlon +from utils.widgets import CustomSelectMultiple class BirdClassAdmin(admin.ModelAdmin): @@ -20,9 +21,12 @@ class BirdAdmin(admin.ModelAdmin): class GalleryFormPlus(GalleryForm): def __init__(self, *args, **kwargs): - super(GalleryFormPlus, self).__init__(*args, **kwargs) self.base_fields['seen_by'].widget = CustomSelectMultiple() - + super(GalleryFormPlus, self).__init__(*args, **kwargs) + + class Meta: + model = BirdSighting + fields = '__all__' class BirdSightingAdmin(OSMGeoAdmin): form = GalleryFormPlus diff --git a/app/utils/views.py b/app/utils/views.py index 26253ff..108293e 100644 --- a/app/utils/views.py +++ b/app/utils/views.py @@ -1,6 +1,7 @@ from itertools import chain from django.views.generic import ListView from photos.models import LuxImage, LuxVideo +from django.shortcuts import render_to_response from django.shortcuts import render from django.template import RequestContext diff --git a/app/utils/widgets.py b/app/utils/widgets.py index 240820a..eac4631 100644 --- a/app/utils/widgets.py +++ b/app/utils/widgets.py @@ -25,6 +25,38 @@ def markdown_to_html(txt): return md.convert(txt) +class CustomSelectMultiple(SelectMultiple): + def render_options(self, choices, selected_choices): + if not selected_choices: + # there is CreatView and we have no selected choices - render all selected + render_option = self.render_option + else: + # there is UpdateView and we have selected choices - render as default + render_option = super(CustomSelectMultiple, self).render_option + + selected_choices = set(force_text(v) for v in selected_choices) + output = [] + for option_value, option_label in chain(self.choices, choices): + if isinstance(option_label, (list, tuple)): + output.append(format_html('<optgroup label="{0}">', force_text(option_value))) + for option in option_label: + output.append(render_option(selected_choices, *option)) + output.append('</optgroup>') + else: + + output.append(render_option(selected_choices, option_value, option_label)) + return '\n'.join(output) + + def render_option(self, selected_choices, option_value, option_label): + option_value = force_text(option_value) + selected_html = mark_safe(' selected="selected"') + + return format_html('<option value="{0}"{1}>{2}</option>', + option_value, + selected_html, + force_text(option_label)) + + class TagListFilter(admin.SimpleListFilter): # Human-readable title which will be displayed in the # right admin sidebar just above the filter options. |