wired badge awarding into pledge modified and pledge complete signals

pull/1/head
eric 2012-10-01 18:28:26 -04:00
parent 33e652881c
commit 759e39f61d
3 changed files with 47 additions and 8 deletions

View File

@ -944,6 +944,18 @@ class Wishes(models.Model):
class Meta: class Meta:
db_table = 'core_wishlist_works' db_table = 'core_wishlist_works'
class Badge(models.Model):
name = models.CharField(max_length=72, blank=True)
description = models.TextField(default='', null=True)
@property
def path(self):
return '/static/images/%s.png' % self.name
pledger= Badge.objects.get(name='pledger')
pledger2= Badge.objects.get(name='pledger2')
class UserProfile(models.Model): class UserProfile(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
user = models.OneToOneField(User, related_name='profile') user = models.OneToOneField(User, related_name='profile')
@ -959,16 +971,32 @@ class UserProfile(models.Model):
goodreads_user_name = models.CharField(max_length=200, null=True, blank=True) goodreads_user_name = models.CharField(max_length=200, null=True, blank=True)
goodreads_auth_token = models.TextField(null=True, blank=True) goodreads_auth_token = models.TextField(null=True, blank=True)
goodreads_auth_secret = models.TextField(null=True, blank=True) goodreads_auth_secret = models.TextField(null=True, blank=True)
goodreads_user_link = models.CharField(max_length=200, null=True, blank=True) goodreads_user_link = models.CharField(max_length=200, null=True, blank=True)
def add_pledge_badge(self):
#give user a badge
if self.badges.all().count():
if self.badges.all()[0].id == pledger.id:
self.badges.remove(pledger)
self.badges.add(pledger2)
else:
self.badges.add(pledger)
class Badge(models.Model): def reset_pledge_badge(self):
name = models.CharField(max_length=72, blank=True) #count user pledges
description = models.TextField(default='', null=True) n_pledges = self.pledge_count
if self.badges.exists():
self.badges.remove(pledger)
self.badges.remove(pledger2)
if n_pledges == 1:
self.badges.add(pledger)
elif n_pledges > 1:
self.badges.add(pledger2)
@property @property
def path(self): def pledge_count(self):
return '/static/images/%s.png' % self.name return self.user.transaction_set.exclude(status='NONE').exclude(status='Canceled',reason=None).exclude(anonymous=True).count()
#class CampaignSurveyResponse(models.Model): #class CampaignSurveyResponse(models.Model):
# # generic # # generic
# campaign = models.ForeignKey("Campaign", related_name="surveyresponse", null=False) # campaign = models.ForeignKey("Campaign", related_name="surveyresponse", null=False)

View File

@ -177,6 +177,8 @@ def handle_pledge_modified(sender, transaction=None, up_or_down=None, **kwargs):
# up_or_down is 'increased', 'decreased', or 'canceled' # up_or_down is 'increased', 'decreased', or 'canceled'
if transaction==None or up_or_down==None: if transaction==None or up_or_down==None:
return return
if up_or_down == 'canceled':
transaction.user.profile.reset_pledge_badge()
notification.queue([transaction.user], "pledge_status_change", { notification.queue([transaction.user], "pledge_status_change", {
'site':Site.objects.get_current(), 'site':Site.objects.get_current(),
'transaction': transaction, 'transaction': transaction,
@ -190,6 +192,10 @@ pledge_modified.connect(handle_pledge_modified)
def handle_you_have_pledged(sender, transaction=None, **kwargs): def handle_you_have_pledged(sender, transaction=None, **kwargs):
if transaction==None: if transaction==None:
return return
#give user a badge
transaction.user.profile.add_pledge_badge()
notification.queue([transaction.user], "pledge_you_have_pledged", { notification.queue([transaction.user], "pledge_you_have_pledged", {
'site':Site.objects.get_current(), 'site':Site.objects.get_current(),
'transaction': transaction 'transaction': transaction

View File

@ -14,7 +14,6 @@ from regluit.payment.models import Transaction
from regluit.core.models import Campaign, Wishlist, Work from regluit.core.models import Campaign, Wishlist, Work
from regluit.core.signals import handle_transaction_charged from regluit.core.signals import handle_transaction_charged
from regluit.payment.parameters import * from regluit.payment.parameters import *
from regluit.payment.paypal import *
import traceback import traceback
from django.core.validators import URLValidator from django.core.validators import URLValidator
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -352,6 +351,12 @@ class TransactionTest(TestCase):
t.user = user t.user = user
t.save() t.save()
#test pledge adders
user.profile.reset_pledge_badge()
self.assertEqual(user.profile.badges.all()[0].name,'pledger')
user.profile.add_pledge_badge()
self.assertEqual(user.profile.badges.all()[0].name,'pledger2')
p = PaymentManager() p = PaymentManager()
results = p.query_campaign(c,campaign_total=True, summary=False) results = p.query_campaign(c,campaign_total=True, summary=False)
self.assertEqual(results[0].amount, D('12.34')) self.assertEqual(results[0].amount, D('12.34'))