diff --git a/cookbook/migrations/0026_auto_20200219_1605.py b/cookbook/migrations/0026_auto_20200219_1605.py new file mode 100644 index 000000000..70aff6a66 --- /dev/null +++ b/cookbook/migrations/0026_auto_20200219_1605.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.2 on 2020-02-19 15:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0025_userpreference_nav_color'), + ] + + operations = [ + migrations.AddField( + model_name='recipe', + name='cors_link', + field=models.CharField(blank=True, max_length=1024, null=True), + ), + migrations.AlterField( + model_name='recipe', + name='link', + field=models.CharField(blank=True, max_length=512, null=True), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index d40da63ec..cb3a6d17d 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -84,7 +84,8 @@ class Recipe(models.Model): storage = models.ForeignKey(Storage, on_delete=models.PROTECT, blank=True, null=True) file_uid = models.CharField(max_length=256, default="") file_path = models.CharField(max_length=512, default="") - link = models.CharField(max_length=512, default="") + link = models.CharField(max_length=512, null=True, blank=True) + cors_link = models.CharField(max_length=1024, null=True, blank=True) keywords = models.ManyToManyField(Keyword, blank=True) working_time = models.IntegerField(default=0) waiting_time = models.IntegerField(default=0) diff --git a/cookbook/provider/dropbox.py b/cookbook/provider/dropbox.py index 8d31b8a5b..c6641202e 100644 --- a/cookbook/provider/dropbox.py +++ b/cookbook/provider/dropbox.py @@ -88,6 +88,16 @@ class Dropbox(Provider): response = Dropbox.create_share_link(recipe) return response['url'] + @staticmethod + def get_cors_link(recipe): + if not recipe.link: + recipe.link = Dropbox.get_share_link(recipe) + recipe.save() + + recipe.cors_link = recipe.link.replace('www.dropbox.', 'dl.dropboxusercontent.') + + return recipe.cors_link + @staticmethod def rename_file(recipe, new_name): url = "https://api.dropboxapi.com/2/files/move_v2" diff --git a/cookbook/provider/provider.py b/cookbook/provider/provider.py index cecb7728b..ad84430ec 100644 --- a/cookbook/provider/provider.py +++ b/cookbook/provider/provider.py @@ -11,10 +11,14 @@ class Provider: def get_share_link(recipe): raise Exception('Method not implemented in storage provider') + @staticmethod + def get_cors_link(recipe): + raise Exception('Method not implemented in storage provider') + @staticmethod def rename_file(recipe, new_name): raise Exception('Method not implemented in storage provider') @staticmethod - def delete_file(recipe, new_name): + def delete_file(recipe): raise Exception('Method not implemented in storage provider') diff --git a/cookbook/tables.py b/cookbook/tables.py index aba98fec3..0282bf8bd 100644 --- a/cookbook/tables.py +++ b/cookbook/tables.py @@ -8,8 +8,7 @@ from .models import * class RecipeTable(tables.Table): id = tables.LinkColumn('edit_recipe', args=[A('id')]) - name = tables.TemplateColumn( - "{{record.name}}") + name = tables.LinkColumn('view_recipe', args=[A('id')]) all_tags = tables.Column( attrs={'td': {'class': 'd-none d-lg-table-cell'}, 'th': {'class': 'd-none d-lg-table-cell'}}) diff --git a/cookbook/templates/books.html b/cookbook/templates/books.html index c22c41870..b1186c68c 100644 --- a/cookbook/templates/books.html +++ b/cookbook/templates/books.html @@ -41,7 +41,7 @@ {% for r in b.recipes %}
-
  • {{ r.recipe.name }}
  • +
  • {{ r.recipe.name }}
  • @@ -58,5 +58,4 @@
    {% endfor %} - {% include 'include/recipe_open_modal.html' %} {% endblock %} \ No newline at end of file diff --git a/cookbook/templates/include/recipe_open_modal.html b/cookbook/templates/include/recipe_open_modal.html index c377bbbb3..6cbb78ac4 100644 --- a/cookbook/templates/include/recipe_open_modal.html +++ b/cookbook/templates/include/recipe_open_modal.html @@ -43,15 +43,12 @@
    {% endif %}
    diff --git a/cookbook/urls.py b/cookbook/urls.py index 1bd66b365..c0cdc0de8 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -60,8 +60,8 @@ urlpatterns = [ path('data/sync/wait', data.sync_wait, name='data_sync_wait'), path('data/statistics', data.statistics, name='data_stats'), - path('api/get_file_link//', api.get_file_link, name='api_get_file_link'), path('api/get_external_file_link//', api.get_external_file_link, name='api_get_external_file_link'), + path('api/get_cors_file_link//', api.get_cors_file_link, name='api_get_cors_file_link'), path('api/sync_all/', api.sync_all, name='api_sync'), diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 41df185f5..12405048f 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -10,40 +10,43 @@ from cookbook.provider.dropbox import Dropbox from cookbook.provider.nextcloud import Nextcloud -@login_required -def get_file_link(request, recipe_id): - recipe = Recipe.objects.get(id=recipe_id) +def update_recipe_links(recipe): + if recipe.storage.method == Storage.DROPBOX: + provider = Dropbox + elif recipe.storage.method == Storage.NEXTCLOUD: + provider = Nextcloud + else: + raise Exception('Provider not implemented') - if recipe.internal: - return HttpResponse(reverse('view_recipe', args=[recipe_id])) - if recipe.storage.method == Storage.DROPBOX: # TODO move to central location (as all provider related functions) - if recipe.link == "": - recipe.link = Dropbox.get_share_link(recipe) # TODO response validation - recipe.save() - if recipe.storage.method == Storage.NEXTCLOUD: - if recipe.link == "": - recipe.link = Nextcloud.get_share_link(recipe) # TODO response validation - recipe.save() + if not recipe.link: + recipe.link = provider.get_share_link(recipe) # TODO response validation in apis + if not recipe.cors_link: + try: + recipe.cors_link = provider.get_cors_link(recipe) + except Exception: + pass - return HttpResponse(recipe.link) + recipe.save() @login_required def get_external_file_link(request, recipe_id): recipe = Recipe.objects.get(id=recipe_id) - - if recipe.storage.method == Storage.DROPBOX: # TODO move to central location (as all provider related functions) - if recipe.link == "": - recipe.link = Dropbox.get_share_link(recipe) # TODO response validation - recipe.save() - if recipe.storage.method == Storage.NEXTCLOUD: - if recipe.link == "": - recipe.link = Nextcloud.get_share_link(recipe) # TODO response validation - recipe.save() + if not recipe.link: + update_recipe_links(recipe) return HttpResponse(recipe.link) +@login_required +def get_cors_file_link(request, recipe_id): + recipe = Recipe.objects.get(id=recipe_id) + if not recipe.cors_link: + update_recipe_links(recipe) + + return HttpResponse(recipe.cors_link) + + @login_required def sync_all(request): monitors = Sync.objects.filter(active=True)