sync working
This commit is contained in:
parent
a155c0c522
commit
e09b4d3074
@ -3,10 +3,17 @@ import requests
|
|||||||
import json
|
import json
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from cookbook.models import Recipe, Category
|
from cookbook.models import Recipe, Monitor, NewRecipe, ImportLog
|
||||||
|
|
||||||
|
|
||||||
def import_all(base_path):
|
def sync_all():
|
||||||
|
monitors = Monitor.objects.all()
|
||||||
|
|
||||||
|
for monitor in monitors:
|
||||||
|
import_all(monitor)
|
||||||
|
|
||||||
|
|
||||||
|
def import_all(monitor):
|
||||||
url = "https://api.dropboxapi.com/2/files/list_folder"
|
url = "https://api.dropboxapi.com/2/files/list_folder"
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
@ -15,20 +22,28 @@ def import_all(base_path):
|
|||||||
}
|
}
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"path": base_path
|
"path": monitor.path
|
||||||
}
|
}
|
||||||
|
|
||||||
r = requests.post(url, headers=headers, data=json.dumps(data))
|
r = requests.post(url, headers=headers, data=json.dumps(data))
|
||||||
try:
|
try:
|
||||||
recipes = r.json()
|
recipes = r.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
log_entry = ImportLog(status='ERROR', msg=str(r), monitor=monitor)
|
||||||
|
log_entry.save()
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
import_count = 0
|
||||||
for recipe in recipes['entries']:
|
for recipe in recipes['entries']:
|
||||||
|
path = recipe['path_lower']
|
||||||
|
if not Recipe.objects.filter(path=path).exists() and not NewRecipe.objects.filter(path=path).exists():
|
||||||
name = os.path.splitext(recipe['name'])[0]
|
name = os.path.splitext(recipe['name'])[0]
|
||||||
insert = Recipe(name=name, path=recipe['path_lower'], category=Category.objects.get(id=0))
|
new_recipe = NewRecipe(name=name, path=path)
|
||||||
insert.save()
|
new_recipe.save()
|
||||||
|
import_count += 1
|
||||||
|
|
||||||
|
log_entry = ImportLog(status='SUCCESS', msg='Imported ' + str(import_count) + ' recipes', monitor=monitor)
|
||||||
|
log_entry.save()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ class Category(models.Model):
|
|||||||
|
|
||||||
|
|
||||||
class Recipe(models.Model):
|
class Recipe(models.Model):
|
||||||
name = models.CharField(max_length=64)
|
name = models.CharField(max_length=128)
|
||||||
path = models.CharField(max_length=512, default="")
|
path = models.CharField(max_length=512, default="")
|
||||||
link = models.CharField(max_length=512, default="")
|
link = models.CharField(max_length=512, default="")
|
||||||
category = models.ForeignKey(Category, on_delete=models.SET_DEFAULT, default=0)
|
category = models.ForeignKey(Category, on_delete=models.SET_DEFAULT, default=0)
|
||||||
@ -41,8 +41,21 @@ class Recipe(models.Model):
|
|||||||
return ', '.join([x.name for x in self.keywords.all()])
|
return ', '.join([x.name for x in self.keywords.all()])
|
||||||
|
|
||||||
|
|
||||||
|
class NewRecipe(models.Model):
|
||||||
|
name = models.CharField(max_length=128)
|
||||||
|
path = models.CharField(max_length=512, default="")
|
||||||
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
|
|
||||||
class Monitor(models.Model):
|
class Monitor(models.Model):
|
||||||
path = models.CharField(max_length=512, default="")
|
path = models.CharField(max_length=512, default="")
|
||||||
last_checked = models.DateTimeField()
|
last_checked = models.DateTimeField()
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
|
||||||
|
class ImportLog(models.Model):
|
||||||
|
monitor = models.ForeignKey(Monitor, on_delete=models.CASCADE)
|
||||||
|
status = models.CharField(max_length=32)
|
||||||
|
msg = models.TextField(default="")
|
||||||
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
@ -81,7 +81,7 @@
|
|||||||
var link = $('#a_recipe_open');
|
var link = $('#a_recipe_open');
|
||||||
link.hide();
|
link.hide();
|
||||||
|
|
||||||
url = "{% url 'get_file_link' recipe_id=12345 %}".replace(/12345/, id);
|
url = "{% url 'api_get_file_link' recipe_id=12345 %}".replace(/12345/, id);
|
||||||
|
|
||||||
link.text("{% trans 'Open Recipe' %}");
|
link.text("{% trans 'Open Recipe' %}");
|
||||||
$('#modal_recipe').modal('show');
|
$('#modal_recipe').modal('show');
|
||||||
|
@ -24,6 +24,8 @@ urlpatterns = [
|
|||||||
path('batch/import', batch.batch_import, name='batch_import'),
|
path('batch/import', batch.batch_import, name='batch_import'),
|
||||||
path('batch/category', batch.batch_edit, name='batch_edit'),
|
path('batch/category', batch.batch_edit, name='batch_edit'),
|
||||||
|
|
||||||
path('api/get_file_link/<int:recipe_id>/', api.get_file_link, name='get_file_link'),
|
path('api/get_file_link/<int:recipe_id>/', api.get_file_link, name='api_get_file_link'),
|
||||||
|
path('api/sync_all/', api.dropbox_sync, name='api_dropbox_sync'),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -15,3 +15,9 @@ def get_file_link(request, recipe_id):
|
|||||||
recipe.save()
|
recipe.save()
|
||||||
|
|
||||||
return HttpResponse(recipe.link)
|
return HttpResponse(recipe.link)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def dropbox_sync(request):
|
||||||
|
dropbox.sync_all()
|
||||||
|
return HttpResponse("Import Successful")
|
||||||
|
Loading…
Reference in New Issue
Block a user