Commit efad2a24 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

added email sending to listing cancellation

parent 0221b45c
...@@ -143,6 +143,39 @@ class SellListingForm(forms.ModelForm): ...@@ -143,6 +143,39 @@ 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 self.fields['winning_bid'].required = True
self.fields['email_message'].initial = ''
class Meta:
model = Listing
class UnSellListingForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.label_class = 'be-bold'
self.helper.layout = Layout(
Fieldset("",
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 cancelled your bid for {{ listing.title }} on SRCT Bookshare.</p><p>We certainly hope that this doesn't come as a shock. :-P</p><p>(If you don't know why you're getting this email, hey're the cc'ed email address-- {{ listing.seller.user.email }}. Contact them ASAP to clear up any confusion.)<p></em>"""),
Field('email_message',
placeholder='I haven\'t heard from you in a couple of days, so I\'m going to have to try to sell my textbook to someone else. :-/'),
HTML("""<em><p>Thanks for using SRCT Bookshare!</p><p>Mason SRCT</p></em></div>"""),
HTML("""<hr/ >"""),
FormActions(Submit('submit', 'Email and Cancel Sale',
css_class='btn-primary'),
Button('cancel', 'Never Mind',
css_class='btn-default',
onclick="history.back()")
),
),
)
super(UnSellListingForm, self).__init__(*args, **kwargs)
self.fields['email_message'].label = "Custom Message"
self.fields['email_message'].initial = ''
class Meta: class Meta:
model = Listing model = Listing
......
...@@ -15,12 +15,19 @@ SRCT Bookshare &bull; {{ listing.title }} &bull; Cancel Sale ...@@ -15,12 +15,19 @@ SRCT Bookshare &bull; {{ listing.title }} &bull; Cancel Sale
</div> </div>
</div> </div>
{% load crispy_forms_tags %}
<div class="row"> <div class="row">
<div class="col-md-8 col-md-offset-2 text-center"> <div class="col-md-8 col-md-offset-2">
<form action="" method="post">{% csrf_token %} <div class="panel panel-default">
{{ form.as_p }} <div class="panel-body">
<input type="submit" value="Back on the Market" class="btn btn-primary" /> <div class="row">
<input type="cancel" value="Never Mind" class="btn btn-default" onclick="history.back()" /> <div class="col-md-10 col-md-offset-1">
{% crispy my_form %}
</div>
</div>
</div>
</div>
</div> </div>
</div> </div>
{% endblock %} {% endblock %}
...@@ -18,7 +18,8 @@ from PIL import Image ...@@ -18,7 +18,8 @@ from PIL import Image
from braces.views import LoginRequiredMixin from braces.views import LoginRequiredMixin
# imports from your apps # imports from your apps
from .models import Listing, Bid, Flag, Rating from .models import Listing, Bid, Flag, Rating
from .forms import ListingForm, BidForm, FlagForm, SellListingForm, RatingForm from .forms import ListingForm, BidForm, FlagForm, SellListingForm,\
UnSellListingForm, RatingForm
from core.models import Student from core.models import Student
...@@ -383,18 +384,47 @@ class SellListing(LoginRequiredMixin, ListingActionMixin, UpdateView): ...@@ -383,18 +384,47 @@ class SellListing(LoginRequiredMixin, ListingActionMixin, UpdateView):
class UnSellListing(LoginRequiredMixin, ListingActionMixin, UpdateView): class UnSellListing(LoginRequiredMixin, ListingActionMixin, UpdateView):
model = Listing model = Listing
fields = [] fields = ['email_message', ]
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'
login_url = 'login' login_url = 'login'
success_msg = "Your sale has been successfully cancelled!" success_msg = """Your sale has been successfully cancelled,
and your email successfully sent!"""
def form_valid(self, form): def form_valid(self, form):
self.obj = self.get_object()
text_email = get_template('email/unsold.txt')
html_email = get_template('email/unsold.html')
email_context = Context({
'bidder_first_name': self.obj.winning_bid.bidder.user.first_name,
'seller_name': self.obj.seller.user.get_full_name(),
'bid_num': self.obj.winning_bid.price,
'listing_title': self.obj.title,
'seller_email': self.obj.seller.user.email,
'seller_email': form.instance.email_message, })
subject, from_email, to, cc = ('Your transaction has been cancelled on Bookshare',
'no-reply@bookshare.srct.io',
#self.obj.winning_bid.bidder.user.email,
#self.obj.seller.user.email)
'success@simulator.amazonses.com',
'success@simulator.amazonses.com')
text_content = text_email.render(email_context)
html_content = html_email.render(email_context)
msg = EmailMultiAlternatives(subject, text_content,
from_email, [to], [cc])
msg.attach_alternative(html_content, "text/html")
msg.send()
# this has to come after the email has been sent, otherwise these are
# cleaned out
form.instance.sold = False form.instance.sold = False
form.instance.date_closed = None form.instance.date_closed = None
form.instance.winning_bid = None form.instance.winning_bid = None
return super(UnSellListing, self).form_valid(form) return super(UnSellListing, self).form_valid(form)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
...@@ -406,6 +436,9 @@ class UnSellListing(LoginRequiredMixin, ListingActionMixin, UpdateView): ...@@ -406,6 +436,9 @@ class UnSellListing(LoginRequiredMixin, ListingActionMixin, UpdateView):
if not(selling_student == me): if not(selling_student == me):
return HttpResponseForbidden() return HttpResponseForbidden()
form = UnSellListingForm()
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