Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Django + Python] Error catching for dining dates #129

Open
judtinzhang opened this issue Oct 12, 2022 · 1 comment
Open

[Django + Python] Error catching for dining dates #129

judtinzhang opened this issue Oct 12, 2022 · 1 comment

Comments

@judtinzhang
Copy link
Member

As we were in a rush to fix dining menus, we left out key error catching! Here is one example. Help us verify the date and make sure it is within range (if today is Monday, then we want the date to be no further than next Sunday).

@rudyroggio
Copy link

rudyroggio commented Dec 6, 2022

Wouldn't something as simple as this suffice?

class Menus(generics.ListAPIView):
    """
    GET: returns list of menus, defaulted to all objects within the week,
    and can specify the filter for a particular day
    """
    serializer_class = DiningMenuSerializer

    def get_queryset(self):
        if date_param := self.kwargs.get("date"):
            date = make_aware(datetime.datetime.strptime(date_param, "%Y-%m-%d"))
            if date < timezone.now().date():
                raise Http404
            elif date > timezone.now().date() + datetime.timedelta(days=6):
                raise Http404
            return DiningMenu.objects.filter(date=date)
        else:
            start_date = timezone.now().date()
            end_date = start_date + datetime.timedelta(days=6)
            return DiningMenu.objects.filter(date__gte=start_date, date__lte=end_date)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants