Created
May 30, 2017 05:57
-
-
Save 63phc/f77456819d5f90cb722ddc5693010be7 to your computer and use it in GitHub Desktop.
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
from django.conf import settings | |
from django.contrib.auth.models import ( | |
BaseUserManager, AbstractBaseUser | |
) | |
from django.contrib import messages | |
from django.core.mail import send_mail | |
from django.conf import settings | |
from django.core.validators import RegexValidator | |
from django.db import models | |
from django.db.models.signals import post_save | |
from .utils import code_generator | |
USERNAME_REGEX = '^[a-zA-Z0-9.+-]*$' | |
class MyUserManager(BaseUserManager): | |
def create_user(self, username, email, password=None): | |
if not email: | |
raise ValueError('Users must have an email address') | |
user = self.model( | |
username = username, | |
email=self.normalize_email(email), | |
) | |
user.set_password(password) | |
user.save(using=self._db) | |
return user | |
def create_superuser(self, username, email, password): | |
user = self.create_user( | |
username, | |
email, | |
password=password, | |
) | |
user.is_admin = True | |
user.is_staff = True | |
user.save(using=self._db) | |
return user | |
class MyUser(AbstractBaseUser): | |
username = models.CharField( | |
max_length=255, | |
validators=[ | |
RegexValidator( | |
regex = USERNAME_REGEX, | |
message = 'Username must be Alpahnumeric or contain any of the following: ". @ + -" ', | |
code='invalid_username' | |
)], | |
unique=True,) | |
email = models.EmailField( | |
verbose_name='email address', | |
max_length=255, | |
unique=True, ) | |
zipcode = models.CharField(max_length=120, default="92660") | |
is_active = models.BooleanField(default=True) | |
is_staff = models.BooleanField(default=False) | |
is_admin = models.BooleanField(default=False) | |
objects = MyUserManager() | |
USERNAME_FIELD = 'username' | |
REQUIRED_FIELDS = ['email'] | |
def get_full_name(self): | |
# The user is identified by their email address | |
return self.email | |
def get_short_name(self): | |
# The user is identified by their email address | |
return self.email | |
def __str__(self): # __unicode__ on Python 2 | |
return self.email | |
def has_perm(self, perm, obj=None): | |
"Does the user have a specific permission?" | |
# Simplest possible answer: Yes, always | |
return True | |
def has_module_perms(self, app_label): | |
"Does the user have permissions to view the app `app_label`?" | |
# Simplest possible answer: Yes, always | |
return True | |
class ActivationProfile(models.Model): | |
user = models.ForeignKey(settings.AUTH_USER_MODEL) | |
key = models.CharField(max_length=120) | |
expired = models.BooleanField(default=False) | |
def save(self, *args, **kwargs): | |
self.key = code_generator() | |
super(ActivationProfile, self).save(*args, **kwargs) | |
def post_save_activation_receiver(sender, instance, created, *args, **kwargs): | |
if created: | |
#send email | |
subject = 'Registration' | |
message = "http://127.0.0.1:8000/activate/{0}".format(instance.key) | |
from_email = settings.EMAIL_HOST_USER | |
recipient_list = ['UserEmail'] | |
print(recipient_list) | |
send_mail(subject, message, from_email, recipient_list,fail_silently=True) | |
post_save.connect(post_save_activation_receiver, sender=ActivationProfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment