Skip to content

Commit

Permalink
fix #37 - use DateTimeField for ask_match_sent
Browse files Browse the repository at this point in the history
  • Loading branch information
groovecoder committed Dec 27, 2014
1 parent 4f1bcd0 commit b399a07
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion auctions/migrations/0003_bid_ask_match_sent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='bid',
name='ask_match_sent',
field=models.BooleanField(default=False),
field=models.DateTimeField(null=True, blank=True),
preserve_default=True,
),
]
8 changes: 5 additions & 3 deletions auctions/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from django.conf import settings
from django.db import models
from django.db.models import Sum
Expand All @@ -11,7 +13,7 @@ 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)
ask_match_sent = models.DateTimeField(null=True, blank=True)
offer = models.DecimalField(max_digits=6, decimal_places=2)


Expand All @@ -20,7 +22,7 @@ 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)
ask_match_sent=None)
.exclude(ask__lte=0))
for bid in met_asks:
send_mail("[codesy] Your ask for %(ask)d for %(url)s has been met" % (
Expand All @@ -29,4 +31,4 @@ def notify_matching_askers(sender, instance, **kwargs):
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)
Bid.objects.filter(id=bid.id).update(ask_match_sent=datetime.now())
4 changes: 3 additions & 1 deletion auctions/tests/model_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

import fudge

from django.conf import settings
Expand Down Expand Up @@ -49,7 +51,7 @@ def test_send_mail_to_matching_askers(self, mock_send_mail):
@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.ask_match_sent = datetime.now()
self.bid1.save()

mock_send_mail.expects_call().with_args(
Expand Down

0 comments on commit b399a07

Please sign in to comment.