From 78b1386a1cbdeb007a84e70bd87606b6efe2ebdd Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 13 Sep 2023 09:35:22 -0500 Subject: [PATCH] remove unused imports, variables and commented code from integrations and templatetags --- cookbook/integration/cookbookapp.py | 3 +-- cookbook/integration/cookmate.py | 9 ++------- cookbook/integration/copymethat.py | 17 ++++++++++++----- cookbook/integration/default.py | 2 +- cookbook/templatetags/custom_tags.py | 17 ++++++++++------- cookbook/templatetags/theming_tags.py | 5 +++-- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/cookbook/integration/cookbookapp.py b/cookbook/integration/cookbookapp.py index 077c32801..21d9d7f30 100644 --- a/cookbook/integration/cookbookapp.py +++ b/cookbook/integration/cookbookapp.py @@ -20,7 +20,6 @@ class CookBookApp(Integration): def get_recipe_from_file(self, file): recipe_html = file.getvalue().decode("utf-8") - # recipe_json, recipe_tree, html_data, images = get_recipe_from_source(recipe_html, 'CookBookApp', self.request) scrape = text_scraper(text=recipe_html) recipe_json = get_from_scraper(scrape, self.request) images = list(dict.fromkeys(get_images_from_soup(scrape.soup, None))) @@ -32,7 +31,7 @@ class CookBookApp(Integration): try: recipe.servings = re.findall('([0-9])+', recipe_json['recipeYield'])[0] - except Exception as e: + except Exception: pass try: diff --git a/cookbook/integration/cookmate.py b/cookbook/integration/cookmate.py index 58fd08244..a51ca45b9 100644 --- a/cookbook/integration/cookmate.py +++ b/cookbook/integration/cookmate.py @@ -1,17 +1,12 @@ -import base64 -import json from io import BytesIO -from gettext import gettext as _ - import requests import validators -from lxml import etree from cookbook.helper.ingredient_parser import IngredientParser -from cookbook.helper.recipe_url_import import parse_servings, parse_time, parse_servings_text +from cookbook.helper.recipe_url_import import parse_servings, parse_servings_text, parse_time from cookbook.integration.integration import Integration -from cookbook.models import Ingredient, Keyword, Recipe, Step +from cookbook.models import Ingredient, Recipe, Step class Cookmate(Integration): diff --git a/cookbook/integration/copymethat.py b/cookbook/integration/copymethat.py index 21ea66521..01d51c844 100644 --- a/cookbook/integration/copymethat.py +++ b/cookbook/integration/copymethat.py @@ -1,4 +1,3 @@ -import re from io import BytesIO from zipfile import ZipFile @@ -26,12 +25,13 @@ class CopyMeThat(Integration): except AttributeError: source = None - recipe = Recipe.objects.create(name=file.find("div", {"id": "name"}).text.strip()[:128], source_url=source, created_by=self.request.user, internal=True, space=self.request.space, ) + recipe = Recipe.objects.create(name=file.find("div", {"id": "name"}).text.strip( + )[:128], source_url=source, created_by=self.request.user, internal=True, space=self.request.space, ) for category in file.find_all("span", {"class": "recipeCategory"}): keyword, created = Keyword.objects.get_or_create(name=category.text, space=self.request.space) recipe.keywords.add(keyword) - + try: recipe.servings = parse_servings(file.find("a", {"id": "recipeYield"}).text.strip()) recipe.working_time = iso_duration_to_minutes(file.find("span", {"meta": "prepTime"}).text.strip()) @@ -61,7 +61,14 @@ class CopyMeThat(Integration): if not isinstance(ingredient, Tag) or not ingredient.text.strip() or "recipeIngredient_spacer" in ingredient['class']: continue if any(x in ingredient['class'] for x in ["recipeIngredient_subheader", "recipeIngredient_note"]): - step.ingredients.add(Ingredient.objects.create(is_header=True, note=ingredient.text.strip()[:256], original_text=ingredient.text.strip(), space=self.request.space, )) + step.ingredients.add( + Ingredient.objects.create( + is_header=True, + note=ingredient.text.strip()[ + :256], + original_text=ingredient.text.strip(), + space=self.request.space, + )) else: amount, unit, food, note = ingredient_parser.parse(ingredient.text.strip()) f = ingredient_parser.get_food(food) @@ -78,7 +85,7 @@ class CopyMeThat(Integration): step.save() recipe.steps.add(step) step = Step.objects.create(instruction='', space=self.request.space, ) - + step.name = instruction.text.strip()[:128] else: step.instruction += instruction.text.strip() + ' \n\n' diff --git a/cookbook/integration/default.py b/cookbook/integration/default.py index 890caad52..00a157966 100644 --- a/cookbook/integration/default.py +++ b/cookbook/integration/default.py @@ -22,7 +22,7 @@ class Default(Integration): if images: try: self.import_recipe_image(recipe, BytesIO(recipe_zip.read(images[0])), filetype=get_filetype(images[0])) - except AttributeError as e: + except AttributeError: traceback.print_exc() return recipe diff --git a/cookbook/templatetags/custom_tags.py b/cookbook/templatetags/custom_tags.py index be8be581e..cdacd8e7e 100644 --- a/cookbook/templatetags/custom_tags.py +++ b/cookbook/templatetags/custom_tags.py @@ -3,19 +3,19 @@ from gettext import gettext as _ import bleach import markdown as md -from django_scopes import ScopeError -from markdown.extensions.tables import TableExtension from django import template from django.db.models import Avg from django.templatetags.static import static from django.urls import NoReverseMatch, reverse +from django_scopes import ScopeError +from markdown.extensions.tables import TableExtension from rest_framework.authtoken.models import Token from cookbook.helper.mdx_attributes import MarkdownFormatExtension from cookbook.helper.mdx_urlize import UrlizeExtension -from cookbook.models import Space, get_model_name +from cookbook.models import get_model_name from recipes import settings -from recipes.settings import STATIC_URL, PLUGINS +from recipes.settings import PLUGINS, STATIC_URL register = template.Library() @@ -69,8 +69,8 @@ def markdown(value): "a": ["href", "alt", "title"], } - parsed_md = parsed_md[3:] # remove outer paragraph - parsed_md = parsed_md[:len(parsed_md)-4] + parsed_md = parsed_md[3:] # remove outer paragraph + parsed_md = parsed_md[:len(parsed_md) - 4] return bleach.clean(parsed_md, tags, markdown_attrs) @@ -144,6 +144,7 @@ def is_debug(): def markdown_link(): return f"{_('You can use markdown to format this field. See the ')}{_('docs here')}" + @register.simple_tag def plugin_dropdown_nav_templates(): templates = [] @@ -152,6 +153,7 @@ def plugin_dropdown_nav_templates(): templates.append(p['nav_dropdown']) return templates + @register.simple_tag def plugin_main_nav_templates(): templates = [] @@ -199,7 +201,8 @@ def base_path(request, path_type): @register.simple_tag def user_prefs(request): - from cookbook.serializer import UserPreferenceSerializer # putting it with imports caused circular execution + from cookbook.serializer import \ + UserPreferenceSerializer # putting it with imports caused circular execution try: return UserPreferenceSerializer(request.user.userpreference, context={'request': request}).data except AttributeError: diff --git a/cookbook/templatetags/theming_tags.py b/cookbook/templatetags/theming_tags.py index da8c692e3..79175be2f 100644 --- a/cookbook/templatetags/theming_tags.py +++ b/cookbook/templatetags/theming_tags.py @@ -1,6 +1,7 @@ -from cookbook.models import UserPreference from django import template from django.templatetags.static import static + +from cookbook.models import UserPreference from recipes.settings import STICKY_NAV_PREF_DEFAULT register = template.Library() @@ -41,4 +42,4 @@ def sticky_nav(request): (request.user.is_authenticated and request.user.userpreference.sticky_navbar): # noqa: E501 return 'position: sticky; top: 0; left: 0; z-index: 1000;' else: - return '' \ No newline at end of file + return ''