# backend/api/v1/schedule/views.py """ ViewSets для django-scheduler (чистый, проверенный код). Параллельно работают со старым calendar_app. """ from django.contrib.contenttypes.models import ContentType from django.db.models import Q from schedule.models import ( CalendarRelation, ) def _admin_requested_all_scope(request) -> bool: user = request.user if not (user.is_staff or user.is_superuser): return False return request.query_params.get("scope") == "all" def _user_accessible_calendar_ids(user): from django.contrib.auth import get_user_model from schedule.models import Calendar from scheduling.models import CalendarBinding User = get_user_model() ct = ContentType.objects.get_for_model(User) relation_ids = CalendarRelation.objects.filter( content_type=ct, object_id=user.id, ).values_list("calendar_id", flat=True) return Calendar.objects.filter( Q(id__in=relation_ids) | Q( binding__type=CalendarBinding.BindingType.DEFAULT, binding__access_scope=CalendarBinding.AccessScope.ALL, ) ).values_list("id", flat=True)