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

Gensim installation problems
How can I make a Python module act as a reference for another module?
Python quit unexpectedly on Mac
NLTK and Stanford parsing
How can I apply numerical gradient in Tensorflow?
How can I generate categorical synthetic samples with imblearn and SMOTE?
Writing a function to initialize classes with uncertain input?
Batch Rendering file from a python script without openeing Maya
Python hex to binary fail
Post and redirect to cross domain URL
Use Unicode text with seaborn
If Loop - Index out of range
Making a Turtle Object Look Look Further for Another Object
Facebook Bot sending reply multiple times
DNS query forwarding if local DNS is resolving
Loading flask configuration using from_object

Categories

HOME
arrays
batch-processing
meshlab
checkbox
antivirus
formal-verification
computer-vision
travis-ci
jscript
filter
devstack
sudo
keystore
android-source
gwtp
react-leaflet
jquery-terminal
scenebuilder
factor-analysis
jogl
pycrypto
distance
sql-update
sensu
fop
piwik
yosys
badge
osmdroid
rhandsontable
owl-api
vertex-buffer
datadog
spreedly
reactivemongo
honeysql
magiczoomplus
linq-to-entities
jupyter-console
rich-text-editor
prototypejs
amazon-clouddrive
timesten
ti-basic
greenhills
tofixed
cdk
rmongodb
picasa
firepath
uiprogressview
shipitjs
faraday
gradle-release-plugin
django-south
bluesnap
spservices
galaxy
anythingslider
file-diffs
kendo-menu
registrykey
risk-analysis
crash-dumps
coypu
cpu-cores
virus
line-numbers
node-inspector
late-static-binding
anjuta
dynamic-proxy
wic
modeshape
jqmobi
dig
html-form-post
client-side-scripting
adomd.net
sharpmap
soa-suite
process-monitor
attachevent
sequelpro
javascriptserializer
scrollto
getusermedia
for-xml-path
requestfactory
pci-bus
callgrind
standardized
data-execution-prevention

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