66from django .db import models , transaction
77from django .db .models import (
88 BooleanField ,
9+ Case ,
910 CharField ,
1011 DateTimeField ,
1112 ForeignKey ,
1617 Q ,
1718 SlugField ,
1819 TextField ,
20+ When ,
1921)
2022from django .db .models .functions import Coalesce
2123from django .utils import formats
@@ -49,9 +51,10 @@ def get_queryset(self):
4951
5052class EventType (Model ):
5153 class ShowParticipantDataChoices (models .IntegerChoices ):
52- EVERYONE = 0 , _ ("to everyone " )
54+ INSTANCE_USERS = 0 , _ ("to logged in users " )
5355 CONFIRMED = 1 , _ ("to confirmed participants" )
5456 RESPONSIBLES = 2 , _ ("only to responsible users" )
57+ PUBLIC = 3 , _ ("to everyone including guests and federated users" )
5558
5659 title = CharField (_ ("title" ), max_length = 254 )
5760 color = CharField (_ ("color" ), max_length = 7 , default = "#343a40" )
@@ -61,7 +64,7 @@ class ShowParticipantDataChoices(models.IntegerChoices):
6164 "If you restrict who can see participant data, others will only be able to see that there is a participation, but not from whom."
6265 ),
6366 choices = ShowParticipantDataChoices .choices ,
64- default = ShowParticipantDataChoices .EVERYONE ,
67+ default = ShowParticipantDataChoices .INSTANCE_USERS ,
6568 )
6669
6770 class Meta :
@@ -149,9 +152,31 @@ def activate(self):
149152
150153class ParticipationQuerySet (PolymorphicQuerySet ):
151154
152- def viewable_by (self , user ):
153- if user .is_anonymous :
154- return self .none ()
155+ def with_show_participant_data_to (self , participant ):
156+ return self .annotate (
157+ show_participant_data = Case (
158+ When (
159+ id__in = self .viewable_by (participant = participant ),
160+ then = True ,
161+ ),
162+ default = False ,
163+ output_field = BooleanField (),
164+ ),
165+ )
166+
167+ def viewable_by (self , participant ):
168+ from ephios .core .signup .participants import LocalUserParticipant
169+
170+ if not isinstance (participant , LocalUserParticipant ):
171+ qs = self .filter (
172+ Q (
173+ shift__event__type__show_participant_data = EventType .ShowParticipantDataChoices .PUBLIC
174+ )
175+ | Q (id__in = participant .all_participations ())
176+ )
177+ return qs .distinct ()
178+
179+ user = getattr (participant , "user" )
155180 viewable_events = get_objects_for_user (user , "core.view_event" )
156181 viewable_userprofiles = get_objects_for_user (user , "core.view_userprofile" )
157182 editable_events = get_objects_for_user (user , "core.change_event" )
@@ -162,7 +187,10 @@ def viewable_by(self, user):
162187 )
163188 qs = self .filter (shift__event__in = viewable_events ).filter (
164189 Q (
165- shift__event__type__show_participant_data = EventType .ShowParticipantDataChoices .EVERYONE
190+ shift__event__type__show_participant_data = EventType .ShowParticipantDataChoices .INSTANCE_USERS
191+ )
192+ | Q (
193+ shift__event__type__show_participant_data = EventType .ShowParticipantDataChoices .PUBLIC
166194 )
167195 | Q (
168196 shift__event__type__show_participant_data = EventType .ShowParticipantDataChoices .CONFIRMED ,
0 commit comments