def edit_planning(request, planning_ekey): """ GET method: Displays the planning edition page POST method: -Gather the data of the planning form. -Test if the planning's form is valid. -Update the planning in the database -If the planning is protected, update the guests emails in the planning -Update the events in the planning -Redirects to the planning's display. """ planning = Planning.objects.get_by_ekey_or_404(planning_ekey) if request.user != planning.creator: redirect_url = f"{reverse('accounts:login')}?next={request.path}" return HttpResponseRedirect(redirect_url) if request.method == 'POST': planning_form = PlanningCreationForm(request.POST, instance=planning) if planning_form.is_valid(): planning_form.save() if planning.protected: new_emails = request.POST.getlist('guest_email') update_guests(planning, new_emails) event_formset = EventInlineFormSet(request.POST, instance=planning) if event_formset.is_valid(): event_formset.save() notifier = Notifier(planning) notifier.notify_events_changes(event_formset) return redirect('participations:view', planning.ekey) planning_form = PlanningCreationForm(instance=planning) event_formset = EventInlineFormSet(instance=planning) return render( request, 'plannings/create_planning.html', { 'event_formset': event_formset, 'planning_form': planning_form, 'form_url': request.path })
def test_notify_event_changes(self): planning = Planning.objects.first() participants = [user.email for user in get_user_model().objects.filter( event__planning__pk=planning.pk).distinct()] # Assert that there are participants self.assertTrue(len(participants)) events = [Event(planning=planning, date=datetime.date(2020, 1, i)) for i in range(1, 4)] formset = EventInlineFormSet() formset.new_objects = [events[0], events[1]] formset.changed_objects = [] formset.deleted_objects = [events[2]] notifier = Notifier(planning) notifier.notify_events_changes(formset) # Assert that all the participants got an email self.assertEqual(len(participants), len(mail.outbox)) for email in mail.outbox: self.assertEqual(1, len(email.to)) self.assertIn(email.to[0], participants) participants.remove(email.to[0]) self.assertEqual(len(participants), 0)