def edit_contact(request, contact_id): try: if request.method == 'POST': form = NewContactForm(data=request.POST) if form.is_valid(): c = form.save(commit=False) c.id = contact_id c.user_id = request.user.id c.save() ch = ContactHistory() ch.user = request.user ch.action = 'edited' ch.contact = Contact.active.get(id=c.id) ch.save() messages.success(request, "contact edited") return HttpResponseRedirect(reverse('dashboard')) else: c = Contact.active.get(id=contact_id, user=request.user) form = NewContactForm(instance=c) return render_to_response('contacts/edit_contact.html', {'dashboard_active':'active', 'form':form, 'contact':c}, context_instance=RequestContext(request)) except Exception, e: messages.error(request, e.message) return render_to_response('contacts/edit_contact.html', {'dashboard_active':'active', 'form':form, 'contact':c}, context_instance=RequestContext(request))
def new_contact(request): try: if request.method == 'POST': form = NewContactForm(request.POST) if form.is_valid(): new_contact = form.save(commit=False) user_exists = User.objects.filter(email=new_contact.email).exists() if user_exists == False: from django.core.mail import send_mail Site.objects.clear_cache() body = render_to_string('accounts/mails/invite.html', {'user':request.user, 'url':'http://' + Site.objects.get_current().domain + reverse('register') }) mail = EmailMessage('Contaker Invitation', body, to=[new_contact.email]) mail.content_subtype = 'html' mail.send() new_contact.user = request.user new_contact.save() ch = ContactHistory() ch.user = request.user ch.action = 'added' ch.contact = Contact.active.get(name=request.POST['name']) ch.save() messages.success(request, "new contact added") return HttpResponseRedirect(reverse('dashboard')) else: form = NewContactForm() return render_to_response('contacts/new_contact.html', {'dashboard_active':'active', 'form':form}, context_instance=RequestContext(request)) except Exception, e: messages.error(request, e.message) return render_to_response('contacts/new_contact.html', {'dashboard_active':'active', 'form':form}, context_instance=RequestContext(request))