diff options
| author | fschildt <florian.schildt@protonmail.com> | 2025-11-27 23:56:22 +0100 |
|---|---|---|
| committer | fschildt <florian.schildt@protonmail.com> | 2025-11-27 23:56:22 +0100 |
| commit | 31894376c10fc08f9f7a054dcde3b36c3b09f25b (patch) | |
| tree | a52d3af6f6a6c6174e23937cb7d91deceed2aecb /apps/cultivation/views.py | |
| parent | 3ad55935b63d8860467ec5c67328747166263fd5 (diff) | |
Diffstat (limited to 'apps/cultivation/views.py')
| -rw-r--r-- | apps/cultivation/views.py | 121 |
1 files changed, 114 insertions, 7 deletions
diff --git a/apps/cultivation/views.py b/apps/cultivation/views.py index 6db6307..9bd203a 100644 --- a/apps/cultivation/views.py +++ b/apps/cultivation/views.py @@ -1,17 +1,124 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect, get_object_or_404 +from django.contrib.auth.decorators import login_required +from django.contrib import messages + +from . import forms +from .models import * -def view_index(request): - return render(request, 'cultivation/index.html', {'form': None}) -def view_cardio(request): +def index(request): return render(request, 'cultivation/index.html', {'form': None}) -def view_strength(request): + +def cardio(request): return render(request, 'cultivation/index.html', {'form': None}) -def view_flexibility(request): + +def strength(request): return render(request, 'cultivation/index.html', {'form': None}) -def view_mind(request): + +def flexibility(request): return render(request, 'cultivation/index.html', {'form': None}) + +@login_required +def mind(request): + events = MindEvent.objects.all().order_by('-date') + + context = dict() + context['events'] = events + + return render(request, 'cultivation/mind.html', context) + + +@login_required +def mind_event_add(request): + context = dict() + context['form'] = forms.MindEventForm() + + if request.method == 'POST': + form = forms.MindEventForm(request.POST) + if form.is_valid(): + obj = form.save(commit=False) + obj.user = request.user + obj.save() + return redirect('cultivation:mind') + else: + print("MindEventForm is invalid!") + print(form.errors) + print(form.non_field_errors()) + print(form.errors.as_data()) + + + return render(request, 'cultivation/mind-event-add.html', context) + + +@login_required +def mind_event_delete(request, pk): + event = get_object_or_404(MindEvent, pk=pk) + + # security check + if event.user != request.user and not request.user.is_staff: + messages.error(request, "You can't delete this event.") + return redirect('cultivation:mind') + + # delete + if request.method == 'POST': + event.delete() + + return redirect('cultivation:mind') + + +@login_required +def mind_materials(request, pk=None): + materials = MindMaterial.objects.all() + context = dict() + context['materials'] = materials + + if pk: + edit_material = get_object_or_404(MindMaterial, pk=pk) + if edit_material.user != request.user and not request.user.is_staff: + messages.error(request, "You can't edit this material.") + return redirect('cultivation:mind_materials') + + if request.method == 'POST': + form = forms.MindMaterialForm(request.POST, instance=edit_material) + if form.is_valid(): + obj = form.save() + return redirect('cultivation:mind_materials') + else: + messages.error(request, "You can't edit this material.") + else: + context['form'] = forms.MindMaterialForm(instance=edit_material) + else: + if request.method == 'POST': + form = forms.MindMaterialForm(request.POST) + if form.is_valid(): + obj = form.save(commit=False) + obj.user = request.user + obj.save() + return redirect('cultivation:mind_materials') + else: + messages.error(request, "You can't delete this material.") + else: + context['form'] = forms.MindMaterialForm() + + return render(request, 'cultivation/mind-materials.html', context) + + +@login_required +def mind_material_delete(request, pk): + material = get_object_or_404(MindMaterial, pk=pk) + + # security check + if request.user != material.user and not request.user.is_staff: + messages.error(request, "You can't delete this material.") + return redirect('cultivation:mind_materials') + + # delete + if request.method == "POST": + material.delete() + + return redirect('cultivation:mind_materials') + |
