def add_multiple_dates( request, template='crud/add_event_as_dates.html', event_form_class=EventForm, ): dtstart = None topic = get_current_topic(request) OccurrenceFormSet = formset_factory(MultipleDateSingleOccurrenceForm, extra=10) if request.method == 'POST': # to add event_planner to event event = Event( event_planner=EnjoyTodayUser.objects.get(user=request.user), #site=get_current_site(request) ) event_form = event_form_class(topic, request.POST, request.FILES, instance=event) formset = OccurrenceFormSet(request.POST) if event_form.is_valid() and formset.is_valid(): event.save() for occurrence_form in formset: if occurrence_form.is_valid and occurrence_form.cleaned_data: occurrence_form.save(event) return redirect('core:event_planner_panel') else: if 'dtstart' in request.GET: try: dtstart = parser.parse(request.GET['dtstart']) except (TypeError, ValueError) as exc: # TODO: A badly formatted date is passed to add_event logging.warning(exc) dtstart = dtstart or datetime.now() event_form = event_form_class(topic) # initial parameter doesnt work here formset = OccurrenceFormSet() context = dict({ 'dtstart': dtstart, 'event_form': event_form, 'formset': formset, }) return render(request, template, context)
def add_multiple_dates( request, template='crud/add_event_as_dates.html', event_form_class=EventForm, ): dtstart = None topic = get_current_topic(request) OccurrenceFormSet = formset_factory(SingleOccurrenceForm, extra=10) if request.method == 'POST': # to add event_planner to event event = Event(event_planner=EnjoyTodayUser.objects.get(user=request.user), #site=get_current_site(request) ) event_form = event_form_class(topic, request.POST, request.FILES, instance=event) formset = OccurrenceFormSet(request.POST) if event_form.is_valid() and formset.is_valid(): event.save() for occurrence_form in formset: if occurrence_form.is_valid and occurrence_form.cleaned_data: occurrence_form.save(event) return redirect('core:event_planner_panel') else: if 'dtstart' in request.GET: try: dtstart = parser.parse(request.GET['dtstart']) except(TypeError, ValueError) as exc: # TODO: A badly formatted date is passed to add_event logging.warning(exc) dtstart = dtstart or datetime.now() event_form = event_form_class(topic) # initial parameter doesnt work here formset = OccurrenceFormSet() context = dict({'dtstart': dtstart, 'event_form': event_form, 'formset': formset, }) return render(request, template, context)
def _add_event_by_topic( request, topic, recurrence_form_class, template='crud/add_event.html', event_form_class=EventForm, ): """ Add a new ``Event`` instance and 1 or more associated ``Occurrence``s. Context parameters: ``dtstart`` a datetime.datetime object representing the GET request value if present, otherwise None ``event_form`` a form object for updating the event ``recurrence_form`` a form object for adding occurrences :rtype: object """ dtstart = None if request.method == 'POST': # to add event_planner to event event = Event( event_planner=EnjoyTodayUser.objects.get(user=request.user), #site=get_current_site(request) ) event_form = event_form_class(topic, request.POST, request.FILES, instance=event) recurrence_form = recurrence_form_class(request.POST) if event_form.is_valid() and recurrence_form.is_valid(): event.save() recurrence_form.save(event) return redirect('core:event_planner_panel') else: if 'dtstart' in request.GET: try: dtstart = parser.parse(request.GET['dtstart']) except (TypeError, ValueError) as exc: # TODO: A badly formatted date is passed to add_event logging.warning(exc) dtstart = dtstart or datetime.now() event_form = event_form_class(topic) # Caution: initial is a form_class parameter and not a request parameter. recurrence_form = recurrence_form_class(initial={'dtstart': dtstart}) context = dict( { 'dtstart': dtstart, 'event_form': event_form, 'recurrence_form': recurrence_form, }, ) return render(request, template, context)
def _add_event_by_topic( request, topic, recurrence_form_class, template='crud/add_event.html', event_form_class=EventForm, ): """ Add a new ``Event`` instance and 1 or more associated ``Occurrence``s. Context parameters: ``dtstart`` a datetime.datetime object representing the GET request value if present, otherwise None ``event_form`` a form object for updating the event ``recurrence_form`` a form object for adding occurrences :rtype: object """ dtstart = None if request.method == 'POST': # to add event_planner to event event = Event(event_planner=EnjoyTodayUser.objects.get(user=request.user), #site=get_current_site(request) ) event_form = event_form_class(topic, request.POST, request.FILES, instance=event) recurrence_form = recurrence_form_class(request.POST) if event_form.is_valid() and recurrence_form.is_valid(): event.save() recurrence_form.save(event) return redirect('core:event_planner_panel') else: if 'dtstart' in request.GET: try: dtstart = parser.parse(request.GET['dtstart']) except(TypeError, ValueError) as exc: # TODO: A badly formatted date is passed to add_event logging.warning(exc) dtstart = dtstart or datetime.now() event_form = event_form_class(topic) # Caution: initial is a form_class parameter and not a request parameter. recurrence_form = recurrence_form_class(initial={'dtstart': dtstart}) context = dict({'dtstart': dtstart, 'event_form': event_form, 'recurrence_form': recurrence_form, }, ) return render(request, template, context)