diff --git a/cookbook/admin.py b/cookbook/admin.py index d4cc533d..1df9f1b9 100644 --- a/cookbook/admin.py +++ b/cookbook/admin.py @@ -7,8 +7,7 @@ from .models import (Comment, CookLog, Food, Ingredient, InviteLink, Keyword, RecipeBook, RecipeBookEntry, RecipeImport, ShareLink, ShoppingList, ShoppingListEntry, ShoppingListRecipe, Space, Step, Storage, Sync, SyncLog, Unit, UserPreference, - ViewLog, Supermarket, SupermarketCategory, SupermarketCategoryRelation, - ImportLog, TelegramBot, BookmarkletImport) + ViewLog, Supermarket, SupermarketCategory, SupermarketCategoryRelation, ImportLog, TelegramBot) class CustomUserAdmin(UserAdmin): @@ -223,13 +222,6 @@ class ImportLogAdmin(admin.ModelAdmin): admin.site.register(ImportLog, ImportLogAdmin) -class BookmarkletImportAdmin(admin.ModelAdmin): - list_display = ('id', 'url', 'created_by', 'created_at',) - - -admin.site.register(BookmarkletImport, BookmarkletImportAdmin) - - class TelegramBotAdmin(admin.ModelAdmin): list_display = ('id', 'name', 'created_by',) diff --git a/cookbook/filters.py b/cookbook/filters.py index a8f5976d..b679a79f 100644 --- a/cookbook/filters.py +++ b/cookbook/filters.py @@ -61,6 +61,7 @@ with scopes_disabled(): model = Recipe fields = ['name', 'keywords', 'foods', 'internal'] + class FoodFilter(django_filters.FilterSet): name = django_filters.CharFilter(lookup_expr='icontains') @@ -68,6 +69,7 @@ with scopes_disabled(): model = Food fields = ['name'] + class ShoppingListFilter(django_filters.FilterSet): def __init__(self, data=None, *args, **kwargs): diff --git a/cookbook/helper/recipe_html_import.py b/cookbook/helper/recipe_html_import.py index 93300a29..3b06dc80 100644 --- a/cookbook/helper/recipe_html_import.py +++ b/cookbook/helper/recipe_html_import.py @@ -75,9 +75,11 @@ def get_recipe_from_source(text, url, space): images = [] text = unquote(text) - # text = normalize_string(text) try: parse_list.append(remove_graph(json.loads(text))) + if not url and 'url' in parse_list[0]: + url = parse_list[0]['url'] + scrape = text_scraper("", url=url) except JSONDecodeError: soup = BeautifulSoup(text, "html.parser") @@ -85,6 +87,8 @@ def get_recipe_from_source(text, url, space): images += get_images_from_source(soup, url) for el in soup.find_all('script', type='application/ld+json'): el = remove_graph(el) + if not url and 'url' in el: + url = el['url'] if type(el) == list: for le in el: parse_list.append(le) @@ -97,15 +101,6 @@ def get_recipe_from_source(text, url, space): parse_list.append(le) elif type(el) == dict: parse_list.append(el) - - # if a url was not provided, try to find one in the first document - if not url and len(parse_list) > 0: - if 'url' in parse_list[0]: - url = parse_list[0]['url'] - - if type(text) == dict: - scrape = text_scraper("", url=url) - elif type(text) == str: scrape = text_scraper(text, url=url) recipe_json = helper.get_from_scraper(scrape, space) diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index eb93be70..15c53f8a 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -294,11 +294,11 @@ def parse_keywords(keyword_json, space): # keywords as list for kw in keyword_json: kw = normalize_string(kw) - if k := Keyword.objects.filter(name=kw, space=space).first(): - if len(k) != 0: + if len(kw) != 0: + if k := Keyword.objects.filter(name=kw, space=space).first(): keywords.append({'id': str(k.id), 'text': str(k)}) - else: - keywords.append({'id': random.randrange(1111111, 9999999, 1), 'text': kw}) + else: + keywords.append({'id': random.randrange(1111111, 9999999, 1), 'text': kw}) return keywords diff --git a/cookbook/migrations/0120_bookmarklet.py b/cookbook/migrations/0120_bookmarklet.py deleted file mode 100644 index 903978ac..00000000 --- a/cookbook/migrations/0120_bookmarklet.py +++ /dev/null @@ -1,34 +0,0 @@ -# Generated by Django 3.1.7 on 2021-03-29 11:05 - -import cookbook.models -from django.conf import settings -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('cookbook', '0119_auto_20210411_2101'), - ] - - operations = [ - migrations.AlterField( - model_name='userpreference', - name='use_fractions', - field=models.BooleanField(default=True), - ), - migrations.CreateModel( - name='BookmarkletImport', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('html', models.TextField()), - ('url', models.CharField(blank=True, max_length=128, null=True)), - ('created_at', models.DateTimeField(auto_now_add=True)), - ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), - ('space', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cookbook.space')), - ], - bases=(models.Model, cookbook.models.PermissionModelMixin), - ), - ] diff --git a/cookbook/models.py b/cookbook/models.py index 26a84d26..75d624b2 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -671,13 +671,3 @@ class ImportLog(models.Model, PermissionModelMixin): def __str__(self): return f"{self.created_at}:{self.type}" - - -class BookmarkletImport(models.Model, PermissionModelMixin): - html = models.TextField() - url = models.CharField(max_length=128, null=True, blank=True) - created_at = models.DateTimeField(auto_now_add=True) - created_by = models.ForeignKey(User, on_delete=models.CASCADE) - - objects = ScopedManager(space='space') - space = models.ForeignKey(Space, on_delete=models.CASCADE) \ No newline at end of file diff --git a/cookbook/serializer.py b/cookbook/serializer.py index c74cede3..fe2db438 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -14,8 +14,7 @@ from cookbook.models import (Comment, CookLog, Food, Ingredient, Keyword, RecipeBook, RecipeBookEntry, RecipeImport, ShareLink, ShoppingList, ShoppingListEntry, ShoppingListRecipe, Step, Storage, Sync, SyncLog, - Unit, UserPreference, ViewLog, SupermarketCategory, - Supermarket, SupermarketCategoryRelation, ImportLog, BookmarkletImport) + Unit, UserPreference, ViewLog, SupermarketCategory, Supermarket, SupermarketCategoryRelation, ImportLog) from cookbook.templatetags.custom_tags import markdown @@ -467,20 +466,6 @@ class ImportLogSerializer(serializers.ModelSerializer): read_only_fields = ('created_by',) -# CORS, REST and Scopes aren't currently working -# Scopes are evaluating before REST has authenticated the user assiging a None space -# I've made the change below to fix the bookmarklet, other serializers likely need a similar/better fix -class BookmarkletImportSerializer(serializers.ModelSerializer): - def create(self, validated_data): - validated_data['created_by'] = self.context['request'].user - validated_data['space'] = self.context['request'].user.userpreference.space - return super().create(validated_data) - - class Meta: - model = BookmarkletImport - fields = ('id', 'url', 'html', 'created_by', 'created_at') - read_only_fields = ('created_by', 'space') - # Export/Import Serializers class KeywordExportSerializer(KeywordSerializer): diff --git a/cookbook/static/js/bookmarklet.js b/cookbook/static/js/bookmarklet.js deleted file mode 100644 index 156ba1ce..00000000 --- a/cookbook/static/js/bookmarklet.js +++ /dev/null @@ -1,47 +0,0 @@ -(function(){ - - var v = "1.3.2"; - - if (window.jQuery === undefined || window.jQuery.fn.jquery < v) { - var done = false; - var script = document.createElement("script"); - script.src = "https://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js"; - script.onload = script.onreadystatechange = function(){ - if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) { - done = true; - initBookmarklet(); - } - }; - document.getElementsByTagName("head")[0].appendChild(script); - } else { - initBookmarklet(); - } - function initBookmarklet() { - (window.bookmarkletTandoor = function() { - let recipe = document.documentElement.innerHTML - let windowName = "ImportRecipe" - let url = localStorage.getItem('importURL') - let redirect = localStorage.getItem('redirectURL') - let token = localStorage.getItem('token') - let params = { 'html' : recipe,'url': window.location.href}; - - const xhr = new XMLHttpRequest(); - xhr.open('POST', url, true); - xhr.setRequestHeader('Content-Type', 'application/json'); - xhr.setRequestHeader('Authorization', 'Token ' + token); - - // listen for `onload` event - xhr.onload = () => { - // process response - if (xhr.readyState == 4 && xhr.status == 201) { - // parse JSON data - window.open(redirect.concat('?id=', JSON.parse(xhr.response).id) ) - } else { - console.error('Error!'); - } - }; - xhr.send(JSON.stringify(params)); - } - )(); - } -})(); diff --git a/cookbook/templates/base.html b/cookbook/templates/base.html index 87666383..b0b49cfb 100644 --- a/cookbook/templates/base.html +++ b/cookbook/templates/base.html @@ -13,8 +13,8 @@ - - + + @@ -24,10 +24,9 @@ - - - - + + + diff --git a/cookbook/templates/import_json_working.html b/cookbook/templates/import_json_working.html deleted file mode 100644 index 6c2d6fce..00000000 --- a/cookbook/templates/import_json_working.html +++ /dev/null @@ -1,385 +0,0 @@ - -{% extends "base.html" %} -{% load crispy_forms_filters %} -{% load i18n %} -{% load static %} - -{% block title %}{% trans 'Import Recipe' %}{% endblock %} - -{% block extra_head %} - {% include 'include/vue_base.html' %} - - - - - -{% endblock %} - - -{% block content %} -
this is the recipe form -
-something terrible happened -
- - -{% endblock %} - -{% block script %} - - - - -{% endblock %} \ No newline at end of file diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html index eb3a502e..99b1f6b7 100644 --- a/cookbook/templates/url_import.html +++ b/cookbook/templates/url_import.html @@ -1,7 +1,6 @@ {% extends "base.html" %} {% load i18n %} {% load static %} -{% load custom_tags %} {% block title %}{% trans 'URL Import' %}{% endblock %} @@ -21,15 +20,9 @@ {% endblock %} {% block content %} +