Don't remove news articles when attached email is removed
authorMagnus Hagander <magnus@hagander.net>
Wed, 11 Nov 2020 14:35:44 +0000 (15:35 +0100)
committerMagnus Hagander <magnus@hagander.net>
Wed, 11 Nov 2020 14:36:51 +0000 (15:36 +0100)
At this point, we set the field to PROTECTED and dont' allow the
deletion at all. In the future we might want to allow a set null
operation, but for now we require the users to contact webmaster@ to
handle that, so we keep it under control.

pgweb/core/forms.py
pgweb/news/migrations/0006_sending_email.py
pgweb/news/models.py

index 2b45fd7919d92acecf9d7c6b3c7b48f914c3f6af..e698cdb269586eadeb78670e0b6c04e558c5eaf2 100644 (file)
@@ -88,6 +88,13 @@ class OrganisationForm(forms.ModelForm):
                 raise ValidationError("Cannot remove all managers from an organsation!")
         return self.cleaned_data['remove_manager']
 
+    def clean_remove_email(self):
+        if self.cleaned_data['remove_email']:
+            for e in self.cleaned_data['remove_email']:
+                if e.newsarticle_set.exists():
+                    raise ValidationError("Cannot remove an email address that has been used to post news articles. Please contact webmaster@postgresql.org to have this removed.")
+        return self.cleaned_data['remove_email']
+
     def save(self, commit=True):
         model = super(OrganisationForm, self).save(commit=False)
 
index b0f1de2c18c0c4d0cd15d601c1aae3c37717ffb5..57e00a487189ce185aa7934f3e90f6f5123f0646 100644 (file)
@@ -15,6 +15,6 @@ class Migration(migrations.Migration):
         migrations.AddField(
             model_name='newsarticle',
             name='email',
-            field=models.ForeignKey(blank=True, help_text='Pick a confirmed email associated with the organisation. This will be used as the reply address of posted news.', null=True, on_delete=django.db.models.deletion.CASCADE, to='core.OrganisationEmail', verbose_name='Reply email'),
+            field=models.ForeignKey(blank=True, help_text='Pick a confirmed email associated with the organisation. This will be used as the reply address of posted news.', null=True, on_delete=django.db.models.deletion.PROTECT, to='core.OrganisationEmail', verbose_name='Reply email'),
         ),
     ]
index 3b2948d9e5b2852bbe6308340f5635d7f247f460..8725f4d6b788611d6d6e56a704671e886e7ab3ab 100644 (file)
@@ -26,7 +26,7 @@ class NewsTag(models.Model):
 
 class NewsArticle(TwoModeratorsMixin, TristateModerateModel):
     org = models.ForeignKey(Organisation, null=False, blank=False, verbose_name="Organisation", help_text="If no organisations are listed, please check the <a href=\"/account/orglist/\">organisation list</a> and contact the organisation manager or <a href=\"mailto:webmaster@postgresql.org\">webmaster@postgresql.org</a> if none are listed.", on_delete=models.CASCADE)
-    email = models.ForeignKey(OrganisationEmail, null=True, blank=True, verbose_name="Reply email", help_text="Pick a confirmed email associated with the organisation. This will be used as the reply address of posted news.", on_delete=models.CASCADE)
+    email = models.ForeignKey(OrganisationEmail, null=True, blank=True, verbose_name="Reply email", help_text="Pick a confirmed email associated with the organisation. This will be used as the reply address of posted news.", on_delete=models.PROTECT)
     date = models.DateField(null=False, blank=False, default=date.today, db_index=True)
     title = models.CharField(max_length=200, null=False, blank=False)
     content = models.TextField(null=False, blank=False)