forked from codesy/codesy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix codesy#37 - send email when askers' bids are met
- Loading branch information
1 parent
96be55c
commit 0484c35
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('auctions', '0002_auto_20141005_0226'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='bid', | ||
name='ask_match_sent', | ||
field=models.BooleanField(default=False), | ||
preserve_default=True, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,32 @@ | ||
from django.conf import settings | ||
from django.db import models | ||
from django.db.models import Sum | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
|
||
from mailer import send_mail | ||
|
||
|
||
class Bid(models.Model): | ||
user = models.ForeignKey(settings.AUTH_USER_MODEL) | ||
url = models.URLField() | ||
ask = models.DecimalField(max_digits=6, decimal_places=2) | ||
ask_match_sent = models.BooleanField(default=False) | ||
offer = models.DecimalField(max_digits=6, decimal_places=2) | ||
|
||
|
||
@receiver(post_save, sender=Bid) | ||
def notify_matching_askers(sender, instance, **kwargs): | ||
issue_bids = Bid.objects.filter(url=instance.url).aggregate(Sum('offer')) | ||
met_asks = (Bid.objects.filter(url=instance.url, | ||
ask__lte=issue_bids['offer__sum'], | ||
ask_match_sent=False) | ||
.exclude(ask__lte=0)) | ||
for bid in met_asks: | ||
send_mail("[codesy] Your ask for %(ask)d for %(url)s has been met" % ( | ||
{'ask': bid.ask, 'url': bid.url}), | ||
"Bidders have met your asking price for %s." % bid.url, | ||
settings.DEFAULT_FROM_EMAIL, | ||
[bid.user.email]) | ||
# use .update to avoid recursive signal processing | ||
Bid.objects.filter(id=bid.id).update(ask_match_sent=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import fudge | ||
|
||
from django.conf import settings | ||
from django.test import TestCase | ||
|
||
from model_mommy import mommy | ||
|
||
|
||
class NotifyMatchersReceiverTest(TestCase): | ||
def setUp(self): | ||
""" | ||
Set up the following market of Bids for a single bug: | ||
user1: ask 50, offer 0 | ||
user2: ask 100, offer 10 | ||
user3: ask 0, offer 30 | ||
""" | ||
self.url = 'http://github.com/codesy/codesy/issues/37' | ||
self.bid1 = mommy.make('auctions.Bid', user__email='user1@test.com', | ||
ask=50, offer=0, url=self.url) | ||
self.bid2 = mommy.make('auctions.Bid', user__email='user2@test.com', | ||
ask=100, offer=10, url=self.url) | ||
self.bid3 = mommy.make('auctions.Bid', user__email='user3@test.com', | ||
ask=0, offer=30, url=self.url) | ||
|
||
def _add_url(self, string): | ||
return string.format(url=self.url) | ||
|
||
@fudge.patch('auctions.models.send_mail') | ||
def test_send_mail_to_matching_askers(self, mock_send_mail): | ||
user = mommy.make(settings.AUTH_USER_MODEL) | ||
|
||
mock_send_mail.expects_call().with_args( | ||
self._add_url('[codesy] Your ask for 50 for {url} has been met'), | ||
self._add_url('Bidders have met your asking price for {url}.'), | ||
settings.DEFAULT_FROM_EMAIL, | ||
['user1@test.com'] | ||
) | ||
mock_send_mail.next_call().with_args( | ||
self._add_url('[codesy] Your ask for 100 for {url} has been met'), | ||
self._add_url('Bidders have met your asking price for {url}.'), | ||
settings.DEFAULT_FROM_EMAIL, | ||
['user2@test.com'] | ||
) | ||
|
||
offer_bid = mommy.prepare( | ||
'auctions.Bid', offer=100, user=user, ask=1000, url=self.url) | ||
offer_bid.save() | ||
|
||
@fudge.patch('auctions.models.send_mail') | ||
def test_only_send_mail_to_unsent_matching_askers(self, mock_send_mail): | ||
user = mommy.make(settings.AUTH_USER_MODEL) | ||
self.bid1.ask_match_sent=True | ||
self.bid1.save() | ||
|
||
mock_send_mail.expects_call().with_args( | ||
self._add_url('[codesy] Your ask for 100 for {url} has been met'), | ||
self._add_url('Bidders have met your asking price for {url}.'), | ||
settings.DEFAULT_FROM_EMAIL, | ||
['user2@test.com'] | ||
) | ||
|
||
offer_bid = mommy.prepare( | ||
'auctions.Bid', offer=100, user=user, ask=1000, url=self.url) | ||
offer_bid.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters