Esempio n. 1
0
def update_entry(request, entry_id):
    """
    Give the user the ability to update their closed entries.  If this method
    is invoked via a GET request, the user is presented with a form that is
    populated with the entry's original data.  If this method is invoked via a
    POST request, the data the user entered in the form will be validated.  If
    the data are valid, the entry will be updated accordingly.  If the data are
    invalid, the user is presented with the form again, either until they abort
    or enter valid data.
    """

    try:
        # retrieve the log entry
        entry = Entry.objects.get(pk=entry_id,
                                  user=request.user,
                                  end_time__isnull=False)
    except:
        # entry does not exist
        messages.add_message(request, messages.ERROR, 'No such log entry.')
        return HttpResponseRedirect(reverse('pendulum-entries'))

    if request.method == 'POST':
        # populate the form with the updated data
        form = AddUpdateEntryForm(request.POST, instance=entry)

        # validate the form data
        if form.is_valid():
            # the data are valid... save them
            form.save()

            # create a message for the user
            messages.add_message(request, messages.INFO, 'The entry has been updated successfully.')

            # redirect them to the log entry list
            return HttpResponseRedirect(reverse('pendulum-entries'))
        else:
            # create an error message
            messages.add_message(request, messages.ERROR, 'Please fix the errors below.')
    else:
        # populate the form with the original entry information
        form = AddUpdateEntryForm(instance=entry)

    return render_to_response('pendulum/add_update_entry.html',
                              {'form': form,
                               'add_update': 'Update',
                               'callback': reverse('pendulum-update', args=[entry_id])},
                              context_instance=RequestContext(request))
Esempio n. 2
0
def add_entry(request):
    """
    Give users a way to add entries in the past.  This is beneficial if the
    website goes down or there is no connectivity for whatever reason.  When
    this method is invoked via a GET request, the user is presented with an
    empty form, which is then posted back to this method.  When this method
    is invoked via a POST request, the form data are validated.  If the data
    are valid, they will be saved as a new entry, and the user will be sent
    back to their log entry list.  If the data are invalid, the user will see
    the form again with the information they entered until they either abort
    or enter valid data.
    """

    if request.method == 'POST':
        # populate the form with the posted data
        form = AddUpdateEntryForm(request.POST)

        # validate the data
        if form.is_valid():
            # the data are valid... save them
            entry = form.save(commit=False)
            entry.user = request.user
            entry.site = Site.objects.get_current()
            entry.save()

            # create a message for the user
            messages.add_message(request, messages.INFO, 'The entry has been added successfully.')

            # redirect them to the log entry list
            return HttpResponseRedirect(reverse('pendulum-entries'))
        else:
            # create an error message for the user to see
            messages.add_message(request, messages.ERROR, 'Please correct the errors below.')
    else:
        # send back an empty form
        form = AddUpdateEntryForm()

    return render_to_response('pendulum/add_update_entry.html',
                              {'form': form,
                               'add_update': 'Add',
                               'callback': reverse('pendulum-add')},
                              context_instance=RequestContext(request))