Пример #1
0
def create_event_for_content(request):
    """
    Create an event for the content submitted
    """
    from events.settings import DEFAULT_RULE_ID
    try:
        rule = Rule.objects.get(id=DEFAULT_RULE_ID)
    except:
        rule = None

    from events.forms import ContentEventForm
    cal_event = {}
    if request.method == 'POST':
        form = ContentEventForm(request.POST)
        if form.is_valid():
            if '_' in form.cleaned_data['content_type_id']:
                ctype_id, calrelation_id = map(int, form.cleaned_data['content_type_id'].split('_'))
            else:
                ctype_id = int(form.cleaned_data['content_type_id'])
            ctype = ContentType.objects.get_for_id(ctype_id)
            obj = ctype.get_object_for_this_type(id=form.cleaned_data['object_id'])
            if form.cleaned_data['calendar_id']:
                calendar = Calendar.objects.get(id=form.cleaned_data['calendar_id'])
            else:
                calendar = Calendar.objects.get_calendar_for_object(ctype)
            start = form.cleaned_data['start']
            event = Event.objects.create(
                start=start,
                end=start,
                all_day=True,
                calendar=calendar,
                title=unicode(obj)[:30] + "...",
                description=unicode(obj),
                creator=request.user,
                rule=rule
            )
            event.save()
            EventRelation.objects.create_relation(event, obj)
        o = event._create_occurrence(start)
        occurrence_id = encode_occurrence(o)
        cal_event = {
            'id': occurrence_id,
            'allDay': True,
            'event_id': event.pk,
            'start': event.start.isoformat(),
            'end': event.end.isoformat(),
            'title': event.title,
            'description': event.description,
            'delete_url': "%s?id=%s&action=cancel" % (reverse('ajax_edit_event', kwargs={'calendar_slug': calendar.slug}), o.event.pk),
            'delete_occurrence_url': "%s?id=%s&action=cancel" % (reverse('ajax_edit_event', kwargs={'calendar_slug': calendar.slug}), occurrence_id),
            'edit_url': reverse('admin:events_event_change', args=(o.event.pk, )),
            'update_url': reverse('ajax_edit_event', kwargs={'calendar_slug': calendar.slug}),
            'update_occurrence_url': "%s?id=%s" % (reverse('ajax_edit_event', kwargs={'calendar_slug': calendar.slug}), occurrence_id),
            'repeating_id': event.rule_id,
            'repeating_name': getattr(event.rule, "name", ""),
            'repeats': event.rule is not None,
            'calendar_slug': calendar.slug,
        }

    return JSONResponse(cal_event)
Пример #2
0
def calendar_events(request, calendar_slug):
    """
    JSON events feed class conforming to the JQuery FullCalendar and
    jquery-week-calendar CalEvent standard.

    [1]: http://code.google.com/p/jquery-week-calendar/
    [2]: http://arshaw.com/fullcalendar
    Corresponds to: http://arshaw.com/fullcalendar/docs/#calevent-objects
    """
    calendar = get_object_or_404(Calendar, slug=calendar_slug)

    start = request.GET.get('start', None)
    end = request.GET.get('end', None)
    if settings.USE_TZ:
        start = start and datetime.datetime.fromtimestamp(int(start), tzutc())
        end = end and datetime.datetime.fromtimestamp(int(end), tzutc())
    else:
        start = start and datetime.datetime.fromtimestamp(int(start))
        end = end and datetime.datetime.fromtimestamp(int(end))

    events = GET_EVENTS_FUNC(request, calendar)
    period = Period(events, start, end)
    cal_events = []
    for o in period.get_occurrences():
        audience_bits = [x for x in o.event.appropriate_for.get_set_bits() if x in VALID_AUDIENCES]
        audiences = [AUDIENCE_TYPES[x]['name'][0] for x in audience_bits]
        if o.event.all_day:
            start = o.start.date().isoformat()
            diff = o.end - o.start
            end = o.start.date() + datetime.timedelta(days=diff.days)
            end = end.isoformat()
        else:
            start = o.start.isoformat()
            end = o.end.isoformat()
        occurrence_id = encode_occurrence(o)
        cal_event = {
            'id': occurrence_id,
            'allDay': o.event.all_day,
            'event_id': o.event.pk,
            'start': start,
            'end': end,
            'title': "%s %s" % ("".join(audiences), o.title),
            'description': o.description,
            'delete_url': "%s?id=%s&action=cancel" % (reverse('ajax_edit_event', kwargs={'calendar_slug': calendar_slug}), o.event.pk),
            'delete_occurrence_url': "%s?id=%s&action=cancel" % (reverse('ajax_edit_event', kwargs={'calendar_slug': calendar_slug}), occurrence_id),
            'edit_url': reverse('admin:events_event_change', args=(o.event.pk, )),
            'update_url': reverse('ajax_edit_event', kwargs={'calendar_slug': calendar_slug}),
            'update_occurrence_url': "%s?id=%s" % (reverse('ajax_edit_event', kwargs={'calendar_slug': calendar_slug}), occurrence_id),
            'repeating_id': o.event.rule_id,
            'repeating_name': getattr(o.event.rule, "name", ""),
            'repeats': o.event.rule is not None,
            'audiences': audiences,
        }
        cal_events.append(cal_event)

    return JSONResponse(cal_events)
Пример #3
0
def calendar_events(request, calendar_slug):
    """
    JSON events feed class conforming to the JQuery FullCalendar and
    jquery-week-calendar CalEvent standard.

    [1]: http://code.google.com/p/jquery-week-calendar/
    [2]: http://arshaw.com/fullcalendar
    Corresponds to: http://arshaw.com/fullcalendar/docs/#calevent-objects
    """
    calendar = get_object_or_404(Calendar, slug=calendar_slug)

    start = request.GET.get('start', None)
    end = request.GET.get('end', None)
    if settings.USE_TZ:
        start = start and datetime.datetime.fromtimestamp(int(start), tzutc())
        end = end and datetime.datetime.fromtimestamp(int(end), tzutc())
    else:
        start = start and datetime.datetime.fromtimestamp(int(start))
        end = end and datetime.datetime.fromtimestamp(int(end))

    events = GET_EVENTS_FUNC(request, calendar)
    period = Period(events, start, end)
    cal_events = []
    for o in period.get_occurrences():
        audience_bits = [
            x for x in o.event.appropriate_for.get_set_bits()
            if x in VALID_AUDIENCES
        ]
        audiences = [AUDIENCE_TYPES[x]['name'][0] for x in audience_bits]
        if o.event.all_day:
            start = o.start.date().isoformat()
            diff = o.end - o.start
            end = o.start.date() + datetime.timedelta(days=diff.days)
            end = end.isoformat()
        else:
            start = o.start.isoformat()
            end = o.end.isoformat()
        occurrence_id = encode_occurrence(o)
        cal_event = {
            'id':
            occurrence_id,
            'allDay':
            o.event.all_day,
            'event_id':
            o.event.pk,
            'start':
            start,
            'end':
            end,
            'title':
            "%s %s" % ("".join(audiences), o.title),
            'description':
            o.description,
            'delete_url':
            "%s?id=%s&action=cancel" %
            (reverse('ajax_edit_event',
                     kwargs={'calendar_slug': calendar_slug}), o.event.pk),
            'delete_occurrence_url':
            "%s?id=%s&action=cancel" %
            (reverse('ajax_edit_event',
                     kwargs={'calendar_slug': calendar_slug}), occurrence_id),
            'edit_url':
            reverse('admin:events_event_change', args=(o.event.pk, )),
            'update_url':
            reverse('ajax_edit_event', kwargs={'calendar_slug':
                                               calendar_slug}),
            'update_occurrence_url':
            "%s?id=%s" %
            (reverse('ajax_edit_event',
                     kwargs={'calendar_slug': calendar_slug}), occurrence_id),
            'repeating_id':
            o.event.rule_id,
            'repeating_name':
            getattr(o.event.rule, "name", ""),
            'repeats':
            o.event.rule is not None,
            'audiences':
            audiences,
        }
        cal_events.append(cal_event)

    return JSONResponse(cal_events)
Пример #4
0
def create_event_for_content(request):
    """
    Create an event for the content submitted
    """
    from events.settings import DEFAULT_RULE_ID
    try:
        rule = Rule.objects.get(id=DEFAULT_RULE_ID)
    except:
        rule = None

    from events.forms import ContentEventForm
    cal_event = {}
    if request.method == 'POST':
        form = ContentEventForm(request.POST)
        if form.is_valid():
            if '_' in form.cleaned_data['content_type_id']:
                ctype_id, calrelation_id = map(
                    int, form.cleaned_data['content_type_id'].split('_'))
            else:
                ctype_id = int(form.cleaned_data['content_type_id'])
            ctype = ContentType.objects.get_for_id(ctype_id)
            obj = ctype.get_object_for_this_type(
                id=form.cleaned_data['object_id'])
            if form.cleaned_data['calendar_id']:
                calendar = Calendar.objects.get(
                    id=form.cleaned_data['calendar_id'])
            else:
                calendar = Calendar.objects.get_calendar_for_object(ctype)
            start = form.cleaned_data['start']
            event = Event.objects.create(start=start,
                                         end=start,
                                         all_day=True,
                                         calendar=calendar,
                                         title=unicode(obj)[:30] + "...",
                                         description=unicode(obj),
                                         creator=request.user,
                                         rule=rule)
            event.save()
            EventRelation.objects.create_relation(event, obj)
        o = event._create_occurrence(start)
        occurrence_id = encode_occurrence(o)
        cal_event = {
            'id':
            occurrence_id,
            'allDay':
            True,
            'event_id':
            event.pk,
            'start':
            event.start.isoformat(),
            'end':
            event.end.isoformat(),
            'title':
            event.title,
            'description':
            event.description,
            'delete_url':
            "%s?id=%s&action=cancel" %
            (reverse('ajax_edit_event',
                     kwargs={'calendar_slug': calendar.slug}), o.event.pk),
            'delete_occurrence_url':
            "%s?id=%s&action=cancel" %
            (reverse('ajax_edit_event',
                     kwargs={'calendar_slug': calendar.slug}), occurrence_id),
            'edit_url':
            reverse('admin:events_event_change', args=(o.event.pk, )),
            'update_url':
            reverse('ajax_edit_event', kwargs={'calendar_slug':
                                               calendar.slug}),
            'update_occurrence_url':
            "%s?id=%s" %
            (reverse('ajax_edit_event',
                     kwargs={'calendar_slug': calendar.slug}), occurrence_id),
            'repeating_id':
            event.rule_id,
            'repeating_name':
            getattr(event.rule, "name", ""),
            'repeats':
            event.rule is not None,
            'calendar_slug':
            calendar.slug,
        }

    return JSONResponse(cal_event)