Created
October 9, 2019 06:02
-
-
Save wolfsyntax/b7da3f00d0e7d821b5cd7d7e96bf58d3 to your computer and use it in GitHub Desktop.
django-snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#models.py | |
from django.db.models.signals import post_save, post_delete | |
from django.contrib.auth.models import User | |
class UserProfile(models.Model): | |
user = models.OneToOneField(User, on_delete=models.CASCADE) | |
phone = models.CharField(max_length=13) | |
gender = models.CharField(choices=GENDER, max_length=1) | |
birthdate = models.DateField(blank=True) | |
loyalty_point = models.FloatField(default=0) | |
def __str__(self): | |
return self.user.username | |
def get_total_points(self): | |
return self.loyalty_point | |
class Meta: | |
db_table = "auth_user_profile" | |
verbose_name = "Customer Account" | |
verbose_name_plural = "Customers Account" | |
def userprofile_receiver(sender, instance=None, created=False, **kwargs): | |
if created : | |
print("\n\n\n\n\n\n\n\n\nSender: {}\nInstance: {}\nCreated: {}\nkwargs: {}\n\n\n\n\n\n".format(sender, instance,created,kwargs)) | |
UserProfile.objects.create(user=instance) | |
print("\n\n\n\n\nUser has been created\n\n\n\n") | |
instance.userprofile.save() | |
post_save.connect(userprofile_receiver,sender=User) | |
# templates/../register.html | |
{% extends 'base.html' %} | |
{% load static %} | |
{% block content %} | |
<div class="container-fluid"> | |
<div class="row h-100 px-0"> | |
<div class="col-12 col-sm-10 offset-sm-1 col-lg-8 offset-lg-2 col-xl-6 offset-xl-3 my-auto" style="position: relative;"> | |
<form class="my-5 p-0" method="post"> | |
<div class="form-row my-3 py-2"> | |
<div class="col text-center"> | |
<a href="{% url 'sitemap:home' %}" title="Metrokart"> | |
<img src="" height="100px" class="d-flex justify-content-center mx-auto" alt="company logo"> | |
</a> | |
</div> | |
</div> | |
{% csrf_token %} | |
<div class = "row"> | |
<div class="col-12 col-lg-6"> | |
<div class="form-row my-0 my-md-1"> | |
<div class="col-12 col-md-6 my-1 my-md-0"> | |
<label class="py-0 my-1 d-none d-lg-inline-block col-form-label-sm">First name <strong class="text-danger">*</strong></label> | |
<input type="text" class="form-control" placeholder="First name" name="first_name" value="{% if form.first_name.value %}{{form.first_name.value}}{% endif %}" required> | |
</div> | |
<div class="col-12 col-md-6 my-1 my-md-0"> | |
<label class="py-0 my-1 d-none d-lg-inline-block col-form-label-sm">Last name <strong class="text-danger">*</strong></label> | |
<input type="text" class="form-control" placeholder="Last name" name="last_name" value="{% if form.last_name.value %}{{form.last_name.value}}{% endif %}" required> | |
</div> | |
</div> | |
<div class="form-row my-0 my-md-1 my-lg-2"> | |
<div class="col my-1 my-md-0"> | |
<label class="py-0 my-1 d-none d-lg-inline-block col-form-label-sm">Username <strong class="text-danger">*</strong></label> | |
<input type="text" class="form-control" placeholder="Username" name="username" value="{% if form.username.value %}{{form.username.value}}{% endif %}" required> | |
</div> | |
</div> | |
<div class="form-row my-0 my-md-1 my-lg-2"> | |
<div class="col my-1 my-md-0"> | |
<label class="py-0 my-1 d-none d-lg-inline-block col-form-label-sm">Email Address <strong class="text-danger">*</strong></label> | |
<input type="email" class="form-control" placeholder="Email address" name="email" value="{% if form.email.value %}{{form.email.value}}{% endif %}" required> | |
</div> | |
</div> | |
<div class="form-row my-0 my-md-1 my-lg-2"> | |
<div class="col my-1 my-md-0"> | |
<label class="py-0 my-1 d-none d-lg-inline-block col-form-label-sm">Mobile Phone <strong class="text-danger">*</strong></label> | |
<input type="text" class="form-control" placeholder="Mobile phone" name="phone" value="{% if form.phone.value %}{{form.phone.value}}{% endif %}" required> | |
</div> | |
</div> | |
</div> | |
<div class="col-12 col-lg-6"> | |
<div class="form-row my-0 my-md-1 my-lg-2"> | |
<div class="col-12 col-md-8 my-1 my-md-0"> | |
<label class="py-0 my-1 d-none d-lg-inline-block col-form-label-sm">Birthdate <strong class="text-danger">*</strong></label> | |
<input type="date" class="form-control" placeholder="Birthdate" name="birthdate" value="{% if form.birthdate.value %}{{form.birthdate.value}}{% endif %}" required> | |
</div> | |
<div class="col-12 col-md-4 my-1 my-md-0"> | |
<label class="py-0 my-1 d-none d-lg-inline-block col-form-label-sm">Gender <strong class="text-danger">*</strong></label> | |
<select name="gender" class="custom-select"> | |
<option value="">Gender</option> | |
<option value="M">Male</option> | |
<option value="F">Female</option> | |
<option value="C">Custom</option> | |
</select> | |
</div> | |
</div> | |
<div class="form-row my-0 my-md-1 my-lg-2"> | |
<div class="col-12 col-md-6 my-1 my-md-0"> | |
<label class="py-0 my-1 d-none d-lg-inline-block col-form-label-sm">Password <strong class="text-danger">*</strong></label> | |
<input type="password" class="form-control" placeholder="New password" name="password" value="" required> | |
<span class="help-tooltip sr-only sr-only-focusable">First name</span> | |
</div> | |
<div class="col-12 col-md-6 my-1 my-md-0"> | |
<label class="py-0 my-1 d-none d-lg-inline-block col-form-label-sm">Confirm Password <strong class="text-danger">*</strong></label> | |
<input type="password" class="form-control" placeholder="Confirm password" name="conf_password" value="" required> | |
</div> | |
</div> | |
<div class="form-row my-3"> | |
<div class="col help-tooltip form-text"> | |
By creating an account, you agree to our <a href="">Terms</a>, <a href="">Data Policy</a> and <a href="">Cookies Policy</a>. | |
</div> | |
</div> | |
<div class="form-row"> | |
<button type="submit" class="btn btn-primary col"> | |
Sign Up | |
</button> | |
</div> | |
<div class="form-row my-3"> | |
<div class="col help-tooltip form-text text-center"> | |
Already member? <a href="{% url 'account:login' %}" class="font-weight-bold">Login</a> | |
</div> | |
</div> | |
</div> | |
</div> | |
</form> | |
</div> | |
<div class="col d-flex justify-content-end" aria-live="polite" aria-atomic="true" style="position: relative; min-height: 200px;"> | |
<div class="" style="position: absolute; top: 10px; right: 10px; float: right;"> | |
{% if form.conf_password.errors %} | |
{% for error in form.conf_password.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">Confirm Password</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% if form.password.errors %} | |
{% for error in form.password.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">New Password</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% if form.first_name.errors %} | |
{% for error in form.first_name.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">First name</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% if form.password.errors %} | |
{% for error in password.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">Password</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% if form.last_name.errors %} | |
{% for error in form.last_name.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">Last name</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% if form.username.errors %} | |
{% for error in form.username.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">Username</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% if form.email.errors %} | |
{% for error in form.email.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">Email Address</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% if form.phone.errors %} | |
{% for error in form.phone.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">Mobile Phone</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% if form.birthdate.errors %} | |
{% for error in form.birthdate.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header bg-danger text-white"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">Birthdate</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
{% if form.gender.errors %} | |
{% for error in form.gender.errors %} | |
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="10000"> | |
<div class="toast-header bg-danger text-white"> | |
<img src="..." class="rounded mr-2" alt="..."> | |
<strong class="mr-auto">Error</strong> | |
<small class="text-muted">just now</small> | |
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
</div> | |
<div class="toast-body"> | |
{{ error }} | |
</div> | |
</div> | |
{% endfor %} | |
{% endif %} | |
</div> | |
</div> | |
</div> | |
</div> | |
{% endblock %} | |
# forms.py | |
from django import forms | |
from django.contrib.auth.models import User | |
from django.core.mail import EmailMessage, send_mail, EmailMultiAlternatives | |
from django.template.loader import get_template, render_to_string | |
from django.contrib.auth import update_session_auth_hash | |
from .models import UserProfile | |
import re # Regular Expression | |
GENDER = ( | |
('M', 'Male'), | |
('F', 'Female'), | |
('C', 'Custom'), | |
) | |
class UserRegistrationForm(forms.Form): | |
email = forms.EmailField(max_length=254, error_messages={"required": "Email is required."}) | |
username = forms.CharField(max_length=64, error_messages={"required":"Username is required."}) | |
first_name = forms.RegexField(regex="^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$", max_length=64, error_messages={"required": "First name is required."}) | |
last_name = forms.RegexField(regex="^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$", max_length=64, error_messages={"required":"Last name is required"}) | |
password = forms.CharField(max_length=30, error_messages={"required":"Password is required"}, help_text="Password must be a strong password") | |
conf_password = forms.CharField(max_length=30, error_messages={"required":"Confirm Password is required."}) | |
phone = forms.RegexField(regex=r"^(\+63|0)9[0-9]{9}$", max_length=13, error_messages={"invalid":"Phone number is invalid format","required": "Mobile Phone is required."}) | |
gender = forms.MultipleChoiceField(choices=GENDER) | |
birthdate = forms.DateField() | |
def clean(self): | |
cd = super(UserRegistrationForm, self).clean() | |
password = self.cleaned_data['password'] | |
conf_password = self.cleaned_data['conf_password'] | |
# (?=.*[a-hj-np-z])(?=.*[A-HJ-NP-Z])(?=.*(\d|!0)).{8,} | |
if not re.match('^(?=.*[a-z])(?=.*[A-Z])(?=.*[2-9])(?=.*(_|[^\w])).{8,}$', password): | |
self.add_error('password', 'Password must contains alpha-numeric and special characters.') | |
if len(password) < 8: | |
self.add_error('password', 'Password must contains at least eight (8) alpha-numeric and special characters.') | |
if password != conf_password: | |
self.add_error('conf_password', 'Confirm Password not match.') | |
return cd | |
def clean_email(self): | |
# Get the email | |
email = self.cleaned_data.get('email', '') | |
print("\n\n\nValidating Email address\n\n\n") | |
# Check to see if any users already exist with this email as a username. | |
try: | |
match = User.objects.get(email=email) | |
except User.DoesNotExist: | |
# Unable to find a user, this is fine | |
return email | |
# A user was found with this as a username, raise an error. | |
raise forms.ValidationError('This Email address is already in use.') | |
def clean_username(self): | |
# Get the email | |
uname = self.cleaned_data.get('username', '') | |
try: | |
match = User.objects.get(username=uname) | |
except User.DoesNotExist: | |
# Unable to find a user, this is fine | |
return uname | |
# A user was found with this as a username, raise an error. | |
raise forms.ValidationError('Username is already in use.') | |
def save(self, args=False): | |
cd = self.cleaned_data | |
#username = cd['email'].split("@")[0] | |
print("\n\n\n\n\n\n\n\nSaving Data:\n{}\n\n\n\n\n\n\n\n\n\n".format(cd)) | |
userdata = User.objects.create_user(username=cd['username'], email=cd['email'], password=cd['password']) | |
userdata.first_name = cd['first_name'] | |
userdata.last_name = cd['last_name'] | |
userdata.is_staff = args | |
if args: | |
userdata.is_active = False | |
userdata.save() | |
#userprofile = UserProfile.objects.create(user_id=userdata.id, phone=cd['phone'], gender=cd['gender'], birthdate=cd['birthdate']) | |
#userprofile.save() | |
# print("\n\nData successfully save!\n\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment