Guest User

Untitled

a guest
Mar 15th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. #settings
  2. AUTH_PROFILE_MODULE = 'client.Client'
  3. LOGIN_REDIRECT_URL = '/acces-client/'
  4. LOGIN_URL = '/acces-client/login/'
  5. LOGOUT_URL = '/acces-client/logout/'
  6.  
  7. #-*- encoding: utf-8 -*-
  8.  
  9. #Import Django
  10. from django.db import models
  11. from django.contrib.auth.models import User
  12. from django.contrib.localflavor.ch import ch_states
  13. from django.utils.translation import ugettext as _
  14.  
  15. #views
  16. class Client(models.Model):
  17.     """
  18.         Model pour la base de donnée des clients
  19.     """
  20.     user = models.ForeignKey(User)
  21.     company = models.CharField(max_length=512, verbose_name=_(u"Société"), blank=True)
  22.     adress = models.CharField(max_length=512, verbose_name=_(u"Adresse *"))
  23.     zipcode = models.IntegerField(max_length=4, verbose_name=_(u"Code postal *"))
  24.     city = models.CharField(max_length=128, verbose_name=_(u"Ville *"))
  25.     website = models.URLField(verbose_name=_(u"Site internet"), blank=True)
  26.     phone = models.CharField(max_length=128, verbose_name=_(u"Téléphone *"))
  27.     mobile = models.CharField(max_length=128, verbose_name=_(u"Natel"), blank=True)
  28.     fax = models.CharField(max_length=128, verbose_name=_(u"fax"), blank=True)
  29.    
  30.     def __unicode__(self):
  31.         return self.user
  32.  
  33. #forms
  34. #-*- encoding: utf-8 -*-
  35.  
  36. #Import Django
  37. from django.forms import ModelForm
  38. from django import forms
  39. from django.contrib.localflavor.ch.forms import CHPhoneNumberField, CHZipCodeField, CHStateSelect
  40. from django.forms.extras.widgets import *
  41. from django.utils.translation import ugettext as _
  42.  
  43.  
  44. # Import models
  45. from client.models import Client
  46.  
  47. class loginForm(forms.Form):
  48.     """
  49.         Formulaire de connection
  50.     """
  51.     username = forms.CharField(max_length=100, label=_(u"Email"))
  52.     password = forms.CharField(widget=forms.PasswordInput, label=_(u"Mot de passe"))
  53.    
  54.    
  55. class registerForm(forms.Form):
  56.     """
  57.         Formulaire d'enregistrement
  58.     """
  59.     email = forms.EmailField()
  60.     password = forms.CharField(widget=forms.PasswordInput, label=_("Mot de passe"))
  61.     password2 = forms.CharField(widget=forms.PasswordInput, label=_("Confirmation du mot de passe"))
  62.    
  63.  
  64. class informationsForm(forms.Form):
  65.     """
  66.         Formulaire d'information sur l'utilisateur
  67.     """
  68.     email = forms.EmailField(label=_(u"Email*"))
  69.     password = forms.CharField(widget=forms.PasswordInput, label=_("Mot de passe*"),)
  70.     password2 = forms.CharField(widget=forms.PasswordInput, label=_("Confirmation du mot de passe*"))
  71.     lastname = forms.CharField(max_length=100, label=_(u"Nom*"))
  72.     firstname = forms.CharField(max_length=100, label=_(u"Prénom*"))
  73.     zipcode = CHZipCodeField(label=_(u"Code postal*"))
  74.     city = forms.CharField(widget=CHStateSelect)
  75.     phone = CHPhoneNumberField(label=_(u"Téléphone*"))
  76.     mobile = CHPhoneNumberField(label=_(u"Natel"), required=False)
  77.     fax = CHPhoneNumberField(label=_(u"Fax"), required=False)
  78.    
  79.        
  80. #views
  81. url(r'informations/', 'informations', {'template_name': 'informations.html'}, name='client_informations'),
  82. #views
  83. @login_required
  84. def informations(request, template_name):
  85.     if request.method == 'POST':
  86.         form = informationsForm(request.POST)
  87.         if form.is_valid():
  88.             user = request.user
  89.             # we take the field names of the user to use them to extract information from the form
  90.             user_fields = fields_for_model(user).keys()
  91.             # we create a dictionary with the data that belongs to the user
  92.             new_data = dict((field, form.cleaned_data.pop(field)) for field in user_fields)
  93.             form.cleaned_data['email']
  94.             form.cleaned_data['password']
  95.             form.cleaned_data['lastname']
  96.             form.cleaned_data['firstname']
  97.             form.cleaned_data['zipcode']
  98.             form.cleaned_data['city']
  99.             form.cleaned_data['phone']
  100.             form.cleaned_data['mobile']
  101.             form.cleaned_data['fax']
  102.             # we set the new data on the user
  103.             for field in new_data:
  104.                 setattr(user, field, new_data[field])
  105.                 user.save()
  106.                 return redirect('client_informations')
  107.     else:
  108.         user = request.user
  109.         infosUser = model_to_dict(user)
  110.         infosClient = model_to_dict(Client.user)
  111.         infosUser.update(infosClient)
  112.         infosForm = informationsForm(initial=infosUser)
  113.         return render_to_response(template_name, { "forms": infosForm }, context_instance = RequestContext(request))
Add Comment
Please, Sign In to add comment