Advertisement
marcosroot

Untitled

Mar 25th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. # forms.py
  2. from django import forms
  3. from django.contrib.auth.forms import UserCreationForm
  4. from .models import User, Client, Company, Job
  5.  
  6.  
  7. class UserForm(UserCreationForm):
  8.     class Meta:
  9.         model = User
  10.         fields = ("email",)
  11.  
  12. class ClientForm(forms.ModelForm):
  13.     class Meta:
  14.         model = Client
  15.         fields = ("first_name", "last_name", "birth", "skills")
  16.  
  17.  
  18. # views.py
  19. from django.shortcuts import render, redirect
  20. from django.contrib.auth import login, authenticate
  21. from django.views import generic
  22. from .forms import UserForm, ClientForm
  23.  
  24. class ClientFormView(generic.View):
  25.  
  26.     def get(self, request, *args, **kwargs):
  27.         template_name = "users/registration/client_form.html"
  28.         context = {"form_user": UserForm, "form_client": ClientForm}
  29.         return render(request, template_name, context)
  30.  
  31.     def post(self, request, *args, **kwargs):
  32.         template_name = "users/registration/client_form.html"
  33.         context = {"form_user": UserForm, "form_client": ClientForm}
  34.  
  35.         form_user = UserForm(request.POST)
  36.         form_client = ClientForm(request.POST)
  37.  
  38.         if form_user.is_valid() and form_client.is_valid():
  39.             email = form_user.cleaned_data["email"]
  40.             password_raw = form_user.cleaned_data["password1"]
  41.             form_user.save()
  42.             instance_client = form_client.save(commit=False)
  43.  
  44.             user = authenticate(email=email, password=password_raw)
  45.             if user is not None:
  46.                 instance_client.user = user
  47.                 instance_client.save()
  48.                 login(request, user)
  49.                 return redirect('main:home')
  50.  
  51.         return render(request, template_name, context)
  52.  
  53. # client_form.html
  54. <form action="." method="post">
  55.     {% csrf_token %}
  56.     {{form_user.as_p}}
  57.     {{form_client.as_p}}
  58.     <button type="submit">SEND</button>
  59. </form>
  60.  
  61. # models.py
  62. from django.db import models
  63. from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
  64. from .managers import UserManager
  65. import tagulous
  66.  
  67. # Create your models here.
  68.  
  69. class User(AbstractBaseUser, PermissionsMixin):
  70.     email = models.EmailField(unique=True, max_length=250)
  71.     date_joined = models.DateTimeField(auto_now_add=True)
  72.  
  73.     is_staff = models.BooleanField(default=False)
  74.     is_active = models.BooleanField(default=True)
  75.  
  76.     objects = UserManager()
  77.  
  78.     USERNAME_FIELD = "email"
  79.     REQUIRED_FIELDS = []
  80.  
  81.     class Meta:
  82.         ordering = ['email']
  83.  
  84. # for clients
  85. class Client(models.Model):
  86.     user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="client")
  87.     first_name = models.CharField(max_length=30)
  88.     last_name = models.CharField(max_length=60)
  89.     birth = models.DateField()
  90.     skills = tagulous.models.TagField(force_lowercase=True)
  91.  
  92.     def get_full_name(self):
  93.         return f"{self.first_name} {self.last_name}"
  94.  
  95.     def get_short_name(self):
  96.         return self.first_name
  97.  
  98.     def __str__(self):
  99.         return f"{self.first_name} - {self.user.email}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement