From 3bc1daa72ee01eec8f6037d7a06f0142f2d94bbe Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 30 Nov 2021 18:48:07 +0100 Subject: [PATCH] fixed import --- cookbook/views/api.py | 71 ++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/cookbook/views/api.py b/cookbook/views/api.py index bb404ac45..8e35b3339 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -36,7 +36,7 @@ from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest, Cus CustomIsShare, CustomIsShared, CustomIsUser, group_required) from cookbook.helper.recipe_html_import import get_recipe_from_source -from cookbook.helper.recipe_search import get_facet, search_recipes +from cookbook.helper.recipe_search import get_facet, search_recipes, old_search from cookbook.helper.recipe_url_import import get_from_scraper from cookbook.models import (Automation, BookmarkletImport, CookLog, Food, ImportLog, Ingredient, Keyword, MealPlan, MealType, Recipe, RecipeBook, RecipeBookEntry, @@ -47,7 +47,7 @@ from cookbook.provider.dropbox import Dropbox from cookbook.provider.local import Local from cookbook.provider.nextcloud import Nextcloud -from cookbook.schemas import FilterSchema, QueryOnlySchema, RecipeSchema, TreeSchema,QueryParamAutoSchema +from cookbook.schemas import FilterSchema, TreeSchema, QueryParamAutoSchema, QueryParam from cookbook.serializer import (AutomationSerializer, BookmarkletImportSerializer, CookLogSerializer, FoodSerializer, ImportLogSerializer, @@ -110,7 +110,8 @@ class FuzzyFilterMixin(ViewSetMixin): if fuzzy: self.queryset = ( self.queryset - .annotate(exact=Case(When(name__iexact=query, then=(Value(100))), default=Value(0))) # put exact matches at the top of the result set + .annotate(exact=Case(When(name__iexact=query, then=(Value(100))), + default=Value(0))) # put exact matches at the top of the result set .annotate(trigram=TrigramSimilarity('name', query)).filter(trigram__gt=0.2) .order_by('-exact', '-trigram') ) @@ -118,7 +119,8 @@ class FuzzyFilterMixin(ViewSetMixin): # TODO have this check unaccent search settings or other search preferences? self.queryset = ( self.queryset - .annotate(exact=Case(When(name__iexact=query, then=(Value(100))), default=Value(0))) # put exact matches at the top of the result set + .annotate(exact=Case(When(name__iexact=query, then=(Value(100))), + default=Value(0))) # put exact matches at the top of the result set .filter(name__icontains=query).order_by('-exact', 'name') ) @@ -202,7 +204,8 @@ class MergeMixin(ViewSetMixin): source.delete() return Response(content, status=status.HTTP_200_OK) except Exception: - content = {'error': True, 'msg': _(f'An error occurred attempting to merge {source.name} with {target.name}')} + content = {'error': True, + 'msg': _(f'An error occurred attempting to merge {source.name} with {target.name}')} return Response(content, status=status.HTTP_400_BAD_REQUEST) @@ -410,7 +413,8 @@ class RecipeBookViewSet(viewsets.ModelViewSet, StandardFilterMixin): permission_classes = [CustomIsOwner] def get_queryset(self): - self.queryset = self.queryset.filter(Q(created_by=self.request.user) | Q(shared=self.request.user)).filter(space=self.request.space).distinct() + self.queryset = self.queryset.filter(Q(created_by=self.request.user) | Q(shared=self.request.user)).filter( + space=self.request.space).distinct() return super().get_queryset() @@ -428,7 +432,9 @@ class RecipeBookEntryViewSet(viewsets.ModelViewSet, viewsets.GenericViewSet): permission_classes = [CustomIsOwner] def get_queryset(self): - queryset = self.queryset.filter(Q(book__created_by=self.request.user) | Q(book__shared=self.request.user)).filter(book__space=self.request.space).distinct() + queryset = self.queryset.filter( + Q(book__created_by=self.request.user) | Q(book__shared=self.request.user)).filter( + book__space=self.request.space).distinct() recipe_id = self.request.query_params.get('recipe', None) if recipe_id is not None: @@ -500,7 +506,8 @@ class StepViewSet(viewsets.ModelViewSet): permission_classes = [CustomIsUser] pagination_class = DefaultPagination query_params = [ - QueryParam(name='recipe', description=_('ID of recipe a step is part of. For multiple repeat parameter.'), qtype='int'), + QueryParam(name='recipe', description=_('ID of recipe a step is part of. For multiple repeat parameter.'), + qtype='int'), QueryParam(name='query', description=_('Query string matched (fuzzy) against object name.'), qtype='string'), ] schema = QueryParamAutoSchema() @@ -511,7 +518,7 @@ class StepViewSet(viewsets.ModelViewSet): if len(recipes) > 0: self.queryset = self.queryset.filter(recipe__in=recipes) if query is not None: - queryset = queryset.filter(Q(name__icontains=query) | Q(recipe__name__icontains=query)) + self.queryset = self.queryset.filter(Q(name__icontains=query) | Q(recipe__name__icontains=query)) return self.queryset.filter(recipe__space=self.request.space) @@ -540,20 +547,29 @@ class RecipeViewSet(viewsets.ModelViewSet): # TODO split read and write permission for meal plan guest permission_classes = [CustomIsShare | CustomIsGuest] pagination_class = RecipePagination -# TODO the boolean params below (keywords_or through new) should be updated to boolean types with front end refactored accordingly + # TODO the boolean params below (keywords_or through new) should be updated to boolean types with front end refactored accordingly query_params = [ - QueryParam(name='query', description=_('Query string matched (fuzzy) against recipe name. In the future also fulltext search.')), - QueryParam(name='keywords', description=_('ID of keyword a recipe should have. For multiple repeat parameter.'), qtype='int'), - QueryParam(name='foods', description=_('ID of food a recipe should have. For multiple repeat parameter.'), qtype='int'), + QueryParam(name='query', description=_( + 'Query string matched (fuzzy) against recipe name. In the future also fulltext search.')), + QueryParam(name='keywords', description=_('ID of keyword a recipe should have. For multiple repeat parameter.'), + qtype='int'), + QueryParam(name='foods', description=_('ID of food a recipe should have. For multiple repeat parameter.'), + qtype='int'), QueryParam(name='units', description=_('ID of unit a recipe should have.'), qtype='int'), QueryParam(name='rating', description=_('Rating a recipe should have. [0 - 5]'), qtype='int'), QueryParam(name='books', description=_('ID of book a recipe should be in. For multiple repeat parameter.')), - QueryParam(name='keywords_or', description=_('If recipe should have all (AND=''false'') or any (OR=''true'') of the provided keywords.')), - QueryParam(name='foods_or', description=_('If recipe should have all (AND=''false'') or any (OR=''true'') of the provided foods.')), - QueryParam(name='books_or', description=_('If recipe should be in all (AND=''false'') or any (OR=''true'') of the provided books.')), - QueryParam(name='internal', description=_('If only internal recipes should be returned. [''true''/''false'']')), - QueryParam(name='random', description=_('Returns the results in randomized order. [''true''/''false'']')), - QueryParam(name='new', description=_('Returns new results first in search results. [''true''/''false'']')), + QueryParam(name='keywords_or', description=_( + 'If recipe should have all (AND=''false'') or any (OR=''true'') of the provided keywords.')), + QueryParam(name='foods_or', description=_( + 'If recipe should have all (AND=''false'') or any (OR=''true'') of the provided foods.')), + QueryParam(name='books_or', description=_( + 'If recipe should be in all (AND=''false'') or any (OR=''true'') of the provided books.')), + QueryParam(name='internal', + description=_('If only internal recipes should be returned. [''true''/''false'']')), + QueryParam(name='random', + description=_('Returns the results in randomized order. [''true''/''false'']')), + QueryParam(name='new', + description=_('Returns new results first in search results. [''true''/''false'']')), ] schema = QueryParamAutoSchema() @@ -628,12 +644,17 @@ class ShoppingListEntryViewSet(viewsets.ModelViewSet): serializer_class = ShoppingListEntrySerializer permission_classes = [CustomIsOwner | CustomIsShared] query_params = [ - QueryParam(name='id', description=_('Returns the shopping list entry with a primary key of id. Multiple values allowed.'), qtype='int'), + QueryParam(name='id', + description=_('Returns the shopping list entry with a primary key of id. Multiple values allowed.'), + qtype='int'), QueryParam( name='checked', - description=_('Filter shopping list entries on checked. [''true'', ''false'', ''both'', ''recent'']
- ''recent'' includes unchecked items and recently completed items.') + description=_( + 'Filter shopping list entries on checked. [''true'', ''false'', ''both'', ''recent'']
- ''recent'' includes unchecked items and recently completed items.') ), - QueryParam(name='supermarket', description=_('Returns the shopping list entries sorted by supermarket category order.'), qtype='int'), + QueryParam(name='supermarket', + description=_('Returns the shopping list entries sorted by supermarket category order.'), + qtype='int'), ] schema = QueryParamAutoSchema() @@ -764,7 +785,8 @@ def get_recipe_file(request, recipe_id): @group_required('user') def sync_all(request): if request.space.demo or settings.HOSTED: - messages.add_message(request, messages.ERROR, _('This feature is not yet available in the hosted version of tandoor!')) + messages.add_message(request, messages.ERROR, + _('This feature is not yet available in the hosted version of tandoor!')) return redirect('index') monitors = Sync.objects.filter(active=True).filter(space=request.user.userpreference.space) @@ -801,7 +823,8 @@ def share_link(request, pk): if request.space.allow_sharing: recipe = get_object_or_404(Recipe, pk=pk, space=request.space) link = ShareLink.objects.create(recipe=recipe, created_by=request.user, space=request.space) - return JsonResponse({'pk': pk, 'share': link.uuid, 'link': request.build_absolute_uri(reverse('view_recipe', args=[pk, link.uuid]))}) + return JsonResponse({'pk': pk, 'share': link.uuid, + 'link': request.build_absolute_uri(reverse('view_recipe', args=[pk, link.uuid]))}) else: return JsonResponse({'error': 'sharing_disabled'}, status=403)