def remove_contact(request, username=None, medium=None, id=None): ''' delete a piece of contact info. ''' context = RequestContext(request) MediumModel = models.__getattribute__(medium.capitalize()) medium_obj = get_object_or_404(MediumModel, id=id) if request.method == 'POST': if 'cancel' in request.POST: return HttpResponseRedirect(reverse('member', args=[username])) medium_obj.delete() return HttpResponseRedirect(reverse('member', args=[username])) user = get_object_or_404(User, username=username) context['this_user'] = user context['contact'] = medium_obj context['medium'] = medium template = get_template('membership/remove_contact.html') return HttpResponse(template.render(context))
def contact_form(request, username=None, medium=None, id=0): ''' sub-page of member edit page. ? ''' context = RequestContext(request) referer = request.META.get('HTTP_REFERER', '') user = get_object_or_404(User, username=username) # medium may be 'address', 'phone', or 'email' MediumForm = forms.__getattribute__(medium.capitalize() + 'Form') MediumModel = models.__getattribute__(medium.capitalize()) if id: medium_obj = get_object_or_404(MediumModel, id=id) else: medium_obj = MediumModel() context['add'] = True if request.method == 'POST': if 'cancel' in request.POST: return HttpResponseRedirect(reverse('member', args=[username])) form = MediumForm(request.POST, instance=medium_obj) referer = request.POST.get('referer') if form.is_valid(): instance = form.save(commit=False) instance.member = user.get_profile() instance.save() if referer: return HttpResponseRedirect(referer) else: return HttpResponseRedirect(reverse('member', args=[username])) else: form = MediumForm(instance=medium_obj) context['form'] = form context['medium'] = medium context['referer'] = referer # use 'this_user' because context['user'] overrides logged-in user context['this_user'] = user return render_to_response('membership/contact_form.html', context)