Skip to content

Commit

Permalink
Remove the project model dependency from account models
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Jul 8, 2024
1 parent 21eab09 commit efea2ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
14 changes: 14 additions & 0 deletions accounts/apps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
from django.apps import AppConfig
from django.contrib.auth import signals
from django.utils.text import capfirst
from django.utils.translation import gettext_lazy as _


def add_user_to_projects(sender, user, **kwargs):
project_model = user.projects.model
domain = user.email.rsplit("@")[-1]
for project in project_model.objects.filter(
_email_domains__icontains=domain
).exclude(users=user):
if domain in project.email_domains:
project.users.add(user)


class AccountsConfig(AppConfig):
name = "accounts"
default_auto_field = "django.db.models.AutoField"
verbose_name = capfirst(_("Authentication"))

def ready(self):
signals.user_logged_in.connect(add_user_to_projects)
15 changes: 0 additions & 15 deletions accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from authlib.base_user import BaseUser
from authlib.roles import RoleField
from django.contrib.auth import signals
from django.db import models
from django.utils.crypto import get_random_string
from django.utils.translation import gettext_lazy as _

from projects.models import Project


class User(BaseUser):
full_name = models.CharField(_("full name"), max_length=200)
Expand Down Expand Up @@ -36,15 +33,3 @@ def cycle_token(self):
self.save()

cycle_token.alters_data = True


def add_user_to_projects(sender, user, **kwargs):
domain = user.email.rsplit("@")[-1]
for project in Project.objects.filter(_email_domains__icontains=domain).exclude(
users=user
):
if domain in project.email_domains:
project.users.add(user)


signals.user_logged_in.connect(add_user_to_projects)

0 comments on commit efea2ba

Please sign in to comment.