From a18bebe7bd1c228708a9c0582dd200076d2007e0 Mon Sep 17 00:00:00 2001 From: Jim Schaad Date: Sat, 9 Feb 2013 18:09:33 +0000 Subject: [PATCH] Make sure that the same rule cannot be entered twice in a community list. As part of this fix, you need to do the following: 1. Eliminate duplicates currently in the data base. This statement can be used to identify them. SELECT e1.id id1, e2.id id2, e1.community_list_id, e1.rule_type, e1.value FROM ietf_utf8.community_rule e1 JOIN ietf_utf8.community_rule e2 ON (e1.id != e2.id AND e1.community_list_id=e2.community_list_id AND e1.rule_type=e2.rule_type AND e1.value=e2.value); 2. Delete any current duplicate lines in the text. This uses the following commands as of the check-in DELETE FROM TABLE ietf_utf8.community_rule WHERE id=19 OR id=91 OR id=177 3. Add a constraint to the table so that it will enforce the rule itself ALTER TABLE ietf_utf8.community_rule ADD CONSTRAINT pc_CommunityRule UNIQUE (community_list_id, rule_type, value) - Legacy-Id: 5426 --- ietf/community/models.py | 3 +++ ietf/community/views.py | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ietf/community/models.py b/ietf/community/models.py index 399ebf3f9..5b8b0cfff 100644 --- a/ietf/community/models.py +++ b/ietf/community/models.py @@ -126,6 +126,9 @@ class Rule(models.Model): value = models.CharField( max_length=255) + class Meta: + unique_together= ("community_list", "rule_type", "value") + last_updated = models.DateTimeField( auto_now=True) diff --git a/ietf/community/views.py b/ietf/community/views.py index f24c1d8c3..78a01bb21 100644 --- a/ietf/community/views.py +++ b/ietf/community/views.py @@ -3,6 +3,7 @@ import uuid import datetime import hashlib from datetime import timedelta +from django.db import IntegrityError from django.conf import settings from django.contrib.auth import REDIRECT_FIELD_NAME @@ -25,7 +26,10 @@ def _manage_list(request, clist): rule_form = RuleForm(request.POST, clist=clist) display_form = DisplayForm(instance=display_config) if rule_form.is_valid(): - rule_form.save() + try: + rule_form.save() + except IntegrityError: + pass; rule_form = RuleForm(clist=clist) display_form = DisplayForm(instance=display_config) elif request.method == 'POST' and request.POST.get('save_display', None):