python


Email conformation system-how to set submited emails to recipient_list in signal


I successfully,manually sent an email to specific email address and activated the user but I cannot figure out how to:
1 Set submitted emails into recipient_list in my post_save_activation_receiver signal,since email field itself is defined in other model(if it is possible)?
2 Make this emails that I sent more secure, because gmail is treating them like potential fishing content.
models.py
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
# Create your models here.
from .utils import code_generator
USERNAME_REGEX = '^[a-zA-Z0-9.+-]*$'
class MyUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
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):
"""
Creates and saves a superuser with the given email, date of
birth and 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
# #property
# def is_staff(self):
# "Is the user a member of staff?"
# # Simplest possible answer: All admins are staff
# return self.is_admin
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)
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
city = models.CharField(max_length=120, null=True, blank=True)
def __str__(self):
return str(self.user.username)
def __unicode__(self):
return str(self.user.username)
def post_save_user_model_receiver(sender, instance, created, *args, **kwargs):
if created:
try:
Profile.objects.create(user=instance)
ActivationProfile.objects.create(user=instance)
except:
pass
post_save.connect(post_save_user_model_receiver, sender=settings.AUTH_USER_MODEL)
forms.py
from django import forms
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.contrib.auth import authenticate, get_user_model
from django.core.validators import RegexValidator
from django.db.models import Q
from .models import USERNAME_REGEX
User = get_user_model()
class UserLoginForm(forms.Form):
query = forms.CharField(label='Username / Email')
password = forms.CharField(label='Password', widget=forms.PasswordInput)
def clean(self, *args, **kwargs):
query = self.cleaned_data.get("query")
password = self.cleaned_data.get("password")
user_qs_final = User.objects.filter(
Q(username__iexact=query)|
Q(email__iexact=query)
).distinct()
if not user_qs_final.exists() and user_qs_final.count() != 1:
raise forms.ValidationError("Invalid credentials -- user not exist")
user_obj = user_qs_final.first()
if not user_obj.check_password(password):
# log auth tries
raise forms.ValidationError("Invalid credentials -- passowrd invalid")
if not user_obj.is_active:
raise forms.ValidationError("Inactive user. Please verify your email address.")
self.cleaned_data["user_obj"] = user_obj
return super(UserLoginForm, self).clean(*args, **kwargs)
class UserCreationForm(forms.ModelForm):
"""A form for creating new users. Includes all the required
fields, plus a repeated password."""
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'email',)
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(UserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.is_active = False
# create a new user hash for activating email.
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""
password = ReadOnlyPasswordHashField()
class Meta:
model = User
fields = ('username', 'email', 'password', 'is_staff', 'is_active', 'is_admin')
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
views.py
from django.contrib.auth import login, get_user_model, logout
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render
# Create your views here.
User = get_user_model()
from .forms import UserCreationForm, UserLoginForm
from .models import ActivationProfile
def home(request):
if request.user.is_authenticated():
print(request.user.profile.city)
return render(request, "base.html", {})
def register(request, *args, **kwargs):
form = UserCreationForm(request.POST or None)
if form.is_valid():
form.save()
return HttpResponseRedirect("/login")
return render(request, "accounts/register.html", {"form": form})
def login_view(request, *args, **kwargs):
form = UserLoginForm(request.POST or None)
if form.is_valid():
user_obj = form.cleaned_data.get('user_obj')
login(request, user_obj)
return HttpResponseRedirect("/")
return render(request, "accounts/login.html", {"form": form})
def logout_view(request):
logout(request)
return HttpResponseRedirect("/login")
def activate_user_view(request, code=None, *args, **kwargs):
if code:
act_profile_qs = ActivationProfile.objects.filter(key=code)
if act_profile_qs.exists() and act_profile_qs.count() == 1:
act_obj = act_profile_qs.first()
if not act_obj.expired:
user_obj = act_obj.user
user_obj.is_active = True
user_obj.save()
act_obj.expired = True
act_obj.save()
return HttpResponseRedirect("/login")
# invalid code
return HttpResponseRedirect("/login")

Related Links

Celery concurrency configuration for io/cpu bound task
Function that takes 3 list arguments and returns all the combinations [duplicate]
how to read files with special characters in python
Using the Python Request Library to get Google Oauth2 tokens
(Beginner)Python functions Codeacademy
Python script hangs
How to shift a list nth time in Left or Right and fill it up with 0 in python
NumPy, why equality check does not work for an array of objects?
String formatting of timedeltas in Pandas
Celery - How to send task from remote machine?
YouTube Api v3 handling Exceptions in Python
Copy data from one oracle database to another with Python
converting python program into executable
How to use multidatabase across multiproject in django?
SSLHandshakeError when connecting to Google Analytics using Google APIs Client Library for Python
Pandas Pivot Table Display in ReportLab

Categories

HOME
caching
ssas-2012
freepascal
ubuntu-16.04
ruby-on-rails-3
codeeffects
chaiscript
ext.net
file-io
triggers
deeplearning4j
android-viewpager
google-shopping
spinnaker
nuxt.js
symfony2-easyadmin
line-api
jquery-terminal
bar-chart
factor-analysis
ada
jsonserializer
linq-to-sql
plupload
multiple-columns
rails-activerecord
cruisecontrol.net
ghost-inspector
croppic
classpath
solidworks
construct-2
division
phpspreadsheet
viewstate
google-guava-cache
lego
rhel.net
elfinder
dql
topic-modeling
linq-to-entities
catia
fastq
prototypejs
email-parsing
seamless-immutable
spell-checking
slidesjs
laravel-query-builder
svn-merge
keycode
sql-server-administration
ebtables
children
gitweb
multiple-file-upload
windows-nt
brython
emokit
date-format
cctray
jta
hornetq
mysqldumpslow
subresource-integrity
built-in
ipojo
wif
mogrify
bridge
cpu-cores
web-component-tester
es2015
ctrlp
viewflipper
cg
vcl
funq
ssms-addin
kraken.js
bounds
cdata
robospice
vertical-rhythm
nimrod
execvp
visual-studio-2003
apache2.2
adomd.net
magickwand
vertical-scrolling
blockingqueue
static-variables
plone-funnelweb
rose-db-object
supersized
v4l
cross-domain-policy
nsindexpath
nemerle
smooth
for-xml-path
ruby-debug
pivotal-crm
hardware-acceleration
jquery-ui-button
plinq
kpi
weborb

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App