From 9a62b6e4e7b1a304887ce5a5f20a1eea8db794f6 Mon Sep 17 00:00:00 2001 From: smilerz Date: Fri, 26 Mar 2021 14:47:24 -0500 Subject: [PATCH] added bookmarklet model and api --- cookbook/admin.py | 9 +++++- cookbook/migrations/0117_bookmarkletimport.py | 29 +++++++++++++++++++ cookbook/models.py | 10 +++++++ cookbook/serializer.py | 14 ++++++++- cookbook/urls.py | 2 +- cookbook/views/api.py | 25 ++++++++-------- 6 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 cookbook/migrations/0117_bookmarkletimport.py diff --git a/cookbook/admin.py b/cookbook/admin.py index 1df9f1b9..ed145623 100644 --- a/cookbook/admin.py +++ b/cookbook/admin.py @@ -7,7 +7,8 @@ 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) + ViewLog, Supermarket, SupermarketCategory, SupermarketCategoryRelation, + ImportLog, TelegramBot, BookmarkletImport) class CustomUserAdmin(UserAdmin): @@ -222,6 +223,12 @@ 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/migrations/0117_bookmarkletimport.py b/cookbook/migrations/0117_bookmarkletimport.py new file mode 100644 index 00000000..7b023017 --- /dev/null +++ b/cookbook/migrations/0117_bookmarkletimport.py @@ -0,0 +1,29 @@ +# Generated by Django 3.1.7 on 2021-03-26 17:42 + +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', '0116_auto_20210319_0012'), + ] + + operations = [ + 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 75d624b2..26a84d26 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -671,3 +671,13 @@ 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 fe2db438..d9ae6a50 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -14,7 +14,8 @@ 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) + Unit, UserPreference, ViewLog, SupermarketCategory, + Supermarket, SupermarketCategoryRelation, ImportLog, BookmarkletImport) from cookbook.templatetags.custom_tags import markdown @@ -466,6 +467,17 @@ class ImportLogSerializer(serializers.ModelSerializer): read_only_fields = ('created_by',) +class BookmarkletImportSerializer(serializers.ModelSerializer): + def create(self, validated_data): + validated_data['created_by'] = self.context['request'].user + validated_data['space'] = self.context['request'].space + return super().create(validated_data) + + class Meta: + model = BookmarkletImport + fields = ('id', 'url', 'html', 'created_by', 'created_at') + read_only_fields = ('created_by',) + # Export/Import Serializers class KeywordExportSerializer(KeywordSerializer): diff --git a/cookbook/urls.py b/cookbook/urls.py index 8bbf1ef6..f9810aa0 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -36,6 +36,7 @@ router.register(r'recipe-book', api.RecipeBookViewSet) router.register(r'recipe-book-entry', api.RecipeBookEntryViewSet) router.register(r'supermarket', api.SupermarketViewSet) router.register(r'import-log', api.ImportLogViewSet) +router.register(r'bookmarklet-import', api.BookmarkletImportViewSet) urlpatterns = [ path('', views.index, name='index'), @@ -96,7 +97,6 @@ urlpatterns = [ path('api/recipe-from-source/', api.recipe_from_source, name='api_recipe_from_source'), path('api/backup/', api.get_backup, name='api_backup'), path('api/ingredient-from-string/', api.ingredient_from_string, name='api_ingredient_from_string'), - path('api/bookmarklet/', api.bookmarklet, name='api_bookmarklet'), path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'), path('dal/food/', dal.IngredientsAutocomplete.as_view(), name='dal_food'), diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 334e51f6..b7020dc5 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -36,7 +36,7 @@ from cookbook.models import (CookLog, Food, Ingredient, Keyword, MealPlan, MealType, Recipe, RecipeBook, ShoppingList, ShoppingListEntry, ShoppingListRecipe, Step, Storage, Sync, SyncLog, Unit, UserPreference, - ViewLog, RecipeBookEntry, Supermarket, ImportLog) + ViewLog, RecipeBookEntry, Supermarket, ImportLog, BookmarkletImport) from cookbook.provider.dropbox import Dropbox from cookbook.provider.local import Local from cookbook.provider.nextcloud import Nextcloud @@ -51,7 +51,9 @@ from cookbook.serializer import (FoodSerializer, IngredientSerializer, StorageSerializer, SyncLogSerializer, SyncSerializer, UnitSerializer, UserNameSerializer, UserPreferenceSerializer, - ViewLogSerializer, CookLogSerializer, RecipeBookEntrySerializer, RecipeOverviewSerializer, SupermarketSerializer, ImportLogSerializer) + ViewLogSerializer, CookLogSerializer, + RecipeBookEntrySerializer, RecipeOverviewSerializer, + SupermarketSerializer, ImportLogSerializer, BookmarkletImportSerializer) from recipes.settings import DEMO from recipe_scrapers import scrape_me, WebsiteNotImplementedError, NoSchemaFoundInWildMode @@ -412,6 +414,15 @@ class ImportLogViewSet(viewsets.ModelViewSet): return self.queryset.filter(space=self.request.space).all() +class BookmarkletImportViewSet(viewsets.ModelViewSet): + queryset = BookmarkletImport.objects + serializer_class = BookmarkletImportSerializer + permission_classes = [CustomIsUser] + + def get_queryset(self): + return self.queryset.filter(space=self.request.space).all() + + # -------------- non django rest api views -------------------- def get_recipe_provider(recipe): @@ -657,13 +668,3 @@ def ingredient_from_string(request): }, status=200 ) - - -@group_required('user') -@csrf_exempt -def bookmarklet(request): - if request.method == "POST": - if 'recipe' in request.POST: - recipe_json, recipe_tree, recipe_html, images = get_recipe_from_source(request.POST['recipe'], request.POST['url'], request.space) - return render(request, 'url_import.html', {'recipe_json': recipe_json, 'recipe_tree': recipe_tree, 'recipe_html': recipe_html, 'preview': 'true'}) - return HttpResponseRedirect(reverse('view_search'))