Initial version
This commit is contained in:
parent
ad0c78f57f
commit
a0b976a758
9 changed files with 122 additions and 0 deletions
4
CHANGES.rst
Normal file
4
CHANGES.rst
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
0.0.1 (2012-09-27)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* Initial version from the next `snippet <http://djangosnippets.org/snippets/1200/>`_
|
4
MANIFEST.in
Normal file
4
MANIFEST.in
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
include README.rst
|
||||||
|
include AUTHORS
|
||||||
|
include CHANGES
|
||||||
|
include *.py
|
29
setup.py
Normal file
29
setup.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#http://djangosnippets.org/users/danielroseman/
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import os
|
||||||
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
|
|
||||||
|
def read(*rnames):
|
||||||
|
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name="django-multiselectfield",
|
||||||
|
version="0.0.1",
|
||||||
|
author="Daniel Roseman",
|
||||||
|
description="Django multiple select field",
|
||||||
|
long_description=(read('README.rst') + '\n\n' + read('CHANGES.rst')),
|
||||||
|
classifiers=[
|
||||||
|
'Development Status :: 4 - Beta',
|
||||||
|
'Framework :: Django',
|
||||||
|
'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
|
||||||
|
],
|
||||||
|
license="LGPL 3",
|
||||||
|
keywords="django,multiple,select,field,choices",
|
||||||
|
url='https://github.com/goinnn/django-multiselectfield',
|
||||||
|
packages=find_packages(where='src'),
|
||||||
|
package_dir={'': 'src'},
|
||||||
|
include_package_data=True,
|
||||||
|
zip_safe=False,
|
||||||
|
)
|
2
src/multiselectfield/__init__.py
Normal file
2
src/multiselectfield/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
from multiselectfield.db.fields import MultiSelectField
|
||||||
|
from multiselectfield.forms.fields import MultiSelectFormField
|
0
src/multiselectfield/db/__init__.py
Normal file
0
src/multiselectfield/db/__init__.py
Normal file
64
src/multiselectfield/db/fields.py
Normal file
64
src/multiselectfield/db/fields.py
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
from django.db import models
|
||||||
|
from django.utils.text import capfirst
|
||||||
|
from django.core import exceptions
|
||||||
|
|
||||||
|
from multiselectfield import MultiSelectFormField
|
||||||
|
|
||||||
|
|
||||||
|
class MultiSelectField(models.Field):
|
||||||
|
""" Choice values can not contain commas. """
|
||||||
|
__metaclass__ = models.SubfieldBase
|
||||||
|
|
||||||
|
def get_internal_type(self):
|
||||||
|
return "CharField"
|
||||||
|
|
||||||
|
def get_choices_default(self):
|
||||||
|
return self.get_choices(include_blank=False)
|
||||||
|
|
||||||
|
def get_choices_selected(self, arr_choices=''):
|
||||||
|
if not arr_choices:
|
||||||
|
return False
|
||||||
|
list = []
|
||||||
|
for choice_selected in arr_choices:
|
||||||
|
list.append(choice_selected[0])
|
||||||
|
return list
|
||||||
|
|
||||||
|
def _get_FIELD_display(self, field):
|
||||||
|
value = getattr(self, field.attname)
|
||||||
|
choicedict = dict(field.choices)
|
||||||
|
|
||||||
|
def value_to_string(self, obj):
|
||||||
|
value = self._get_val_from_obj(obj)
|
||||||
|
return self.get_db_prep_value(value)
|
||||||
|
|
||||||
|
def validate(self, value, model_instance):
|
||||||
|
arr_choices = self.get_choices_selected(self.get_choices_default())
|
||||||
|
for opt_select in value:
|
||||||
|
if (opt_select not in arr_choices):
|
||||||
|
raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value)
|
||||||
|
return
|
||||||
|
|
||||||
|
def formfield(self, **kwargs):
|
||||||
|
defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name),
|
||||||
|
'help_text': self.help_text, 'choices': self.choices}
|
||||||
|
if self.has_default():
|
||||||
|
defaults['initial'] = self.get_default()
|
||||||
|
defaults.update(kwargs)
|
||||||
|
return MultiSelectFormField(**defaults)
|
||||||
|
|
||||||
|
def get_db_prep_value(self, value, connection, prepared=False):
|
||||||
|
if isinstance(value, basestring):
|
||||||
|
return value
|
||||||
|
elif isinstance(value, list):
|
||||||
|
return ",".join(value)
|
||||||
|
|
||||||
|
def to_python(self, value):
|
||||||
|
if value is not None:
|
||||||
|
return value if isinstance(value, list) else value.split(',')
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def contribute_to_class(self, cls, name):
|
||||||
|
super(MultiSelectField, self).contribute_to_class(cls, name)
|
||||||
|
if self.choices:
|
||||||
|
func = lambda self, fieldname = name, choicedict = dict(self.choices): ",".join([choicedict.get(value, value) for value in getattr(self, fieldname)])
|
||||||
|
setattr(cls, 'get_%s_display' % self.name, func)
|
0
src/multiselectfield/forms/__init__.py
Normal file
0
src/multiselectfield/forms/__init__.py
Normal file
19
src/multiselectfield/forms/fields.py
Normal file
19
src/multiselectfield/forms/fields.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
from django import forms
|
||||||
|
from django.contrib.humanize.templatetags.humanize import apnumber
|
||||||
|
from django.template.defaultfilters import pluralize
|
||||||
|
|
||||||
|
|
||||||
|
class MultiSelectFormField(forms.MultipleChoiceField):
|
||||||
|
widget = forms.CheckboxSelectMultiple
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.max_choices = kwargs.pop('max_choices', 0)
|
||||||
|
super(MultiSelectFormField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def clean(self, value):
|
||||||
|
if not value and self.required:
|
||||||
|
raise forms.ValidationError(self.error_messages['required'])
|
||||||
|
if value and self.max_choices and len(value) > self.max_choices:
|
||||||
|
raise forms.ValidationError('You must select a maximum of %s choice%s.'
|
||||||
|
% (apnumber(self.max_choices), pluralize(self.max_choices)))
|
||||||
|
return value
|
Loading…
Reference in a new issue