mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-05 17:02:47 +00:00
Refactor contacts app.
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
# encoding: utf-8
|
||||||
|
from django import forms
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.forms import ModelForm
|
from django.forms import ModelForm
|
||||||
|
|
||||||
@@ -9,6 +11,32 @@ class Contact(models.Model):
|
|||||||
contact_name = models.CharField(max_length=255, blank=True, null=True)
|
contact_name = models.CharField(max_length=255, blank=True, null=True)
|
||||||
note = 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:
|
class Meta:
|
||||||
model = Contact
|
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
|
||||||
|
@@ -8,16 +8,17 @@
|
|||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
{{ form.user_email.as_hidden }}
|
{{ form.user_email.as_hidden }}
|
||||||
<label>邮箱:</label>
|
<label>邮箱:</label>
|
||||||
<input id="id_contact_email" type="text" maxlength="255" value="{{ old_contact_email }}" name="contact_email" readonly="readonly">
|
{{ form.contact_email }}
|
||||||
<label>名字(可选):</label>
|
<label>名字(可选):</label>
|
||||||
{{ form.contact_name }}
|
{{ form.contact_name }}
|
||||||
<label>备注(可选):</label>
|
<label>备注(可选):</label>
|
||||||
{{ form.note }}
|
{{ form.note }}
|
||||||
<input type="hidden" name="contact_id" value="{{ contact_id }}" />
|
<p class="error">
|
||||||
<input type="hidden" name="old_contact_email" value="{{ old_contact_email }}" />
|
{% for field in form %}
|
||||||
{% if error_msg %}
|
{{ field.errors }}
|
||||||
<p class="error">{{ error_msg }}</p>
|
{% endfor %}
|
||||||
{% endif %}
|
{{ form.non_field_errors }}
|
||||||
|
</p>
|
||||||
<input type="submit" value="提交" class="submit" class="submit" />
|
<input type="submit" value="提交" class="submit" class="submit" />
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
<ul class="with-bg">
|
<ul class="with-bg">
|
||||||
<li><a href="#" id="contact-add">添加联系人</a></li>
|
<li><a href="#" id="contact-add">添加联系人</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
<form action="{{ SITE_ROOT }}contacts/add/" method="post" id="contact-add-form" class="hide">
|
<form action="{% url contact_add_post %}" method="post" id="contact-add-form" class="hide">
|
||||||
<h4>添加联系人</h4>
|
<h4>添加联系人</h4>
|
||||||
{{ form.user_email.as_hidden }}
|
{{ form.user_email.as_hidden }}
|
||||||
<label>邮箱:</label>{{ form.contact_email }}<br />
|
<label>邮箱:</label>{{ form.contact_email }}<br />
|
||||||
@@ -69,8 +69,10 @@ $('#contact-add-form').submit(function() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var self = $(this),
|
||||||
|
url = self.attr("action");
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '{{ SITE_ROOT }}contacts/add/',
|
url: url,
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
cache: 'false',
|
cache: 'false',
|
||||||
@@ -86,9 +88,14 @@ $('#contact-add-form').submit(function() {
|
|||||||
if (data['success']) {
|
if (data['success']) {
|
||||||
location.reload(true);
|
location.reload(true);
|
||||||
} else {
|
} else {
|
||||||
$('#contact-add-error').html(data['error']).attr('class', 'error');
|
apply_form_error('contact-add-form', data['error']);
|
||||||
$('#simplemodal-container').css('height', $('#contact-add-form').height());
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
error: function(data, textStatus, jqXHR) {
|
||||||
|
var errors = $.parseJSON(data.responseText);
|
||||||
|
$.each(errors, function(index, value) {
|
||||||
|
apply_form_error('contact-add-form', value[0]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -7,6 +7,7 @@ urlpatterns = patterns('',
|
|||||||
url(r'^$', contact_list),
|
url(r'^$', contact_list),
|
||||||
url(r'^list/$', contact_list, name='contact_list'),
|
url(r'^list/$', contact_list, name='contact_list'),
|
||||||
url(r'^add/$', contact_add, name='contact_add'),
|
url(r'^add/$', contact_add, name='contact_add'),
|
||||||
|
url(r'^add/post/$', contact_add_post, name='contact_add_post'),
|
||||||
url(r'^edit/$', contact_edit, name='contact_edit'),
|
url(r'^edit/$', contact_edit, name='contact_edit'),
|
||||||
url(r'^delete/$', contact_delete, name='contact_delete'),
|
url(r'^delete/$', contact_delete, name='contact_delete'),
|
||||||
)
|
)
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
from django.http import HttpResponse, HttpResponseRedirect
|
from django.http import HttpResponse, HttpResponseBadRequest, \
|
||||||
|
HttpResponseRedirect
|
||||||
from django.shortcuts import render_to_response, Http404
|
from django.shortcuts import render_to_response, Http404
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.template import RequestContext
|
from django.template import RequestContext
|
||||||
@@ -9,15 +10,15 @@ from django.core.exceptions import ObjectDoesNotExist
|
|||||||
from django.forms.models import modelformset_factory
|
from django.forms.models import modelformset_factory
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
|
||||||
from models import Contact
|
from models import Contact, ContactAddForm, ContactEditForm
|
||||||
from models import AddContactForm
|
from utils import go_error
|
||||||
|
|
||||||
from seaserv import ccnet_rpc, ccnet_threaded_rpc
|
from seaserv import ccnet_rpc, ccnet_threaded_rpc
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def contact_list(request):
|
def contact_list(request):
|
||||||
contacts = Contact.objects.filter(user_email=request.user.username)
|
contacts = Contact.objects.filter(user_email=request.user.username)
|
||||||
form = AddContactForm({'user_email':request.user.username})
|
form = ContactAddForm({'user_email':request.user.username})
|
||||||
return render_to_response('contacts/contact_list.html', {
|
return render_to_response('contacts/contact_list.html', {
|
||||||
'contacts': contacts,
|
'contacts': contacts,
|
||||||
'form': form,
|
'form': form,
|
||||||
@@ -25,104 +26,100 @@ def contact_list(request):
|
|||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def contact_add(request):
|
def contact_add_post(request):
|
||||||
error_msg = None
|
"""
|
||||||
if request.method == 'POST':
|
Handle ajax post to add a contact.
|
||||||
form = AddContactForm(request.POST)
|
"""
|
||||||
# for request from contact_add form in group_info.html
|
|
||||||
group_id = int(request.GET.get('group_id', 0))
|
if not request.is_ajax() and not request.method == 'POST':
|
||||||
# for ajax request from contact_add in contact_list.html
|
raise Http404
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
|
content_type = 'application/json; charset=utf-8'
|
||||||
|
|
||||||
|
form = ContactAddForm(request.POST)
|
||||||
|
if form.is_valid():
|
||||||
|
contact = Contact()
|
||||||
|
contact.user_email = form.cleaned_data['user_email']
|
||||||
|
contact.contact_email = form.cleaned_data['contact_email']
|
||||||
|
contact.contact_name = form.cleaned_data['contact_name']
|
||||||
|
contact.note = form.cleaned_data['note']
|
||||||
|
contact.save()
|
||||||
|
|
||||||
|
result['success'] = True
|
||||||
|
return HttpResponse(json.dumps(result), content_type=content_type)
|
||||||
|
else:
|
||||||
|
return HttpResponseBadRequest(json.dumps(form.errors),
|
||||||
|
content_type=content_type)
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def contact_add(request):
|
||||||
|
"""
|
||||||
|
Handle normal request to add a contact.
|
||||||
|
"""
|
||||||
|
if request.method != 'POST':
|
||||||
|
raise Http404
|
||||||
|
|
||||||
|
group_id = request.GET.get('group_id', '0')
|
||||||
|
try:
|
||||||
|
group_id_int = int(group_id)
|
||||||
|
except ValueError:
|
||||||
|
return go_error('小组ID必须为整数')
|
||||||
|
|
||||||
|
form = ContactAddForm(request.POST)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
contact_email = form.cleaned_data['contact_email']
|
contact_email = form.cleaned_data['contact_email']
|
||||||
contact_name = form.cleaned_data['contact_name']
|
|
||||||
note = form.cleaned_data['note']
|
|
||||||
emailuser = ccnet_threaded_rpc.get_emailuser(contact_email)
|
|
||||||
if not emailuser:
|
|
||||||
error_msg = u"用户不存在"
|
|
||||||
elif contact_email == request.user.username:
|
|
||||||
error_msg = u"不能添加自己为联系人"
|
|
||||||
elif Contact.objects.filter(user_email=request.user.username,
|
|
||||||
contact_email=contact_email).count() > 0:
|
|
||||||
error_msg = u"联系人列表中已有该用户"
|
|
||||||
elif request.user.org and \
|
|
||||||
not ccnet_threaded_rpc.org_user_exists(request.user.org.org_id,
|
|
||||||
contact_email):
|
|
||||||
error_msg = u"当前企业不存在该用户"
|
|
||||||
else:
|
|
||||||
contact = Contact()
|
|
||||||
contact.user_email = request.user.username
|
|
||||||
contact.contact_email = contact_email
|
|
||||||
contact.contact_name = contact_name
|
|
||||||
contact.note = note
|
|
||||||
contact.save()
|
|
||||||
if not group_id:
|
|
||||||
result['success'] = True
|
|
||||||
return HttpResponse(json.dumps(result), content_type='application/json; charset=utf-8')
|
|
||||||
else:
|
|
||||||
messages.success(request, u"您已成功添加%s为联系人" % contact_email)
|
|
||||||
return HttpResponseRedirect(reverse("group_info", args=(group_id,)))
|
|
||||||
|
|
||||||
if error_msg:
|
contact = Contact()
|
||||||
if not group_id:
|
contact.user_email = form.cleaned_data['user_email']
|
||||||
result['error'] = error_msg
|
contact.contact_email = contact_email
|
||||||
return HttpResponse(json.dumps(result), content_type='application/json; charset=utf-8')
|
contact.contact_name = form.cleaned_data['contact_name']
|
||||||
|
contact.note = form.cleaned_data['note']
|
||||||
|
contact.save()
|
||||||
|
|
||||||
|
messages.success(request, u"您已成功添加 %s 为联系人" % contact_email)
|
||||||
else:
|
else:
|
||||||
messages.error(request, error_msg)
|
messages.error(request, '操作失败')
|
||||||
return HttpResponseRedirect(reverse("group_info", args=(group_id,)))
|
return HttpResponseRedirect(reverse("group_info", args=(group_id,)))
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def contact_edit(request):
|
def contact_edit(request):
|
||||||
error_msg = None
|
"""
|
||||||
contact_id = None
|
Edit contact info.
|
||||||
old_contact_email = None
|
"""
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = AddContactForm(request.POST)
|
form = ContactEditForm(request.POST)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
contact_id = request.POST.get('contact_id')
|
user_email = form.cleaned_data['user_email']
|
||||||
old_contact_email = request.POST.get('old_contact_email')
|
|
||||||
contact_email = form.cleaned_data['contact_email']
|
contact_email = form.cleaned_data['contact_email']
|
||||||
contact_name = form.cleaned_data['contact_name']
|
contact_name = form.cleaned_data['contact_name']
|
||||||
note = form.cleaned_data['note']
|
note = form.cleaned_data['note']
|
||||||
emailuser = ccnet_threaded_rpc.get_emailuser(contact_email)
|
try:
|
||||||
if not emailuser:
|
contact = Contact.objects.get(user_email=user_email,
|
||||||
error_msg = u"用户不存在"
|
contact_email=contact_email)
|
||||||
elif contact_email == request.user.username:
|
except Contact.DoesNotExist:
|
||||||
error_msg = u"不能添加自己为联系人"
|
return go_error(request, '用户不存在')
|
||||||
elif old_contact_email != contact_email and \
|
|
||||||
Contact.objects.filter(user_email=request.user.username,
|
|
||||||
contact_email=contact_email).count() > 0:
|
|
||||||
error_msg = u"联系人列表中已有该用户"
|
|
||||||
else:
|
else:
|
||||||
contact = Contact(id=contact_id)
|
|
||||||
contact.user_email = request.user.username
|
|
||||||
contact.contact_email = contact_email
|
|
||||||
contact.contact_name = contact_name
|
contact.contact_name = contact_name
|
||||||
contact.note = note
|
contact.note = note
|
||||||
contact.save()
|
contact.save()
|
||||||
return HttpResponseRedirect(reverse("contact_list"))
|
return HttpResponseRedirect(reverse('contact_list'))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
contact_email = request.GET.get('email')
|
contact_email = request.GET.get('email', '')
|
||||||
c = Contact.objects.filter(user_email=request.user.username,
|
c = Contact.objects.filter(user_email=request.user.username,
|
||||||
contact_email=contact_email)
|
contact_email=contact_email)
|
||||||
if not c:
|
if not c:
|
||||||
error_msg = u'用户不存在'
|
return go_error(request, '用户不存在')
|
||||||
form = AddContactForm({'contact_email': contact_email})
|
|
||||||
else:
|
else:
|
||||||
init_data = {'user_email':request.user.username,
|
init_data = {'user_email':request.user.username,
|
||||||
'contact_email':contact_email,
|
'contact_email':contact_email,
|
||||||
'contact_name':c.get().contact_name,
|
'contact_name':c.get().contact_name,
|
||||||
'note':c.get().note}
|
'note':c.get().note}
|
||||||
contact_id = c.get().id
|
form = ContactEditForm(init_data)
|
||||||
old_contact_email = c.get().contact_email
|
|
||||||
form = AddContactForm(init_data)
|
|
||||||
|
|
||||||
return render_to_response('contacts/contact_edit.html', {
|
return render_to_response('contacts/contact_edit.html', {
|
||||||
'form': form,
|
'form': form,
|
||||||
'error_msg': error_msg,
|
|
||||||
'contact_id': contact_id,
|
|
||||||
'old_contact_email': old_contact_email,
|
|
||||||
}, context_instance=RequestContext(request))
|
}, context_instance=RequestContext(request))
|
||||||
|
|
||||||
|
|
||||||
|
@@ -127,3 +127,11 @@ function prepareCSRFToken(xhr, settings) {
|
|||||||
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
|
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function apply_form_error(formid, error_msg) {
|
||||||
|
var form_err = $("#" + formid + " .error"),
|
||||||
|
container = $("#simplemodal-container");
|
||||||
|
|
||||||
|
form_err.html(error_msg).attr('class', 'error');
|
||||||
|
container.css('height', $('#'+formid).height());
|
||||||
|
}
|
||||||
|
@@ -62,41 +62,37 @@ $('#encrypt-switch').click(function () {
|
|||||||
$('#repo-create-form input[type="password"]').attr('disabled', true).addClass('input-disabled');
|
$('#repo-create-form input[type="password"]').attr('disabled', true).addClass('input-disabled');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
function showError(err) {
|
|
||||||
$('#repo-create-form .error').html(err).attr('class','error');
|
|
||||||
$('#simplemodal-container').css('height', $('#repo-create-form').height());
|
|
||||||
}
|
|
||||||
$('#repo-create-submit').click(function() {
|
$('#repo-create-submit').click(function() {
|
||||||
var passwd = $('#repo-create-form input[name="passwd"]'),
|
var passwd = $('#repo-create-form input[name="passwd"]'),
|
||||||
passwd_again = $('#repo-create-form input[name="passwd_again"]');
|
passwd_again = $('#repo-create-form input[name="passwd_again"]');
|
||||||
|
|
||||||
if (!$('#repo-name').val()) {
|
if (!$('#repo-name').val()) {
|
||||||
showError('目录名不能为空。');
|
apply_form_error('repo-create-form', '目录名不能为空。');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!$('#repo-desc').val()) {
|
if (!$('#repo-desc').val()) {
|
||||||
showError('描述不能为空。');
|
apply_form_error('repo-create-form', '描述不能为空。');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ($('#encrypt-switch').attr('checked')) {
|
if ($('#encrypt-switch').attr('checked')) {
|
||||||
if (!passwd.val()) {
|
if (!passwd.val()) {
|
||||||
showError('密码不能为空。');
|
apply_form_error('repo-create-form', '密码不能为空。');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!passwd_again.val()) {
|
if (!passwd_again.val()) {
|
||||||
showError('请确认密码。');
|
apply_form_error('repo-create-form', '请确认密码。');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (passwd.val().length < 3) {
|
if (passwd.val().length < 3) {
|
||||||
showError('密码太短。');
|
apply_form_error('repo-create-form', '密码太短。');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (passwd.val().length > 15) {
|
if (passwd.val().length > 15) {
|
||||||
showError('密码太长。');
|
apply_form_error('repo-create-form', '密码太长。');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (passwd.val() != passwd_again.val()) {
|
if (passwd.val() != passwd_again.val()) {
|
||||||
showError('两次输入的密码不一致。');
|
apply_form_error('repo-create-form', '两次输入的密码不一致。');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,13 +114,13 @@ $('#repo-create-submit').click(function() {
|
|||||||
if (data['success']) {
|
if (data['success']) {
|
||||||
location.reload(true);
|
location.reload(true);
|
||||||
} else {
|
} else {
|
||||||
showError(data['error']);
|
apply_form_error('repo-create-form', data['error']);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: function(data, textStatus, jqXHR) {
|
error: function(data, textStatus, jqXHR) {
|
||||||
var errors = $.parseJSON(data.responseText);
|
var errors = $.parseJSON(data.responseText);
|
||||||
$.each(errors, function(index, value) {
|
$.each(errors, function(index, value) {
|
||||||
showError(value[0]);
|
apply_form_error('repo-create-form', value[0]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user