Skip to content

Commit

Permalink
Hidden input to listing forms that explicitly indicates which form wa…
Browse files Browse the repository at this point in the history
…s submitted
  • Loading branch information
Ekaterina-Vititneva committed Sep 15, 2024
1 parent 580a0f1 commit bed936f
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1 on 2024-09-15 16:22

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('auctions', '0009_rename_last_modifed_by_listing_last_modified_by'),
]

operations = [
migrations.AlterField(
model_name='listing',
name='category',
field=models.CharField(blank=True, choices=[('Electronics', 'Electronics'), ('Fashion', 'Fashion'), ('Home', 'Home'), ('Toys', 'Toys'), ('Books', 'Books'), ('Sports', 'Sports'), ('Health', 'Health'), ('Beauty', 'Beauty'), ('Jewelry', 'Jewelry'), ('Garden', 'Garden'), ('Music', 'Music')], max_length=64),
),
]
162 changes: 79 additions & 83 deletions project_2/commerce/auctions/templates/auctions/listing.html
Original file line number Diff line number Diff line change
@@ -1,95 +1,91 @@
{% extends "auctions/layout.html" %}

{% block title %}
{{ title }}
{% endblock %}

{% block body %}
{% extends "auctions/layout.html" %} {% block title %} {{ title }} {% endblock %} {% block body %}

<h2>Listing: {{ listing.title }}</h2>

{% if not listing.active %}
<div class="alert alert-danger">This listing is closed.</div>
{% if listing.last_modified_by == user %}
<div class="alert alert-success">Congratulations! You have won this auction.</div>
{% endif %}
{% endif %}
<div class="alert alert-danger">This listing is closed.</div>
{% if listing.last_modified_by == user %}
<div class="alert alert-success">Congratulations! You have won this auction.</div>
{% endif %} {% endif %}

<div class="listing">
<div class="listing-top">
{% if listing.imageURL %}
<img src= "{{ listing.imageURL }}" alt="listingImage" height="400px" class="listing-image-single">
<div class="listing-top">
{% if listing.imageURL %}
<img
src="{{ listing.imageURL }}"
alt="listingImage"
height="400px"
class="listing-image-single"
/>
{% else %}
<div class="placeholder-rectangle-listing"></div>
{% endif %}
<div class="bid">
<h4 class="listing-bid">{{ listing.bid }}€</h4>
{% if user.is_authenticated %}
<form action="{% url 'listing' listing.title %}" method="post" class="bid-form">
{% csrf_token %} {{ form.as_p }}
<input type="hidden" name="form_type" value="bid_form" />
<!-- Other form elements -->
<button type="submit" class="btn btn-primary btn-submit">Submit</button>
</form>

<form method="post">
{% csrf_token %} {% if listing in user.watchlist.all %}
<button type="submit" name="remove_from_watchlist" class="btn btn-danger">
Remove from Watchlist
</button>
{% else %}
<div class="placeholder-rectangle-listing"></div>
<button type="submit" name="add_to_watchlist" class="btn btn-primary">
Add to Watchlist
</button>
{% endif %}
<div class = "bid">
<h4 class="listing-bid">{{ listing.bid }}€</h4>
{% if user.is_authenticated %}
<form action="{% url 'listing' listing.title %}" method="post" class="bid-form">
<div class="mb-3">
{% csrf_token %}
{{ form.as_p }}
{% if messages %}
<div>
{% for message in messages %}
<div class="alert custom-alert {% if message.tags %}alert-{{ message.tags }}{% else %}alert-info{% endif %}" role="alert">
<div>{{ message | safe }}</div>
</div>
{% endfor %}
</div>
{% endif %}
<button type="submit" class="btn btn-primary btn-submit">Submit</button>
</div>
</form>
<form method="post">
{% csrf_token %}
{% if listing in user.watchlist.all %}
<button type="submit" name="remove_from_watchlist" class="btn btn-danger">Remove from Watchlist</button>
{% else %}
<button type="submit" name="add_to_watchlist" class="btn btn-primary">Add to Watchlist</button>
{% endif %}
</form>
{% if user == listing.created_by %}
<form method="post">
{% csrf_token %}
{% if listing.active == True %}
<button type="submit" name="close_listing" class="btn btn-danger">Close Listing</button>
{% endif %}
</form>
{% endif %}
{% endif %}

</div>
<div class = "listing-details">
<div>Category: {{ listing.category }}</div>
<div>Listed by {{ listing.created_by }}</div>
<div>Last Bid by {{ listing.last_modified_by }}</div>
<div>Created time: {{ listing.created_at }}</div>
<div>Updated time: {{ listing.updated_at }}</div>
<div>Statuse:
{%if listing.active%}
<span class="badge text-bg-success">Active</span>
{% else %}
<span class="badge text-bg-danger">Closed</span>
{% endif %}
</div>
</div>
</form>
{% if user == listing.created_by %}
<form method="post">
{% csrf_token %} {% if listing.active == True %}
<button type="submit" name="close_listing" class="btn btn-danger">Close Listing</button>
{% endif %}
</form>
{% endif %} {% endif %}
</div>
<div class="listing-details">
<div>Category: {{ listing.category }}</div>
<div>Listed by {{ listing.created_by }}</div>
<div>Last Bid by {{ listing.last_modified_by }}</div>
<div>Created time: {{ listing.created_at }}</div>
<div>Updated time: {{ listing.updated_at }}</div>
<div>
Statuse: {%if listing.active%}
<span class="badge text-bg-success">Active</span>
{% else %}
<span class="badge text-bg-danger">Closed</span>
{% endif %}
</div>
</div>
<div class="listing-bottom">
<p class="listing-description-heading">Description</p>
<p class="listing-description">{{ listing.description }}</p>
<div class="comments">
<p class="listing-description-heading">Comments</p>
{% if user.is_authenticated %}
<form action="{% url 'listing' listing.title %}" method="post">
{% csrf_token %}
{{ comment_form.as_p }}
<button type="submit" class="btn btn-primary" name="comment">Add Comment</button>
</form>
{% endif %}
</div>
</div>
<div class="listing-bottom">
<p class="listing-description-heading">Description</p>
<p class="listing-description">{{ listing.description }}</p>
<div class="comments">
<p class="listing-description-heading">Comments</p>
{% if user.is_authenticated %}
<form action="{% url 'listing' listing.title %}" method="post">
{% csrf_token %} {{ comment_form.as_p }}
<input type="hidden" name="form_type" value="comment_form" />
<button type="submit" class="btn btn-primary">Add Comment</button>
</form>

{% endif %} {% for comment in comments %}
<div class="comment-item">
<p><strong>{{ comment.commenter.username }}</strong> ({{ comment.timestamp }}):</p>
<p>{{ comment.comment }}</p>
</div>
{% empty %}
<p>No comments yet. Be the first to comment!</p>
{% endfor %}
</div>
</div>
</div>

{% endblock %}
{% endblock %}
54 changes: 29 additions & 25 deletions project_2/commerce/auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ def listing(request, title):
comment_form = CommentForm()

if request.method == "POST":
if 'bid' in request.POST:
form_type = request.POST.get('form_type')
if form_type == 'bid_form':
return handle_bid(request, listing)
elif 'comment' in request.POST:
elif form_type == 'comment_form':
return handle_comment(request, listing)
elif 'close_listing' in request.POST:
return handle_close_listing(request, listing)
Expand All @@ -117,45 +118,48 @@ def listing(request, title):
})



def handle_bid(request, listing):
form = BidForm(request.POST)
if form.is_valid():
bid_amount = form.cleaned_data['bid']
if bid_amount > listing.bid:
listing.bid = bid_amount
listing.last_modified_by = request.user
listing.save()

new_bid = Bid(
price=bid_amount,
bidder=request.user,
listing=listing,
)
new_bid.save()

messages.success(request, "Your bid was placed successfully!")
else:
messages.error(request, "Your bid must be higher than the current bid.", extra_tags='danger')

def handle_comment(request, listing):
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
new_comment = Comment(
listing=listing,
commenter=request.user,
comment=comment_form.cleaned_data['comment']
)
new_comment.save()
messages.success(request, "Your comment was added successfully!")
else:
messages.error(request, "Please enter a valid bid.", extra_tags='danger')
print("Form is invalid:", comment_form.errors)
messages.error(request, "There was a problem with your comment. Please try again.")

return redirect('listing', title=listing.title)





def handle_comment(request, listing):
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
new_comment = comment_form.save(commit=False)
new_comment.listing = listing
new_comment.user = request.user
new_comment = Comment(
listing=listing,
commenter=request.user,
comment=comment_form.cleaned_data['comment'] # Access cleaned form data
)
new_comment.save()
messages.success(request, "Your comment was added successfully!")
else:
print("Form is invalid:", comment_form.errors)
messages.error(request, "There was a problem with your comment. Please try again.")

return redirect('listing', title=listing.title)





def handle_close_listing(request, listing):
if listing.active:
listing.active = False
Expand Down

0 comments on commit bed936f

Please sign in to comment.