basic meal plan working

This commit is contained in:
vabene1111 2020-01-17 17:47:23 +01:00
parent 7449380434
commit afa69c647d
3 changed files with 63 additions and 43 deletions

View File

@ -15,9 +15,11 @@
<div class="row">
<div class="col-md-12">
<form>
<form action="{% url 'view_plan' %}" method="post">
{% csrf_token %}
<label>{% trans 'Week' %}
<input id="id_week" class="form-control" type="week" onchange="document.forms[0].submit()">
<input name="week" id="id_week" class="form-control" type="week"
onchange="document.forms[0].submit()" value="{{ js_week }}">
</label>
</form>
</div>
@ -27,50 +29,30 @@
<div class="col-md-12 table-responsive">
<table class="table table-bordered">
<tr style="text-align: center">
<th id="th_day_1">?</th>
<th id="th_day_2">?</th>
<th id="th_day_3">?</th>
<th id="th_day_4">?</th>
<th id="th_day_5">?</th>
<th id="th_day_6">?</th>
<th id="th_day_7">?</th>
</tr>
<tr>
<td colspan="7" style="text-align: center"><h5>{% trans 'Breakfast' %}</h5></td>
</tr>
<tr>
<td id="td_breakfast_1"></td>
<td id="td_breakfast_2"></td>
<td id="td_breakfast_3"></td>
<td id="td_breakfast_4"></td>
<td id="td_breakfast_5"></td>
<td id="td_breakfast_6"></td>
<td id="td_breakfast_7"></td>
{% for d in days %}
<th>{{ d }}</th>
{% endfor %}
</tr>
{% for plan_key, plan_value in plan.items %}
<tr>
<td colspan="7" style="text-align: center"><h5>{% trans 'Lunch' %}</h5></td>
</tr>
<tr>
<tr>
<td colspan="7" style="text-align: center"><h5>{{ plan_value.type_name }}</h5></td>
</tr>
<tr>
{% for day_key, days_value in plan_value.days.items %}
<td>
{% for mp in days_value %}
<a href="#" onclick="openRecipe({{ mp.recipe.id }})">{{ mp.recipe.name }}</a><br/>
{% endfor %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tr>
<tr>
<td colspan="7" style="text-align: center"><h5>{% trans 'Dinner' %}</h5></td>
</tr>
<tr>
</tr>
<tr>
<td colspan="7" style="text-align: center"><h5>{% trans 'Other' %}</h5></td>
</tr>
<tr>
</tr>
</table>
</div>
</div>
{% include 'include/recipe_open_modal.html' %}
{% endblock %}

View File

@ -121,7 +121,7 @@ class MealPlanCreate(LoginRequiredMixin, CreateView):
obj = form.save(commit=False)
obj.user = self.request.user
obj.save()
return HttpResponseRedirect(self.get_success_url())
return HttpResponseRedirect(reverse('view_plan'))
def get_context_data(self, **kwargs):
context = super(MealPlanCreate, self).get_context_data(**kwargs)

View File

@ -1,3 +1,6 @@
import copy
from datetime import datetime, timedelta
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404
@ -69,7 +72,42 @@ def books(request):
return render(request, 'books.html', {'book_list': book_list})
def get_start_end_from_week(p_year, p_week):
first_day_of_week = datetime.strptime(f'{p_year}-W{int(p_week) - 1}-1', "%Y-W%W-%w").date()
last_day_of_week = first_day_of_week + timedelta(days=6.9)
return first_day_of_week, last_day_of_week
def get_days_from_week(start, end):
delta = end - start
days = []
for i in range(delta.days + 1):
days.append(start + timedelta(days=i))
return days
@login_required()
def meal_plan(request):
form = MealPlanForm()
return render(request, 'meal_plan.html', {'form': form})
js_week = datetime.now().strftime("%Y-W%V")
if request.method == "POST":
js_week = request.POST['week']
year, week = js_week.split('-')
first_day, last_day = get_start_end_from_week(year, week.replace('W', ''))
days = get_days_from_week(first_day, last_day)
days_dict = {}
for d in days:
days_dict[d] = []
plan = {}
for t in MealPlan.MEAL_TYPES:
plan[t[0]] = {'type_name': t[1], 'days': copy.deepcopy(days_dict)}
for d in days:
plan_day = MealPlan.objects.filter(date=d).all()
for p in plan_day:
plan[p.meal]['days'][d].append(p)
print(plan)
return render(request, 'meal_plan.html', {'js_week': js_week, 'plan': plan, 'days': days})