11from django import forms
22from django .db .models import Count , Exists , OuterRef , Q
3+ from django .utils .functional import cached_property
34from django .utils .timezone import now
45from django .utils .translation import gettext_lazy as _
56from django_scopes .forms import SafeModelChoiceField
67
78from pretalx .cfp .forms .cfp import CfPFormMixin
89from pretalx .common .forms .fields import ImageField
910from pretalx .common .forms .widgets import MarkdownWidget
10- from pretalx .common .mixins .forms import PublicContent , RequestRequire
11+ from pretalx .common .mixins .forms import PublicContent , RequestRequire , QuestionFieldsMixin
1112from pretalx .common .mixins .views import Filterable
1213from pretalx .submission .forms .track_select_widget import TrackSelectWidget
13- from pretalx .submission .models import Answer , Question , Submission , SubmissionStates
14+ from pretalx .submission .models import Answer , Question , Submission , SubmissionStates , QuestionTarget , QuestionVariant
1415
1516
16- class InfoForm (CfPFormMixin , RequestRequire , PublicContent , forms .ModelForm ):
17+ class InfoForm (CfPFormMixin , RequestRequire , PublicContent , QuestionFieldsMixin , forms .ModelForm ):
1718 additional_speaker = forms .EmailField (
1819 label = _ ("Additional Speaker" ),
1920 help_text = _ (
@@ -30,6 +31,13 @@ class InfoForm(CfPFormMixin, RequestRequire, PublicContent, forms.ModelForm):
3031
3132 def __init__ (self , event , ** kwargs ):
3233 self .event = event
34+ self .submission = kwargs .pop ("submission" , None )
35+ self .track = kwargs .pop ("track" , None ) or getattr (
36+ self .submission , "track" , None
37+ )
38+ self .submission_type = kwargs .pop ("submission_type" , None ) or getattr (
39+ self .submission , "submission_type" , None
40+ )
3341 self .readonly = kwargs .pop ("readonly" , False )
3442 self .access_code = kwargs .pop ("access_code" , None )
3543 self .default_values = {}
@@ -60,6 +68,55 @@ def __init__(self, event, **kwargs):
6068 for f in self .fields .values ():
6169 f .disabled = True
6270
71+ self .target_type = kwargs .pop ("target" , QuestionTarget .SUBMISSION )
72+ self .for_reviewers = kwargs .pop ("for_reviewers" , False )
73+ if self .target_type == QuestionTarget .SUBMISSION :
74+ target_object = self .submission
75+ elif self .target_type == QuestionTarget .SPEAKER :
76+ target_object = self .speaker
77+
78+ self .queryset = Question .all_objects .filter (event = self .event , active = True )
79+ if self .target_type :
80+ self .queryset = self .queryset .filter (target = self .target_type )
81+ else :
82+ self .queryset = self .queryset .exclude (target = QuestionTarget .REVIEWER )
83+ if self .track :
84+ self .queryset = self .queryset .filter (
85+ Q (tracks__in = [self .track ]) | Q (tracks__isnull = True )
86+ )
87+ if self .submission_type :
88+ self .queryset = self .queryset .filter (
89+ Q (submission_types__in = [self .submission_type ])
90+ | Q (submission_types__isnull = True )
91+ )
92+
93+ for question in self .queryset .prefetch_related ("options" ):
94+ initial_object = None
95+ initial = question .default_answer
96+ if target_object :
97+ answers = [
98+ a
99+ for a in target_object .answers .all ()
100+ if a .question_id == question .id
101+ ]
102+ if answers :
103+ initial_object = answers [0 ]
104+ initial = (
105+ answers [0 ].answer_file
106+ if question .variant == QuestionVariant .FILE
107+ else answers [0 ].answer
108+ )
109+
110+ field = self .get_field (
111+ question = question ,
112+ initial = initial ,
113+ initial_object = initial_object ,
114+ readonly = self .readonly ,
115+ )
116+ field .question = question
117+ field .answer = initial_object
118+ self .fields [f"question_{ question .pk } " ] = field
119+
63120 def _set_track (self , instance = None ):
64121 if "track" in self .fields :
65122 if (
@@ -160,6 +217,14 @@ def _set_slot_count(self, instance=None):
160217 )
161218 )
162219
220+ @cached_property
221+ def submission_fields (self ):
222+ return [
223+ forms .BoundField (self , field , name )
224+ for name , field in self .fields .items ()
225+ if hasattr (field , "question" ) and field .question .target == QuestionTarget .SUBMISSION
226+ ]
227+
163228 def save (self , * args , ** kwargs ):
164229 for key , value in self .default_values .items ():
165230 setattr (self .instance , key , value )
0 commit comments