Esempio n. 1
0
def edit_calendar_occurrence(request, event_id, event, occurrence):
    instance_event_details = EventDetails.objects.get_eventdetails_for_object(
        occurrence)
    calendars = get_user_calendars(request.user, ['manager', 'creator'])
    if event.calendar.pk not in [c.calendar_id for c in calendars]:
        return HttpResponseRedirect(reverse('view_calendar', args=[]))
    initial_data = {}
    initial_data['start'] = occurrence.start
    initial_data['end'] = occurrence.end
    initial_data['title'] = occurrence.title
    initial_data['description'] = occurrence.description

    if request.method == 'POST':
        form = EditEventForm(data=request.POST, instance=event)
        form_details = EditEventDetailsForm(
            data=request.POST, instance=instance_event_details)

        action = request.POST.get('action', None)
        if action and action == 'deleteevent':
            occurrence.cancel()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        if form.is_valid():
            occurrence.title = form.cleaned_data['title']
            occurrence.description = form.cleaned_data['description']
            occurrence.start = form.cleaned_data[
                'start'].strftime('%Y-%m-%d %H:%M:%S')
            occurrence.end = form.cleaned_data[
                'end'].strftime('%Y-%m-%d %H:%M:%S')
            occurrence.save()
            form_details.save()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))
        else:
            form = EditEventForm(data=request.POST)
            form_details = EditEventDetailsForm(data=request.POST)
    else:
        form = EditEventForm(instance=event, initial=initial_data)
        form_details = EditEventDetailsForm(instance=instance_event_details)

    extra_context = {
        'form': form,
        'boccurrence': True,
        'event': event,
        'form_details': form_details,
        'calendars': calendars,
        'calendar': ZornaCalendar.objects.get(calendar=event.calendar)
    }

    context = RequestContext(request)
    return render_to_response('calendars/calendar_edit.html', extra_context, context_instance=context)
Esempio n. 2
0
def edit_calendar_occurrence(request, event_id, event, occurrence):
    instance_event_details = EventDetails.objects.get_eventdetails_for_object(
        occurrence)
    calendars = get_user_calendars(request.user, ['manager', 'creator'])
    if event.calendar.pk not in [c.calendar_id for c in calendars]:
        return HttpResponseRedirect(reverse('view_calendar', args=[]))
    initial_data = {}
    initial_data['start'] = occurrence.start
    initial_data['end'] = occurrence.end
    initial_data['title'] = occurrence.title
    initial_data['description'] = occurrence.description

    if request.method == 'POST':
        form = EditEventForm(data=request.POST, instance=event)
        form_details = EditEventDetailsForm(data=request.POST,
                                            instance=instance_event_details)

        action = request.POST.get('action', None)
        if action and action == 'deleteevent':
            occurrence.cancel()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        if form.is_valid():
            occurrence.title = form.cleaned_data['title']
            occurrence.description = form.cleaned_data['description']
            occurrence.start = form.cleaned_data['start'].strftime(
                '%Y-%m-%d %H:%M:%S')
            occurrence.end = form.cleaned_data['end'].strftime(
                '%Y-%m-%d %H:%M:%S')
            occurrence.save()
            form_details.save()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))
        else:
            form = EditEventForm(data=request.POST)
            form_details = EditEventDetailsForm(data=request.POST)
    else:
        form = EditEventForm(instance=event, initial=initial_data)
        form_details = EditEventDetailsForm(instance=instance_event_details)

    extra_context = {
        'form': form,
        'boccurrence': True,
        'event': event,
        'form_details': form_details,
        'calendars': calendars,
        'calendar': ZornaCalendar.objects.get(calendar=event.calendar)
    }

    context = RequestContext(request)
    return render_to_response('calendars/calendar_edit.html',
                              extra_context,
                              context_instance=context)
Esempio n. 3
0
def edit_calendar_reccurrent_event(request, event_id, event, occurrence):
    instance_event_details = EventDetails.objects.get_eventdetails_for_object(
        event)
    calendars = get_user_calendars(request.user, ['manager', 'creator'])
    if event.calendar.pk not in [c.calendar_id for c in calendars]:
        return HttpResponseRedirect(reverse('view_calendar', args=[]))
    original_start = event.start
    params = event.rule.get_params()
    initial_data = {}
    initial_data['rule'] = event.rule.frequency
    if 'interval' in params:
        initial_data['interval'] = params['interval']
    if 'byweekday' in params:
        initial_data['weekdays'] = params['byweekday'] if type(
            params['byweekday']) == type(list()) else [params['byweekday']]
    initial_data['start'] = occurrence.start
    initial_data['end'] = occurrence.end
    initial_data['title'] = occurrence.title
    initial_data['description'] = occurrence.description

    if request.method == 'POST':
        form = EditEventForm(data=request.POST, instance=event)
        form_details = EditEventDetailsForm(data=request.POST,
                                            instance=instance_event_details)

        action = request.POST.get('action', None)
        if action and action == 'deleteone':
            occurrence.cancel()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        if action and action == 'deleteall':
            persisted_occurrences = event.occurrence_set.all()
            for occ in persisted_occurrences:
                try:
                    EventDetails.objects.get_eventdetails_for_object(
                        occ).delete()
                except:
                    pass
            event.rule.delete()
            event.delete()
            instance_event_details.delete()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        if action and action == 'updateevent' and form.is_valid(
        ):  # update only this occurrence
            occurrence.title = form.cleaned_data['title']
            occurrence.description = form.cleaned_data['description']
            occurrence.start = form.cleaned_data['start'].strftime(
                '%Y-%m-%d %H:%M:%S')
            occurrence.end = form.cleaned_data['end'].strftime(
                '%Y-%m-%d %H:%M:%S')
            occurrence.save()
            EventDetails.objects.create_details(
                occurrence,
                request.POST.get('location', ''),
                request.POST.get('free_busy', 0),
                request.POST.get('privacy', 0),
            )
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        if form.is_valid():
            calendar = get_object_or_404(ZornaCalendar,
                                         pk=request.POST['calendar_id'])
            evt = form.save(commit=False)
            evt.calendar = calendar.calendar
            if 'end_recurring_period' in form.cleaned_data and form.cleaned_data[
                    'end_recurring_period']:
                evt.end_recurring_period = evt.end_recurring_period + \
                    datetime.timedelta(days=1, seconds= -1)
            rule = event.rule
            if rule and request.POST['rule'] == '':
                persisted_occurrences = event.occurrence_set.all()
                for occ in persisted_occurrences:
                    try:
                        EventDetails.objects.get_eventdetails_for_object(
                            occ).delete()
                    except:
                        pass
                event.rule = None
                rule.delete()

            if request.POST['rule'] != '':
                params = "interval:" + request.POST['interval']
                if request.POST['rule'] == 'WEEKLY':
                    wl = request.POST.getlist('weekdays')
                    if not wl:
                        wl = [str((int(evt.start.strftime('%w')) + 6) % 7)]
                    if wl:
                        params += ";byweekday:" + ",".join(wl)
                if evt.rule:
                    evt.rule.name = request.POST['rule']
                    evt.rule.frequency = request.POST['rule']
                    evt.rule.params = params
                    evt.rule.save()
                else:
                    rule = Rule(name=request.POST['rule'],
                                frequency=request.POST['rule'],
                                params=params)
                    rule.save()
                    evt.rule = rule
            if occurrence.start.date() == evt.start.date():
                td = evt.end.date() - evt.start.date()
                evt.start = datetime.datetime.combine(original_start.date(),
                                                      evt.start.time())
                evt.end = datetime.datetime.combine(original_start.date() + td,
                                                    evt.end.time())
            evt.save()
            form_details.save()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))
        else:
            form = EditEventForm(data=request.POST)
            form_details = EditEventDetailsForm(data=request.POST)
    else:
        form = EditEventForm(instance=event, initial=initial_data)
        form_details = EditEventDetailsForm(instance=instance_event_details)

    extra_context = {
        'form': form,
        'boccurrence': False,
        'event': event,
        'form_details': form_details,
        'calendars': calendars,
        'calendar': ZornaCalendar.objects.get(calendar=event.calendar)
    }

    context = RequestContext(request)
    return render_to_response('calendars/calendar_edit.html',
                              extra_context,
                              context_instance=context)
Esempio n. 4
0
def edit_calendar_simple_event(request, event_id, event, occurrence):
    instance_event_details = EventDetails.objects.get_eventdetails_for_object(
        event)
    calendars = get_user_calendars(request.user, ['manager', 'creator'])
    if event.calendar.pk not in [c.calendar_id for c in calendars]:
        return HttpResponseRedirect(reverse('view_calendar', args=[]))

    if request.method == 'POST':
        action = request.POST.get('action', None)
        if action and action == 'deleteevent':
            event.delete()
            instance_event_details.delete()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        form = EditEventForm(data=request.POST, instance=event)
        form_details = EditEventDetailsForm(data=request.POST,
                                            instance=instance_event_details)
        if form.is_valid():
            calendar = get_object_or_404(ZornaCalendar,
                                         pk=request.POST['calendar_id'])
            event = form.save(commit=False)
            event.calendar = calendar.calendar
            if 'end_recurring_period' in form.cleaned_data and form.cleaned_data[
                    'end_recurring_period']:
                event.end_recurring_period = event.end_recurring_period + \
                    datetime.timedelta(days=1, seconds= -1)
            if request.POST['rule'] != '':
                params = "interval:" + request.POST['interval']
                if request.POST['rule'] == 'WEEKLY':
                    wl = request.POST.getlist('weekdays')
                    if not wl:
                        wl = [str((int(event.start.strftime('%w')) + 6) % 7)]
                    if wl:
                        params += ";byweekday:" + ",".join(wl)
                rule = Rule(name=request.POST['rule'],
                            frequency=request.POST['rule'],
                            params=params)
                rule.save()
                event.rule = rule
            event.save()
            form_details.save()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))
        else:
            form = EditEventForm(data=request.POST)
            form_details = EditEventDetailsForm(data=request.POST)
    else:
        form = EditEventForm(instance=event)
        form_details = EditEventDetailsForm(instance=instance_event_details)

    extra_context = {
        'form': form,
        'boccurrence': False,
        'event': event,
        'form_details': form_details,
        'calendars': calendars,
        'calendar': ZornaCalendar.objects.get(calendar=event.calendar)
    }

    context = RequestContext(request)
    return render_to_response('calendars/calendar_edit.html',
                              extra_context,
                              context_instance=context)
Esempio n. 5
0
def create_calendar_event(request):
    start = request.GET.get('start', None)
    end = request.GET.get('end', None)
    title = request.GET.get('title', None)

    initial_data = {}
    if title:
        initial_data['title'] = title

    if start:
        try:
            start = datetime.datetime.fromtimestamp(float(start))
            initial_data['start'] = start
            if end:
                initial_data['end'] = datetime.datetime.fromtimestamp(
                    float(end))
            else:
                initial_data['end'] = start + datetime.timedelta(minutes=30)
        except TypeError:
            raise Http404
        except ValueError:
            raise Http404

    calendars = get_user_calendars(request.user, ['manager', 'creator'])
    if not calendars:
        return HttpResponseRedirect(reverse('view_calendar', args=[]))

    if request.method == 'POST':
        form = EditEventForm(data=request.POST)
        form_details = EditEventDetailsForm(data=request.POST)
        calendar_id = request.POST.get('calendar_id', None)
        if calendar_id and form.is_valid():
            calendar = get_object_or_404(ZornaCalendar, pk=calendar_id)
            if not calendar.pk in [c.pk for c in calendars]:
                # TODO Display warning message
                return HttpResponseRedirect(reverse('view_calendar', args=[]))
            event = form.save(commit=False)
            event.creator = request.user
            event.calendar = calendar.calendar
            if 'end_recurring_period' in form.cleaned_data and form.cleaned_data[
                    'end_recurring_period']:
                event.end_recurring_period = event.end_recurring_period + \
                    datetime.timedelta(days=1, seconds= -1)

            if request.POST['rule'] != '':
                params = "interval:" + request.POST['interval']
                if request.POST['rule'] == 'WEEKLY':
                    weekdays = request.POST.getlist('weekdays')
                    if not weekdays:
                        weekdays = [
                            str((int(event.start.strftime('%w')) + 6) % 7)
                        ]
                    params += ";byweekday:" + ",".join(weekdays)
                rule = Rule(name=request.POST['rule'],
                            frequency=request.POST['rule'],
                            params=params)
                rule.save()
                event.rule = rule

            event.save()
            evtd = form_details.save(commit=False)
            evtd.ct = ContentType.objects.get_for_model(type(event))
            evtd.object_id = event.pk
            evtd.content_object = event
            evtd.save()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))
        else:
            form = EditEventForm(data=request.POST)
            form_details = EditEventDetailsForm(data=request.POST)
    else:
        form = EditEventForm(initial=initial_data)
        form_details = EditEventDetailsForm()

    extra_context = {
        'form': form,
        'boccurrence': False,
        'event': None,
        'form_details': form_details,
        'calendars': calendars,
        'calendar': get_personal_calendar(request.user),
    }

    context = RequestContext(request)
    return render_to_response('calendars/calendar_edit.html',
                              extra_context,
                              context_instance=context)
Esempio n. 6
0
def edit_calendar_reccurrent_event(request, event_id, event, occurrence):
    instance_event_details = EventDetails.objects.get_eventdetails_for_object(
        event)
    calendars = get_user_calendars(request.user, ['manager', 'creator'])
    if event.calendar.pk not in [c.calendar_id for c in calendars]:
        return HttpResponseRedirect(reverse('view_calendar', args=[]))
    original_start = event.start
    params = event.rule.get_params()
    initial_data = {}
    initial_data['rule'] = event.rule.frequency
    if 'interval' in params:
        initial_data['interval'] = params['interval']
    if 'byweekday' in params:
        initial_data['weekdays'] = params['byweekday'] if type(params[
                                                               'byweekday']) == type(list()) else [params['byweekday']]
    initial_data['start'] = occurrence.start
    initial_data['end'] = occurrence.end
    initial_data['title'] = occurrence.title
    initial_data['description'] = occurrence.description

    if request.method == 'POST':
        form = EditEventForm(data=request.POST, instance=event)
        form_details = EditEventDetailsForm(
            data=request.POST, instance=instance_event_details)

        action = request.POST.get('action', None)
        if action and action == 'deleteone':
            occurrence.cancel()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        if action and action == 'deleteall':
            persisted_occurrences = event.occurrence_set.all()
            for occ in persisted_occurrences:
                try:
                    EventDetails.objects.get_eventdetails_for_object(
                        occ).delete()
                except:
                    pass
            event.rule.delete()
            event.delete()
            instance_event_details.delete()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        if action and action == 'updateevent' and form.is_valid():  # update only this occurrence
            occurrence.title = form.cleaned_data['title']
            occurrence.description = form.cleaned_data['description']
            occurrence.start = form.cleaned_data[
                'start'].strftime('%Y-%m-%d %H:%M:%S')
            occurrence.end = form.cleaned_data[
                'end'].strftime('%Y-%m-%d %H:%M:%S')
            occurrence.save()
            EventDetails.objects.create_details(
                occurrence,
                request.POST.get('location', ''),
                request.POST.get('free_busy', 0),
                request.POST.get('privacy', 0),
            )
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        if form.is_valid():
            calendar = get_object_or_404(
                ZornaCalendar, pk=request.POST['calendar_id'])
            evt = form.save(commit=False)
            evt.calendar = calendar.calendar
            if 'end_recurring_period' in form.cleaned_data and form.cleaned_data['end_recurring_period']:
                evt.end_recurring_period = evt.end_recurring_period + \
                    datetime.timedelta(days=1, seconds= -1)
            rule = event.rule
            if rule and request.POST['rule'] == '':
                persisted_occurrences = event.occurrence_set.all()
                for occ in persisted_occurrences:
                    try:
                        EventDetails.objects.get_eventdetails_for_object(
                            occ).delete()
                    except:
                        pass
                event.rule = None
                rule.delete()

            if request.POST['rule'] != '':
                params = "interval:" + request.POST['interval']
                if request.POST['rule'] == 'WEEKLY':
                    wl = request.POST.getlist('weekdays')
                    if not wl:
                        wl = [str((int(evt.start.strftime('%w')) + 6) % 7)]
                    if wl:
                        params += ";byweekday:" + ",".join(wl)
                if evt.rule:
                    evt.rule.name = request.POST['rule']
                    evt.rule.frequency = request.POST['rule']
                    evt.rule.params = params
                    evt.rule.save()
                else:
                    rule = Rule(name=request.POST['rule'],
                                frequency=request.POST['rule'],
                                params=params
                                )
                    rule.save()
                    evt.rule = rule
            if occurrence.start.date() == evt.start.date():
                td = evt.end.date() - evt.start.date()
                evt.start = datetime.datetime.combine(
                    original_start.date(), evt.start.time())
                evt.end = datetime.datetime.combine(
                    original_start.date() + td, evt.end.time())
            evt.save()
            form_details.save()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))
        else:
            form = EditEventForm(data=request.POST)
            form_details = EditEventDetailsForm(data=request.POST)
    else:
        form = EditEventForm(instance=event, initial=initial_data)
        form_details = EditEventDetailsForm(instance=instance_event_details)

    extra_context = {
        'form': form,
        'boccurrence': False,
        'event': event,
        'form_details': form_details,
        'calendars': calendars,
        'calendar': ZornaCalendar.objects.get(calendar=event.calendar)
    }

    context = RequestContext(request)
    return render_to_response('calendars/calendar_edit.html', extra_context, context_instance=context)
Esempio n. 7
0
def edit_calendar_simple_event(request, event_id, event, occurrence):
    instance_event_details = EventDetails.objects.get_eventdetails_for_object(
        event)
    calendars = get_user_calendars(request.user, ['manager', 'creator'])
    if event.calendar.pk not in [c.calendar_id for c in calendars]:
        return HttpResponseRedirect(reverse('view_calendar', args=[]))

    if request.method == 'POST':
        action = request.POST.get('action', None)
        if action and action == 'deleteevent':
            event.delete()
            instance_event_details.delete()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))

        form = EditEventForm(data=request.POST, instance=event)
        form_details = EditEventDetailsForm(
            data=request.POST, instance=instance_event_details)
        if form.is_valid():
            calendar = get_object_or_404(
                ZornaCalendar, pk=request.POST['calendar_id'])
            event = form.save(commit=False)
            event.calendar = calendar.calendar
            if 'end_recurring_period' in form.cleaned_data and form.cleaned_data['end_recurring_period']:
                event.end_recurring_period = event.end_recurring_period + \
                    datetime.timedelta(days=1, seconds= -1)
            if request.POST['rule'] != '':
                params = "interval:" + request.POST['interval']
                if request.POST['rule'] == 'WEEKLY':
                    wl = request.POST.getlist('weekdays')
                    if not wl:
                        wl = [str((int(event.start.strftime('%w')) + 6) % 7)]
                    if wl:
                        params += ";byweekday:" + ",".join(wl)
                rule = Rule(
                    name=request.POST['rule'],
                    frequency=request.POST['rule'],
                    params=params
                )
                rule.save()
                event.rule = rule
            event.save()
            form_details.save()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))
        else:
            form = EditEventForm(data=request.POST)
            form_details = EditEventDetailsForm(data=request.POST)
    else:
        form = EditEventForm(instance=event)
        form_details = EditEventDetailsForm(instance=instance_event_details)

    extra_context = {
        'form': form,
        'boccurrence': False,
        'event': event,
        'form_details': form_details,
        'calendars': calendars,
        'calendar': ZornaCalendar.objects.get(calendar=event.calendar)
    }

    context = RequestContext(request)
    return render_to_response('calendars/calendar_edit.html', extra_context, context_instance=context)
Esempio n. 8
0
def create_calendar_event(request):
    start = request.GET.get('start', None)
    end = request.GET.get('end', None)
    title = request.GET.get('title', None)

    initial_data = {}
    if title:
        initial_data['title'] = title

    if start:
        try:
            start = datetime.datetime.fromtimestamp(float(start))
            initial_data['start'] = start
            if end:
                initial_data[
                    'end'] = datetime.datetime.fromtimestamp(float(end))
            else:
                initial_data['end'] = start + datetime.timedelta(minutes=30)
        except TypeError:
            raise Http404
        except ValueError:
            raise Http404

    calendars = get_user_calendars(request.user, ['manager', 'creator'])
    if not calendars:
        return HttpResponseRedirect(reverse('view_calendar', args=[]))

    if request.method == 'POST':
        form = EditEventForm(data=request.POST)
        form_details = EditEventDetailsForm(data=request.POST)
        calendar_id = request.POST.get('calendar_id', None)
        if calendar_id and form.is_valid():
            calendar = get_object_or_404(ZornaCalendar, pk=calendar_id)
            if not calendar.pk in [c.pk for c in calendars]:
                # TODO Display warning message
                return HttpResponseRedirect(reverse('view_calendar', args=[]))
            event = form.save(commit=False)
            event.creator = request.user
            event.calendar = calendar.calendar
            if 'end_recurring_period' in form.cleaned_data and form.cleaned_data['end_recurring_period']:
                event.end_recurring_period = event.end_recurring_period + \
                    datetime.timedelta(days=1, seconds= -1)

            if request.POST['rule'] != '':
                params = "interval:" + request.POST['interval']
                if request.POST['rule'] == 'WEEKLY':
                    weekdays = request.POST.getlist('weekdays')
                    if not weekdays:
                        weekdays = [str((int(
                            event.start.strftime('%w')) + 6) % 7)]
                    params += ";byweekday:" + ",".join(weekdays)
                rule = Rule(name=request.POST['rule'],
                            frequency=request.POST['rule'],
                            params=params
                            )
                rule.save()
                event.rule = rule

            event.save()
            evtd = form_details.save(commit=False)
            evtd.ct = ContentType.objects.get_for_model(type(event))
            evtd.object_id = event.pk
            evtd.content_object = event
            evtd.save()
            return HttpResponseRedirect(reverse('view_calendar', args=[]))
        else:
            form = EditEventForm(data=request.POST)
            form_details = EditEventDetailsForm(data=request.POST)
    else:
        form = EditEventForm(initial=initial_data)
        form_details = EditEventDetailsForm()

    extra_context = {
        'form': form,
        'boccurrence': False,
        'event': None,
        'form_details': form_details,
        'calendars': calendars,
        'calendar': get_personal_calendar(request.user),
    }

    context = RequestContext(request)
    return render_to_response('calendars/calendar_edit.html', extra_context, context_instance=context)