Example #1
0
def editContact(request, farm_id, contact_id):
    farm = get_object_or_404(Farm, pk=farm_id)
    memo = request.user.profile.member_organization
    permitted = request.user.has_perm('farms.uniauth')
    if memo not in farm.member_organization.all() and not permitted:
        return HttpResponseRedirect(reverse('farms:index'))
    contact = get_object_or_404(Contact, pk=contact_id)
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            new_save = form.save(commit=False)
            new_save.id = contact_id
            new_save.farm = farm
            new_save.save()
            return HttpResponseRedirect(
                reverse('farms:detailfarm', args=(farm_id,)))
        else:
            return render(
                request, 'farms/edit_contact.html',
                {'form': form, 'farm': farm, 'error': 'Form Had an Error'})
    else:
        form = ContactForm(instance=contact)
        return render(
            request, 'farms/edit_contact.html',
            {'form': form, 'farm': farm, "object": contact})
Example #2
0
def newContact(request, farm_id):
    farm = get_object_or_404(Farm, pk=farm_id)
    memo = request.user.profile.member_organization
    permitted = request.user.has_perm('farms.uniauth')
    if memo not in farm.member_organization.all() and not permitted:
        return HttpResponseRedirect(reverse('farms:index'))
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            new_contact = form.save()
            new_contact.farm = farm
            new_contact.save()
            if request.POST['action'] == 'Save':
                return HttpResponseRedirect(
                    reverse('farms:detailfarm', args=(farm_id,)))
            else:
                form = ContactForm()
                notice = 'Contact {0}, {1} has been saved'.format(
                    new_contact.first_name, new_contact.last_name)
                return render(
                    request, 'farms/new_contact.html',
                    {'form': form, 'farm': farm, 'notice': notice})

        else:
            return render(
                request, 'farms/new_contact.html',
                {'form': form,
                 'farm': farm,
                 'error': 'Form was incorrectly filled out'}
            )
    else:
        form = ContactForm()
        return render(
            request, 'farms/new_contact.html',
            {'form': form, 'farm': farm, 'notice': ''})