Commit b64a2adf authored by Thijs de Jong's avatar Thijs de Jong
Browse files

Merge branch 'feature/nextcloud-current-board-group' into 'master'

Add special automatic NextCloud group with current board members

See merge request !1129
parents 17728666 d7ca6e55
import datetime
from django.contrib.auth.models import Permission
from django.db.models import Q
from django.utils import timezone
......@@ -8,8 +10,9 @@ from activemembers.api.serializers import (
NextCloudMemberSerializer,
NextCloudGroupSerializer
)
from activemembers.models import MemberGroupMembership, MemberGroup
from activemembers.models import MemberGroupMembership, MemberGroup, Board
from members.models import Member
from utils.snippets import datetime_to_lectureyear
class NextCloudUsersView(ListAPIView):
......@@ -34,15 +37,17 @@ class NextCloudUsersView(ListAPIView):
class NextCloudGroupsView(ListAPIView):
permission_classes = [NextCloudPermission]
queryset = MemberGroup.objects.exclude(
name_en='admin', name_nl='admin').all()
queryset = (MemberGroup.objects
.exclude(name_en='admin')
.exclude(active=False)
.all())
serializer_class = NextCloudGroupSerializer
def list(self, request, *args, **kwargs):
response = super().list(request, *args, **kwargs)
perm = Permission.objects.get(content_type__app_label='auth',
codename='nextcloud_admin')
users = Member.current_members.filter(
admin_users = Member.current_members.filter(
Q(is_superuser=True) |
Q(groups__permissions=perm) |
Q(user_permissions=perm) |
......@@ -51,12 +56,24 @@ class NextCloudGroupsView(ListAPIView):
Q(membergroupmembership__until__gte=timezone.now())))
).distinct().values_list('username', flat=True)
if users.count() > 0:
response.data = list(response.data) + [
{
'pk': -1,
'name': 'admin',
'members': users
}
]
current_year = datetime_to_lectureyear(datetime.date.today())
try:
board = Board.objects.get(
since__year=current_year, until__year=current_year + 1)
board_users = board.members.values_list('username', flat=True)
except Board.DoesNotExist:
board_users = []
response.data = list(response.data) + [
{
'pk': -1,
'name': 'admin',
'members': admin_users
},
{
'pk': -2,
'name': 'Board',
'members': board_users
}
]
return response
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment