view gcalendar/views.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -0500
parents 74e84f5fc948
children 68c3343f3318
line wrap: on
line source
"""
Views for the gcalendar application.
"""

from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.http import HttpResponse
from django.http import HttpResponseBadRequest
from django.http import HttpResponseForbidden
from django.http import HttpResponseRedirect
from django.http import Http404
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
from django.template import RequestContext

from gcalendar.forms import EventEntryForm
from gcalendar.models import Event


def index(request):
    user = request.user
    if user.is_authenticated():
        tz = user.profile.time_zone
    else:
        tz = 'US/Pacific'

    return render_to_response('gcalendar/index.html', {
        'tz': tz,
        },
        context_instance = RequestContext(request))


@login_required
def add_event(request):
    if request.method == 'POST':
        form = EventEntryForm(request.POST)
        if form.is_valid():
            event = form.save(commit=False)
            event.user = request.user
            event.repeat = 'none'
            event.save()
            return HttpResponseRedirect(reverse('gcalendar-add_thanks'))
    else:
        form = EventEntryForm()

    return render_to_response('gcalendar/event.html', {
        'title': 'Add Calendar Event',
        'form': form,
        },
        context_instance = RequestContext(request))


@login_required
def add_thanks(request):
    return render_to_response('gcalendar/thanks_add.html', {
        },
        context_instance = RequestContext(request))


@login_required
def edit_events(request):
    events = Event.objects.filter(user=request.user, status=Event.ON_CAL).\
            order_by('-start_date')
    paginator = Paginator(events, 25)
    num = request.GET.get('page')
    try:
        page = paginator.page(num)
    except PageNotAnInteger:
        page = paginator.page(1)
    except EmptyPage:
        page = paginator.page(paginator.num_pages)

    return render_to_response('gcalendar/edit.html', {
        'page': page,
        },
        context_instance = RequestContext(request))


@login_required
def edit_event(request, event_id):
    event = get_object_or_404(Event, pk=event_id)
    if event.user != request.user:
        raise Http404

    if request.method == 'POST':
        form = EventEntryForm(request.POST, instance=event)
        if form.is_valid():
            event = form.save(commit=False)
            event.user = request.user
            event.repeat = 'none'
            event.status = Event.EDIT_REQ
            event.save()
            return HttpResponseRedirect(reverse('gcalendar-edit_thanks'))
    else:
        form = EventEntryForm(instance=event)

    return render_to_response('gcalendar/event.html', {
        'title': 'Change Calendar Event',
        'form': form,
        },
        context_instance = RequestContext(request))


@login_required
def edit_thanks(request):
    return render_to_response('gcalendar/thanks_edit.html', {
        },
        context_instance = RequestContext(request))


def delete_event(request):
    """This view marks an event for deletion. It is called via AJAX."""
    if request.user.is_authenticated():
        id = request.POST.get('id', None)
        if id is None or not id.isdigit():
            return HttpResponseBadRequest()
        try:
            event = Event.objects.get(pk=id)
        except Event.DoesNotExist:
            return HttpResponseBadRequest()
        if request.user != event.user:
            return HttpResponseForbidden()

        event.status = Event.DEL_REQ
        event.save()
        return HttpResponse(id)

    return HttpResponseForbidden()


# vim: ts=4 sw=4