Esempio n. 1
0
def edit_contact_view(request, user_id, contact_id):
    if int(user_id) != request.user.pk:
        raise Http404
    try:
        contact = Contact.objects.get(pk=contact_id)
        if contact.owner.pk != request.user.pk:
            raise Http404
    except Contact.DoesNotExist:
        raise Http404
    if request.method == 'POST':
        form = ContactForm(request.POST, instance=contact)
        if form.is_valid():
            contact.name = form.cleaned_data.get('name')
            contact.email = form.cleaned_data.get('email')
            contact.phone_str = form.cleaned_data.get('phone_str')
            contact.phone_int = clean_num(form.cleaned_data.get('phone_str'))
            contact.description = form.cleaned_data.get('description')
            contact.save()
            return redirect(view_contact_view, request.user.pk, contact.pk)
        else:
            messages.add_message(request, messages.INFO, "Your form didn't validate. Please check it and try again.")
            return redirect(edit_event_view, request.user.pk, contact_id)
    else:
        contact_form = ContactForm(instance=contact)
        context = {'contact_form': contact_form, 'contact_id': contact_id}
        return render(request, 'iuvo_app/edit_contact.html', context)
Esempio n. 2
0
def create_contact_view(request, user_id):
    if int(user_id) != request.user.pk:
        raise Http404
    if request.method == 'POST':
        try:
            form = ContactForm(request.POST)
            if form.is_valid():
                contact = form.save(commit=False)
                contact.owner = request.user
                contact.name = form.cleaned_data.get('name')
                contact.email = form.cleaned_data.get('email')
                contact.phone_str = form.cleaned_data.get('phone_str')
                contact.phone_int = clean_num(
                    form.cleaned_data.get('phone_str'))
                contact.description = form.cleaned_data.get('description')
                contact.save()
                return redirect(view_contact_view, request.user.pk, contact.pk)
            else:
                return redirect(create_contact_view, request.user.pk)
        except:
            messages.add_message(request, messages.INFO, "Your form didn't validate. Please check it and try again.")
            return redirect(create_contact_view, request.user.pk)
    else:
        context = {'contact_form': ContactForm()}
        return render(request, 'iuvo_app/create_contact.html', context)