diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 90ba9e575..a7623e064 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,13 +2,11 @@ - - - + - + @@ -56,50 +54,18 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + @@ -107,31 +73,46 @@ + + + + + + + + + + + + + + + + + + + + + + + - - - + + + + + - - + + - - - - - - - - - - - - + + @@ -140,8 +121,8 @@ - - + + @@ -178,7 +159,6 @@ @@ -323,7 +305,7 @@ - + @@ -361,7 +343,7 @@ + + + + os.path.splitext(recipe['name'])[0] + Python + EXPRESSION + + + splitext(recipe['name'])[0] + Python + EXPRESSION + + + recipe['name'] + Python + EXPRESSION + + + recipes[1]['name'] + Python + EXPRESSION + + + recipes[0]['name'] + Python + EXPRESSION + + + r.json() + Python + EXPRESSION + + - - - - - - - - - - - - - - - - - @@ -499,13 +503,6 @@ - - - - - - - @@ -589,14 +586,6 @@ - - - - - - - - @@ -613,22 +602,6 @@ - - - - - - - - - - - - - - - - @@ -645,71 +618,12 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -722,21 +636,146 @@ - + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/cookbook/forms.py b/cookbook/forms.py index c2a63dad1..766376583 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -25,6 +25,9 @@ class RecipeForm(forms.ModelForm): self.fields['name'].widget.attrs.update({'class': 'form-control'}) self.fields['category'].widget.attrs.update({'class': 'form-control'}) self.fields['keywords'].widget.attrs.update({'class': 'form-control'}) + self.helper = FormHelper() + self.helper.form_method = 'post' + self.helper.add_input(Submit('save', _('Save'), css_class='btn-primary')) class CategoryForm(forms.ModelForm): diff --git a/cookbook/helper/dropbox.py b/cookbook/helper/dropbox.py index 5ac386e3c..57a176c3e 100644 --- a/cookbook/helper/dropbox.py +++ b/cookbook/helper/dropbox.py @@ -1,10 +1,35 @@ +import os import requests import json from django.conf import settings +from cookbook.models import Recipe, Category + def import_all(base_path): - print("test") + url = "https://api.dropboxapi.com/2/files/list_folder" + + headers = { + "Authorization": "Bearer " + settings.DROPBOX_API_KEY, + "Content-Type": "application/json" + } + + data = { + "path": base_path + } + + r = requests.post(url, headers=headers, data=json.dumps(data)) + try: + recipes = r.json() + except ValueError: + return r + + for recipe in recipes['entries']: + name = os.path.splitext(recipe['name'])[0] + insert = Recipe(name=name, path=recipe['path_lower'], category=Category.objects.get(id=0)) + insert.save() + + return True def get_share_link(recipe_path): diff --git a/cookbook/migrations/0004_auto_20180205_2316.py b/cookbook/migrations/0004_auto_20180205_2316.py new file mode 100644 index 000000000..7ff2e3fb3 --- /dev/null +++ b/cookbook/migrations/0004_auto_20180205_2316.py @@ -0,0 +1,28 @@ +# Generated by Django 2.0.2 on 2018-02-05 22:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0003_recipe_path'), + ] + + operations = [ + migrations.AddField( + model_name='recipe', + name='link', + field=models.CharField(default='', max_length=512), + ), + migrations.AlterField( + model_name='recipe', + name='keywords', + field=models.ManyToManyField(blank=True, to='cookbook.Keyword'), + ), + migrations.AlterField( + model_name='recipe', + name='path', + field=models.CharField(default='', max_length=512), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 0a1c36555..7e03414ff 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -26,8 +26,9 @@ class Category(models.Model): class Recipe(models.Model): name = models.CharField(max_length=64) path = models.CharField(max_length=512, default="") + link = models.CharField(max_length=512, default="") category = models.ForeignKey(Category, on_delete=models.CASCADE) - keywords = models.ManyToManyField(Keyword) + keywords = models.ManyToManyField(Keyword, blank=True) created_by = models.IntegerField(default=0) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) diff --git a/cookbook/templates/index.html b/cookbook/templates/index.html index 708c45b96..a60e1e517 100644 --- a/cookbook/templates/index.html +++ b/cookbook/templates/index.html @@ -29,22 +29,74 @@ {% endif %} + + + {% endblock %} \ No newline at end of file diff --git a/cookbook/templates/new_recipe.html b/cookbook/templates/new_recipe.html index d491e236b..b4c6839a9 100644 --- a/cookbook/templates/new_recipe.html +++ b/cookbook/templates/new_recipe.html @@ -1,9 +1,6 @@ {% extends "base.html" %} +{% load crispy_forms_tags %} {% block content %} -
{% csrf_token %} - {{ form.as_p }} - -
- + {% crispy form %} {% endblock %} \ No newline at end of file diff --git a/cookbook/views/api.py b/cookbook/views/api.py index f381f162d..30116c454 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -1,5 +1,4 @@ from django.http import HttpResponse -import json from cookbook.models import Recipe from cookbook.helper import dropbox @@ -7,5 +6,9 @@ from cookbook.helper import dropbox def get_file_link(request, recipe_id): recipe = Recipe.objects.get(id=recipe_id) - response = dropbox.get_share_link(recipe.path) - return HttpResponse(response['url']) + if recipe.link == "": + response = dropbox.get_share_link(recipe.path) + recipe.link = response['url'] + recipe.save() + + return HttpResponse(recipe.link) diff --git a/cookbook/views/views.py b/cookbook/views/views.py index b1d80628b..83ea7c58d 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -4,6 +4,7 @@ from django_tables2 import RequestConfig from cookbook.filters import RecipeFilter from cookbook.forms import * +from cookbook.helper import dropbox from cookbook.tables import RecipeTable, CategoryTable, KeywordTable @@ -12,7 +13,7 @@ def index(request): f = RecipeFilter(request.GET, queryset=Recipe.objects.all()) table = RecipeTable(f.qs) - RequestConfig(request, paginate={'per_page': 3}).configure(table) + RequestConfig(request, paginate={'per_page': 25}).configure(table) return render(request, 'index.html', {'recipes': table, 'filter': f}) else: @@ -24,7 +25,7 @@ def import_recipes(request): if request.method == "POST": form = ImportForm(request.POST) if form.is_valid(): - + dropbox.import_all(form.cleaned_data['path']) return redirect('index') else: form = ImportForm() diff --git a/recipes/settings.py b/recipes/settings.py index c0b70e81e..9be10c501 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -18,7 +18,7 @@ from recipes.secret_settings import * # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = ['192.168.178.27'] +ALLOWED_HOSTS = ['192.168.178.27', '127.0.0.1'] LOGIN_REDIRECT_URL = "index" LOGOUT_REDIRECT_URL = "index"