Signals page for django-allauth: https://django-allauth.readthedocs.io/en/latest/signals.html
I am trying to hook the email_verified signal:
allauth.account.signals.email_confirmed(request, email_address)
And am getting nothing whenever a user confirms their email. Here is my code:
from allauth.account.signals import email_confirmed
from channels import Group
def send_to_user_socket(sender, **kwargs):
Group('%s.get-started' % (kwargs['request'].user.username)).send({'text': json.dumps({'message': 'Email confirmed'})})
email_confirmed.connect(send_to_user_socket)
What am I doing wrong? Thanks in advance.
Edit: here is my apps.py code:
from django.apps import AppConfig
class EngineConfig(AppConfig):
name = 'engine'
def ready(self):
import engine.signals
I had a similar issue, but it was resolved by doing the following
In your init.py file within the particular app, add
default_app_config = "{enter_you_app_name}.apps.{Appname}Config"
eg
default_app_config = "authentication.apps.AuthenticationConfig"
also import
from django.dispatch import receiver
#reciever(enter_the_correct_details)
def send_to_user_socket(sender, **kwargs):
Group('%s.get-started' % (kwargs['request'].user.username)).send({'text': json.dumps({'message': 'Email confirmed'})})
email_confirmed.connect(send_to_user_socket)
Related
I want to initialize the database table with some predefined instances.
# apps.py
from django.apps import AppConfig
from django.db.models.signals import post_migrate
def initialize(sender, **kwargs):
from .models import Address
Address.objects.create(
# address fields
)
print('Created')
class BackendConfig(AppConfig):
name = 'backend'
def ready(self):
print('Ready')
post_migrate.connect(initialize, sender=self)
However nothing was created and nothing was printed after migration like the signal not triggered at all.
Sorry I am dumbass.
Need to mention default_app_config = 'backend.apps.BackendConfig' in __init__.py
I am using django-simple-history to save history of data. I want to save an extra field value to each history model before it is saved. I found the reference code in documentation mentioned above but cant use it. Please help.
from django.dispatch import receiver
from simple_history.signals import (
pre_create_historical_record,
post_create_historical_record
)
#receiver(pre_create_historical_record)
def pre_create_historical_record_callback(sender, **kwargs):
print("Sent before saving historical record")
#receiver(post_create_historical_record)
def post_create_historical_record_callback(sender, **kwargs):
print("Sent after saving historical record")
apps.py file
from django.apps import AppConfig
class LogAppConfig(AppConfig):
name = 'log_app'
def ready(self):
import log_app.signals
signals.py file
from simple_history.signals import (pre_create_historical_record, post_create_historical_record)
#receiver(pre_create_historical_record)
def pre_create_historical_record_callback(sender, **kwargs):
print("signal is running")
history_instance = kwargs['history_instance']
I made this before with django like this:
signals/handlers.py
from django.dispatch import receiver
from django.conf import settings
from django.contrib.auth.models import Group
from users.models import *
#receiver(post_save, sender=settings.AUTH_USER_MODEL)
def save_profile(sender, instance, created, **kwargs):
if created:
g1 = Group.objects.get(name='Editors')
instance.groups.add(g1)
apps.py
from django.apps import AppConfig
class RegistrationConfig(AppConfig):
name = 'registration'
def ready(self):
import registration.signals.handlers
but I don't know how to make it with wagtail !
thanks.
Instead of using Django signals, Wagtail has hooks that simplify this for you. You can also send password reset email etc... after creating user using the same technique.
Just create a wagtail_hooks.py in your app:
from django.contrib.auth.models import Group
from wagtail.core import hooks
#hooks.register('after_create_user')
def add_user_to_group(request, user):
if user:
group, created = Group.objects.get_or_create(name='Group Name')
user.groups.add(group)
Docs: https://docs.wagtail.io/en/latest/reference/hooks.html?highlight=after_create_user#id40
I'd like to trigger a simple plaintext email to admin(s) when a new user registers for my Django app. What's the cleanest way to do this?
You can use a post save signal for this. For example:
# <app>/signals.py:
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.mail import send_mail
#receiver(post_save, sender=User)
def send_email_to_admin(sender, instance, created, **kwargs):
if created:
send_mail(
'<Subject>User {} has been created'.format(instance.username),
'<Body>A new user has been created',
'from#example.com',
['admin#example.com'],
fail_silently=False,
)
# <app>/apps.py
from django.apps import AppConfig
class YourAppConfig(AppConfig):
name = 'app_name'
verbose_name = _('app_name')
def ready(self):
import .signals # noqa
Here, I am using django's mail sending functionality as example, if you are using that then please make sure you have properly configured the settings.
I am trying to create a project for creating feeds/activity feeds of a user with the help of a blog.
This is the signals.py:
from django.db.models import signals
from django.contrib.contenttypes.models import ContentType
from django.dispatch import dispatcher
from blogs.models import Blog
from picture.models import Photo
from models import StreamItem
def create_stream_item(sender, instance, signal, *args, **kwargs):
# Check to see if the object was just created for the first time
if 'created' in kwargs:
if kwargs['created']:
create = True
# Get the instance's content type
ctype = ContentType.object.get_for_model(instance)
if create:
si = StreamItem.objects.get_or_create(content_type=ctype, object_id=instance.id, pub_date = instance.pub_date)
# Send a signal on post_save for each of these models
for modelname in [Blog, Photo]:
dispatcher.connect(create_stream_item, signal=signals.post_save, sender=modelname)
When I try to run the server, it gives me an error:
AttributeError: 'module' object has no attribute 'connect'
Please help me out. Would be much appreciate. Thank you.
replace
from django.dispatch import dispatcher -> from django.dispatch import Signal
dispatcher.connect -> Signal.connect
dispatcher is module, interpreter tell it for you.