Add user to group automatically after registration in Wagtail - django

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

Related

Django initialize table with post_migrate not working

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

Trigger an email to admin when new user registers in a Django app

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.

Assign default group to new user Django

I'm trying to assign a group to every new user registered into the system. I've already read something about it in another questions but I don't really know where to add the necessary code to make it work.
I'm using Django 2.1.3 and I'm logging users using allauth (social login, but it shouldn't make any difference as a new instance in the User table is created)
You can use a #post_save signal for example that, each time a User is created, adds the given group to the groups of the User. Typically signals reside in a file named handlers.py in the signals directory of an app, so you probably should create or modify the files listed in boldface:
app/
signals/
__init__.py
handlers.py
__init__.py
apps.py
...
# app/signals/handlers.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from django.contrib.auth.models import Group
#receiver(post_save, sender=settings.AUTH_USER_MODEL)
def save_profile(sender, instance, created, **kwargs):
if created:
g1 = Group.objects.get(name='group_name')
instance.groups.add(g1)
where group_name is the name of the group you want to add.
You should then import the handlers.py module in your MyAppConfig (create one if you do not have constructed such config yet):
# app/apps.py
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'app'
verbose_name = "My app"
def ready(self):
import app.signals.handlers
and register the MyAppConfig in the __init__.py of the app:
# app/__init__.py
default_app_config = 'app.apps.MyAppConfig'
If this should happen for any new User instance, you can connect a handler to the post_save signal:
from django.db.models.signals import post_save
from django.dispatch import receiver
#receiver(post_save, sender=User)
def handle_new_job(sender, **kwargs):
if kwargs.get('created', False):
user = kwargs.get('instance')
g = Group.objects.get(name='whatever')
user.groups.add(g)
Include this code in your app and make sure it is imported as stated e.g. here.

How do I hook a django-allauth signal?

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)

Django starting id field from 1000

I would like to start my ids on a django model from 1000. I've found this response on Stackoverflow but I am missing something in my implementation because it is not working.
This is my code in apps.py
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.db import IntegrityError
from invoice.models import Invoice
def my_callback(sender, **kwargs):
if sender.name =="invoice":
try:
Invoice.objects.create(id=999)
Invoice.objects.delete()
except IntegrityError:
pass
class InvoiceConfig(AppConfig):
name = 'invoice'
def ready(self):
post_migrate.connect(my_callback, sender=self)
I've then ensure migrate takes place but the model continues to increment from low numbers. What am I missing?