Adding new model for gift codes. Can now use gift codes for premium accounts.

This commit is contained in:
Samuel Clay 2015-06-30 16:05:58 -07:00
parent 647ca40c3b
commit 552311517d
3 changed files with 87 additions and 19 deletions

View file

@ -5,7 +5,7 @@ from vendor.zebra.forms import StripePaymentForm
from django.utils.safestring import mark_safe
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from apps.profile.models import change_password, blank_authenticate
from apps.profile.models import change_password, blank_authenticate, MGiftCode
from apps.social.models import MSocialProfile
PLANS = [
@ -191,15 +191,23 @@ class RedeemCodeForm(forms.Form):
if len(gift_code) != 12:
raise forms.ValidationError('Your gift code should be 12 characters long.')
req = requests.get('https://www.thinkup.com/join/api/bundle/', params={'code': gift_code})
response = req.json()
newsblur_gift_code = MGiftCode.objects.filter(gift_code__iexact=gift_code)
if newsblur_gift_code:
# Native gift codes
newsblur_gift_code = newsblur_gift_code[0]
return newsblur_gift_code.gift_code
else:
# Thinkup / Good Web Bundle
req = requests.get('https://www.thinkup.com/join/api/bundle/', params={'code': gift_code})
response = req.json()
is_valid = response.get('is_valid', None)
if is_valid:
return gift_code
elif is_valid == False:
raise forms.ValidationError('Your gift code is invalid. Check it for errors.')
elif response.get('error', None):
raise forms.ValidationError('Your gift code is invalid, says the server: %s' % response['error'])
is_valid = response.get('is_valid', None)
if is_valid:
return gift_code
elif is_valid == False:
raise forms.ValidationError('Your gift code is invalid. Check it for errors.')
elif response.get('error', None):
raise forms.ValidationError('Your gift code is invalid, says the server: %s' % response['error'])
return gift_code

View file

@ -4,6 +4,7 @@ import dateutil
import stripe
import hashlib
import redis
import uuid
import mongoengine as mongo
from django.db import models
from django.db import IntegrityError
@ -277,13 +278,19 @@ class Profile(models.Model):
last_year = datetime.datetime.now() - datetime.timedelta(days=364)
recent_payments_count = 0
oldest_recent_payment_date = None
free_lifetime_premium = False
for payment in payment_history:
if payment.payment_amount == 0:
free_lifetime_premium = True
if payment.payment_date > last_year:
recent_payments_count += 1
if not oldest_recent_payment_date or payment.payment_date < oldest_recent_payment_date:
oldest_recent_payment_date = payment.payment_date
if oldest_recent_payment_date:
if free_lifetime_premium:
self.premium_expire = None
self.save()
elif oldest_recent_payment_date:
new_premium_expire = (oldest_recent_payment_date +
datetime.timedelta(days=365*recent_payments_count))
# Only move premium expire forward, never earlier. Also set expiration if not premium.
@ -942,6 +949,46 @@ class PaymentHistory(models.Model):
total = cls.objects.all().aggregate(sum=Sum('payment_amount'))
print "\nTotal: $%s" % total['sum']
class MGiftCode(mongo.Document):
gifting_user_id = mongo.IntField()
receiving_user_id = mongo.IntField()
gift_code = mongo.StringField(max_length=12)
duration_days = mongo.IntField()
payment_amount = mongo.IntField()
created_date = mongo.DateTimeField(default=datetime.datetime.now)
meta = {
'collection': 'gift_codes',
'allow_inheritance': False,
'indexes': ['gifting_user_id', 'receiving_user_id', 'created_date'],
}
def __unicode__(self):
return "%s gifted %s on %s: %s (redeemed %s times)" % (self.gifting_user_id, self.receiving_user_id, self.created_date, self.gift_code, self.redeemed)
@property
def redeemed(self):
redeemed_code = MRedeemedCode.objects.filter(gift_code=self.gift_code)
return len(redeemed_code)
@staticmethod
def create_code(gift_code=None):
u = unicode(uuid.uuid4())
code = u[:8] + u[9:13]
if gift_code:
code = gift_code + code[len(gift_code):]
return code
@classmethod
def add(cls, duration=0, gifting_user_id=None, receiving_user_id=None, gift_code=None, payment=0):
return cls.objects.create(gift_code=cls.create_code(gift_code),
gifting_user_id=gifting_user_id,
receiving_user_id=receiving_user_id,
duration_days=duration,
payment_amount=payment)
class MRedeemedCode(mongo.Document):
user_id = mongo.IntField()
gift_code = mongo.StringField()
@ -960,7 +1007,26 @@ class MRedeemedCode(mongo.Document):
def record(cls, user_id, gift_code):
cls.objects.create(user_id=user_id,
gift_code=gift_code)
@classmethod
def redeem(cls, user, gift_code):
newsblur_gift_code = MGiftCode.objects.filter(gift_code__iexact=gift_code)
if newsblur_gift_code:
newsblur_gift_code = newsblur_gift_code[0]
PaymentHistory.objects.create(user=user,
payment_date=datetime.datetime.now(),
payment_amount=newsblur_gift_code.payment_amount,
payment_provider='newsblur-gift')
else:
# Thinkup / Good Web Bundle
PaymentHistory.objects.create(user=user,
payment_date=datetime.datetime.now(),
payment_amount=12,
payment_provider='good-web-bundle')
cls.record(user.pk, gift_code)
user.profile.activate_premium()
logging.user(user, "~FG~BBRedeeming gift code: %s~FW" % gift_code)
class RNewUserQueue:

View file

@ -129,13 +129,7 @@ def redeem_code(request):
form = RedeemCodeForm(data=request.POST)
if form.is_valid():
gift_code = request.POST['gift_code']
PaymentHistory.objects.create(user=request.user,
payment_date=datetime.datetime.now(),
payment_amount=12,
payment_provider='good-web-bundle')
MRedeemedCode.record(request.user.pk, gift_code)
request.user.profile.activate_premium()
logging.user(request.user, "~FG~BBRedeeming gift code: %s~FW" % gift_code)
MRedeemedCode.redeem(user=request.user, gift_code=gift_code)
return render_to_response('reader/paypal_return.xhtml',
{}, context_instance=RequestContext(request))