diff --git a/contacts/models.py b/contacts/models.py index d060de9c69..8425d70aea 100644 --- a/contacts/models.py +++ b/contacts/models.py @@ -1,3 +1,5 @@ +# encoding: utf-8 +from django import forms from django.db import models from django.forms import ModelForm @@ -9,6 +11,32 @@ class Contact(models.Model): contact_name = models.CharField(max_length=255, blank=True, null=True) note = models.CharField(max_length=255, blank=True, null=True) -class AddContactForm(ModelForm): + class Meta: + unique_together = ("user_email", "contact_email") + +class ContactAddForm(ModelForm): class Meta: model = Contact + + def clean(self): + user_email = self.cleaned_data['user_email'] + contact_email = self.cleaned_data['contact_email'] + if user_email == contact_email: + raise forms.ValidationError('不能添加自己为联系人') + elif Contact.objects.filter(user_email=user_email, + contact_email=contact_email).count() > 0: + raise forms.ValidationError('联系人列表中已有该用户') + else: + return self.cleaned_data + +class ContactEditForm(ModelForm): + class Meta: + model = Contact + + def __init__(self, *args, **kwargs): + super(ContactEditForm, self).__init__(*args, **kwargs) + self.fields['contact_email'].widget.attrs['readonly'] = True + + def clean(self): + # This is used to override unique index check + return self.cleaned_data diff --git a/contacts/templates/contacts/contact_edit.html b/contacts/templates/contacts/contact_edit.html index 868cacd443..1ec4f20d0d 100644 --- a/contacts/templates/contacts/contact_edit.html +++ b/contacts/templates/contacts/contact_edit.html @@ -8,16 +8,17 @@
diff --git a/contacts/templates/contacts/contact_list.html b/contacts/templates/contacts/contact_list.html index 858c7e5200..b3650a9fa4 100644 --- a/contacts/templates/contacts/contact_list.html +++ b/contacts/templates/contacts/contact_list.html @@ -6,7 +6,7 @@ -