Example #1
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data()
     context.update({
         'slot_form':
         CreateSlotForm(self.request.user,
                        instance=DiningList(date=self.date))
     })
     return context
Example #2
0
    def post(self, request, *args, **kwargs):
        context = self.get_context_data()

        context['slot_form'] = CreateSlotForm(
            request.user, request.POST, instance=DiningList(date=self.date))

        if context['slot_form'].is_valid():
            dining_list = context['slot_form'].save()
            messages.success(request,
                             "You successfully created a new dining list")
            return redirect(dining_list)

        return self.render_to_response(context)
Example #3
0
    def test_get_registered_user(self):
        dlist = DiningList().get_latest()

        dlist.save()

        dpart = DiningParticipation(user=self.testUser, dining_list=dlist)

        dpart.save()

        # Tests

        self.assertEqual(len(dlist.get_participants()), 1)

        dpart.delete()
        dlist.delete()
    def test_association_unique_for_date(self):
        """Tests that there can be only one dining slot for an association for each date."""
        # Save one dining list
        self.form.save()

        # Try creating another one with same association
        dl = DiningList(date=self.dining_date)
        data = {
            'dish': 'Kwark',
            'association': str(self.association1.pk),
            'max_diners': '18',
            'serve_time': '17:00'
        }
        form = CreateSlotForm(self.user1, data, instance=dl)
        self.assertFalse(form.is_valid())
        self.assertTrue(form.has_error('association'))
 def setUp(self):
     self.association1 = Association.objects.create(name="Quadrivium")
     self.user1 = User.objects.create_user('jan')
     UserMembership.objects.create(related_user=self.user1,
                                   association=self.association1,
                                   is_verified=True,
                                   verified_on=timezone.now())
     # Date two days in the future
     self.dining_date = timezone.now().date() + timedelta(days=2)
     self.form_data = {
         'dish': 'Kwark',
         'association': str(self.association1.pk),
         'max_diners': '18',
         'serve_time': '17:00'
     }
     self.dining_list = DiningList(date=self.dining_date)
     self.form = CreateSlotForm(self.user1,
                                self.form_data,
                                instance=self.dining_list)
    def test_invalid_association(self):
        """Tests using an association which the user is not a member of.

        NOTE: when there is 1 available association, the form sets the association field to disabled. This means that
        the value that is sent with the form is totally ignored in favor of the initial value of the field. Therefore
        setting a different invalid association in the form data results in the form data actually being valid, but it
        does not use this association, instead it uses the other association which the user is actually a member of.

        Source: https://docs.djangoproject.com/en/2.2/ref/forms/fields/#disabled
        """
        association = Association.objects.create(name='Knights')
        form_data = {
            'dish': 'Boter',
            'association': str(association.pk),
            'max_diners': '20',
            'serve_time': '18:00'
        }
        form = CreateSlotForm(self.user1,
                              form_data,
                              instance=DiningList(date=self.dining_date))
        self.assertTrue(form.is_valid())
        # Check that the actual association is not Knights but Quadrivium
        self.assertEqual(self.association1, form.instance.association)
 def setUp(self):
     self.dining_list = DiningList(date=date(2123, 1, 2),
                                   sign_up_deadline=datetime(
                                       2100, 2, 2, tzinfo=timezone.utc),
                                   association=self.association)