2012-05-04 08:30:07 +00:00
|
|
|
# encoding: utf-8
|
2012-07-20 08:23:59 +00:00
|
|
|
import simplejson as json
|
2012-07-30 11:47:11 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseBadRequest, \
|
|
|
|
HttpResponseRedirect
|
2012-05-04 08:30:07 +00:00
|
|
|
from django.shortcuts import render_to_response, Http404
|
2012-02-11 03:12:54 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.template import RequestContext
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2012-05-04 08:30:07 +00:00
|
|
|
from django.forms.models import modelformset_factory
|
2012-07-12 03:42:21 +00:00
|
|
|
from django.contrib import messages
|
2012-02-11 03:12:54 +00:00
|
|
|
|
2012-07-30 11:47:11 +00:00
|
|
|
from models import Contact, ContactAddForm, ContactEditForm
|
2012-07-30 12:44:32 +00:00
|
|
|
from utils import render_error
|
2012-05-04 08:30:07 +00:00
|
|
|
|
2012-06-25 12:42:19 +00:00
|
|
|
from seaserv import ccnet_rpc, ccnet_threaded_rpc
|
2012-09-06 03:07:04 +00:00
|
|
|
from seahub.settings import SITE_ROOT
|
2012-02-11 03:12:54 +00:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def contact_list(request):
|
2012-05-04 08:30:07 +00:00
|
|
|
contacts = Contact.objects.filter(user_email=request.user.username)
|
2012-07-30 11:47:11 +00:00
|
|
|
form = ContactAddForm({'user_email':request.user.username})
|
2012-08-02 04:01:22 +00:00
|
|
|
edit_init_data = {'user_email':request.user.username,
|
|
|
|
'contact_email':'',
|
|
|
|
'contact_name':'',
|
|
|
|
'note':''}
|
|
|
|
edit_form = ContactEditForm(edit_init_data)
|
|
|
|
|
2012-02-11 03:12:54 +00:00
|
|
|
return render_to_response('contacts/contact_list.html', {
|
|
|
|
'contacts': contacts,
|
2012-07-20 08:23:59 +00:00
|
|
|
'form': form,
|
2012-08-02 04:01:22 +00:00
|
|
|
'edit_form': edit_form,
|
2012-02-11 03:12:54 +00:00
|
|
|
}, context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
2012-07-30 11:47:11 +00:00
|
|
|
def contact_add_post(request):
|
|
|
|
"""
|
|
|
|
Handle ajax post to add a contact.
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
form = ContactAddForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
contact_email = form.cleaned_data['contact_email']
|
|
|
|
|
|
|
|
contact = Contact()
|
|
|
|
contact.user_email = form.cleaned_data['user_email']
|
|
|
|
contact.contact_email = contact_email
|
|
|
|
contact.contact_name = form.cleaned_data['contact_name']
|
|
|
|
contact.note = form.cleaned_data['note']
|
|
|
|
contact.save()
|
|
|
|
|
|
|
|
messages.success(request, u"您已成功添加 %s 为联系人" % contact_email)
|
|
|
|
else:
|
|
|
|
messages.error(request, '操作失败')
|
2012-09-06 02:46:28 +00:00
|
|
|
|
|
|
|
referer = request.META.get('HTTP_REFERER', None)
|
|
|
|
if not referer:
|
|
|
|
referer = SITE_ROOT
|
|
|
|
return HttpResponseRedirect(referer)
|
2012-02-11 03:12:54 +00:00
|
|
|
|
2012-05-04 08:30:07 +00:00
|
|
|
@login_required
|
|
|
|
def contact_edit(request):
|
2012-07-30 11:47:11 +00:00
|
|
|
"""
|
2012-08-02 04:01:22 +00:00
|
|
|
Ajax post to edit contact info.
|
2012-07-30 11:47:11 +00:00
|
|
|
"""
|
2012-08-02 04:01:22 +00:00
|
|
|
result = {}
|
|
|
|
content_type = 'application/json; charset=utf-8'
|
|
|
|
form = ContactEditForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
user_email = form.cleaned_data['user_email']
|
|
|
|
contact_email = form.cleaned_data['contact_email']
|
|
|
|
contact_name = form.cleaned_data['contact_name']
|
|
|
|
note = form.cleaned_data['note']
|
|
|
|
contact = Contact.objects.get(user_email=user_email, contact_email=contact_email)
|
|
|
|
contact.contact_name = contact_name
|
|
|
|
contact.note = note
|
|
|
|
contact.save()
|
|
|
|
result['success'] = True
|
|
|
|
return HttpResponse(json.dumps(result), content_type=content_type)
|
2012-05-04 08:30:07 +00:00
|
|
|
else:
|
2012-08-02 04:01:22 +00:00
|
|
|
return HttpResponseBadRequest(json.dumps(form.errors),
|
|
|
|
content_type=content_type)
|
|
|
|
|
2012-05-04 08:30:07 +00:00
|
|
|
|
2012-02-11 03:12:54 +00:00
|
|
|
@login_required
|
|
|
|
def contact_delete(request):
|
2012-05-04 08:30:07 +00:00
|
|
|
user_email = request.user.username
|
|
|
|
contact_email = request.GET.get('email')
|
|
|
|
|
|
|
|
Contact.objects.filter(user_email=user_email, contact_email=contact_email).delete()
|
|
|
|
|
|
|
|
return HttpResponseRedirect(reverse("contact_list"))
|