def edit_view(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('login'))

    person = Person.objects.get(pk=1)

    if request.method == 'POST':
        form = PersonForm(request.POST, request.FILES)
        if form.is_valid():
            person.name = form.cleaned_data['name']
            person.last_name = form.cleaned_data['last_name']
            person.date_of_birth = form.cleaned_data['date_of_birth']
            person.email = form.cleaned_data['email']
            person.jabber = form.cleaned_data['jabber']
            person.skype = form.cleaned_data['skype']
            person.other_contacts = form.cleaned_data['other_contacts']
            person.bio = form.cleaned_data['bio']

            try:
                if request.FILES['photo'].content_type == 'image/jpeg':
                    person.photo.save(request.FILES['photo'].name,
                                      request.FILES['photo'],
                                      save=False)
            except:
                pass

            person.save()

            if request.is_ajax():
                return HttpResponse("Changes have been saved")
            else:
                return HttpResponseRedirect(reverse('home'))
        else:
            if request.is_ajax():
                return HttpResponse("Error: form is not valid")
    else:
        data = {'name': person.name,
                'last_name': person.last_name,
                'date_of_birth': person.date_of_birth,
                'email': person.email,
                'jabber': person.jabber,
                'skype': person.skype,
                'other_contacts': person.other_contacts,
                'bio': person.bio,}
        form = PersonForm(data)

    return render_to_response('hello/edit.html',
                              {'form': form, 'photo': person.photo,},
                              context_instance=RequestContext(request))
Example #2
0
def edit(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('login'))

    if request.method == 'POST':
        form = PersonForm(request.POST, request.FILES)
        person = Person.objects.get(id=1)
        ajax_response = "Data Error"
        if form.is_valid():
            person.name = form.cleaned_data['name']
            person.surname = form.cleaned_data['lastname']
            person.birth_date = form.cleaned_data['birth_date']
            person.bio = form.cleaned_data['bio']
            person.email = form.cleaned_data['email']
            person.jid = form.cleaned_data['jid']
            person.skype = form.cleaned_data['skype']
            person.other_contacts = form.cleaned_data['other_contacts']

            person.photo.save(request.FILES['photo'].name,
                              request.FILES['photo'],
                              save=False)

            person.save()
            ajax_response = "Changes have been saved"
        if request.is_ajax():
            return HttpResponse(ajax_response)
    else:
        # form = PersonForm()
        person = Person.objects.get(id=1)
        data = {'name': person.name,
                'lastname': person.surname,
                'birth_date': person.birth_date,
                'bio': person.bio,
                'email': person.email,
                'jid': person.jid,
                'skype': person.skype,
                'other_contacts': person.other_contacts}
        file_data = {'photo': person.photo}
        form = PersonForm(data, file_data)

    return render_to_response('hello/edit.html',
                              {'form': form, 'person': person},
                              context_instance=RequestContext(request))
Example #3
0
def edit(request):
    person = get_object_or_404(Person, id=settings.MY_ID)
    if request.method == 'POST':
        form = PersonForm(request.POST, request.FILES)
        if form.is_valid():
            form = PersonForm(request.POST, request.FILES, instance=person)
            form.save()
            clear_images_folder(person.photo)
            response_data = {'result': 'Changes have been saved'}
            return HttpResponse(simplejson.dumps(response_data),
                                content_type="application/json")
        else:
            response_data = {'result': 'Error',
                             'errors': dict(form.errors.items())}
            return HttpResponse(simplejson.dumps(response_data),
                                content_type="application/json")
    else:
        form = PersonForm(instance=person)
    return render(request, 'hello/edit.html', {
        'form': form,
        'person': person
    })