From 8b6f7bd7cba248548fade65b0a14861d990de87d Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 6 Jun 2018 23:31:47 +0200 Subject: [PATCH] fixed link creation and updated model --- cookbook/forms.py | 8 +++---- cookbook/helper/dropbox.py | 12 +++++----- cookbook/migrations/0001_initial.py | 10 ++++---- .../migrations/0002_auto_20180606_2300.py | 23 ------------------- cookbook/models.py | 4 ++-- cookbook/tables.py | 4 ++-- cookbook/views/api.py | 2 +- cookbook/views/data.py | 2 +- cookbook/views/new.py | 4 ++-- 9 files changed, 23 insertions(+), 46 deletions(-) delete mode 100644 cookbook/migrations/0002_auto_20180606_2300.py diff --git a/cookbook/forms.py b/cookbook/forms.py index 34ecbda0..717f47b1 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -18,13 +18,13 @@ class EmojiWidget(forms.TextInput): class EditRecipeForm(forms.ModelForm): class Meta: model = Recipe - fields = ('name', 'category', 'keywords', 'path', 'storage', 'file_uid') + fields = ('name', 'category', 'keywords', 'file_path', 'storage', 'file_uid') labels = { 'name': _('Name'), 'category': _('Category'), 'keywords': _('Keywords'), - 'path': _('Path'), + 'file_path': _('Path'), 'file_uid': _('Storage UID'), } widgets = {'keywords': MultiSelectWidget} @@ -72,13 +72,13 @@ class BatchEditForm(forms.Form): class ImportRecipeForm(forms.ModelForm): class Meta: model = Recipe - fields = ('name', 'category', 'keywords', 'path', 'file_uid') + fields = ('name', 'category', 'keywords', 'file_path', 'file_uid') labels = { 'name': _('Name'), 'category': _('Category'), 'keywords': _('Keywords'), - 'path': _('Path'), + 'file_path': _('Path'), 'file_uid': _('File ID'), } widgets = {'keywords': MultiSelectWidget} diff --git a/cookbook/helper/dropbox.py b/cookbook/helper/dropbox.py index da6403f4..f20aeedc 100644 --- a/cookbook/helper/dropbox.py +++ b/cookbook/helper/dropbox.py @@ -31,9 +31,9 @@ def import_all(monitor): import_count = 0 for recipe in recipes['entries']: # TODO check if has_more is set and import that as well path = recipe['path_lower'] - if not Recipe.objects.filter(path=path).exists() and not RecipeImport.objects.filter(path=path).exists(): + if not Recipe.objects.filter(file_path=path).exists() and not RecipeImport.objects.filter(file_path=path).exists(): name = os.path.splitext(recipe['name'])[0] - new_recipe = RecipeImport(name=name, path=path, storage=monitor.storage, file_uid=recipe['id']) + new_recipe = RecipeImport(name=name, file_path=path, storage=monitor.storage, file_uid=recipe['id']) new_recipe.save() import_count += 1 @@ -46,16 +46,16 @@ def import_all(monitor): return True -def get_share_link(recipe_path): - url = "https://api.dropboxapi.com/2/sharing/create_shared_link" +def get_share_link(recipe): + url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings" headers = { - "Authorization": "Bearer " + settings.DROPBOX_API_KEY, + "Authorization": "Bearer " + recipe.storage.token, "Content-Type": "application/json" } data = { - "path": recipe_path + "path": recipe.file_uid } r = requests.post(url, headers=headers, data=json.dumps(data)) diff --git a/cookbook/migrations/0001_initial.py b/cookbook/migrations/0001_initial.py index a2e3e012..e39a95f1 100644 --- a/cookbook/migrations/0001_initial.py +++ b/cookbook/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.0.5 on 2018-06-06 20:53 +# Generated by Django 2.0.5 on 2018-06-06 21:31 from django.db import migrations, models import django.db.models.deletion @@ -41,8 +41,8 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=128)), - ('path', models.CharField(default='', max_length=512)), - ('storage_uid', models.CharField(default='', max_length=256)), + ('file_uid', models.CharField(default='', max_length=256)), + ('file_path', models.CharField(default='', max_length=512)), ('link', models.CharField(default='', max_length=512)), ('created_by', models.IntegerField(default=0)), ('created_at', models.DateTimeField(auto_now_add=True)), @@ -56,8 +56,8 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=128)), - ('storage_uid', models.CharField(default='', max_length=256)), - ('path', models.CharField(default='', max_length=512)), + ('file_uid', models.CharField(default='', max_length=256)), + ('file_path', models.CharField(default='', max_length=512)), ('created_at', models.DateTimeField(auto_now_add=True)), ], ), diff --git a/cookbook/migrations/0002_auto_20180606_2300.py b/cookbook/migrations/0002_auto_20180606_2300.py deleted file mode 100644 index f1d5e38e..00000000 --- a/cookbook/migrations/0002_auto_20180606_2300.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 2.0.5 on 2018-06-06 21:00 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('cookbook', '0001_initial'), - ] - - operations = [ - migrations.RenameField( - model_name='recipe', - old_name='storage_uid', - new_name='file_uid', - ), - migrations.RenameField( - model_name='recipeimport', - old_name='storage_uid', - new_name='file_uid', - ), - ] diff --git a/cookbook/models.py b/cookbook/models.py index 6278cad6..1f6fbc62 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -60,9 +60,9 @@ class Category(models.Model): class Recipe(models.Model): name = models.CharField(max_length=128) - path = models.CharField(max_length=512, default="") storage = models.ForeignKey(Storage, on_delete=models.PROTECT) file_uid = models.CharField(max_length=256, default="") + file_path = models.CharField(max_length=512, default="") link = models.CharField(max_length=512, default="") category = models.ForeignKey(Category, blank=True, on_delete=models.SET_NULL, null=True) keywords = models.ManyToManyField(Keyword, blank=True) @@ -82,7 +82,7 @@ class RecipeImport(models.Model): name = models.CharField(max_length=128) storage = models.ForeignKey(Storage, on_delete=models.PROTECT) file_uid = models.CharField(max_length=256, default="") - path = models.CharField(max_length=512, default="") + file_path = models.CharField(max_length=512, default="") created_at = models.DateTimeField(auto_now_add=True) def __str__(self): diff --git a/cookbook/tables.py b/cookbook/tables.py index 89bbdffb..394c532b 100644 --- a/cookbook/tables.py +++ b/cookbook/tables.py @@ -78,7 +78,7 @@ class SyncTable(tables.Table): class Meta: model = Sync template_name = 'generic/table_template.html' - fields = ('id', 'path', 'storage', 'last_checked') + fields = ('id', 'file_path', 'storage', 'last_checked') class RecipeImportTable(tables.Table): @@ -88,4 +88,4 @@ class RecipeImportTable(tables.Table): class Meta: model = RecipeImport template_name = 'generic/table_template.html' - fields = ('id', 'name', 'path') + fields = ('id', 'name', 'file_path') diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 906a6236..25925e4a 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -14,7 +14,7 @@ def get_file_link(request, recipe_id): if recipe.storage.method == Storage.DROPBOX: if recipe.link == "": - response = dropbox.get_share_link(recipe.path) # TODO response validation + response = dropbox.get_share_link(recipe) # TODO response validation recipe.link = response['url'] recipe.save() diff --git a/cookbook/views/data.py b/cookbook/views/data.py index 5ae330f4..7329e6f5 100644 --- a/cookbook/views/data.py +++ b/cookbook/views/data.py @@ -40,7 +40,7 @@ def sync_wait(request): def batch_import(request): imports = RecipeImport.objects.all() for new_recipe in imports: - recipe = Recipe(name=new_recipe.name, path=new_recipe.path, storage=new_recipe.storage, file_uid=new_recipe.file_uid) + recipe = Recipe(name=new_recipe.name, file_path=new_recipe.path, storage=new_recipe.storage, file_uid=new_recipe.file_uid) recipe.save() new_recipe.delete() diff --git a/cookbook/views/new.py b/cookbook/views/new.py index 36e24764..85b806c8 100644 --- a/cookbook/views/new.py +++ b/cookbook/views/new.py @@ -67,7 +67,7 @@ def create_new_recipe(request, import_id): recipe = Recipe() recipe.storage = new_recipe.storage recipe.name = form.cleaned_data['name'] - recipe.path = form.cleaned_data['path'] + recipe.file_path = form.cleaned_data['file_path'] recipe.file_uid = form.cleaned_data['file_uid'] recipe.category = form.cleaned_data['category'] @@ -83,6 +83,6 @@ def create_new_recipe(request, import_id): messages.add_message(request, messages.ERROR, _('There was an error importing this recipe!')) else: new_recipe = RecipeImport.objects.get(id=import_id) - form = ImportRecipeForm(initial={'path': new_recipe.path, 'name': new_recipe.name, 'file_uid': new_recipe.file_uid}) + form = ImportRecipeForm(initial={'file_path': new_recipe.file_path, 'name': new_recipe.name, 'file_uid': new_recipe.file_uid}) return render(request, 'forms/edit_import_recipe.html', {'form': form})