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):
super(SellListingForm, self).__init__(*args, **kwargs)
self.fields['email_message'].label = "Custom Message"
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(
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-- {{ }}. Contact them ASAP to clear up any confusion.)<p></em>"""),
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',
Button('cancel', 'Never Mind',
super(UnSellListingForm, self).__init__(*args, **kwargs)
self.fields['email_message'].label = "Custom Message"
self.fields['email_message'].initial = ''
class Meta:
model = Listing
......@@ -15,12 +15,19 @@ SRCT Bookshare &bull; {{ listing.title }} &bull; Cancel Sale
{% load crispy_forms_tags %}
<div class="row">
<div class="col-md-8 col-md-offset-2 text-center">
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Back on the Market" class="btn btn-primary" />
<input type="cancel" value="Never Mind" class="btn btn-default" onclick="history.back()" />
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-10 col-md-offset-1">
{% crispy my_form %}
{% endblock %}
......@@ -18,7 +18,8 @@ from PIL import Image
from braces.views import LoginRequiredMixin
# imports from your apps
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
......@@ -383,18 +384,47 @@ class SellListing(LoginRequiredMixin, ListingActionMixin, UpdateView):
class UnSellListing(LoginRequiredMixin, ListingActionMixin, UpdateView):
model = Listing
fields = []
fields = ['email_message', ]
template_suffix_name = '_unsell'
context_object_name = 'listing'
template_name = 'listing_unsell.html'
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):
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': form.instance.email_message, })
subject, from_email, to, cc = ('Your transaction has been cancelled on Bookshare',
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")
# this has to come after the email has been sent, otherwise these are
# cleaned out
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):
......@@ -406,6 +436,9 @@ class UnSellListing(LoginRequiredMixin, ListingActionMixin, UpdateView):
if not(selling_student == me):
return HttpResponseForbidden()
form = UnSellListingForm()
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