CookieCutter adding Users to a Group with Permissions

Hello, I am new to Django and New to CookieCutter.

I spent all day, easily 8 hours on User Permissions. I wanted to go over what I found and if there is a better way of going about this. A more Django or Pythonic way!


I learned:
How permissions work in Django,
What type I might need. Group or is it unique to a user.
How to check the {{ perm }} tag to limit certain things in the template from showing.
How the PermissionsRequiredMixin work.
How to get the Group to display in the admin.
How to make every User apart of the User Group with “Signals post_save”

To start, Cookiecutter uses the Django.AllAuth which handles all the account code like saving and logging in and logging out. I was under the impression I should never really touch that code. So I used the “signal post_save” strategy instead. And it works like a charm.


How I went about it.

Manually created the group in the Admin and added all the permissions I wanted for these “Customers”.


Admin

I added the Group Name to the admin by adding a method to the UserAdmin Class

class UserAdmin(auth_admin.UserAdmin):
    def group(self, user):
        groups = []
        for group in user.groups.all():
            groups.append(group.name)
        return ' '.join(groups)
        group.short_description = 'Groups'

Then add the group to the list_display array

list_display = ["username", "name", "is_superuser", 'group']


Template

To limit the use view of things on the page once the user was in “Customers”
Note: {{ perms }} is automatically added to the page. Thank you Django.

{% if perms.recipes.delete_recipes %} show button {% endif %} 

View

At the VIEW level I set the PermissionsRequiredMixin on the DeleteView

class RecipesDeleteView(PermissionRequiredMixin, DeleteView):
        raise_exception = True
        permission_required = 'recipes.delete_recipes'
        login_url = '403.html'
        def post(self,request,pk):

If someone access this View without the right permissions. They are sent automatically to the 403.html. in the templates folder of cookiecutter.


Signals.py

This is how i made Every User apart of the group with the signal post_save
First create a users/signals.py file

from django.contrib.auth.models import Group

def add_customer_user(sender, instance, created, **kwargs):
    if created:
        g = Group.objects.get(name='Customers')
        g.user_set.add(instance)

apps.py

Now go inside the users/apps.py and add this at the top

from django.db.models.signals import post_save

inside the UsersConfig(AppConfig) use the READY method

def ready(self):
        from .models import User
        from .signals import add_customer_user
        post_save.connect(add_customer_user, sender=User)

If you import the models outside the ready. The APP is not Ready. Get it. That took me a while.


Well, this is what i learned today. I hope this helps others. And please if there is a better way to attack this with cookiecutter. Please let me know. I always want to expand my Django Knowledge.

Thank you all.