def has_permission(self, request, view):

        user_is_representing_any_restaurant = not request.user.is_anonymous and bool(
            request.user.represented_restaurant_id
        )

        if user_is_representing_any_restaurant:
            if view.action == "create":
                # Allow to upload only if nothing was uploaded today for this restaurant:
                num_of_already_posted_today_by_my_restaurant = (
                    Menu.objects.filter(
                        restaurant_id=request.user.represented_restaurant_id
                    )
                    .filter(date=get_todays_date())
                    .count()
                )
                can_upload_another_menu = (
                    num_of_already_posted_today_by_my_restaurant == 0
                )
                return can_upload_another_menu
            else:
                # If I represent any restaurant, actions are allowed
                # (detailed checks will be in has_object_permission)
                return True
        else:
            # If not restaurant representer, no actions allowed
            return False
 def has_object_permission(self, request, view, menu_item):
     if (
         not request.user.is_anonymous
         and menu_item.restaurant_id == request.user.represented_restaurant_id
     ):
         return menu_item.date == get_todays_date()
     else:
         return False
 def get_queryset(self):
     """
     Filter menus only from my restaurant and from today
     """
     represented_restaurant_id = self.request.user.represented_restaurant_id
     today = get_todays_date()
     if represented_restaurant_id:
         return Menu.objects.filter(date=today).filter(
             restaurant_id=represented_restaurant_id)
     else:
         return Menu.objects.none()
Ejemplo n.º 4
0
    def get_and_cache_stats(self):

        request = self.context.get("request")
        cached_stats = request.mr_stats if hasattr(request,
                                                   "mr_stats") else None

        if not cached_stats:
            today = get_todays_date()
            user = self.get_context_user()
            cached_stats = get_voting_results_of_the_day(
                today, user.represented_organization_id)
            request.mr_stats = cached_stats

        return cached_stats
 def has_permission(self, request, view):
     if self.user_represents_any_org(request):
         if view.action == "create":
             # Allow to vote only if this is my first vote today
             num_of_my_votes_today = (
                 Vote.objects.filter(voter_id=request.user.id)
                 .filter(menu__date=get_todays_date())
                 .count()
             )
             can_post_another_vote = num_of_my_votes_today == 0
             return can_post_another_vote
         else:
             # If I represent any org, actions are allowed
             # (detailed checks will be in has_object_permission)
             return True
     else:
         # If not org representer, no actions allowed
         return False
 def get_queryset(self):
     """
     Filter menus - what are options for today
     """
     today = get_todays_date()
     return Menu.objects.filter(date=today)
 def perform_create(self, serializer):
     serializer.save(
         date=get_todays_date(),
         restaurant=self.request.user.represented_restaurant,
     )
Ejemplo n.º 8
0
 def get_queryset(self):
     today = get_todays_date()
     return Menu.objects.filter(date=today)