refactored storage backend

This commit is contained in:
vabene1111 2019-11-14 10:29:24 +01:00
parent 9a22c37862
commit 2114ed037a
8 changed files with 108 additions and 92 deletions

View File

@ -1,2 +1 @@
from cookbook.helper.dal import *
from cookbook.helper.dropbox import *

View File

@ -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']

View File

@ -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)

View File

View File

@ -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']

View File

@ -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')

View File

View File

@ -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