diff --git a/cookbook/forms.py b/cookbook/forms.py index 21b0c9ea..02724ba5 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -15,7 +15,7 @@ class EmojiWidget(forms.TextInput): js = ('custom/js/form_emoji.js',) -class EditRecipeForm(forms.ModelForm): +class ExternalRecipeForm(forms.ModelForm): class Meta: model = Recipe fields = ('name', 'keywords', 'file_path', 'storage', 'file_uid') @@ -29,6 +29,19 @@ class EditRecipeForm(forms.ModelForm): widgets = {'keywords': MultiSelectWidget} +class InternalRecipeForm(forms.ModelForm): + class Meta: + model = Recipe + fields = ('name', 'instructions', 'keywords') + + labels = { + 'name': _('Name'), + 'keywords': _('Keywords'), + 'instructions': _('Instructions'), + } + widgets = {'keywords': MultiSelectWidget} + + class KeywordForm(forms.ModelForm): class Meta: model = Keyword diff --git a/cookbook/migrations/0002_auto_20191113_2224.py b/cookbook/migrations/0002_auto_20191113_2224.py new file mode 100644 index 00000000..5af89b8c --- /dev/null +++ b/cookbook/migrations/0002_auto_20191113_2224.py @@ -0,0 +1,46 @@ +# Generated by Django 2.2.7 on 2019-11-13 21:24 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Ingredients', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=128)), + ], + ), + migrations.CreateModel( + name='RecipeIngredients', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('unit', models.CharField(max_length=128)), + ('ingredient', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='cookbook.Ingredients')), + ], + ), + migrations.RemoveField( + model_name='recipe', + name='category', + ), + migrations.AddField( + model_name='recipe', + name='instructions', + field=models.TextField(blank=True), + ), + migrations.DeleteModel( + name='Category', + ), + migrations.AddField( + model_name='recipeingredients', + name='recipe', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cookbook.Recipe'), + ), + ] diff --git a/cookbook/migrations/0003_auto_20191113_2235.py b/cookbook/migrations/0003_auto_20191113_2235.py new file mode 100644 index 00000000..4e33b440 --- /dev/null +++ b/cookbook/migrations/0003_auto_20191113_2235.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.7 on 2019-11-13 21:35 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0002_auto_20191113_2224'), + ] + + operations = [ + migrations.AlterField( + model_name='recipe', + name='storage', + field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.PROTECT, to='cookbook.Storage'), + ), + ] diff --git a/cookbook/migrations/0004_auto_20191113_2238.py b/cookbook/migrations/0004_auto_20191113_2238.py new file mode 100644 index 00000000..023a67e2 --- /dev/null +++ b/cookbook/migrations/0004_auto_20191113_2238.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.7 on 2019-11-13 21:38 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0003_auto_20191113_2235'), + ] + + operations = [ + migrations.AlterField( + model_name='recipe', + name='storage', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='cookbook.Storage'), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index f6967932..6678f160 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -46,9 +46,14 @@ class Keyword(models.Model): return "{0} {1}".format(self.icon, self.name) +class Ingredients(models.Model): + name = models.CharField(max_length=128) + + class Recipe(models.Model): name = models.CharField(max_length=128) - storage = models.ForeignKey(Storage, on_delete=models.PROTECT) + instructions = models.TextField(blank=True) + storage = models.ForeignKey(Storage, on_delete=models.PROTECT, blank=True, null=True) file_uid = models.CharField(max_length=256, default="") file_path = models.CharField(max_length=512, default="") link = models.CharField(max_length=512, default="") @@ -65,6 +70,12 @@ class Recipe(models.Model): return ', '.join([(x.icon + x.name) for x in self.keywords.all()]) +class RecipeIngredients(models.Model): + recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) + unit = models.CharField(max_length=128) + ingredient = models.ForeignKey(Ingredients, models.PROTECT) + + class RecipeImport(models.Model): name = models.CharField(max_length=128) storage = models.ForeignKey(Storage, on_delete=models.PROTECT) diff --git a/cookbook/views/edit.py b/cookbook/views/edit.py index d529c51d..7f6853d8 100644 --- a/cookbook/views/edit.py +++ b/cookbook/views/edit.py @@ -5,7 +5,7 @@ from django.urls import reverse_lazy, reverse from django.utils.translation import gettext as _ from django.views.generic import UpdateView, DeleteView -from cookbook.forms import EditRecipeForm, KeywordForm, StorageForm, SyncForm +from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage @@ -75,7 +75,7 @@ class ImportUpdate(LoginRequiredMixin, UpdateView): class RecipeUpdate(LoginRequiredMixin, UpdateView): model = Recipe - form_class = EditRecipeForm + form_class = ExternalRecipeForm template_name = "generic/edit_template.html" def form_valid(self, form): diff --git a/cookbook/views/new.py b/cookbook/views/new.py index c0d2e723..fcc4341b 100644 --- a/cookbook/views/new.py +++ b/cookbook/views/new.py @@ -6,14 +6,14 @@ from django.urls import reverse_lazy from django.utils.translation import gettext as _ from django.views.generic import CreateView -from cookbook.forms import ImportRecipeForm, RecipeImport, KeywordForm, Storage, StorageForm +from cookbook.forms import ImportRecipeForm, RecipeImport, KeywordForm, Storage, StorageForm, InternalRecipeForm from cookbook.models import Keyword, Recipe -class RecipeCreate(LoginRequiredMixin, CreateView): # this exists for completeness but is not in use at the moment +class RecipeCreate(LoginRequiredMixin, CreateView): template_name = "generic/new_template.html" model = Recipe - fields = ['name', 'keywords'] + form_class = InternalRecipeForm success_url = reverse_lazy('index') def get_context_data(self, **kwargs): diff --git a/recipes/settings.py b/recipes/settings.py index 38432f3d..8e87296a 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/2.0/ref/settings/ import os from django.contrib import messages from dotenv import load_dotenv +from django.utils.translation import gettext_lazy as _ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -125,6 +126,11 @@ USE_L10N = True USE_TZ = True +LANGUAGES = [ + ('de', _('German')), + ('en', _('English')), +] + # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.0/howto/static-files/ diff --git a/recipes/urls.py b/recipes/urls.py index 1c13dc8e..09503d92 100644 --- a/recipes/urls.py +++ b/recipes/urls.py @@ -18,8 +18,7 @@ from django.urls import include, path from django.contrib import admin urlpatterns = [ - path('', lambda r: HttpResponseRedirect('cookbook/')), + path('', include('cookbook.urls')), path('admin/', admin.site.urls), - path('cookbook/', include('cookbook.urls')), path('accounts/', include('django.contrib.auth.urls')), ]