Commit 0a9f750c authored by Jean Michel Rouly's avatar Jean Michel Rouly
Browse files

Moved closing/selling textbooks over to a form-based system.

parent 1b823506
......@@ -8,15 +8,8 @@ GMU Bookshare • Seller Profile
<script>
function insert_target( source, target ){
var form = document.getElementById(source);
var input = document.getElementById("book_id");
if( ! input ){
input = document.createElement("input");
}
input.setAttribute("type", "hidden");
input.setAttribute("id", "book_id");
input.setAttribute("value", target);
form.appendChild( input );
var element = document.getElementById( source );
element.setAttribute("value", target);
}
</script>
......@@ -96,9 +89,9 @@ function insert_target( source, target ){
<span class="label label-default">Closed</span>
{% else %}
{% if seller.user = request.user %}
<a href="#" onclick="insert_target('CloseConfirmForm', '{{ listing.pk }}');"
<a href="#" onclick="insert_target('{{close_form.book_id.id_for_label}}', '{{ listing.pk }}');"
data-toggle="modal" data-target="#CloseConfirm">Close</a> /
<a href="#" onclick="insert_target('FinalPriceForm', '{{ listing.pk }}');"
<a href="#" onclick="insert_target('{{FinalPrice_form.book_id.id_for_label}}', '{{ listing.pk }}');"
data-toggle="modal" data-target="#FinalPrice">Sold</a>
{% else %}
<span class="label label-success">Unsold</span>
......@@ -159,15 +152,18 @@ function insert_target( source, target ){
<!-- Final Price Modal -->
<div class="modal fade" id="FinalPrice" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal fade" id="FinalPrice" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" id="FinalPriceForm" method="post"
action="" role="form">
{%csrf_token%}
{{FinalPrice_form.book_id}}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Final Selling Price</h4>
</div>
......@@ -190,7 +186,8 @@ function insert_target( source, target ){
<div class="modal-footer">
<a class="btn btn-default btn-sm" data-dismiss="modal">Close</a>
<input type="submit" class="btn btn-primary btn-sm" value="Mark as Sold" />
<input type="submit" class="btn btn-primary btn-sm"
value="Mark as Sold" name="sold" />
</div>
</form>
</div><!-- /.modal-content -->
......@@ -198,15 +195,19 @@ function insert_target( source, target ){
</div><!-- /.modal -->
<!-- Close Confirm Modal -->
<div class="modal fade" id="CloseConfirm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal fade" id="CloseConfirm" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" id="CloseConfirmForm" method="post"
action="" role="form">
{%csrf_token%}
{{ close_form.book_id }}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Confirm Listing Closure</h4>
</div>
......@@ -218,7 +219,8 @@ function insert_target( source, target ){
<button class="btn btn-default btn-sm" data-dismiss="modal">
Cancel
</button>
<input type="submit" class="btn btn-primary btn-sm" value="Close Listing" />
<input type="submit" class="btn btn-primary btn-sm"
value="Close Listing" name="closed" />
</div>
</form>
......
from website.models import Listing,Seller
from website.forms import ListingForm, FinalPriceForm
from website.forms import ListingForm, FinalPriceForm, CloseForm
from bids.models import Bid
from bids.forms import BidForm
from lookouts.models import Lookout
......@@ -7,6 +7,7 @@ from django.http import Http404
from django.conf import settings
from django.shortcuts import render, render_to_response, get_object_or_404, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.exceptions import PermissionDenied
from django.utils import timezone
from datetime import datetime,timedelta
from django.contrib.auth.decorators import login_required
......@@ -121,15 +122,6 @@ def index(request):
)
# User's lookouts page.
@login_required
def lookouts(request):
return render(request, 'lookouts.html', {
},
)
# User profile page
@login_required
def profile(request, username):
......@@ -138,7 +130,40 @@ def profile(request, username):
seller = get_object_or_404(Seller, user__username=username)
listings = Listing.objects.filter(seller__user__username=username)
lookouts = Lookout.objects.filter(owner__user__username=username)
FinalPrice_form = FinalPriceForm()
FinalPrice_form = FinalPriceForm(prefix="finalPrice")
close_form = CloseForm(prefix="close")
if request.method == 'POST':
# Parse the ClosedForm input fields
if 'closed' in request.POST:
close_form = CloseForm( request.POST, prefix="close" )
if close_form.is_valid():
book_id = close_form.cleaned_data.get('book_id')
listing = Listing.objects.get(pk=book_id)
if listing.seller == request.user.seller:
listing.active = False
listing.save()
return redirect('profile', username)
else:
raise PermissionDenied("You do not own this listing.")
# Parse the FinalPriceForm input fields
elif 'sold' in request.POST:
FinalPrice_form = FinalPriceForm( request.POST, prefix="finalPrice" )
if FinalPrice_form.is_valid():
book_id = FinalPrice_form.cleaned_data.get('book_id')
listing = Listing.objects.get(pk=book_id)
try:
final_price = int(FinalPrice_form.cleaned_data.get('final_price'))
except ValueError, TypeError:
final_price = 0
if listing.seller == request.user.seller:
listing.finalPrice = final_price
listing.sold = True
listing.active = False
listing.save()
return redirect('profile', username)
else:
raise PermissionDenied("You do not own this listing.")
return render(request, 'profile.html', {
'seller' : seller,
......@@ -146,6 +171,7 @@ def profile(request, username):
'lookouts': lookouts,
'total_sold' : totalSold( username ),
'FinalPrice_form' : FinalPrice_form,
'close_form' : close_form,
},
)
......@@ -217,39 +243,6 @@ def create_listing(request):
},
)
@login_required
def close_listing(request, book_id):
user = request.user
listing = Listing.objects.get(pk=book_id)
if listing.seller.user == user:
listing.active = False
listing.save()
return redirect('profile', request.user.username)
@login_required
def sell_listing(request, book_id):
user = request.user
listing = Listing.objects.get(pk=book_id)
if listing.seller.user == user:
if request.method == 'POST':
finalPrice_form = FinalPriceForm( request.POST )
if finalPrice_form.is_valid():
try:
listing.finalPrice = int(finalPrice_form.cleaned_data['final_price'])
except ValueError, TypeError:
listing.finalPrice = listing.price
listing.sold = True
listing.active = False
listing.save()
return redirect('profile', request.user.username)
@login_required
def search(request):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment