Intitial teams support #147

Merged
Oleg-Komarov merged 34 commits from teams-support into main 2024-05-23 19:43:54 +02:00
Showing only changes of commit 1e755b112f - Show all commits

View File

@ -1,7 +1,7 @@
import logging import logging
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import models from django.db import models, transaction
from django.urls import reverse from django.urls import reverse
from common.model_mixins import CreatedModifiedMixin from common.model_mixins import CreatedModifiedMixin
@ -49,3 +49,22 @@ class TeamsUsers(CreatedModifiedMixin, models.Model):
@property @property
def is_manager(self) -> bool: def is_manager(self) -> bool:
return self.role == TEAM_ROLE_MANAGER return self.role == TEAM_ROLE_MANAGER
@transaction.atomic
def delete(self):
# erase extension.team field if the user was the last maintainer from the team
for extension in self.user.extensions.filter(team=self.team).all():
# assuming small datasets, not optimizing db access
authors = extension.authors.all()
has_other_authors_from_the_team = False
for author in authors:
if author == self.user:
continue
if self.team in author.teams.all():
has_other_authors_from_the_team = True
break
if not has_other_authors_from_the_team:
extension.team = None
extension.save(update_fields={'team'})
return super().delete()