Add wishes
This commit is contained in:
parent
d2792ba10e
commit
17d672cc3b
|
@ -3,7 +3,7 @@ from django.contrib.auth.forms import UserCreationForm
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from friendly_captcha.fields import FrcCaptchaField
|
from friendly_captcha.fields import FrcCaptchaField
|
||||||
|
|
||||||
from .models import Offer, PflaenzliUser
|
from .models import Offer, PflaenzliUser, Wish
|
||||||
|
|
||||||
|
|
||||||
class CreateOfferForm(forms.ModelForm):
|
class CreateOfferForm(forms.ModelForm):
|
||||||
|
@ -27,3 +27,9 @@ class FilterForm(forms.Form):
|
||||||
'<i class="fa-solid fa-location-dot"></i> Zipcode'))
|
'<i class="fa-solid fa-location-dot"></i> Zipcode'))
|
||||||
distance = forms.IntegerField(required=False, label=mark_safe(
|
distance = forms.IntegerField(required=False, label=mark_safe(
|
||||||
'<i class="fa-solid fa-signs-post"></i> Distance (km)'))
|
'<i class="fa-solid fa-signs-post"></i> Distance (km)'))
|
||||||
|
|
||||||
|
|
||||||
|
class WishForm(forms.ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = Wish
|
||||||
|
fields = ['title']
|
||||||
|
|
44
pflaenzli/pflaenzli/templates/user/wish.html
Normal file
44
pflaenzli/pflaenzli/templates/user/wish.html
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
{% extends 'base.html' %}
|
||||||
|
{% load crispy_forms_tags %}
|
||||||
|
{% block title %}Whishlist{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
<div class="mb-3">
|
||||||
|
<h1>{{ title }} Wishlist</h1>
|
||||||
|
</div>
|
||||||
|
<div class="mb-5">
|
||||||
|
{% if wishes %}
|
||||||
|
<ul class="list-group">
|
||||||
|
{% for wish in wishes %}
|
||||||
|
<li class="list-group-item d-flex justify-content-between align-items-center">
|
||||||
|
{{ wish.title }}
|
||||||
|
{% if own %}
|
||||||
|
<span>
|
||||||
|
<form method="post" action="{% url 'delete_wish' wish.id %}">
|
||||||
|
{% csrf_token %}
|
||||||
|
<button type="submit" class="btn btn-danger">
|
||||||
|
<i class="fas fa-trash-alt" aria-hidden="true" title="Delete"></i>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</span>
|
||||||
|
{% endif %}
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% else %}
|
||||||
|
<div class="alert alert-warning" role="alert">There are currently no wishes!</div>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
{% if form %}
|
||||||
|
<hr class="mb-5"/>
|
||||||
|
<div class="mb-3">
|
||||||
|
<h3>
|
||||||
|
<i class="fas fa-star"></i> New wish
|
||||||
|
</h3>
|
||||||
|
<form method="post" enctype="multipart/form-data">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form|crispy }}
|
||||||
|
<button type="submit" class="btn btn-pfl">Add</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
|
@ -1,45 +0,0 @@
|
||||||
{% extends 'base.html.twig' %}
|
|
||||||
|
|
||||||
{% block title %}Whishlist{% endblock %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
{% for message in app.flashes('error') %}
|
|
||||||
<div class="alert alert-error" role="alert">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
{% for message in app.flashes('success') %}
|
|
||||||
<div class="alert alert-success" role="alert">
|
|
||||||
{{ message }}
|
|
||||||
</div>
|
|
||||||
{% endfor %}
|
|
||||||
|
|
||||||
<div class="mb-3">
|
|
||||||
<h1>Your Wishlist</h1>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="mb-3">
|
|
||||||
{% if wishes == [] %}
|
|
||||||
<div class="alert alert-warning" role="alert">
|
|
||||||
There are currently no wishes!
|
|
||||||
</div>
|
|
||||||
{% else %}
|
|
||||||
<ul class="list-group">
|
|
||||||
{% for wish in wishes %}
|
|
||||||
<li class="list-group-item d-flex justify-content-between align-items-center"> {{ wish.title }}
|
|
||||||
<span>
|
|
||||||
<a href="{{ path('delete_wish', {'urlId': wish.urlid}) }}" class="btn btn-danger" aria-label="Delete"><i class="fas fa-trash-alt" aria-hidden="true" title="Delete"></i></a>
|
|
||||||
</span>
|
|
||||||
</li>
|
|
||||||
{% endfor %}
|
|
||||||
</ul>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<hr>
|
|
||||||
|
|
||||||
<div class="mb-3">
|
|
||||||
<h3><i class="fas fa-star"></i> New wish</h3>
|
|
||||||
{{ form(wish_form) }}
|
|
||||||
</div>
|
|
||||||
{% endblock %}
|
|
|
@ -15,10 +15,12 @@ urlpatterns = [
|
||||||
path("offer/<int:offer_id>/edit/", views.offer_edit, name="offer_edit"),
|
path("offer/<int:offer_id>/edit/", views.offer_edit, name="offer_edit"),
|
||||||
path("offer/<int:offer_id>/trade/", views.offer_trade, name="offer_trade"),
|
path("offer/<int:offer_id>/trade/", views.offer_trade, name="offer_trade"),
|
||||||
path("accounts/<int:user_id>", views.user_detail, name="user_detail"),
|
path("accounts/<int:user_id>", views.user_detail, name="user_detail"),
|
||||||
|
path("accounts/<int:user_id>/wishlist/", views.wishlist, name="wishlist"),
|
||||||
path('accounts/login/', auth_views.LoginView.as_view(template_name='registration/login.html')),
|
path('accounts/login/', auth_views.LoginView.as_view(template_name='registration/login.html')),
|
||||||
path('accounts/profile/', auth_views.LoginView.as_view(template_name='user/detail.html'), name='user_profile'),
|
path('accounts/profile/', auth_views.LoginView.as_view(template_name='user/detail.html'), name='user_profile'),
|
||||||
path('accounts/register/', views.register_user, name='register_user'),
|
path('accounts/register/', views.register_user, name='register_user'),
|
||||||
path('accounts/', include('django.contrib.auth.urls')),
|
path('accounts/', include('django.contrib.auth.urls')),
|
||||||
path("faq/", TemplateView.as_view(template_name='app/faq.html'), name="faq"),
|
path("faq/", TemplateView.as_view(template_name='app/faq.html'), name="faq"),
|
||||||
path("imprint/", TemplateView.as_view(template_name='app/imprint.html'), name="imprint"),
|
path("imprint/", TemplateView.as_view(template_name='app/imprint.html'), name="imprint"),
|
||||||
|
path("wish/delete/<str:wish_id>", views.delete_wish, name='delete_wish'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
from django.shortcuts import render, get_object_or_404, redirect
|
from django.shortcuts import render, get_object_or_404, redirect, reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth import login
|
from django.contrib.auth import login
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
|
from django.views.decorators.csrf import csrf_protect
|
||||||
|
from django.views.decorators.http import require_POST
|
||||||
from django.http import HttpResponseForbidden
|
from django.http import HttpResponseForbidden
|
||||||
|
|
||||||
from .forms import CreateOfferForm, RegistrationForm, FilterForm
|
from .forms import CreateOfferForm, RegistrationForm, FilterForm, WishForm
|
||||||
|
|
||||||
from .models import PflaenzliUser, Offer, Wish
|
from .models import PflaenzliUser, Offer, Wish
|
||||||
from .mail import send_offer_email
|
from .mail import send_offer_email
|
||||||
|
@ -88,6 +90,43 @@ def offer_edit(request, offer_id):
|
||||||
return render(request, "basic_form.html", {"form": form, "button_label": "Update", "title": "Edit Offer"})
|
return render(request, "basic_form.html", {"form": form, "button_label": "Update", "title": "Edit Offer"})
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def wishlist(request, user_id):
|
||||||
|
wishes = Wish.objects.filter(user=user_id)
|
||||||
|
|
||||||
|
if user_id == request.user.id:
|
||||||
|
title = "Your"
|
||||||
|
if request.method == "POST":
|
||||||
|
form = WishForm(request.POST)
|
||||||
|
if form.is_valid():
|
||||||
|
wish = form.save(commit=False)
|
||||||
|
wish.user = request.user
|
||||||
|
wish.save()
|
||||||
|
messages.success(request, "Wish added successfully!")
|
||||||
|
form = WishForm()
|
||||||
|
else:
|
||||||
|
form = None
|
||||||
|
user = get_object_or_404(PflaenzliUser, id=user_id)
|
||||||
|
title = f"{user.username}'s"
|
||||||
|
|
||||||
|
return render(request, "user/wish.html", {"title": title, "form": form, "wishes": wishes, "own": user_id == request.user.id})
|
||||||
|
|
||||||
|
|
||||||
|
@csrf_protect
|
||||||
|
@require_POST
|
||||||
|
@login_required
|
||||||
|
def delete_wish(request, wish_id):
|
||||||
|
wish = get_object_or_404(Wish, id=wish_id)
|
||||||
|
|
||||||
|
if wish.user != request.user:
|
||||||
|
messages.error(request, "You don't have permission to delete this wish!")
|
||||||
|
return redirect("wishlist", request.user.id)
|
||||||
|
|
||||||
|
wish.delete()
|
||||||
|
|
||||||
|
return redirect("wishlist", request.user.id)
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def offer_trade(request, offer_id):
|
def offer_trade(request, offer_id):
|
||||||
offer = get_object_or_404(Offer, id=offer_id)
|
offer = get_object_or_404(Offer, id=offer_id)
|
||||||
|
|
Loading…
Reference in a new issue