forked from beba/foerderbarometer
				
			Refactor CheckForm to use model field 'terms_accepted' instead of separate 'check' field
This commit is contained in:
		
							parent
							
								
									0b9fb801bd
								
							
						
					
					
						commit
						6698d6a6f3
					
				| 
						 | 
					@ -156,38 +156,16 @@ class CheckForm(FdbForm):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, *args, **kwargs):
 | 
					    def __init__(self, *args, **kwargs):
 | 
				
			||||||
        super().__init__(*args, **kwargs)
 | 
					        super().__init__(*args, **kwargs)
 | 
				
			||||||
        self.fields['check'] = BooleanField(
 | 
					
 | 
				
			||||||
            required=True,
 | 
					        # Check if the model field 'terms_accepted' is present
 | 
				
			||||||
            label=format_html(
 | 
					        if 'terms_accepted' in self.fields:
 | 
				
			||||||
 | 
					            # Make the field required (HTML5 validation)
 | 
				
			||||||
 | 
					            self.fields['terms_accepted'].required = True
 | 
				
			||||||
 | 
					            # Set custom label with link to terms
 | 
				
			||||||
 | 
					            self.fields['terms_accepted'].label = format_html(
 | 
				
			||||||
                "Ich stimme den <a href='{}'>Nutzungsbedingungen</a> zu",
 | 
					                "Ich stimme den <a href='{}'>Nutzungsbedingungen</a> zu",
 | 
				
			||||||
                self.termstoaccept
 | 
					                self.termstoaccept
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def save(self, commit=True):
 | 
					 | 
				
			||||||
        """Save the form instance and map the 'check' field to the model's 'terms_accepted' field, saving to the database if commit=True."""
 | 
					 | 
				
			||||||
        # Call the parent form's save() method with commit=False
 | 
					 | 
				
			||||||
        # to get a model instance without saving it to the database yet.
 | 
					 | 
				
			||||||
        instance = super().save(commit=False)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # If the model has a "terms_accepted" field,
 | 
					 | 
				
			||||||
        # copy the value from the form field "check" into it.
 | 
					 | 
				
			||||||
        if hasattr(instance, "terms_accepted"):
 | 
					 | 
				
			||||||
            instance.terms_accepted = bool(self.cleaned_data.get('check'))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # If commit=True (default), save the instance to the database now.
 | 
					 | 
				
			||||||
        if commit:
 | 
					 | 
				
			||||||
            instance.save()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Return the model instance (saved if commit=True, otherwise unsaved).
 | 
					 | 
				
			||||||
        return instance
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    """Baseclass for all classes which need a check for Nutzungsbedingungen"""
 | 
					 | 
				
			||||||
#    def __init__(self, *args, **kwargs):
 | 
					 | 
				
			||||||
#    check = BooleanField(required=True,
 | 
					 | 
				
			||||||
#                        label=format_html("Ich stimme den <a href='{}'>Nutzungsbedingungen</a> zu",
 | 
					 | 
				
			||||||
#                        termstoaccept))
 | 
					 | 
				
			||||||
#                         NUTZUNGSBEDINGUNGEN))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class LiteratureForm(CheckForm):
 | 
					class LiteratureForm(CheckForm):
 | 
				
			||||||
| 
						 | 
					@ -198,7 +176,7 @@ class LiteratureForm(CheckForm):
 | 
				
			||||||
        self.fields['selfbuy_give_data'].required = True
 | 
					        self.fields['selfbuy_give_data'].required = True
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        model = Literature
 | 
					        model = Literature
 | 
				
			||||||
        fields = ['cost', 'info', 'source', 'notes', 'selfbuy', 'selfbuy_data', 'selfbuy_give_data']
 | 
					        fields = ['cost', 'info', 'source', 'notes', 'selfbuy', 'selfbuy_data', 'selfbuy_give_data', 'terms_accepted']
 | 
				
			||||||
        exclude = ['intern_notes', 'survey_mail_send', 'mail_state']
 | 
					        exclude = ['intern_notes', 'survey_mail_send', 'mail_state']
 | 
				
			||||||
    class Media:
 | 
					    class Media:
 | 
				
			||||||
        js = ('dropdown/js/literature.js',)
 | 
					        js = ('dropdown/js/literature.js',)
 | 
				
			||||||
| 
						 | 
					@ -225,7 +203,7 @@ class EmailForm(CheckForm):
 | 
				
			||||||
    # TODO: add some javascript to show/hide other-field
 | 
					    # TODO: add some javascript to show/hide other-field
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        model = Email
 | 
					        model = Email
 | 
				
			||||||
        fields = ['domain', 'address', 'other', 'adult']
 | 
					        fields = ['domain', 'address', 'other', 'adult', 'terms_accepted']
 | 
				
			||||||
        exclude = ['intern_notes', 'survey_mail_send', 'mail_state']
 | 
					        exclude = ['intern_notes', 'survey_mail_send', 'mail_state']
 | 
				
			||||||
    class Media:
 | 
					    class Media:
 | 
				
			||||||
        js = ('dropdown/js/mail.js',)
 | 
					        js = ('dropdown/js/mail.js',)
 | 
				
			||||||
| 
						 | 
					@ -243,7 +221,7 @@ class BusinessCardForm(CheckForm):
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        model = BusinessCard
 | 
					        model = BusinessCard
 | 
				
			||||||
        exclude = ['intern_notes', 'survey_mail_send', 'mail_state']
 | 
					        exclude = ['intern_notes', 'survey_mail_send', 'mail_state']
 | 
				
			||||||
        fields = ['project', 'data', 'variant', 'url_of_pic', 'send_data_to_print', 'sent_to']
 | 
					        fields = ['project', 'data', 'variant', 'url_of_pic', 'send_data_to_print', 'sent_to', 'terms_accepted']
 | 
				
			||||||
    class Media:
 | 
					    class Media:
 | 
				
			||||||
        js = ('dropdown/js/businessCard.js',)
 | 
					        js = ('dropdown/js/businessCard.js',)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -252,5 +230,5 @@ class ListForm(CheckForm):
 | 
				
			||||||
    termstoaccept = settings.NUTZUNGSBEDINGUNGEN_MAILINGLISTEN
 | 
					    termstoaccept = settings.NUTZUNGSBEDINGUNGEN_MAILINGLISTEN
 | 
				
			||||||
    class Meta:
 | 
					    class Meta:
 | 
				
			||||||
        model = List
 | 
					        model = List
 | 
				
			||||||
        fields = ['domain', 'address']
 | 
					        fields = ['domain', 'address', 'terms_accepted']
 | 
				
			||||||
        exclude = ['intern_notes', 'survey_mail_send','mail_state']
 | 
					        exclude = ['intern_notes', 'survey_mail_send','mail_state']
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue