@@ -47,6 +47,18 @@ def get_count(self, choice: Choice) -> int:
4747
4848class QuestionSerializer (serializers .ModelSerializer ):
4949 id = serializers .IntegerField (required = False )
50+
51+ def __init__ (self , * args , ** kwargs ):
52+ super ().__init__ (* args , ** kwargs )
53+ if not getattr (settings , "A4_POLL_QUESTION_IMAGES" , True ):
54+ for field in [
55+ "image_base64" ,
56+ "image_url" ,
57+ "image_alt_text" ,
58+ "image_help_text" ,
59+ ]:
60+ self .fields .pop (field , None )
61+
5062 isReadOnly = serializers .SerializerMethodField (method_name = "get_is_read_only" )
5163 authenticated = serializers .SerializerMethodField ()
5264 choices = ChoiceSerializer (many = True )
@@ -72,6 +84,7 @@ class QuestionSerializer(serializers.ModelSerializer):
7284 required = False , allow_blank = True , allow_null = True , write_only = True
7385 )
7486 image_url = serializers .SerializerMethodField (method_name = "get_image_url" )
87+ image_help_text = serializers .SerializerMethodField ()
7588
7689 class Meta :
7790 model = Question
@@ -81,6 +94,8 @@ class Meta:
8194 "help_text" ,
8295 "image_base64" ,
8396 "image_url" ,
97+ "image_alt_text" ,
98+ "image_help_text" ,
8499 "multiple_choice" ,
85100 "is_open" ,
86101 "is_confidential" ,
@@ -100,6 +115,9 @@ class Meta:
100115 def get_image_url (self , question ):
101116 return question .image .url if question .image else None
102117
118+ def get_image_help_text (self , question ):
119+ return str (question ._meta .get_field ("image" ).help_text )
120+
103121 def _base64_to_image (self , base64_str ):
104122 if "base64," in base64_str :
105123 format , imgstr = base64_str .split (";base64," )
@@ -349,23 +367,30 @@ def update(self, instance, validated_data):
349367 for q_id in set (instance .questions .values_list ("id" , flat = True )) - keep_ids :
350368 self ._delete_question_with_image (q_id , instance )
351369
370+ question_images_enabled = getattr (settings , "A4_POLL_QUESTION_IMAGES" , True )
371+
352372 # Update or create questions
353373 for weight , q_data in enumerate (questions_data ):
374+ defaults = {
375+ "poll" : instance ,
376+ "label" : q_data .get ("label" , "" ),
377+ "help_text" : q_data .get ("help_text" , "" ),
378+ "multiple_choice" : q_data .get ("multiple_choice" , False ),
379+ "is_open" : q_data .get ("is_open" , False ),
380+ "is_confidential" : q_data .get ("is_confidential" , False ),
381+ "weight" : weight ,
382+ }
383+ if question_images_enabled :
384+ defaults ["image_alt_text" ] = q_data .get ("image_alt_text" , "" )
385+
354386 question , _ = Question .objects .update_or_create (
355387 id = q_data .get ("id" ),
356- defaults = {
357- "poll" : instance ,
358- "label" : q_data .get ("label" , "" ),
359- "help_text" : q_data .get ("help_text" , "" ),
360- "multiple_choice" : q_data .get ("multiple_choice" , False ),
361- "is_open" : q_data .get ("is_open" , False ),
362- "is_confidential" : q_data .get ("is_confidential" , False ),
363- "weight" : weight ,
364- },
388+ defaults = defaults ,
365389 )
366390
367- image_data = q_data .get ("image" ) or q_data .get ("image_base64" )
368- self ._handle_question_image (question , image_data )
391+ if question_images_enabled :
392+ image_data = q_data .get ("image" ) or q_data .get ("image_base64" )
393+ self ._handle_question_image (question , image_data )
369394 if not question .is_open and "choices" in q_data :
370395 self ._update_choices (q_data ["choices" ], question )
371396
0 commit comments