Commit 901ee445 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

added values to valid function instead of roundabout way with prepopulated hidden widgets to trades

parent e1263304
......@@ -67,6 +67,3 @@ class DetailStudent(LoginRequiredMixin, DetailView):
return context
login_url = '/'
# manage all listings -- close your listings, delete your listings, see your bids, remove your bids, close your bids, etc -- on both sides of the transactions, and the ratings
# other students see all of your previous transactions but without the editing options ofc
......@@ -18,7 +18,6 @@ class ListingForm( forms.ModelForm ):
self.helper.layout = Layout(
Fieldset("",
'seller',
Field('isbn', placeholder='0801884039'),
Field('title', placeholder='Squirrels: The Animal Answer Guide'),
Field('author', placeholder='Richard W. Thorington, Jr., and Katie Ferrell'),
......@@ -43,7 +42,6 @@ class ListingForm( forms.ModelForm ):
class Meta:
model = Listing
include = ('seller', 'isbn', 'title', 'author', 'edition', 'year', 'condition', 'access_code', 'price', 'photo', 'description',)
class BidForm( forms.ModelForm ):
......@@ -57,7 +55,6 @@ class BidForm( forms.ModelForm ):
self.helper.layout = Layout(
Fieldset("",
'bidder',
'listing',
HTML("<div class='col-md-4'>"),
AppendedPrependedText('price','$', '.00', placeholder="whole numbers"),
......@@ -73,7 +70,6 @@ class BidForm( forms.ModelForm ):
class Meta:
model = Bid
include = ('bidder', 'listing', 'price', 'text',)
class FlagForm( forms.ModelForm ):
......@@ -83,11 +79,8 @@ class FlagForm( forms.ModelForm ):
# bootstrap3 formatting
self.helper.label_class='be-bold'
self.helper.layout = Layout(
Fieldset("",
'flagger',
'listing',
'reason',
HTML("""<hr/ >"""),
FormActions(
......@@ -99,7 +92,6 @@ class FlagForm( forms.ModelForm ):
class Meta:
model = Flag
include = ('flagger', 'listing', 'reason',)
#class EditListingForm( forms.ModelForm ):
......@@ -108,13 +100,11 @@ class SellListingForm( forms.ModelForm ):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.label_class='be-bold'
self.helper.label_class='be-bold'
self.helper.layout = Layout(
Fieldset("",
'sold',
'winning_bid',
'date_closed',
HTML("""<hr/ >"""),
HTML("""<strong>Your Email to Your Bidder</strong>"""),
HTML("""<div class="well"><em><p>Hey there!</p><p>Seller {{ listing.seller.user.first_name }} {{ listing.seller.user.last_name }} has picked your bid for {{ listing.title }} on SRCT Bookshare. They're the cc'ed email address-- {{ listing.seller.user.email }}.</p><p>Watch your email to arrange all the final touches to get your book.</p></em>"""),
......@@ -128,11 +118,12 @@ class SellListingForm( forms.ModelForm ):
)
super(SellListingForm, self).__init__(*args, **kwargs)
self.fields['email_message'].label = "Custom Message"
self.fields['winning_bid'].required = True
class Meta:
model = Listing
include = ('sold', 'winning_bid', 'date_closed', 'email_message',)
# not necessary
class UnSellListingForm( forms.ModelForm ):
def __init__(self, *args, **kwargs):
......@@ -140,9 +131,6 @@ class UnSellListingForm( forms.ModelForm ):
self.helper.layout = Layout(
Fieldset("",
'sold',
'winning_bid',
'date_closed',
FormActions(
Submit('submit', 'Back on the Market', css_class='btn-primary'),
Button('cancel', 'Never Mind', css_class='btn-default', onclick="history.back()")),
......@@ -152,8 +140,8 @@ class UnSellListingForm( forms.ModelForm ):
class Meta:
model = Listing
include = ('sold', 'winning_bid', 'date_closed',)
# not neccessary
class CancelListingForm( forms.ModelForm ):
def __init__(self, *args, **kwargs):
......@@ -161,8 +149,6 @@ class CancelListingForm( forms.ModelForm ):
self.helper.layout = Layout(
Fieldset("",
'cancelled',
'date_closed',
FormActions(
Submit('submit', 'Cancel Your Listing', css_class='btn-primary'),
Button('cancel', 'Never Mind', css_class='btn-default', onclick="history.back()")),
......@@ -172,16 +158,14 @@ class CancelListingForm( forms.ModelForm ):
class Meta:
model = Listing
include = ('cancelled', 'date_closed',)
# not neccesary
class ReopenListingForm( forms.ModelForm ):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset("",
'cancelled',
'date_closed',
FormActions(
Submit('submit', 'Reopen Your Listing', css_class='btn-primary'),
Button('cancel', 'Never Mind', css_class='btn-default', onclick="history.back()")),
......@@ -191,4 +175,3 @@ class ReopenListingForm( forms.ModelForm ):
class Meta:
model = Listing
include = ('cancelled', 'date_closed',)
......@@ -60,22 +60,24 @@ class ListListings(LoginRequiredMixin, ListView):
class CreateListing(LoginRequiredMixin, CreateView):
model = Listing
form_class = ListingForm
fields = ['isbn', 'title', 'author', 'edition', 'year', 'condition',
'access_code', 'price', 'photo', 'description']
template_name = 'create_listing.html'
context_object_name = 'listing'
# ISBN query!
login_url = '/'
def get_context_data(self, **kwargs):
context = super(CreateListing, self).get_context_data(**kwargs)
def form_valid(self, form):
me = Student.objects.get(user=self.request.user)
form = ListingForm(initial={'seller' : me})
form.fields['seller'].widget = HiddenInput()
form.instance.seller = me
return super(CreateListing, self).form_valid(form)
context['my_form'] = form
def get_context_data(self, **kwargs):
context = super(CreateListing, self).get_context_data(**kwargs)
form = ListingForm()
context['my_form'] = form
return context
# These next two views are tied together...
......@@ -91,10 +93,8 @@ class DetailListing(DetailView):
# make the form available to the template on get
# set the bidder and the listing
form = BidForm(initial={'bidder' : me, 'listing' : self.get_object()})
form.fields['bidder'].widget = HiddenInput()
form = BidForm(initial={'listing': self.get_object()})
form.fields['listing'].widget = HiddenInput()
context['my_form'] = form
# bids, filter by listing name of the current listing, order by date created
......@@ -109,11 +109,17 @@ class DetailListing(DetailView):
class CreateBid(CreateView):
model = Bid
form_class = BidForm
fields = ['listing', 'price', 'text',]
context_object_name = 'bid'
template_name = 'detail_listing.html'
login_url = '/'
def form_valid(self, form):
me = Student.objects.get(user=self.request.user)
form.instance.bidder = me
return super(CreateBid, self).form_valid(form)
def get_success_url(self):
return reverse('detail_listing', kwargs={'slug':self.object.listing.slug})
......@@ -135,11 +141,24 @@ class ListingPage(LoginRequiredMixin, View):
class CreateFlag(LoginRequiredMixin, CreateView):
model = Flag
fields = ['reason',]
template_name = 'create_flag.html'
context_object_name = 'flag'
login_url = '/'
def form_valid(self, form):
me = Student.objects.get(user=self.request.user)
current_url = self.request.get_full_path()
listing_slug = current_url.split('/')[3]
# [u'', u'share', u'listing', u'C1s3oD', u'flag']
selected_listing = Listing.objects.get(slug=listing_slug)
form.instance.flagger = me
form.instance.listing = selected_listing
return super(CreateFlag, self).form_valid(form)
def get_success_url(self):
return reverse('detail_listing', kwargs={'slug':self.object.listing.slug})
......@@ -147,18 +166,12 @@ class CreateFlag(LoginRequiredMixin, CreateView):
context = super(CreateFlag, self).get_context_data(**kwargs)
me = Student.objects.get(user=self.request.user)
# duplicated code!!!
current_url = self.request.get_full_path()
listing_slug = current_url.split('/')[3]
# [u'', u'share', u'listing', u'C1s3oD', u'flag']
selected_listing = Listing.objects.get(slug=listing_slug)
form = FlagForm(initial={'flagger' : me, 'listing' : selected_listing})
form.fields['flagger'].widget = HiddenInput()
form.fields['listing'].widget = HiddenInput()
context['my_form'] = form
selling_student = selected_listing.seller
# you can't flag your own listing
......@@ -171,6 +184,9 @@ class CreateFlag(LoginRequiredMixin, CreateView):
raise Http404
context['listing'] = selected_listing
form = FlagForm()
context['my_form'] = form
return context
class DeleteFlag(LoginRequiredMixin, DeleteView):
......@@ -230,14 +246,20 @@ class EditListing(LoginRequiredMixin, UpdateView):
class SellListing(LoginRequiredMixin, UpdateView):
model = Listing
fields = ['sold', 'email_message', 'winning_bid', 'date_closed']
fields = ['email_message', 'winning_bid',]
template_suffix_name = '_sell'
context_object_name = 'listing'
template_name = 'listing_sell.html'
form_class = SellListingForm
login_url = '/'
def form_valid(self, form):
today = date.today()
form.instance.sold = True
form.instance.date_closed = today
return super(SellListing, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(SellListing, self).get_context_data(**kwargs)
......@@ -252,14 +274,8 @@ class SellListing(LoginRequiredMixin, UpdateView):
# because the page shouldn't exist in this scenario
raise Http404
today = date.today()
form = SellListingForm(initial={'sold' : True, 'date_closed' : today})
form = SellListingForm()
form.fields['winning_bid'].queryset = Bid.objects.filter(listing=self.get_object())
# why does the asterisk appear, but submitting still works without it?
form.fields['winning_bid'].required = True
form.fields['sold'].widget = HiddenInput()
form.fields['date_closed'].widget = HiddenInput()
context['my_form'] = form
......@@ -267,14 +283,19 @@ class SellListing(LoginRequiredMixin, UpdateView):
class UnSellListing(LoginRequiredMixin, UpdateView):
model = Listing
fields = ['sold', 'winning_bid', 'date_closed']
fields = []
template_suffix_name = '_unsell'
context_object_name = 'listing'
template_name = 'listing_unsell.html'
form_class = UnSellListingForm
login_url = '/'
def form_valid(self, form):
form.instance.sold = False
form.instance.date_closed = None
form.instance.winning_bid = None
return super(UnSellListing, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(UnSellListing, self).get_context_data(**kwargs)
......@@ -284,27 +305,27 @@ class UnSellListing(LoginRequiredMixin, UpdateView):
if not(selling_student == me):
return HttpResponseForbidden()
today = date.today()
form = UnSellListingForm(initial={'sold' : False, 'date_closed' : '', 'winning_bid' : ''})
form.fields['sold'].widget = HiddenInput()
form.fields['date_closed'].widget = HiddenInput()
form.fields['winning_bid'].widget = HiddenInput()
form = UnSellListingForm()
context['my_form'] = form
return context
class CancelListing(LoginRequiredMixin, UpdateView):
model = Listing
fields = ['cancelled', 'date_closed',]
fields = []
template_suffix_name = '_cancel'
context_object_name = 'listing'
template_name = 'listing_cancel.html'
form_class = CancelListingForm
login_url = '/'
def form_valid(self, form):
today = date.today()
form.instance.cancelled = True
form.instance.date_closed = today
return super(CancelListing, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(CancelListing, self).get_context_data(**kwargs)
......@@ -316,24 +337,25 @@ class CancelListing(LoginRequiredMixin, UpdateView):
today = date.today()
form = CancelListingForm(initial={'cancelled' : True, 'date_closed' : today})
form.fields['cancelled'].widget = HiddenInput()
form.fields['date_closed'].widget = HiddenInput()
form = CancelListingForm()
context['my_form'] = form
return context
class ReopenListing(LoginRequiredMixin, UpdateView):
model = Listing
fields = ['cancelled']
fields = []
template_suffix_name = '_reopen'
context_object_name = 'listing'
template_name = 'listing_reopen.html'
form_class = ReopenListingForm
login_url = '/'
def form_valid(self, form):
form.instance.cancelled = False
form.instance.date_closed = None
return super(ReopenListing, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(ReopenListing, self).get_context_data(**kwargs)
......@@ -343,10 +365,7 @@ class ReopenListing(LoginRequiredMixin, UpdateView):
if not(selling_student == me):
return HttpResponseForbidden()
form = ReopenListingForm(initial={'cancelled' : False, 'date_closed' : ''})
form.fields['cancelled'].widget = HiddenInput()
form.fields['date_closed'].widget = HiddenInput()
form = ReopenListingForm()
context['my_form'] = form
return 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