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