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)
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)