~netlandish/django-wiki

56ed42152b929de4e45886332690e112ce623955 — Benjamin Bach 4 years ago a3a0744
Ensure that there is always 1 default Settings object
2 files changed, 20 insertions(+), 3 deletions(-)

M src/wiki/plugins/notifications/forms.py
M src/wiki/plugins/notifications/views.py
M src/wiki/plugins/notifications/forms.py => src/wiki/plugins/notifications/forms.py +19 -3
@@ 32,13 32,17 @@ class ArticleSubscriptionModelMultipleChoiceField(
class SettingsModelForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super().__init__(*args, **kwargs)
        instance = kwargs.get('instance', None)
        self.__editing_instance = False
        if instance:
            self.__editing_instance = True
            self.fields['delete_subscriptions'] = ArticleSubscriptionModelMultipleChoiceField(
                models.ArticleSubscription.objects.filter(subscription__settings=instance),
                models.ArticleSubscription.objects.filter(
                    subscription__settings=instance,
                    article__current_revision__deleted=False,
                ),
                label=gettext("Remove subscriptions"),
                required=False,
                help_text=gettext("Select article subscriptions to remove from notifications"),


@@ 58,7 62,8 @@ class SettingsModelForm(forms.ModelForm):
            )

    def save(self, *args, **kwargs):
        instance = super().save(*args, **kwargs)
        instance = super().save(*args, commit=False, **kwargs)
        instance.user = self.user
        if self.__editing_instance:
            self.cleaned_data['delete_subscriptions'].delete()
            if self.cleaned_data['email'] == 1:


@@ 69,6 74,7 @@ class SettingsModelForm(forms.ModelForm):
                instance.subscription_set.all().update(
                    send_emails=True,
                )
        instance.save()
        return instance




@@ 76,6 82,16 @@ class BaseSettingsFormSet(BaseModelFormSet):

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')

        # Ensure that at least 1 default settings object exists
        all_settings = Settings.objects.filter(user=self.user).order_by('is_default')
        if not all_settings.exists():
            Settings.objects.create(user=self.user, is_default=True)
        else:
            to_update = all_settings.first()
            if not to_update.is_default:
                to_update.is_default = True
                to_update.save()
        super().__init__(*args, **kwargs)

    def get_queryset(self):


@@ 84,7 100,7 @@ class BaseSettingsFormSet(BaseModelFormSet):
            subscription__articlesubscription__article__current_revision__deleted=False,
        ).prefetch_related(
            'subscription_set__articlesubscription',
        ).distinct()
        ).order_by('is_default').distinct()


SettingsFormSet = modelformset_factory(

M src/wiki/plugins/notifications/views.py => src/wiki/plugins/notifications/views.py +1 -0
@@ 42,6 42,7 @@ class NotificationSettings(FormView):
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['user'] = self.request.user
        kwargs['form_kwargs'] = {'user': self.request.user}
        return kwargs

    def get_context_data(self, **kwargs):