allow multiple emails for sectors (#8268)
This commit is contained in:
parent
6f64d3b239
commit
d50d427ee3
|
@ -17,10 +17,54 @@
|
|||
from django.utils.text import slugify
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core import validators
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from .models import EncombrantsManagement, Commune, Street, Sector
|
||||
|
||||
|
||||
class ListValidator(object):
|
||||
def __init__(self, item_validator):
|
||||
self.item_validator = item_validator
|
||||
|
||||
def __call__(self, value):
|
||||
for i, item in enumerate(value):
|
||||
try:
|
||||
self.item_validator(item)
|
||||
except ValidationError, e:
|
||||
raise
|
||||
|
||||
|
||||
class CommaSeparatedEmailField(forms.Field):
|
||||
|
||||
def __init__(self, dedup=True, max_length=None, min_length=None, *args,
|
||||
**kwargs):
|
||||
self.dedup = dedup
|
||||
self.max_length = max_length
|
||||
self.min_length = min_length
|
||||
item_validators = [validators.EmailValidator()]
|
||||
super(CommaSeparatedEmailField, self).__init__(*args, **kwargs)
|
||||
for item_validator in item_validators:
|
||||
self.validators.append(ListValidator(item_validator))
|
||||
|
||||
def to_python(self, value):
|
||||
if value in validators.EMPTY_VALUES:
|
||||
return []
|
||||
|
||||
value = [item.strip() for item in value.split(',') if item.strip()]
|
||||
if self.dedup:
|
||||
value = list(set(value))
|
||||
return value
|
||||
|
||||
def clean(self, value):
|
||||
values = self.to_python(value)
|
||||
self.validate(values)
|
||||
self.run_validators(values)
|
||||
return value
|
||||
|
||||
from .models import EncombrantsManagement, Commune, Street
|
||||
|
||||
class EncombrantsManagementForm(forms.ModelForm):
|
||||
|
||||
class Meta:
|
||||
model = EncombrantsManagement
|
||||
exclude = ('slug', 'users')
|
||||
|
@ -54,6 +98,14 @@ class CommuneForm(forms.ModelForm):
|
|||
return obj
|
||||
|
||||
|
||||
class SectorForm(forms.ModelForm):
|
||||
contact_email = CommaSeparatedEmailField(label=_('Emails'),
|
||||
required=False,
|
||||
help_text=_('separated by commas'))
|
||||
class Meta:
|
||||
model = Sector
|
||||
|
||||
|
||||
class StreetsForm(forms.Form):
|
||||
streets = forms.CharField(widget=forms.Textarea(attrs={'cols': 25, 'rows': 10}),
|
||||
help_text=_('one street by line'),
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('passerelle_montpellier_encombrants', '0006_auto_20150626_0221'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='sector',
|
||||
name='contact_email',
|
||||
field=models.CharField(help_text='separated by commas', max_length=128, verbose_name='Contact Emails', blank=True),
|
||||
preserve_default=True,
|
||||
),
|
||||
]
|
|
@ -43,8 +43,9 @@ class EncombrantsManagement(BaseResource):
|
|||
|
||||
|
||||
class Sector(models.Model):
|
||||
contact_email = models.EmailField(blank=True,
|
||||
verbose_name=_('Contact Email'))
|
||||
contact_email = models.CharField(max_length=128, blank=True,
|
||||
verbose_name=_('Contact Emails'),
|
||||
help_text=_('separated by commas'))
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Sector')
|
||||
|
|
|
@ -88,8 +88,9 @@ def email_sectors(formdefs, when):
|
|||
sectors[sector.contact_email].append(data)
|
||||
|
||||
for destination, data in sectors.iteritems():
|
||||
destinations = [d.strip() for d in destination.split(',') if d.strip()]
|
||||
mail = EmailMessage(subject.render(context), message.render(context),
|
||||
settings.DEFAULT_FROM_EMAIL, [destination])
|
||||
settings.DEFAULT_FROM_EMAIL, destinations)
|
||||
attachement_body = StringIO.StringIO()
|
||||
attachment = csv.writer(attachement_body)
|
||||
attachment.writerow([e.encode("utf-8") for e in header])
|
||||
|
|
|
@ -28,6 +28,7 @@ from passerelle import utils as passerelle_utils
|
|||
from .models import EncombrantsManagement, Sector, Commune, CollectDay, Street
|
||||
from .forms import EncombrantsManagementForm, EncombrantsManagementUpdateForm, CommuneForm, StreetsForm
|
||||
from .forms import EncombrantsManagementForm, EncombrantsManagementUpdateForm, CommuneForm
|
||||
from .forms import SectorForm
|
||||
from .utils import prefix_cleanup, get_sector
|
||||
|
||||
|
||||
|
@ -67,12 +68,14 @@ class SectorListView(ListView):
|
|||
|
||||
class SectorCreateView(CreateView):
|
||||
model = Sector
|
||||
form_class = SectorForm
|
||||
template_name = 'passerelle/manage/service_form.html'
|
||||
success_url = reverse_lazy('montpellier-encombrants-sector-listing')
|
||||
|
||||
|
||||
class SectorUpdateView(UpdateView):
|
||||
model = Sector
|
||||
form_class = SectorForm
|
||||
template_name = 'passerelle/manage/service_form.html'
|
||||
success_url = reverse_lazy('montpellier-encombrants-sector-listing')
|
||||
|
||||
|
|
Loading…
Reference in New Issue