Commit 707af165 authored by Christine Dela Rosa's avatar Christine Dela Rosa

Add ValidationErrors for StartAuction, Edit+Delete item and Adjust auctionstart/end format

parent ce44a995
{% extends 'boodlesite\templates\base.html' %}
{% load static %}
{% block title %}Schedule Auction{% endblock %}
{% block styles %}
{% endblock %}
{% block content %}
......
......@@ -42,7 +42,48 @@
<h4>{{ item.itemname }}</h4>
<p>{{item.itemspecs}} <span class="item-price">PHP {{item.floorprice}}</span></p>
</div>
<div class="item-btns"> <button>Auction</button> <button>Edit</button> <button>Delete</button>
<div class="item-btns"> <button>Auction</button> <button><a href="{% url 'edititemid' item.itemid %}">Edit</a></button>
<!-- <button>Delete</button> -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#delete_{{ item.itemid }}"
data-whatever="@mdo">Delete</button>
<div class="modal fade" id="delete_{{ item.itemid }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Item</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form action="" method="POST">
<div class="form-group">
<h3>Are you sure you want to delete {{item.itemname}}?</h3>
{% csrf_token %}
<!-- input is to get the itemid to POST for deletion -->
<!-- {{form.itemid.value|default_if_none:item.itemid }} -->
<input name="itemid" type="hidden" value="{{item.itemid}}">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-secondary" name="Submit"> Confirm </button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
......@@ -72,7 +113,6 @@
<button type="button"><a href="{% url 'additemid' current_store.storeid %}">Add Item</a></button>
</div>
]
</div>
......
from datetime import datetime
from tracemalloc import start
from django import forms
from django.forms import (ModelForm,
TextInput, Textarea, widgets)
TextInput, Textarea, widgets, MultiWidget)
from django.utils.translation import gettext_lazy as _
from .models import *
from django.utils import timezone
import datetime, pytz
from django.contrib.admin.widgets import AdminSplitDateTime
from django.contrib.admin import widgets
from django.core.exceptions import ValidationError
class PlaceBidForm(forms.ModelForm):
......@@ -43,7 +51,17 @@ class AddItemForm(forms.ModelForm):
'floorprice': _('Floor Price')
}
class DeleteItemForm(forms.Form):
itemid = forms.IntegerField()
widgets = {'itemid': forms.HiddenInput()}
class StartAuctionForm(forms.ModelForm):
# the widget is supposed to have a pop up but not showing, keeping here bc it separates date and time nicely
auctionstart = forms.SplitDateTimeField(widget=AdminSplitDateTime())
auctionend = forms.SplitDateTimeField(widget=AdminSplitDateTime())
class Meta:
model = Auction
fields = '__all__'
......@@ -57,8 +75,27 @@ class StartAuctionForm(forms.ModelForm):
}
# datetime_format = ['%Y-%m-%d %H:%M']
# widgets = { 'auctionstart' : forms.DateTimeInput(input_formats=['%Y-%m-%d %H:%M']), 'auctionend' : forms.DateTimeInput(input_formats=['%Y-%m-%d %H:%M'])}
# widgets = { 'auctionstart' : forms.AdminSplitDateTime()} #, 'auctionend' : forms.SplitDateTimeField()}
# vv fix later, is missing time widget
# widgets = { 'auctionstart' : forms.SelectDateWidget, 'auctionend':forms.SelectDateWidget}
# 'itemid': forms.HiddenInput()}
def clean(self):
super().clean()
end_time = self.cleaned_data['auctionend']
start_time = self.cleaned_data['auctionstart']
current_date = timezone.now()
auctioned_item = self.cleaned_data['itemid']
auctions = Auction.objects.all()
if start_time > end_time:
raise ValidationError('Start date should be before end date.')
elif start_time < current_date or end_time < current_date:
raise ValidationError('Date cannot be in the past')
else:
for auc in auctions:
if auc.itemid == auctioned_item:
raise ValidationError('Auction Already Exists, pick another Item')
# Generated by Django 3.2.12 on 2022-04-07 18:31
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('main', '0002_auctionbid_authgroup_authgrouppermissions_and_more'),
]
operations = [
migrations.CreateModel(
name='Store',
fields=[
('storeid', models.AutoField(primary_key=True, serialize=False)),
('storename', models.CharField(max_length=255)),
('storedesc', models.CharField(max_length=700)),
],
options={
'db_table': 'store',
'managed': False,
},
),
]
from django.urls import path
from .views import *
from django.views.i18n import JavaScriptCatalog
urlpatterns = [
path('', homepage, name='index'),
......@@ -11,6 +12,8 @@ urlpatterns = [
path('store/<int:pk>', mystore, name='storeid'),
path('additem', addItem, name='additem'),
path('additem/<int:pk>', addItem, name='additemid'),
path('edititem/<int:pk>', editItem, name='edititemid'),
path('jsi18n', JavaScriptCatalog.as_view(), name='js-catlog'),
path('startauction', startAuction, name='startauction'),
path('startauction/<int:pk>', startAuction, name='startauctionid'),
......
......@@ -94,12 +94,31 @@ def tempstore(request): # temp view
def mystore(request, pk):
#### Access to store 1 [ edit accordingly when it becomes accessible thru a user ] ####
# pk is storeid
current_store = Store.objects.get(pk=pk)
store_items = Item.objects.filter(storeid=pk)
all_auctions = Auction.objects.all()
form = DeleteItemForm()
if request.method == "POST":
form = DeleteItemForm(request.POST)
if form.is_valid():
item_id = form.cleaned_data['itemid']
current_item = Item.objects.get(itemid=item_id)
for auction in all_auctions:
if auction.itemid == current_item:
Auction.objects.filter(itemid=item_id).delete()
Item.objects.get(itemid=item_id).delete()
return redirect('storeid', pk=pk)
context = {
'current_store':current_store,
'store_items':store_items
'store_items':store_items,
'form':form
}
......@@ -125,27 +144,57 @@ def addItem(request, pk):
return render(request, "boodlesite/templates/additem.html", context)
def editItem(request, pk):
item = Item.objects.get(itemid=pk)
current_store = item.storeid.storeid
form = AddItemForm(instance=item)
if request.method == 'POST':
form = AddItemForm(request.POST, instance=item)
if form.is_valid():
form.save()
return redirect('storeid', pk=current_store)
context = {
'form':form,
}
return render(request, "boodlesite/templates/additem.html", context)
def startAuction(request, pk):
# pk is store id
current_store = Store.objects.get(pk=pk)
store_id = current_store.storeid
# get items under this store
store_items = Item.objects.filter(storeid=pk)
form = StartAuctionForm(initial={'storeid':current_store})
# temp: all auctions
all_auctions = Auction.objects.all()
form = StartAuctionForm(initial={'auctionstart':datetime.now()})
if request.method == 'POST':
form = StartAuctionForm(request.POST,initial={'storeid':current_store})
form = StartAuctionForm(request.POST)
if form.is_valid():
form.save()
return redirect('storeid', pk=pk)
try:
title = form.cleaned_data['title']
info = form.cleaned_data['info']
starttime = form.cleaned_data['auctionstart']
endtime = form.cleaned_data['auctionend']
current_item = form.cleaned_data['itemid']
new_auction = Auction(title=title, info=info, auctionstart=starttime, auctionend=endtime, itemid=current_item)
new_auction.save()
return redirect(f"/startauction/{pk}")
except:
pass
context = {
'current_store':current_store,
'store_items': store_items,
'all_auctions':all_auctions,
'form':form
}
return render(request, "boodlesite/templates/startauction.html", context)
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