Intitial teams support #147
@ -69,6 +69,7 @@ class TeamsUsers(CreatedModifiedMixin, models.Model):
|
|||||||
|
|
||||||
return super().delete()
|
return super().delete()
|
||||||
|
|
||||||
|
@property
|
||||||
def may_leave(self) -> bool:
|
def may_leave(self) -> bool:
|
||||||
nr_of_managers = TeamsUsers.objects.filter(role=TEAM_ROLE_MANAGER, team=self.team).count()
|
nr_of_managers = TeamsUsers.objects.filter(role=TEAM_ROLE_MANAGER, team=self.team).count()
|
||||||
user_is_manager = (
|
user_is_manager = (
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
<h2>
|
<h2>
|
||||||
{% blocktranslate with team_name=object.name %}Leave team {{ team_name }}?{% endblocktranslate %}
|
{% blocktranslate with team_name=object.name %}Leave team {{ team_name }}?{% endblocktranslate %}
|
||||||
</h2>
|
</h2>
|
||||||
|
{% if may_leave %}
|
||||||
<p>
|
<p>
|
||||||
{% if will_lose_access_to %}
|
{% if will_lose_access_to %}
|
||||||
{% blocktranslate %}
|
{% blocktranslate %}
|
||||||
@ -32,6 +33,11 @@
|
|||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
{% else %}
|
||||||
|
<p>
|
||||||
|
{% trans 'You cannot leave this team because you are the only manager.' %}
|
||||||
|
</p>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
33
teams/tests/test_leave.py
Normal file
33
teams/tests/test_leave.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
|
||||||
|
from common.tests.factories.teams import TeamFactory
|
||||||
|
from common.tests.factories.users import UserFactory
|
||||||
|
from constants.base import TEAM_ROLE_MANAGER
|
||||||
|
from teams.models import TeamsUsers
|
||||||
|
|
||||||
|
|
||||||
|
class TeamLeaveTest(TestCase):
|
||||||
|
def test_the_only_manager_cant_leave(self):
|
||||||
|
team = TeamFactory(slug='test-team')
|
||||||
|
user = UserFactory()
|
||||||
|
TeamsUsers(team=team, user=user, role=TEAM_ROLE_MANAGER).save()
|
||||||
|
self.assertEqual(user.teams.count(), 1)
|
||||||
|
|
||||||
|
self.client.force_login(user)
|
||||||
|
response = self.client.get(reverse('teams:leave-team', args=[team.slug]))
|
||||||
|
self.assertContains(response, 'cannot leave')
|
||||||
|
self.client.post(reverse('teams:leave-team', args=[team.slug]))
|
||||||
|
user.refresh_from_db()
|
||||||
|
self.assertEqual(user.teams.count(), 1)
|
||||||
|
|
||||||
|
# create another manager
|
||||||
|
user2 = UserFactory()
|
||||||
|
TeamsUsers(team=team, user=user2, role=TEAM_ROLE_MANAGER).save()
|
||||||
|
# try to leave again
|
||||||
|
response = self.client.get(reverse('teams:leave-team', args=[team.slug]))
|
||||||
|
self.assertNotContains(response, 'cannot leave')
|
||||||
|
self.client.post(reverse('teams:leave-team', args=[team.slug]))
|
||||||
|
user.refresh_from_db()
|
||||||
|
self.assertEqual(user.teams.count(), 0)
|
@ -26,12 +26,15 @@ class LeaveTeamView(LoginRequiredMixin, DetailView):
|
|||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
team = self.get_object()
|
team = self.get_object()
|
||||||
team_user = TeamsUsers.objects.filter(team=team, user=self.request.user).first()
|
team_user = TeamsUsers.objects.filter(team=team, user=self.request.user).first()
|
||||||
if team_user:
|
if team_user and team_user.may_leave:
|
||||||
team_user.delete()
|
team_user.delete()
|
||||||
return redirect('teams:list')
|
return redirect('teams:list')
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
|
team = self.get_object()
|
||||||
|
team_user = TeamsUsers.objects.filter(team=team, user=self.request.user).first()
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
|
context['may_leave'] = team_user.may_leave
|
||||||
context['will_lose_access_to'] = list(
|
context['will_lose_access_to'] = list(
|
||||||
Extension.objects.authored_by(self.request.user)
|
Extension.objects.authored_by(self.request.user)
|
||||||
.exclude(maintainer__user_id=self.request.user.pk)
|
.exclude(maintainer__user_id=self.request.user.pk)
|
||||||
|
Loading…
Reference in New Issue
Block a user