Add custom ModelForm class that supports field choice sorting. Use the new form class to sort the user profile language field and the document language fields. Thanks to Baptiste GAILLET @bat79a for the original idea. GitLab issue #292.
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
2.1.2 (2016-05-xx)
|
||||||
|
==================
|
||||||
|
- Sort document languages and user profile locale language lists. GitLab issue #292.
|
||||||
|
|
||||||
2.1.1 (2016-05-17)
|
2.1.1 (2016-05-17)
|
||||||
==================
|
==================
|
||||||
- Fix navigation issue that make it impossible to add new sources. GitLab issue #288.
|
- Fix navigation issue that make it impossible to add new sources. GitLab issue #288.
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from operator import itemgetter
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
@@ -114,10 +115,44 @@ class LicenseForm(FileDisplayForm):
|
|||||||
FILENAME = 'LICENSE'
|
FILENAME = 'LICENSE'
|
||||||
|
|
||||||
|
|
||||||
class LocaleProfileForm(forms.ModelForm):
|
class ModelForm(forms.ModelForm):
|
||||||
|
"""
|
||||||
|
ModelForm subclass that supports field choices sorting
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
# Dictionary of field names and the key used to sort the field
|
||||||
|
sorted_fields = None
|
||||||
|
|
||||||
|
# Example:
|
||||||
|
# sorted_fields = {'language': operator.itemgetter(1))}
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(ModelForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
for field, key in getattr(self.Meta, 'sorted_fields', {}).items():
|
||||||
|
# Would be the cleaner "opts.sorted_fields" if these were addressed
|
||||||
|
# https://code.djangoproject.com/ticket/5793
|
||||||
|
# of a get_options_class for Forms/ModelForms
|
||||||
|
# https://code.djangoproject.com/ticket/18540
|
||||||
|
choices = self.fields[field].choices
|
||||||
|
|
||||||
|
if not self.fields[field].required:
|
||||||
|
# Remove empty choice before sorting
|
||||||
|
empty_choice = choices.pop(0)
|
||||||
|
|
||||||
|
self.fields[field].choices = sorted(choices, key=key)
|
||||||
|
|
||||||
|
if not self.fields[field].required:
|
||||||
|
# Add empty choice after sorting
|
||||||
|
self.fields[field].choices.insert(0, empty_choice)
|
||||||
|
|
||||||
|
|
||||||
|
class LocaleProfileForm(ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
fields = ('language', 'timezone')
|
fields = ('language', 'timezone')
|
||||||
model = UserLocaleProfile
|
model = UserLocaleProfile
|
||||||
|
sorted_fields = {'language': itemgetter(1)}
|
||||||
|
|
||||||
|
|
||||||
class LocaleProfileForm_view(DetailForm):
|
class LocaleProfileForm_view(DetailForm):
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import absolute_import, unicode_literals
|
from __future__ import absolute_import, unicode_literals
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from operator import itemgetter
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
@@ -8,7 +9,7 @@ from django.template.defaultfilters import filesizeformat
|
|||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from acls.models import AccessControlList
|
from acls.models import AccessControlList
|
||||||
from common.forms import DetailForm
|
from common.forms import DetailForm, ModelForm
|
||||||
from permissions import Permission
|
from permissions import Permission
|
||||||
|
|
||||||
from .models import (
|
from .models import (
|
||||||
@@ -60,13 +61,14 @@ class DocumentPreviewForm(forms.Form):
|
|||||||
preview = forms.CharField(widget=DocumentPagesCarouselWidget())
|
preview = forms.CharField(widget=DocumentPagesCarouselWidget())
|
||||||
|
|
||||||
|
|
||||||
class DocumentForm(forms.ModelForm):
|
class DocumentForm(ModelForm):
|
||||||
"""
|
"""
|
||||||
Form sub classes from DocumentForm used only when editing a document
|
Form sub classes from DocumentForm used only when editing a document
|
||||||
"""
|
"""
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Document
|
|
||||||
fields = ('label', 'description', 'language')
|
fields = ('label', 'description', 'language')
|
||||||
|
model = Document
|
||||||
|
sorted_fields = {'language': itemgetter(1)}
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
document_type = kwargs.pop('document_type', None)
|
document_type = kwargs.pop('document_type', None)
|
||||||
|
|||||||
Reference in New Issue
Block a user