2012-07-30 11:47:11 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
from django import forms
|
2012-02-11 03:12:54 +00:00
|
|
|
from django.db import models
|
2012-05-04 08:30:07 +00:00
|
|
|
from django.forms import ModelForm
|
2012-02-11 03:12:54 +00:00
|
|
|
|
|
|
|
class Contact(models.Model):
|
|
|
|
"""Record user's contacts."""
|
|
|
|
|
2012-05-04 08:30:07 +00:00
|
|
|
user_email = models.CharField(max_length=255)
|
|
|
|
contact_email = models.CharField(max_length=255)
|
|
|
|
contact_name = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
note = models.CharField(max_length=255, blank=True, null=True)
|
|
|
|
|
2012-07-30 11:47:11 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("user_email", "contact_email")
|
|
|
|
|
|
|
|
class ContactAddForm(ModelForm):
|
2012-05-04 08:30:07 +00:00
|
|
|
class Meta:
|
|
|
|
model = Contact
|
2012-07-30 11:47:11 +00:00
|
|
|
|
|
|
|
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
|