flask-user registration: add one more mail id - python

I am using flask-user package for user registration. Once the user clicks on register, by default it will send an email to the users' email. I want to add admin email along with that. Is it possible to customize it?
I found this but I am not sure how to proceed with that.
Any help would be great.

After taking help of my colleague, I figured out that this can be done by listening to 'signals' sent by the application. For example:
#user_registered.connect_via(app)
def after_user_registered(sender, user, **kwargs):
msg = Message(subject="A new user registered",
body='Some message',
recipients=["admin#example.com"])
mail.send(msg)
PS: This I wanted to implement so that admin can get an email whenever a new user is registered. Hope this helps someone :)

Related

How can I update the email address using the confirmation code in Django?

I use all the functionalities of dj-rest-auth to register, log in, confirm the email address, change the password, reset the password and many more. Unfortunately, the library does not support changing the email address. I would like the authenticated user to first enter the account password and the new email address. After the successful process of authentication, I would like to send the user a specially generated confirmation code. Only when he enters it, the old email address will be changed to the new one. As far as I know, there is no such functionality in dj-rest-auth. Unfortunately, I also have not found any current solutions or libraries for this purpose anywhere. Did anyone have such a problem and could share his solution here? Thank you in advance.
Though i don't have any solution for what you want accurately but here is a replace.
You can use django all-auth and some email backend to send an email to the new added email to confirm the new email. In the sent email, there will be a confirmation link and the user has to click that to confirm the new email.
After using django all-auth you only have to add an email backend which will help in sending email. Rest will be maintained by all-auth.
e.g,
In your settings.py you can add an SMTP email backend to send email from your selected gmail account.
Add these lines of code to your settings.py;
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = DEFAULT_FROM_EMAIL = 'HOST_EMAIL' #HOST_EMAIL is your email from which you want to send email to the user.
EMAIL_HOST_PASSWORD = 'HOST_PASSWORD' #HOST_PASSWORD is the password of the email you are using as HOST_EMAIL
But after doing all these things, to make it work locally, you need to go to your google account which you are using as HOST_EMAIL. Go to manage google account >> security >> Turn on less secure apps. Then you will be able to send email to the user.
NOTE: If you have 2-factor authentication turned on for your google account, then these steps will not work. That type of account has some different setup.

How do I check authentication across all views in Django using Pyrebase?

Okay so, ordinary Django allows you to simply:
if request.user.is_authenticated:
I want to be able to do the same in Pyrebase. Have the views sort of already know which user has logged in based on the current session without having to sign the user in in all views.
I have tried:
def sign_in(request):
user = firebase.auth().sign_in_with_email_and_password('email', 'password')
user_token = firebase.auth().refresh(user['refreshToken']
request.session['session_id'] = user_token
I noticed this creates a session ID for me. But I don't know how to associate it with the current user and I know it has something to do with the refresh token.
If I don't check authentication, anyone can visit any page of my site without signing in.

Django Send emails to all users in the database table

I have started django building my first app tutorials, i have to send email to all my users store in the database table on some special Ocations. i have searched on google and found many apis but found it very hard to configure with my app.
here is my model.py
class Users(models.Model):
UserID = models.IntegerField(verbose_name='User ID',max_length=255,primary_key=True)
UserName = models.CharField(verbose_name='User Name',max_length=254,null=True,blank=True)
Email = models.EmailField(verbose_name='Email',max_length=254,null=True,blank=True)
Phone = models.CharField(verbose_name='Phone Number',max_length=254,null=True,blank=True)
i want to have a function here which should get all users one-by-one and send email also tells the status weather the email has been sent or not.
battery's answer is ok, but i would do this way:
recievers = []
for user in Users.objects.all():
recievers.append(user.email)
send_mail(subject, message, from_email, recievers)
this way, you will open only once connection to mail server rather than opening for each email.
Sending email is very simple.
For your Users model this would be:
for user in Users.objects.all():
send_mail(subject, message, from_email,
user.Email)
Checkout Django's Documentation on send emails for more details.
Would be useful if you mention what problem you are facing if you've tried this.
Also note, IntegerField does not have a max_length argument.
for user in Users.objects.all():
send_mail(subject, message, from_email,
user.Email)
This is the best solutions and it works well

Django reset password not catching invalid email ids

I'm using Django's built in reset password views with custom templates. Although it works successfully, it does not show an error message if the email id submitted is not already registered in the site.
I followed the instructions here to help me reach till this point.
#urls.py
url(r'^users/password/reset/$',
'django.contrib.auth.views.password_reset',
{'post_reset_redirect' : '/users/password/reset/done/'},
name="reset_password"
),
url(r'^users/password/reset/done/$',
'django.contrib.auth.views.password_reset_done'
),
url(r'^users/password/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
'django.contrib.auth.views.password_reset_confirm',
{'post_reset_redirect' : '/users/password/done/'}
),
url(r'^users/password/done/$',
'django.contrib.auth.views.password_reset_complete'
),
Whatever email id I give, Django always redirects me to /users/password/reset/done/ which says:
We've e-mailed you instructions for setting your password to the e-mail address
you submitted.
You should be receiving it shortly.
This is misleading, since users might've entered their email address wrong and still be seeing the above page.
How do I show an error message if the email address is not registered?
Thanks in advance.
The easy way to implement that is to use django-allauth. Works without any extra setting, leaving the message: The e-mail address is not assigned to any user account.
I have not worked with raw users in a while, but if you absolutely need that you could try to create your own view and verify manually that the email is in the server (if it is you can call the original function). You may need to extend the template or the form to redirect the action to your own view.

Django-Registration: How to prevent logged in user from registering?

I've just started to use django-registration. I have two questions:
How do you prevent a logged in user from going to the register page?
How do you automatically sign in a user after activation?
I prefer not changing any code in the app itself.
For question 2, I've already read the docs where it says to write "a function which listens for the appropriate signal; your function should set the backend attribute of the user to the correct authentication backend, and then call django.contrib.auth.login() to log the user in." I don't know django well enough to understand what this means or how to implement this. Could you guys help/point me in the right direction?
Edit:
Tried doing some signals, does not yet work, not sure what's wrong:
def loginActivationCallback(sender, user, request, **kwargs):
print user
print "registered"
user_registered.connect(loginActivationCallback)
Also because I'm using Django 1.5, I didn't do pip install django-registration(does not fully support 1.5), but instead copied the registration folder into my project. Not sure if this affects the signals.
Simply what you can do is check in your register view
if request.user.is_authenticated:
#redirect user to the profile page
return HttpResponseRedirect('/profile/')
from registration.signals import user_activated
def login_user(sender, user, request, **kwargs):
user.backend='django.contrib.auth.backends.ModelBackend'
login(request,user)
user_activated.connect(login_user)

Categories

Resources