From 2114ed037a7fa9faff98503fee56aea43d09ff1b Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Thu, 14 Nov 2019 10:29:24 +0100 Subject: [PATCH] refactored storage backend --- cookbook/helper/__init__.py | 1 - cookbook/helper/dropbox.py | 85 --------------------------------- cookbook/models.py | 3 +- cookbook/provider/__init__.py | 0 cookbook/provider/dropbox.py | 88 +++++++++++++++++++++++++++++++++++ cookbook/provider/provider.py | 12 +++++ cookbook/provider/webdav.py | 0 cookbook/views/api.py | 11 +++-- 8 files changed, 108 insertions(+), 92 deletions(-) delete mode 100644 cookbook/helper/dropbox.py create mode 100644 cookbook/provider/__init__.py create mode 100644 cookbook/provider/dropbox.py create mode 100644 cookbook/provider/provider.py create mode 100644 cookbook/provider/webdav.py diff --git a/cookbook/helper/__init__.py b/cookbook/helper/__init__.py index bde2ba3a..824c1c59 100644 --- a/cookbook/helper/__init__.py +++ b/cookbook/helper/__init__.py @@ -1,2 +1 @@ from cookbook.helper.dal import * -from cookbook.helper.dropbox import * diff --git a/cookbook/helper/dropbox.py b/cookbook/helper/dropbox.py deleted file mode 100644 index 7e10049e..00000000 --- a/cookbook/helper/dropbox.py +++ /dev/null @@ -1,85 +0,0 @@ -import os -from datetime import datetime - -import requests -import json -from django.conf import settings - -from cookbook.models import Recipe, Sync, RecipeImport, SyncLog - - -def import_all(monitor): - url = "https://api.dropboxapi.com/2/files/list_folder" - - headers = { - "Authorization": "Bearer " + monitor.storage.token, - "Content-Type": "application/json" - } - - data = { - "path": monitor.path - } - - r = requests.post(url, headers=headers, data=json.dumps(data)) - try: - recipes = r.json() - except ValueError: - log_entry = SyncLog(status='ERROR', msg=str(r), monitor=monitor) - log_entry.save() - return r - - 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(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, file_path=path, storage=monitor.storage, file_uid=recipe['id']) - new_recipe.save() - import_count += 1 - - log_entry = SyncLog(status='SUCCESS', msg='Imported ' + str(import_count) + ' recipes', sync=monitor) - log_entry.save() - - monitor.last_checked = datetime.now() - monitor.save() - - return True - - -def create_share_link(recipe): - url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings" - - headers = { - "Authorization": "Bearer " + recipe.storage.token, - "Content-Type": "application/json" - } - - data = { - "path": recipe.file_uid - } - - r = requests.post(url, headers=headers, data=json.dumps(data)) - - return r.json() - - -def get_share_link(recipe): - url = "https://api.dropboxapi.com/2/sharing/list_shared_links" - - headers = { - "Authorization": "Bearer " + recipe.storage.token, - "Content-Type": "application/json" - } - - data = { - "path": recipe.file_uid - } - - r = requests.post(url, headers=headers, data=json.dumps(data)) - p = r.json() - - for l in p['links']: - return l['url'] - - response = create_share_link(recipe) - return response['url'] diff --git a/cookbook/models.py b/cookbook/models.py index 924b7861..13c1ef48 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -4,7 +4,8 @@ from django.db import models class Storage(models.Model): DROPBOX = 'DB' - STORAGE_TYPES = ((DROPBOX, 'Dropbox'),) + DAV = 'DAV' + STORAGE_TYPES = ((DROPBOX, 'Dropbox'), (DAV, 'WebDAV')) name = models.CharField(max_length=128) method = models.CharField(choices=STORAGE_TYPES, max_length=128, default=DROPBOX) diff --git a/cookbook/provider/__init__.py b/cookbook/provider/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cookbook/provider/dropbox.py b/cookbook/provider/dropbox.py new file mode 100644 index 00000000..8236144c --- /dev/null +++ b/cookbook/provider/dropbox.py @@ -0,0 +1,88 @@ +import os +from datetime import datetime + +import requests +import json + +from cookbook.models import Recipe, RecipeImport, SyncLog +from cookbook.provider.provider import Provider + + +class Dropbox(Provider): + + @staticmethod + def import_all(monitor): + url = "https://api.dropboxapi.com/2/files/list_folder" + + headers = { + "Authorization": "Bearer " + monitor.storage.token, + "Content-Type": "application/json" + } + + data = { + "path": monitor.path + } + + r = requests.post(url, headers=headers, data=json.dumps(data)) + try: + recipes = r.json() + except ValueError: + log_entry = SyncLog(status='ERROR', msg=str(r), monitor=monitor) + log_entry.save() + return r + + 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(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, file_path=path, storage=monitor.storage, file_uid=recipe['id']) + new_recipe.save() + import_count += 1 + + log_entry = SyncLog(status='SUCCESS', msg='Imported ' + str(import_count) + ' recipes', sync=monitor) + log_entry.save() + + monitor.last_checked = datetime.now() + monitor.save() + + return True + + @staticmethod + def create_share_link(recipe): + url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings" + + headers = { + "Authorization": "Bearer " + recipe.storage.token, + "Content-Type": "application/json" + } + + data = { + "path": recipe.file_uid + } + + r = requests.post(url, headers=headers, data=json.dumps(data)) + + return r.json() + + @staticmethod + def get_share_link(recipe): + url = "https://api.dropboxapi.com/2/sharing/list_shared_links" + + headers = { + "Authorization": "Bearer " + recipe.storage.token, + "Content-Type": "application/json" + } + + data = { + "path": recipe.file_uid + } + + r = requests.post(url, headers=headers, data=json.dumps(data)) + p = r.json() + + for l in p['links']: + return l['url'] + + response = Dropbox.create_share_link(recipe) + return response['url'] diff --git a/cookbook/provider/provider.py b/cookbook/provider/provider.py new file mode 100644 index 00000000..2ce69796 --- /dev/null +++ b/cookbook/provider/provider.py @@ -0,0 +1,12 @@ +class Provider: + @staticmethod + def import_all(monitor): + raise Exception('Method not implemented in storage provider') + + @staticmethod + def create_share_link(recipe): + raise Exception('Method not implemented in storage provider') + + @staticmethod + def get_share_link(recipe): + raise Exception('Method not implemented in storage provider') diff --git a/cookbook/provider/webdav.py b/cookbook/provider/webdav.py new file mode 100644 index 00000000..e69de29b diff --git a/cookbook/views/api.py b/cookbook/views/api.py index ac57e8c5..bd0cb4e2 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -1,12 +1,13 @@ from django.contrib import messages -from django.http import HttpResponse, HttpResponseRedirect -from django.urls import reverse_lazy, reverse +from django.http import HttpResponse +from django.urls import reverse from django.utils.translation import gettext as _ from django.contrib.auth.decorators import login_required from django.shortcuts import redirect from cookbook.models import Recipe, Sync, Storage -from cookbook.helper import dropbox +from cookbook.provider import dropbox +from cookbook.provider.dropbox import Dropbox @login_required @@ -17,7 +18,7 @@ def get_file_link(request, recipe_id): return HttpResponse(reverse('view_recipe', args=[recipe_id])) if recipe.storage.method == Storage.DROPBOX: if recipe.link == "": - recipe.link = dropbox.get_share_link(recipe) # TODO response validation + recipe.link = Dropbox.get_share_link(recipe) # TODO response validation recipe.save() return HttpResponse(recipe.link) @@ -30,7 +31,7 @@ def sync_all(request): error = False for monitor in monitors: if monitor.storage.method == Storage.DROPBOX: - ret = dropbox.import_all(monitor) + ret = Dropbox.import_all(monitor) if not ret: error = True