added title field and custom validation
This commit is contained in:
parent
fd4354f16d
commit
e9da17151a
@ -242,8 +242,17 @@ class RecipeBookForm(forms.ModelForm):
|
||||
|
||||
|
||||
class MealPlanForm(forms.ModelForm):
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(MealPlanForm, self).clean()
|
||||
|
||||
if cleaned_data['title'] == '' and cleaned_data['recipe'] is None:
|
||||
raise forms.ValidationError(_('You must provide at least a recipe or a title.'))
|
||||
|
||||
return cleaned_data
|
||||
|
||||
class Meta:
|
||||
model = MealPlan
|
||||
fields = ('recipe', 'meal', 'note', 'date')
|
||||
fields = ('recipe', 'title', 'meal', 'note', 'date')
|
||||
|
||||
widgets = {'recipe': SelectWidget, 'date': DateWidget}
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Generated by Django 3.0.4 on 2020-04-13 17:48
|
||||
# Generated by Django 3.0.5 on 2020-05-02 12:46
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
@ -7,10 +7,15 @@ import django.db.models.deletion
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('cookbook', '0030_recipeingredient_note'),
|
||||
('cookbook', '0040_auto_20200502_1433'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='mealplan',
|
||||
name='title',
|
||||
field=models.CharField(blank=True, default='', max_length=64),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='mealplan',
|
||||
name='recipe',
|
@ -47,7 +47,7 @@ class UserPreference(models.Model):
|
||||
PLAN = 'PLAN'
|
||||
BOOKS = 'BOOKS'
|
||||
|
||||
PAGES = ((SEARCH, _('Search')), (PLAN, _('Meal-Plan')), (BOOKS, _('Books')), )
|
||||
PAGES = ((SEARCH, _('Search')), (PLAN, _('Meal-Plan')), (BOOKS, _('Books')),)
|
||||
|
||||
# Search Style
|
||||
SMALL = 'SMALL'
|
||||
@ -221,16 +221,14 @@ class MealPlan(models.Model):
|
||||
MEAL_TYPES = ((BREAKFAST, _('Breakfast')), (LUNCH, _('Lunch')), (DINNER, _('Dinner')), (OTHER, _('Other')),)
|
||||
|
||||
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, blank=True, null=True)
|
||||
title = models.CharField(max_length=64, blank=True, default='')
|
||||
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
meal = models.CharField(choices=MEAL_TYPES, max_length=128, default=BREAKFAST)
|
||||
note = models.TextField(blank=True)
|
||||
date = models.DateField()
|
||||
|
||||
def note_head(self):
|
||||
try:
|
||||
return self.note.split('\n')[0]
|
||||
except:
|
||||
return ''
|
||||
|
||||
def __str__(self):
|
||||
return self.meal + ' (' + str(self.date) + ') ' + str(self.recipe)
|
||||
if self.title:
|
||||
return self.title
|
||||
return str(self.recipe)
|
||||
|
||||
|
@ -77,7 +77,7 @@
|
||||
{% for mp in days_value %}
|
||||
<a href="{% url 'edit_meal_plan' mp.pk %}"><i class="fas fa-edit"></i></a>
|
||||
{% if mp.recipe %}
|
||||
<a href="{% url 'view_recipe' mp.recipe.id %}">{{ mp.recipe.name }}</a><br/>
|
||||
<a href="{% url 'view_recipe' mp.recipe.id %}">{{ mp }}</a><br/>
|
||||
{% endif %}
|
||||
<span>{{ mp.note_head }}</span>
|
||||
{% endfor %}
|
||||
|
Loading…
Reference in New Issue
Block a user