Implement badge rewards of grist
This commit is contained in:
115
local/migrations/0005_auto__add_awardbonus.py
Normal file
115
local/migrations/0005_auto__add_awardbonus.py
Normal file
@@ -0,0 +1,115 @@
|
||||
# encoding: utf-8
|
||||
import datetime
|
||||
from south.db import db
|
||||
from south.v2 import SchemaMigration
|
||||
from django.db import models
|
||||
|
||||
class Migration(SchemaMigration):
|
||||
|
||||
def forwards(self, orm):
|
||||
|
||||
# Adding model 'AwardBonus'
|
||||
db.create_table('local_awardbonus', (
|
||||
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
|
||||
('badge', self.gf('django.db.models.fields.related.OneToOneField')(related_name='minecraft_bonus', unique=True, to=orm['badges.Badge'])),
|
||||
('value', self.gf('django.db.models.fields.IntegerField')()),
|
||||
))
|
||||
db.send_create_signal('local', ['AwardBonus'])
|
||||
|
||||
|
||||
def backwards(self, orm):
|
||||
|
||||
# Deleting model 'AwardBonus'
|
||||
db.delete_table('local_awardbonus')
|
||||
|
||||
|
||||
models = {
|
||||
'auth.group': {
|
||||
'Meta': {'object_name': 'Group'},
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
|
||||
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
|
||||
},
|
||||
'auth.permission': {
|
||||
'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
|
||||
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
|
||||
},
|
||||
'auth.user': {
|
||||
'Meta': {'object_name': 'User'},
|
||||
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
|
||||
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
|
||||
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
|
||||
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
|
||||
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
|
||||
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
|
||||
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
|
||||
},
|
||||
'badges.award': {
|
||||
'Meta': {'object_name': 'Award'},
|
||||
'badge': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['badges.Badge']"}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'reason': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
|
||||
'timestamp': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
|
||||
'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'awards'", 'to': "orm['auth.User']"})
|
||||
},
|
||||
'badges.badge': {
|
||||
'Meta': {'object_name': 'Badge'},
|
||||
'description': ('django.db.models.fields.TextField', [], {}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'name': ('django.db.models.fields.TextField', [], {}),
|
||||
'secret': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'slug': ('django.db.models.fields.SlugField', [], {'db_index': 'True', 'max_length': '50', 'blank': 'True'}),
|
||||
'users': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'badges'", 'blank': 'True', 'through': "orm['badges.Award']", 'to': "orm['auth.User']"})
|
||||
},
|
||||
'contenttypes.contenttype': {
|
||||
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
|
||||
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
|
||||
},
|
||||
'local.awardbonus': {
|
||||
'Meta': {'object_name': 'AwardBonus'},
|
||||
'badge': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'minecraft_bonus'", 'unique': 'True', 'to': "orm['badges.Badge']"}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'value': ('django.db.models.fields.IntegerField', [], {})
|
||||
},
|
||||
'local.currencyaccount': {
|
||||
'Meta': {'object_name': 'CurrencyAccount'},
|
||||
'balance': ('django.db.models.fields.FloatField', [], {'default': '3000'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'profile': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['minecraft.MinecraftProfile']", 'unique': 'True'}),
|
||||
'status': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
|
||||
'username': ('django.db.models.fields.CharField', [], {'max_length': '255', 'unique': 'True', 'null': 'True'})
|
||||
},
|
||||
'local.invite': {
|
||||
'Meta': {'ordering': "['deleted']", 'object_name': 'Invite'},
|
||||
'claimer': ('django.db.models.fields.related.OneToOneField', [], {'blank': 'True', 'related_name': "'claimed_invite'", 'unique': 'True', 'null': 'True', 'to': "orm['auth.User']"}),
|
||||
'code': ('django.db.models.fields.CharField', [], {'max_length': '30'}),
|
||||
'creator': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'invites'", 'to': "orm['auth.User']"}),
|
||||
'deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
|
||||
},
|
||||
'local.quote': {
|
||||
'Meta': {'object_name': 'Quote'},
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'text': ('django.db.models.fields.CharField', [], {'max_length': '50'})
|
||||
},
|
||||
'minecraft.minecraftprofile': {
|
||||
'Meta': {'object_name': 'MinecraftProfile'},
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'mc_username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}),
|
||||
'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'})
|
||||
}
|
||||
}
|
||||
|
||||
complete_apps = ['local']
|
@@ -3,7 +3,9 @@ from django.contrib.auth.models import User
|
||||
import shortuuid
|
||||
from minecraft.models import MinecraftProfile
|
||||
from django.db.models.signals import post_save
|
||||
from badges.models import Badge
|
||||
import badges.api
|
||||
from django.db.models import F
|
||||
from local import update_badges
|
||||
|
||||
class CurrencyAccount(models.Model):
|
||||
@@ -58,3 +60,21 @@ def update_invite_badges(sender, instance, created, **kwargs):
|
||||
update_badges(user)
|
||||
|
||||
post_save.connect(update_invite_badges, sender=Invite)
|
||||
|
||||
class AwardBonus(models.Model):
|
||||
badge = models.OneToOneField(Badge, related_name='minecraft_bonus')
|
||||
value = models.IntegerField()
|
||||
|
||||
def __unicode__(self):
|
||||
return badge.__unicode__()
|
||||
|
||||
def award_grist(sender, award, *args, **kwargs):
|
||||
try:
|
||||
bonus = award.badge.minecraft_bonus
|
||||
account = award.user.minecraftprofile.currencyaccount
|
||||
account.balance = F('balance')+bonus.value
|
||||
account.save()
|
||||
except AwardBonus.DoesNotExist:
|
||||
pass
|
||||
|
||||
badges.api.badge_awarded.connect(award_grist)
|
||||
|
@@ -77,6 +77,24 @@ class AccountCreationTest(TestCase):
|
||||
self.assertIsNotNone(user.minecraftprofile.currencyaccount)
|
||||
user.delete()
|
||||
|
||||
class RewardTest(TestCase):
|
||||
def setUp(self):
|
||||
self.user = User.objects.create_user('User', 'test@example.com')
|
||||
self.badge = badges.api.create_badge("api_test", "API Test", "test")
|
||||
self.reward = models.AwardBonus.objects.create(badge=self.badge, value=1000)
|
||||
|
||||
def tearDown(self):
|
||||
self.user.delete()
|
||||
self.badge.delete()
|
||||
self.reward.delete()
|
||||
|
||||
def testReward(self):
|
||||
account = self.user.minecraftprofile.currencyaccount
|
||||
preValue = account.balance
|
||||
badges.api.award(self.user, "api_test")
|
||||
account = models.CurrencyAccount.objects.get(pk=account.pk)
|
||||
self.assertEqual(account.balance-preValue, self.reward.value)
|
||||
|
||||
class InviteBadgeTest(TestCase):
|
||||
def setUp(self):
|
||||
self.inviter = User.objects.create_user('Inviter', 'test@example.com')
|
||||
|
Reference in New Issue
Block a user