Commit 7706def0 authored by Patrick Colmant's avatar Patrick Colmant

Use of self._errors in UserDataForm

parent d52e4675
......@@ -36,15 +36,15 @@ from repanier.models import Purchase
from repanier.models import BankAccount
import django
class LocalizedModelForm(forms.ModelForm):
def __new__(cls, *args, **kwargs):
new_class = super(LocalizedModelForm, cls).__new__(cls, *args, **kwargs)
for field in new_class.base_fields.values():
if isinstance(field, django.forms.DecimalField):
field.localize = True
field.widget.is_localized = True
return new_class
# import django
# class LocalizedModelForm(forms.ModelForm):
# def __new__(cls, *args, **kwargs):
# new_class = super(LocalizedModelForm, cls).__new__(cls, *args, **kwargs)
# for field in new_class.base_fields.values():
# if isinstance(field, django.forms.DecimalField):
# field.localize = True
# field.widget.is_localized = True
# return new_class
# Filters in the right sidebar of the change list page of the admin
from django.contrib.admin import SimpleListFilter
......@@ -190,46 +190,58 @@ class UserDataForm(forms.ModelForm):
super(UserDataForm, self).__init__(*args, **kwargs)
self.user = None
def error(self,field, msg):
if field not in self._errors:
self._errors[field]= self.error_class([msg])
def clean(self):
# Check that the two password entries match
# The SiteStaff has no first_name or last_name because it's a function with login/pwd.
# A SiteCustomer with a first_name and last_name is responsible of this funcition.
if not self['first_name'].html_name in self.data:
if 'first_name' in self._errors:
del self._errors['first_name']
self.data['first_name'] = self.fields['first_name'].initial
if not self['last_name'].html_name in self.data:
if 'last_name' in self._errors:
del self._errors['last_name']
self.data['last_name'] = self.fields['last_name'].initial
# Check that the password is set when it's a new user.
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
initial_username = self.fields['username'].initial
if initial_username=='':
if not password1:
self.error('password1',_('The given password must be set'))
# Check that the two password entries match
if password1 and password2 and password1 != password2:
raise forms.ValidationError(_("Passwords must match"))
self.error('password1',_("Passwords must match"))
# Check that the user name is set. The required attribute is not a waranty
# that the browser hasn't removed it.
username = self.cleaned_data.get("username")
if not username:
raise forms.ValidationError(_('The given username must be set'))
self.error('username',_('The given username must be set'))
# Check that the email is set
email = self.cleaned_data.get("email")
if not email:
raise forms.ValidationError(_('The given email must be set'))
self.error('email',_('The given email must be set'))
# Check that the email is not already used
user=None
email = User.objects.normalize_email(email)
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
pass
if user != None and username!=user.username:
raise forms.ValidationError(_('The given email exist already and is used by another user'))
if self.fields['username'].initial=='':
if not password1:
raise forms.ValidationError(_('The given password must be set'))
if user != None and initial_username!=user.username:
self.error('email',_('The given email is used by another user'))
# Check that the username is not already used
if self.fields['username'].initial!=username:
user=None
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
pass
if user != None:
raise forms.ValidationError(_('The given username exist already and is used by another user'))
if not self['first_name'].html_name in self.data:
if 'first_name' in self._errors:
del self._errors['first_name']
self.data['first_name'] = self.fields['first_name'].initial
# self.cleaned_data['first_name'] = self.fields['first_name'].initial
if not self['last_name'].html_name in self.data:
if 'last_name' in self._errors:
del self._errors['last_name']
self.data['last_name'] = self.fields['last_name'].initial
# self.cleaned_data['last_name'] = self.fields['last_name'].initial
self.error('username',_('The given username is used by another user'))
super(UserDataForm, self).clean()
return self.cleaned_data
......
......@@ -37,3 +37,7 @@ class ContactFormAjax(forms.Form):
class OrderForm(forms.Form):
email = forms.EmailField(label='Your Email')
permanence_memo = HTMLField(blank=True)
class OrderTestForm(forms.Form):
email = forms.EmailField(label='Your Email')
permanence_memo = HTMLField(blank=True)
......@@ -8,8 +8,18 @@
{{ email }}
{{ permanence_memo |safe }}
<ul>
{% for producer in siteproducer_list %}
<li>{{ producer.long_profile_name }}</li>
{% for producer in siteproducer_set %}
<li>{{ producer.long_profile_name }}</li>
{% endfor %}
</ul>
<ul>
{% for offeritem in offeritem_set %}
{% ifchanged offeritem.product.site_producer %}
<b>{{ offeritem.product.site_producer.long_profile_name }}</b>
{% endifchanged %}
<li>{{ offeritem.product.long_name }}</li>
{% empty %}
<li>Sorry, no product in this list.</li>
{% endfor %}
</ul>
</div>
......
{% extends 'subpage_base.html' %}
{% load cms_tags sekizai_tags i18n l10n crispy_forms_tags %}
{% block sub_content %}
<div class="col-md-12">
{% crispy form %}
</div>
<div class="col-md-12">
{{ email }}
{{ permanence_memo |safe }}
<ul>
{% for producer in siteproducer_set %}
<li>{{ producer.long_profile_name }}</li>
{% endfor %}
</ul>
<ul>
{% for offeritem in offeritem_set %}
{% ifchanged offeritem.product.site_producer %}
<b>{{ offeritem.product.site_producer.long_profile_name }}</b>
{% endifchanged %}
<li>{{ offeritem.product.long_name }}</li>
{% empty %}
<li>Sorry, no product in this list.</li>
{% endfor %}
</ul>
</div>
{% endblock %}
\ No newline at end of file
from django.conf.urls import patterns, url
from repanier import views
from django.contrib.auth.decorators import login_required
from views import OrderView
from views import OrderView, OrderTestView
urlpatterns = patterns('',
url(r'^contact/$', views.contact_form, name='contact_form'),
url(r'^contact-ajax/$', views.contact_form_ajax, name='contact_form_ajax'),
# url(r'^preparation/', login_required(PreparationView.as_view()), name='preparation_view'),
url(r'^order/(?P<permanence_id>\d+)/$', login_required(OrderView.as_view()), name='order_view'),
url(r'^order_test/(?P<permanence_id>\d+)/$', login_required(OrderTestView.as_view()), name='order_test_view'),
)
......@@ -15,7 +15,7 @@ from django.template import RequestContext
from repanier.models import OfferItem
from repanier.models import Permanence
from repanier.models import SiteProducer
from repanier.forms import ContactForm, ContactFormAjax, OrderForm
from repanier.forms import ContactForm, ContactFormAjax, OrderForm, OrderTestForm
def render_response(req, *args, **kwargs):
# For csrf : http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/
......@@ -62,27 +62,70 @@ from django.http import Http404
class OrderView(FormView):
template_name = 'repanier/order_form.html'
form_class = OrderForm
form_class = OrderTestForm
success_url = '/thanks/'
def get_context_data(self, **kwargs):
context = super(OrderView,self).get_context_data(**kwargs)
my_permanence = get_object_or_404(Permanence, pk = kwargs['permanence_id'])
if my_permanence.status <> PERMANENCE_OPEN:
if not self.request.user.customer:
raise Http404
permanence = get_object_or_404(Permanence, pk = kwargs['permanence_id'])
if permanence.site_id <> settings.SITE_ID:
raise Http404
if self.request.user.is_anonymous():
if permanence.status <> PERMANENCE_OPEN:
raise Http404
# https://docs.djangoproject.com/en/dev/topics/http/shortcuts/
# https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-display/#dynamic-filtering
context['email'] = 'ask.it@to.me'
context['permanence_memo'] = permanence.memo
siteproducer_set = SiteProducer.objects.all().filter(permanence = permanence)
context['siteproducer_set'] = siteproducer_set
offeritem_set = OfferItem.objects.all().filter(
permanence = permanence
).order_by('product__site_producer__short_profile_name',
'product__department_for_customer__short_name', 'product__long_name')
context['offeritem_set'] = offeritem_set
return context
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(self.form_class)
context = self.get_context_data(form=form, **kwargs)
return self.render_to_response(context)
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
return super(OrderView, self).form_valid(form)
class OrderTestView(FormView):
template_name = 'repanier/order_test_form.html'
form_class = OrderTestForm
success_url = '/thanks/'
def get_context_data(self, **kwargs):
context = super(OrderTestView,self).get_context_data(**kwargs)
if not self.request.user.customer:
raise Http404
if my_permanence.site_id <> settings.SITE_ID:
permanence = get_object_or_404(Permanence, pk = kwargs['permanence_id'])
if permanence.site_id <> settings.SITE_ID:
raise Http404
if permanence.status <> PERMANENCE_OPEN:
raise Http404
# https://docs.djangoproject.com/en/dev/topics/http/shortcuts/
# https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-display/#dynamic-filtering
context['email'] = 'ask.it@to.me'
context['permanence_memo'] = my_permanence.memo
context['siteproducer_list'] = SiteProducer.objects.all().filter(
permanence = my_permanence
)
context['permanence_memo'] = permanence.memo
siteproducer_set = SiteProducer.objects.all().filter(permanence = permanence)
context['siteproducer_set'] = siteproducer_set
offeritem_set = OfferItem.objects.all().filter(
permanence = permanence
).order_by('product__site_producer__short_profile_name',
'product__department_for_customer__short_name', 'product__long_name')
context['offeritem_set'] = offeritem_set
return context
def get(self, request, *args, **kwargs):
......@@ -95,7 +138,7 @@ class OrderView(FormView):
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
return super(OrderView, self).form_valid(form)
return super(OrderTestView, self).form_valid(form)
# class PreparationView(View):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment