40 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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)