django-userena: how to activate account for local development? - django

I just installed django-userena for my accounts management.
Because I'm still at the stage of basic development and I don't have a public domain name. I'm using gmail for my EMAIL_HOST for testing. The default setting from django-userena is using example.com for demo. How can I switch it to my local domain, i.e. 127.0.0.1:8000, so I can make some dummy "users" activated and test it for my other apps?
Thank you!!!
EDITED:
Because I don't have a domain name, when the user click the activation email in his email(e.g. Gmail), it's directed to http://example.com/accounts/activate/hash_as_placeholder/, so the result is that the user can't activate his account. How can I let them activate their accounts on a local domain?

Try this. Add this lines to settings.py
LOGIN_REDIRECT_URL = '/accounts/%(username)s/'
LOGIN_URL = '/accounts/signin/'
LOGOUT_URL = '/accounts/signout/'
and about email configuration, i have issues with gmail if i send a lot of mails.
So for testing purposes i have this in my settings:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' #printed in console
All emails will be printed in console.
EDITED:
Create new file initial_data.json in your project root with
[{
"pk": 1,
"model": "sites.site",
"fields": {
"name": "127.0.0.1:8000",
"domain":"127.0.0.1:8000"
}
}]
and run syncdb of course.
This will change your example.com to 127.0.0.1:8000

Related

How to send emails from django shell with AWS ses?

I have verified my domain with AWS SES and as a result my site successfully does send password verification emails (via allauth).
I would, however, also like to be able to send emails based on local scripts. To do this I have been using django shell_plus:
from django.core.mail import send_mail
send_mail("It works!", "This will get sent through anymail",
"me#mysite.com", ["me#mysite.com"])
I get output of '1' suggesting this the email has been sent successfully but I do not receive an email
I think my config is correct given that the site does successfully send emails:
EMAIL_BACKEND = "anymail.backends.amazon_ses.EmailBackend"
ANYMAIL = {
"AMAZON_SES_CLIENT_PARAMS": {
"aws_access_key_id": AWS_ACCESS_KEY_ID,
"aws_secret_access_key": AWS_SECRET_ACCESS_KEY,
"region_name": "us-east-1",
},
}
Can anyone explain what I need to do to send emails from the terminal i.e. without being directly logged into an AWS server?
Was a basic error - I had the required settings in my production config file, but not in my dev config file

django-allauth Azure Ad Configurations

I'm tyring to setup django azure ad authentication using django-allauth but currently the documentations is not having proper documentation for setting up Azure ad.
I have client_id, secret and tenant_id with me. I have tried few configurations but it seems to be not working.
Config 1:
SOCIALACCOUNT_PROVIDERS = {
'azure': {
'APP': {
'client_id': 'client_id',
'secret': 'secret',
'key': ''
}
}
}
Config 2:
SOCIALACCOUNT_PROVIDERS = {
'azure': {
'APP': {
'client_id': 'client_id',
'secret': 'secret',
'key': '',
'tenant_id': '',
}
}
}
I have experimented few more configuration but its seems to be not working
https://django-allauth.readthedocs.io/en/latest/providers.html
As you say, the documentation is lacking on this integration. I was able to get Azure SSO working with the following configuration in settings for a single tenant app. First, make sure you have all of the following declared in INSTALLED APPS:
"allauth",
"allauth.account",
"allauth.socialaccount",
"allauth.socialaccount.providers.microsoft",
Note the absence of .azure as a provider. My tests revealed that using .microsoft as the provider worked with my registered single tenant applications in Azure AD, where .azure did not, and would throw an error upon sign in. You did specify whether your app is single or multi-tenant.
Secondly, declare your configuration as follows:
SOCIALACCOUNT_PROVIDERS = {
'microsoft': {
'tenant': secrets.AZURE_AD_TENANT_ID,
'client_id': secrets.AZURE_AD_CLIENT_ID,
}
}
secrets. is just my custom secret manager, the important part is the syntax and the IDs you pass. The 'tenant' here is not your subscription tenant ID, but the tenant ID that is displayed in the Overview blade of your registered application in Azure AD. The client_id is in the same Overview area, just above "Object ID" as of this writing. Note the absence of APP: {} above. This threw me at first too. I picked up a clue from this GitHub post.
Finally, to get this to work, you must create a "Social Application" record in django-allauth's admin panel inside the Django Admin. Give the app whatever name you want, and add both the 'Client ID' and the 'Application Secret' here from the Azure AD Registered Application.

Login not working in django when using rewrites from firebase hosting to cloud run

Current Setup: I've got a Django application behind gunicorn running on Cloud Run. Since the region it is deployed in does not support Custom Domains, I have a firebase hosting setup with the following code:
{
"hosting": {
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [{
"source": "/**",
"run": {
"serviceId": "website",
"region": "ap-south1"
}
}]
}
}
The relevant settings in settings.py:
CSRF_TRUSTED_ORIGINS = ['.<domain>.com']
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
The problem: However, the login form on /admin does not work if I access the site using my domain name https://doman.com/admin even though it works fine if I use the Cloud Run endpoint https://endpoint-uw.a.run.app.
Faulty behaviour: When accessing it from my domain, the login page shows up, I enter my credentials and log in, it adds the relevant cookies to my browser but then it redirects me back to the login page.
Could it be that since the URL is being rewritten by firebase django is expecting a cookie from uw.a.run.app? I tried adding the setting SESSION_COOKIE_DOMAIN = '.<domain>.com' but that did not fix it either, it just made the Cloud Run endpoint stop working as well.
Any advice on how to fix this or how to diagnose what is going wrong would be much appreciated, thanks!
The relevant settings in settings.py:
SESSION_COOKIE_NAME = "__session"
as firebase send cookie in the name "__session"

Sending SMTP email with Django and Sendgrid on Heroku

I'm trying to send email using SMTP and sendgrid for a Django app. I'm able to send emails on my local server, but on my heroku app I get an "SMTPServerDisconnected" error saying "connection unexpectedly closed. Is there a way to send SMTP email with sendgrid once deployed to Heroku? I can't seem to find any documentation on this.
Here are my settings for email in settings.py:
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'EMAIL_HOST_USER'
EMAIL_HOST_PASSWORD = 'EMAIL_HOST_PASSWORD'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'email#email.com'
SENDGRID_API_KEY='SENDGRID_API_KEY'
SENDGRID_PASSWORD='SENDGRID_PASSWORD'
SENDGRID_USERNAME='SENDGRID_USERNAME'
Please let me know what settings you use to send SMTP email. Thanks.
You need to go into your Sendgrid dyno. Navigate to Settings>API Keys.
Click on create API Key, at the time of writing this it is a blue button in the top right corner of the page.
copy the key that they generate for you and paste it somewhere on your local machine, also navigate back to your heroku page. Navigate to your app's Settings and click on "reveal config variables". You should now see key value pairs of all the environment variables. In the Key column add "SENDGRID_API_KEY" and in the value column add the key that you copied from the Sendgrid website. At this point the following python code should work:
sg = sendgrid.SendGridAPIClient(os.environ['SENDGRID_API_KEY'])
message = Mail(from_email='example#example.com', to_emails='example#example.com',
subject='Example Subject ', html_content='<strong>and easy to do anywhere, even with Python</strong>')
response = sg.send(message)
If you keep the response variable you can wrap the code in a try except block can try to catch errors. Sorry for the formatting I am still new to posting on stack overflow.

How can I get Django on Google App Engine to automatically email server 500 errors to Admins?

When my App raises a server 500 error, I'm not receiving the automatic Django email that it should be sending: https://docs.djangoproject.com/en/1.3/howto/error-reporting/
I'm using the Google App Engine Django Helper at http://code.google.com/p/google-app-engine-django/
In my settings.py file:
DEBUG = False
ADMINS = (('Support', 'Support#******.com'),)
EMAIL_HOST = ''
SERVER_EMAIL = 'Support#******.com'
In the Google App Engine Dashboard, I've added Support#**.com (The same email in my settings.py) to the admins with the role of Viewer. I've tried changing the role to Developer.
I think the problem is this line:
EMAIL_HOST = ''
Since the Django docs say
In order to send e-mail, Django
requires a few settings telling it how
to connect to your mail server. At the
very least, you’ll need to specify
EMAIL_HOST. . .
But, the there are comments in the settings.py file that came with the google-app-engine-django project that say
# Ensure that email is not sent via SMTP by default to match the standard App
# Engine SDK behaviour. If you want to send email via SMTP then add the name of
# your mailserver here.
EMAIL_HOST = ''
Make sure you specify the 'SERVER_EMAIL' (https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-SERVER_EMAIL) in your settings. Otherwise the emails will be sent from "root#localhost" and AppEngine won't send them.