pmalviya13

Untitled

Dec 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.40 KB | None | 0 0
  1. #models.py
  2.  
  3. from django.db import models
  4. from django.contrib.auth.models import User
  5. from django.db.models.signals import post_save
  6. from django.dispatch import receiver
  7.  
  8. # Create your models here.
  9.  
  10. class Profile(models.Model):
  11.     CLG_CHOICES = (
  12.         ('MIT', 'Mahakal Institute Of Technology'),
  13.         ('MITM', 'Mahakal Institute Of Technology And Management'),
  14.     )
  15.     BRANCH_CHOICES = (
  16.         ('CS', 'Computer Science And Engineering'),
  17.         ('IT', 'Information Technology'),
  18.         ('CE', 'Civil Engineering'),
  19.         ('ME', 'Mechanical Engineering'),
  20.         ('EC', 'Electronic Engineering'),
  21.     )
  22.     college = models.CharField(max_length=5,choices=CLG_CHOICES)
  23.     branch = models.CharField(max_length=5,choices=BRANCH_CHOICES)
  24.     birth_date = models.DateField(blank=True, null=True)
  25.     user = models.OneToOneField(User,on_delete=models.CASCADE,related_name='profile')
  26.  
  27.     def __str__(self):
  28.         return "profile of {}".format(self.user)
  29.  
  30. @receiver(post_save, sender=User)
  31.  
  32. def create_user_profile(sender,instance,created,**kwargs):
  33.     if created:
  34.         Profile.objects.create(user=instance)
  35.  
  36. @receiver(post_save,sender=User)
  37. def save_user_profile(sender,instance,**kwargs):
  38.     instance.profile.save()
  39.  
  40. #forms.py
  41. from django import forms
  42. from django.contrib.auth.models import User
  43. from django.contrib.auth.forms import UserCreationForm
  44. from .models import *
  45.  
  46.  
  47. class SignUpForm(forms.ModelForm):
  48.     CLG_CHOICES = (
  49.         ('MIT', 'Mahakal Institute Of Technology'),
  50.         ('MITM', 'Mahakal Institute Of Technology And Management'),
  51.     )
  52.     BRANCH_CHOICES = (
  53.         ('CS', 'Computer Science And Engineering'),
  54.         ('IT', 'Information Technology'),
  55.         ('CE', 'Civil Engineering'),
  56.         ('ME', 'Mechanical Engineering'),
  57.         ('EC', 'Electronic Engineering'),
  58.     )
  59.     college = forms.ChoiceField(choices=CLG_CHOICES)
  60.     branch = forms.ChoiceField(choices=BRANCH_CHOICES)
  61.     birth_date = forms.DateField()
  62.     password = forms.CharField(label='Password', widget=forms.PasswordInput)
  63.     password2 = forms.CharField(label='Repeat Password', widget=forms.PasswordInput)
  64.  
  65.     class Meta:
  66.         model = User
  67.         fields= ('college','branch','username','email','birth_date','password','password2')
  68.  
  69.     def clean_password2(self):
  70.         cd = self.cleaned_data
  71.         if cd['password'] != cd['password2']:
  72.             raise forms.ValidationError("password don't match.")
  73.         return cd['password2']
  74.  
  75. class UserForm(forms.ModelForm):
  76.     password = forms.CharField(label='Password', widget=forms.PasswordInput)
  77.     password2 = forms.CharField(label='Repeat Password', widget=forms.PasswordInput)
  78.     class Meta:
  79.         model = User
  80.         fields = ['username','email','password','password2']
  81.     def clean_password2(self):
  82.         cd = self.cleaned_data
  83.         if cd['password'] != cd['password2']:
  84.             raise forms.ValidationError("password don't match.")
  85.         return cd['password2']
  86.  
  87. class ProfileForm(forms.ModelForm):
  88.     """CLG_CHOICES = (
  89.        ('MIT', 'Mahakal Institute Of Technology'),
  90.        ('MITM', 'Mahakal Institute Of Technology And Management'),
  91.    )
  92.    BRANCH_CHOICES = (
  93.        ('CS', 'Computer Science And Engineering'),
  94.        ('IT', 'Information Technology'),
  95.        ('CE', 'Civil Engineering'),
  96.        ('ME', 'Mechanical Engineering'),
  97.        ('EC', 'Electronic Engineering'),
  98.    )
  99.    college = forms.ChoiceField(choices=CLG_CHOICES)
  100.    branch = forms.ChoiceField(choices=BRANCH_CHOICES)
  101.    birth_date = forms.DateField()"""
  102.     class Meta:
  103.         model = Profile
  104.         fields = ['college','branch','birth_date']
  105.  
  106. #views.py
  107.  
  108. from django.shortcuts import render,redirect
  109. from . import forms
  110. from .forms import *
  111. from django.contrib.auth import login, authenticate
  112. from django.http import HttpResponse
  113. from django.views.generic import ListView,View
  114. # Create your views here.
  115. def index(request):
  116.     return render(request,'register/index.html')
  117.  
  118.  
  119. class UserFormView(View):
  120.     form_class = UserForm
  121.     profile_class = ProfileForm
  122.     template_name = "register/signup.html"
  123.     def get(self, request):
  124.         form = self.form_class(None)
  125.         profile = self.profile_class(None)
  126.         return render(request, self.template_name, {"form": form,'profile':profile})
  127.  
  128.     def post(self, request):
  129.         form = self.form_class(request.POST)
  130.         profile = self.profile_class(request.POST)
  131.  
  132.         if form.is_valid() and profile.is_valid():
  133.             user = form.save(commit=False)
  134.             username = form.cleaned_data["username"]
  135.             password = form.cleaned_data["password"]
  136.  
  137.             if len(username) == 12:
  138.                 code = username[4]+username[5]
  139.                 branch = ["CS", "IT", "ME", "CE", "EC"]
  140.                 if code in branch:
  141.                     user.set_password(password)
  142.                     user.save()
  143.                     prof = profile.save(commit=False)
  144.                     prof.user = user
  145.                     prof.save()    #giving error, NOT NULL constraint
  146.                     user = authenticate(username= username, password= password)
  147.                     if user is not None:
  148.                         if user.is_active:
  149.                             login(request, user)
  150.                             return  redirect("register:index")
  151.         return render(request, self.template_name, {"form": form,'profile':profile})
Add Comment
Please, Sign In to add comment