Skip to content

Commit

Permalink
actually apply limits everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Jun 4, 2021
1 parent bfe7221 commit 388ef32
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
19 changes: 18 additions & 1 deletion cookbook/views/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@
from cookbook.helper.permission_helper import group_required, has_group_permission
from cookbook.helper.recipe_url_import import parse_cooktime
from cookbook.models import (Comment, Food, Ingredient, Keyword, Recipe,
RecipeImport, Step, Sync, Unit)
RecipeImport, Step, Sync, Unit, UserPreference)
from cookbook.tables import SyncTable


@group_required('user')
def sync(request):
if request.space.max_recipes != 0 and Recipe.objects.filter(space=request.space).count() >= request.space.max_recipes: # TODO move to central helper function
messages.add_message(request, messages.WARNING, _('You have reached the maximum number of recipes for your space.'))
return HttpResponseRedirect(reverse('index'))

if request.space.max_users != 0 and UserPreference.objects.filter(space=request.space).count() > request.space.max_users:
messages.add_message(request, messages.WARNING, _('You have more users than allowed in your space.'))
return HttpResponseRedirect(reverse('index'))

if request.method == "POST":
if not has_group_permission(request.user, ['admin']):
messages.add_message(request, messages.ERROR, _('You do not have the required permissions to view this page!'))
Expand Down Expand Up @@ -109,6 +117,14 @@ def batch_edit(request):
@group_required('user')
@atomic
def import_url(request):
if request.space.max_recipes != 0 and Recipe.objects.filter(space=request.space).count() >= request.space.max_recipes: # TODO move to central helper function
messages.add_message(request, messages.WARNING, _('You have reached the maximum number of recipes for your space.'))
return HttpResponseRedirect(reverse('index'))

if request.space.max_users != 0 and UserPreference.objects.filter(space=request.space).count() > request.space.max_users:
messages.add_message(request, messages.WARNING, _('You have more users than allowed in your space.'))
return HttpResponseRedirect(reverse('index'))

if request.method == 'POST':
data = json.loads(request.body)
data['cookTime'] = parse_cooktime(data.get('cookTime', ''))
Expand Down Expand Up @@ -200,6 +216,7 @@ def import_url(request):

return render(request, 'url_import.html', context)


class Object(object):
pass

Expand Down
2 changes: 1 addition & 1 deletion cookbook/views/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def convert_recipe(request, pk):

@group_required('user')
def internal_recipe_update(request, pk):
if request.space.max_recipes != 0 and Recipe.objects.filter(space=request.space).count() > request.space.max_recipes:
if request.space.max_recipes != 0 and Recipe.objects.filter(space=request.space).count() > request.space.max_recipes: # TODO move to central helper function
messages.add_message(request, messages.WARNING, _('You have reached the maximum number of recipes for your space.'))
return HttpResponseRedirect(reverse('view_recipe', args=[pk]))

Expand Down
10 changes: 9 additions & 1 deletion cookbook/views/import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from cookbook.integration.recipesage import RecipeSage
from cookbook.integration.rezkonv import RezKonv
from cookbook.integration.safron import Safron
from cookbook.models import Recipe, ImportLog
from cookbook.models import Recipe, ImportLog, UserPreference


def get_integration(request, export_type):
Expand Down Expand Up @@ -57,6 +57,14 @@ def get_integration(request, export_type):

@group_required('user')
def import_recipe(request):
if request.space.max_recipes != 0 and Recipe.objects.filter(space=request.space).count() >= request.space.max_recipes: # TODO move to central helper function
messages.add_message(request, messages.WARNING, _('You have reached the maximum number of recipes for your space.'))
return HttpResponseRedirect(reverse('index'))

if request.space.max_users != 0 and UserPreference.objects.filter(space=request.space).count() > request.space.max_users:
messages.add_message(request, messages.WARNING, _('You have more users than allowed in your space.'))
return HttpResponseRedirect(reverse('index'))

if request.method == "POST":
form = ImportForm(request.POST, request.FILES)
if form.is_valid():
Expand Down
2 changes: 1 addition & 1 deletion cookbook/views/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RecipeCreate(GroupRequiredMixin, CreateView):
fields = ('name',)

def form_valid(self, form):
if self.request.space.max_recipes != 0 and Recipe.objects.filter(space=self.request.space).count() >= self.request.space.max_recipes:
if self.request.space.max_recipes != 0 and Recipe.objects.filter(space=self.request.space).count() >= self.request.space.max_recipes: # TODO move to central helper function
messages.add_message(self.request, messages.WARNING, _('You have reached the maximum number of recipes for your space.'))
return HttpResponseRedirect(reverse('index'))

Expand Down

0 comments on commit 388ef32

Please sign in to comment.