2013-05-13 16:17:49 -07:00
|
|
|
import time
|
2010-06-30 13:36:51 -04:00
|
|
|
import datetime
|
2012-12-03 14:35:21 -08:00
|
|
|
import stripe
|
2013-03-20 18:36:15 -07:00
|
|
|
import hashlib
|
2013-05-13 18:03:17 -07:00
|
|
|
import redis
|
2012-07-09 13:55:29 -07:00
|
|
|
import mongoengine as mongo
|
2009-06-16 03:08:55 +00:00
|
|
|
from django.db import models
|
2010-10-25 20:20:59 -04:00
|
|
|
from django.db import IntegrityError
|
2011-07-17 20:53:30 -07:00
|
|
|
from django.db.utils import DatabaseError
|
2010-05-11 21:36:17 -04:00
|
|
|
from django.db.models.signals import post_save
|
2013-02-15 13:47:45 -08:00
|
|
|
from django.db.models import Sum, Avg, Count
|
2011-09-19 08:56:16 -07:00
|
|
|
from django.conf import settings
|
2010-11-08 12:09:55 -05:00
|
|
|
from django.contrib.auth import authenticate
|
2011-09-19 08:56:16 -07:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.core.mail import mail_admins
|
|
|
|
from django.core.mail import EmailMultiAlternatives
|
2011-09-19 09:46:36 -07:00
|
|
|
from django.core.urlresolvers import reverse
|
2011-09-19 08:56:16 -07:00
|
|
|
from django.template.loader import render_to_string
|
2010-10-16 23:24:55 -04:00
|
|
|
from apps.reader.models import UserSubscription
|
2013-07-11 15:55:37 -07:00
|
|
|
from apps.rss_feeds.models import Feed, MStory, MStarredStory
|
2011-01-30 23:56:51 -05:00
|
|
|
from apps.rss_feeds.tasks import NewFeeds
|
2013-02-15 09:52:11 -08:00
|
|
|
from apps.rss_feeds.tasks import SchedulePremiumSetup
|
2013-05-23 18:14:21 -07:00
|
|
|
from apps.feed_import.models import GoogleReaderImporter, OPMLExporter
|
2010-10-23 12:29:18 -04:00
|
|
|
from utils import log as logging
|
2012-07-13 18:42:44 -07:00
|
|
|
from utils import json_functions as json
|
2011-01-20 09:57:23 -05:00
|
|
|
from utils.user_functions import generate_secret_token
|
2011-09-19 08:56:16 -07:00
|
|
|
from vendor.timezones.fields import TimeZoneField
|
2012-12-03 15:17:35 -08:00
|
|
|
from vendor.paypal.standard.ipn.signals import subscription_signup, payment_was_successful
|
2012-12-03 14:35:21 -08:00
|
|
|
from vendor.paypal.standard.ipn.models import PayPalIPN
|
2013-05-23 16:28:39 -07:00
|
|
|
from vendor.paypalapi.interface import PayPalInterface
|
2012-02-28 17:37:01 -08:00
|
|
|
from zebra.signals import zebra_webhook_customer_subscription_created
|
2012-12-03 15:17:35 -08:00
|
|
|
from zebra.signals import zebra_webhook_charge_succeeded
|
2011-09-19 08:56:16 -07:00
|
|
|
|
2010-05-11 21:36:17 -04:00
|
|
|
class Profile(models.Model):
|
2011-05-12 18:15:15 -04:00
|
|
|
user = models.OneToOneField(User, unique=True, related_name="profile")
|
|
|
|
is_premium = models.BooleanField(default=False)
|
2012-12-03 14:35:21 -08:00
|
|
|
premium_expire = models.DateTimeField(blank=True, null=True)
|
2011-09-21 17:49:26 -07:00
|
|
|
send_emails = models.BooleanField(default=True)
|
2011-05-12 18:15:15 -04:00
|
|
|
preferences = models.TextField(default="{}")
|
|
|
|
view_settings = models.TextField(default="{}")
|
2010-09-05 18:08:08 -07:00
|
|
|
collapsed_folders = models.TextField(default="[]")
|
2013-07-05 09:31:49 +02:00
|
|
|
feed_pane_size = models.IntegerField(default=242)
|
2012-03-20 11:15:40 -07:00
|
|
|
tutorial_finished = models.BooleanField(default=False)
|
2012-03-21 12:28:51 -07:00
|
|
|
hide_getting_started = models.NullBooleanField(default=False, null=True, blank=True)
|
|
|
|
has_setup_feeds = models.NullBooleanField(default=False, null=True, blank=True)
|
|
|
|
has_found_friends = models.NullBooleanField(default=False, null=True, blank=True)
|
|
|
|
has_trained_intelligence = models.NullBooleanField(default=False, null=True, blank=True)
|
2011-05-12 18:15:15 -04:00
|
|
|
last_seen_on = models.DateTimeField(default=datetime.datetime.now)
|
|
|
|
last_seen_ip = models.CharField(max_length=50, blank=True, null=True)
|
2012-04-21 18:20:49 -07:00
|
|
|
dashboard_date = models.DateTimeField(default=datetime.datetime.now)
|
2011-05-12 18:15:15 -04:00
|
|
|
timezone = TimeZoneField(default="America/New_York")
|
|
|
|
secret_token = models.CharField(max_length=12, blank=True, null=True)
|
2012-02-27 21:46:34 -08:00
|
|
|
stripe_4_digits = models.CharField(max_length=4, blank=True, null=True)
|
|
|
|
stripe_id = models.CharField(max_length=24, blank=True, null=True)
|
2010-05-11 21:36:17 -04:00
|
|
|
|
2010-10-23 11:20:54 -04:00
|
|
|
def __unicode__(self):
|
2011-09-19 08:56:16 -07:00
|
|
|
return "%s <%s> (Premium: %s)" % (self.user, self.user.email, self.is_premium)
|
2011-01-20 09:57:23 -05:00
|
|
|
|
2013-06-12 13:52:43 -07:00
|
|
|
def canonical(self):
|
2012-07-13 18:42:44 -07:00
|
|
|
return {
|
|
|
|
'is_premium': self.is_premium,
|
|
|
|
'preferences': json.decode(self.preferences),
|
|
|
|
'tutorial_finished': self.tutorial_finished,
|
|
|
|
'hide_getting_started': self.hide_getting_started,
|
|
|
|
'has_setup_feeds': self.has_setup_feeds,
|
|
|
|
'has_found_friends': self.has_found_friends,
|
|
|
|
'has_trained_intelligence': self.has_trained_intelligence,
|
|
|
|
'dashboard_date': self.dashboard_date
|
|
|
|
}
|
|
|
|
|
2011-01-20 09:57:23 -05:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.secret_token:
|
|
|
|
self.secret_token = generate_secret_token(self.user.username, 12)
|
2011-07-17 20:53:30 -07:00
|
|
|
try:
|
|
|
|
super(Profile, self).save(*args, **kwargs)
|
|
|
|
except DatabaseError:
|
|
|
|
print " ---> Profile not saved. Table isn't there yet."
|
2011-01-20 09:57:23 -05:00
|
|
|
|
2012-07-28 18:33:07 -07:00
|
|
|
def delete_user(self, confirm=False):
|
|
|
|
if not confirm:
|
|
|
|
print " ---> You must pass confirm=True to delete this user."
|
|
|
|
return
|
|
|
|
|
|
|
|
from apps.social.models import MSocialProfile, MSharedStory, MSocialSubscription
|
|
|
|
from apps.social.models import MActivity, MInteraction
|
2012-07-29 23:53:02 -07:00
|
|
|
try:
|
|
|
|
social_profile = MSocialProfile.objects.get(user_id=self.user.pk)
|
2013-01-03 10:33:22 -08:00
|
|
|
logging.user(self.user, "Unfollowing %s followings and %s followers" %
|
|
|
|
(social_profile.following_count,
|
|
|
|
social_profile.follower_count))
|
2012-07-29 23:53:02 -07:00
|
|
|
for follow in social_profile.following_user_ids:
|
|
|
|
social_profile.unfollow_user(follow)
|
|
|
|
for follower in social_profile.follower_user_ids:
|
|
|
|
follower_profile = MSocialProfile.objects.get(user_id=follower)
|
|
|
|
follower_profile.unfollow_user(self.user.pk)
|
|
|
|
social_profile.delete()
|
|
|
|
except MSocialProfile.DoesNotExist:
|
2013-01-03 10:33:22 -08:00
|
|
|
logging.user(self.user, " ***> No social profile found. S'ok, moving on.")
|
2012-07-29 23:53:02 -07:00
|
|
|
pass
|
2012-07-28 18:33:07 -07:00
|
|
|
|
|
|
|
shared_stories = MSharedStory.objects.filter(user_id=self.user.pk)
|
2013-01-03 10:33:22 -08:00
|
|
|
logging.user(self.user, "Deleting %s shared stories" % shared_stories.count())
|
2012-07-28 18:33:07 -07:00
|
|
|
for story in shared_stories:
|
2012-10-25 14:18:25 -07:00
|
|
|
try:
|
2013-07-05 16:53:03 -07:00
|
|
|
original_story = MStory.objects.get(story_hash=story.story_hash)
|
2012-10-25 14:18:25 -07:00
|
|
|
original_story.sync_redis()
|
|
|
|
except MStory.DoesNotExist:
|
|
|
|
pass
|
2012-07-28 18:33:07 -07:00
|
|
|
story.delete()
|
|
|
|
|
|
|
|
subscriptions = MSocialSubscription.objects.filter(subscription_user_id=self.user.pk)
|
2013-01-03 10:33:22 -08:00
|
|
|
logging.user(self.user, "Deleting %s social subscriptions" % subscriptions.count())
|
2012-07-28 18:33:07 -07:00
|
|
|
subscriptions.delete()
|
|
|
|
|
|
|
|
interactions = MInteraction.objects.filter(user_id=self.user.pk)
|
2013-01-03 10:33:22 -08:00
|
|
|
logging.user(self.user, "Deleting %s interactions for user." % interactions.count())
|
2012-07-28 18:33:07 -07:00
|
|
|
interactions.delete()
|
|
|
|
|
|
|
|
interactions = MInteraction.objects.filter(with_user_id=self.user.pk)
|
2013-01-03 10:33:22 -08:00
|
|
|
logging.user(self.user, "Deleting %s interactions with user." % interactions.count())
|
2012-07-28 18:33:07 -07:00
|
|
|
interactions.delete()
|
|
|
|
|
|
|
|
activities = MActivity.objects.filter(user_id=self.user.pk)
|
2013-01-03 10:33:22 -08:00
|
|
|
logging.user(self.user, "Deleting %s activities for user." % activities.count())
|
2012-07-28 18:33:07 -07:00
|
|
|
activities.delete()
|
|
|
|
|
|
|
|
activities = MActivity.objects.filter(with_user_id=self.user.pk)
|
2013-01-03 10:33:22 -08:00
|
|
|
logging.user(self.user, "Deleting %s activities with user." % activities.count())
|
2012-07-28 18:33:07 -07:00
|
|
|
activities.delete()
|
|
|
|
|
2013-07-11 15:55:37 -07:00
|
|
|
starred_stories = MStarredStory.objects.filter(user_id=self.user.pk)
|
|
|
|
logging.user(self.user, "Deleting %s starred stories." % starred_stories.count())
|
|
|
|
starred_stories.delete()
|
|
|
|
|
2013-01-03 10:33:22 -08:00
|
|
|
logging.user(self.user, "Deleting user: %s" % self.user)
|
2012-07-28 18:33:07 -07:00
|
|
|
self.user.delete()
|
|
|
|
|
2010-10-23 11:20:54 -04:00
|
|
|
def activate_premium(self):
|
2012-07-05 22:20:49 -07:00
|
|
|
from apps.profile.tasks import EmailNewPremium
|
|
|
|
EmailNewPremium.delay(user_id=self.user.pk)
|
2011-10-26 20:09:28 -07:00
|
|
|
|
2010-10-23 11:20:54 -04:00
|
|
|
self.is_premium = True
|
|
|
|
self.save()
|
2013-05-13 18:03:17 -07:00
|
|
|
self.user.is_active = True
|
|
|
|
self.user.save()
|
2010-10-23 11:20:54 -04:00
|
|
|
|
|
|
|
subs = UserSubscription.objects.filter(user=self.user)
|
|
|
|
for sub in subs:
|
2013-02-15 09:52:11 -08:00
|
|
|
if sub.active: continue
|
2010-10-23 11:20:54 -04:00
|
|
|
sub.active = True
|
2010-10-25 20:20:59 -04:00
|
|
|
try:
|
|
|
|
sub.save()
|
2013-05-10 12:05:24 -07:00
|
|
|
except (IntegrityError, Feed.DoesNotExist):
|
2010-10-25 20:20:59 -04:00
|
|
|
pass
|
2010-10-23 11:20:54 -04:00
|
|
|
|
2013-05-10 12:05:24 -07:00
|
|
|
try:
|
|
|
|
scheduled_feeds = [sub.feed.pk for sub in subs]
|
|
|
|
except Feed.DoesNotExist:
|
|
|
|
scheduled_feeds = []
|
2013-02-15 09:52:11 -08:00
|
|
|
logging.user(self.user, "~SN~FMTasking the scheduling immediate premium setup of ~SB%s~SN feeds..." %
|
|
|
|
len(scheduled_feeds))
|
|
|
|
SchedulePremiumSetup.apply_async(kwargs=dict(feed_ids=scheduled_feeds))
|
|
|
|
|
2011-01-30 23:56:51 -05:00
|
|
|
self.queue_new_feeds()
|
2012-12-05 13:10:11 -08:00
|
|
|
self.setup_premium_history()
|
2010-10-30 00:27:52 -04:00
|
|
|
|
2011-02-23 13:46:47 -05:00
|
|
|
logging.user(self.user, "~BY~SK~FW~SBNEW PREMIUM ACCOUNT! WOOHOO!!! ~FR%s subscriptions~SN!" % (subs.count()))
|
2013-05-10 12:05:24 -07:00
|
|
|
|
|
|
|
return True
|
2012-12-03 14:35:21 -08:00
|
|
|
|
2012-12-05 13:10:11 -08:00
|
|
|
def deactivate_premium(self):
|
|
|
|
self.is_premium = False
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
subs = UserSubscription.objects.filter(user=self.user)
|
|
|
|
for sub in subs:
|
|
|
|
sub.active = False
|
|
|
|
try:
|
|
|
|
sub.save()
|
|
|
|
sub.feed.setup_feed_for_premium_subscribers()
|
2013-05-10 12:05:24 -07:00
|
|
|
except (IntegrityError, Feed.DoesNotExist):
|
2012-12-05 13:10:11 -08:00
|
|
|
pass
|
|
|
|
|
|
|
|
logging.user(self.user, "~BY~FW~SBBOO! Deactivating premium account: ~FR%s subscriptions~SN!" % (subs.count()))
|
|
|
|
|
2013-05-13 18:03:17 -07:00
|
|
|
def activate_free(self):
|
|
|
|
if self.user.is_active:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.user.is_active = True
|
|
|
|
self.user.save()
|
|
|
|
self.send_new_user_queue_email()
|
|
|
|
|
2012-12-03 14:35:21 -08:00
|
|
|
def setup_premium_history(self):
|
|
|
|
existing_history = PaymentHistory.objects.filter(user=self.user)
|
2012-12-03 16:02:59 -08:00
|
|
|
if existing_history.count():
|
|
|
|
print " ---> Deleting existing history: %s payments" % existing_history.count()
|
|
|
|
existing_history.delete()
|
2012-12-03 14:35:21 -08:00
|
|
|
|
|
|
|
# Record Paypal payments
|
|
|
|
paypal_payments = PayPalIPN.objects.filter(custom=self.user.username,
|
|
|
|
txn_type='subscr_payment')
|
2012-12-03 16:02:59 -08:00
|
|
|
if not paypal_payments.count():
|
|
|
|
paypal_payments = PayPalIPN.objects.filter(payer_email=self.user.email,
|
|
|
|
txn_type='subscr_payment')
|
2012-12-03 14:35:21 -08:00
|
|
|
for payment in paypal_payments:
|
|
|
|
PaymentHistory.objects.create(user=self.user,
|
|
|
|
payment_date=payment.payment_date,
|
|
|
|
payment_amount=payment.payment_gross,
|
|
|
|
payment_provider='paypal')
|
|
|
|
|
2013-01-31 16:55:45 -08:00
|
|
|
print " ---> Found %s paypal_payments" % len(paypal_payments)
|
|
|
|
|
2012-12-03 14:35:21 -08:00
|
|
|
# Record Stripe payments
|
|
|
|
if self.stripe_id:
|
|
|
|
stripe.api_key = settings.STRIPE_SECRET
|
|
|
|
stripe_customer = stripe.Customer.retrieve(self.stripe_id)
|
|
|
|
stripe_payments = stripe.Charge.all(customer=stripe_customer.id).data
|
2013-03-29 13:39:35 -07:00
|
|
|
|
|
|
|
existing_history = PaymentHistory.objects.filter(user=self.user,
|
|
|
|
payment_provider='stripe')
|
|
|
|
if existing_history.count():
|
|
|
|
print " ---> Deleting existing history: %s stripe payments" % existing_history.count()
|
|
|
|
existing_history.delete()
|
|
|
|
|
2012-12-03 14:35:21 -08:00
|
|
|
for payment in stripe_payments:
|
|
|
|
created = datetime.datetime.fromtimestamp(payment.created)
|
|
|
|
PaymentHistory.objects.create(user=self.user,
|
|
|
|
payment_date=created,
|
|
|
|
payment_amount=payment.amount / 100.0,
|
|
|
|
payment_provider='stripe')
|
2013-01-31 16:55:45 -08:00
|
|
|
print " ---> Found %s stripe_payments" % len(stripe_payments)
|
2012-12-03 14:35:21 -08:00
|
|
|
|
|
|
|
# Calculate last payment date
|
|
|
|
payment_history = PaymentHistory.objects.filter(user=self.user)
|
|
|
|
most_recent_payment_date = None
|
|
|
|
for payment in payment_history:
|
|
|
|
if not most_recent_payment_date or payment.payment_date > most_recent_payment_date:
|
|
|
|
most_recent_payment_date = payment.payment_date
|
2013-01-31 16:55:45 -08:00
|
|
|
print " ---> %s payments" % len(payment_history)
|
2012-12-03 14:35:21 -08:00
|
|
|
|
|
|
|
if most_recent_payment_date:
|
2013-03-28 11:16:43 -07:00
|
|
|
self.premium_expire = most_recent_payment_date + datetime.timedelta(days=365)
|
2012-12-03 14:35:21 -08:00
|
|
|
self.save()
|
2013-04-05 17:54:10 -07:00
|
|
|
|
2013-07-04 11:29:41 -07:00
|
|
|
def refund_premium(self, partial=False):
|
2013-05-10 12:05:24 -07:00
|
|
|
refunded = False
|
|
|
|
|
2013-04-05 17:54:10 -07:00
|
|
|
if self.stripe_id:
|
|
|
|
stripe.api_key = settings.STRIPE_SECRET
|
|
|
|
stripe_customer = stripe.Customer.retrieve(self.stripe_id)
|
|
|
|
stripe_payments = stripe.Charge.all(customer=stripe_customer.id).data
|
2013-07-04 11:29:41 -07:00
|
|
|
if partial:
|
2013-07-04 11:34:09 -07:00
|
|
|
stripe_payments[0].refund(amount=1200)
|
2013-07-04 11:29:41 -07:00
|
|
|
refunded = 12
|
|
|
|
else:
|
|
|
|
stripe_payments[0].refund()
|
|
|
|
self.cancel_premium()
|
|
|
|
refunded = stripe_payments[0].amount/100
|
2013-05-23 16:28:39 -07:00
|
|
|
logging.user(self.user, "~FRRefunding stripe payment: $%s" % refunded)
|
|
|
|
else:
|
|
|
|
paypal_opts = {
|
|
|
|
'API_ENVIRONMENT': 'PRODUCTION',
|
|
|
|
'API_USERNAME': settings.PAYPAL_API_USERNAME,
|
|
|
|
'API_PASSWORD': settings.PAYPAL_API_PASSWORD,
|
|
|
|
'API_SIGNATURE': settings.PAYPAL_API_SIGNATURE,
|
|
|
|
}
|
|
|
|
paypal = PayPalInterface(**paypal_opts)
|
|
|
|
transaction = PayPalIPN.objects.filter(custom=self.user.username,
|
|
|
|
txn_type='subscr_payment')[0]
|
|
|
|
refund = paypal.refund_transaction(transaction.txn_id)
|
2013-06-25 21:48:22 -07:00
|
|
|
try:
|
|
|
|
refunded = int(float(refund['raw']['TOTALREFUNDEDAMOUNT'][0]))
|
|
|
|
except KeyError:
|
|
|
|
refunded = int(transaction.amount)
|
2013-05-23 16:28:39 -07:00
|
|
|
logging.user(self.user, "~FRRefunding paypal payment: $%s" % refunded)
|
|
|
|
self.cancel_premium()
|
2013-05-10 12:05:24 -07:00
|
|
|
|
|
|
|
return refunded
|
2013-04-05 17:54:10 -07:00
|
|
|
|
2013-03-13 15:46:51 -07:00
|
|
|
def cancel_premium(self):
|
2013-05-23 16:28:39 -07:00
|
|
|
paypal_cancel = self.cancel_premium_paypal()
|
|
|
|
stripe_cancel = self.cancel_premium_stripe()
|
|
|
|
return paypal_cancel or stripe_cancel
|
2013-03-13 15:46:51 -07:00
|
|
|
|
|
|
|
def cancel_premium_paypal(self):
|
2013-05-23 16:28:39 -07:00
|
|
|
transactions = PayPalIPN.objects.filter(custom=self.user.username,
|
|
|
|
txn_type='subscr_signup')
|
|
|
|
if not transactions:
|
|
|
|
return
|
|
|
|
|
|
|
|
paypal_opts = {
|
|
|
|
'API_ENVIRONMENT': 'PRODUCTION',
|
|
|
|
'API_USERNAME': settings.PAYPAL_API_USERNAME,
|
|
|
|
'API_PASSWORD': settings.PAYPAL_API_PASSWORD,
|
|
|
|
'API_SIGNATURE': settings.PAYPAL_API_SIGNATURE,
|
|
|
|
}
|
|
|
|
paypal = PayPalInterface(**paypal_opts)
|
|
|
|
transaction = transactions[0]
|
|
|
|
profileid = transaction.subscr_id
|
|
|
|
paypal.manage_recurring_payments_profile_status(profileid=profileid, action='Cancel')
|
|
|
|
|
|
|
|
logging.user(self.user, "~FRCanceling Paypal subscription")
|
|
|
|
|
|
|
|
return True
|
2013-03-13 15:46:51 -07:00
|
|
|
|
|
|
|
def cancel_premium_stripe(self):
|
|
|
|
if not self.stripe_id:
|
|
|
|
return
|
|
|
|
|
|
|
|
stripe.api_key = settings.STRIPE_SECRET
|
|
|
|
stripe_customer = stripe.Customer.retrieve(self.stripe_id)
|
|
|
|
stripe_customer.cancel_subscription()
|
2013-04-05 17:54:10 -07:00
|
|
|
|
|
|
|
logging.user(self.user, "~FRCanceling Stripe subscription")
|
2010-10-23 11:20:54 -04:00
|
|
|
|
2013-03-13 15:46:51 -07:00
|
|
|
return True
|
2013-05-23 18:14:21 -07:00
|
|
|
|
2011-01-30 23:56:51 -05:00
|
|
|
def queue_new_feeds(self, new_feeds=None):
|
|
|
|
if not new_feeds:
|
|
|
|
new_feeds = UserSubscription.objects.filter(user=self.user,
|
|
|
|
feed__fetched_once=False,
|
|
|
|
active=True).values('feed_id')
|
|
|
|
new_feeds = list(set([f['feed_id'] for f in new_feeds]))
|
2011-02-23 13:46:47 -05:00
|
|
|
logging.user(self.user, "~BB~FW~SBQueueing NewFeeds: ~FC(%s) %s" % (len(new_feeds), new_feeds))
|
2011-01-30 23:56:51 -05:00
|
|
|
size = 4
|
|
|
|
for t in (new_feeds[pos:pos + size] for pos in xrange(0, len(new_feeds), size)):
|
2012-07-12 23:58:29 -07:00
|
|
|
NewFeeds.apply_async(args=(t,), queue="new_feeds")
|
2011-01-30 23:56:51 -05:00
|
|
|
|
|
|
|
def refresh_stale_feeds(self, exclude_new=False):
|
2011-01-17 14:20:36 -05:00
|
|
|
stale_cutoff = datetime.datetime.now() - datetime.timedelta(days=7)
|
|
|
|
stale_feeds = UserSubscription.objects.filter(user=self.user, active=True, feed__last_update__lte=stale_cutoff)
|
2011-01-30 23:56:51 -05:00
|
|
|
if exclude_new:
|
|
|
|
stale_feeds = stale_feeds.filter(feed__fetched_once=True)
|
2011-01-17 14:20:36 -05:00
|
|
|
all_feeds = UserSubscription.objects.filter(user=self.user, active=True)
|
|
|
|
|
2011-02-23 13:46:47 -05:00
|
|
|
logging.user(self.user, "~FG~BBRefreshing stale feeds: ~SB%s/%s" % (
|
|
|
|
stale_feeds.count(), all_feeds.count()))
|
2011-01-17 14:20:36 -05:00
|
|
|
|
|
|
|
for sub in stale_feeds:
|
|
|
|
sub.feed.fetched_once = False
|
|
|
|
sub.feed.save()
|
|
|
|
|
2011-01-18 08:45:35 -05:00
|
|
|
if stale_feeds:
|
2012-01-26 09:32:24 -08:00
|
|
|
stale_feeds = list(set([f.feed_id for f in stale_feeds]))
|
2011-01-30 23:56:51 -05:00
|
|
|
self.queue_new_feeds(new_feeds=stale_feeds)
|
2011-09-19 08:56:16 -07:00
|
|
|
|
2012-11-19 14:28:04 -08:00
|
|
|
def import_reader_starred_items(self, count=20):
|
|
|
|
importer = GoogleReaderImporter(self.user)
|
|
|
|
importer.import_starred_items(count=count)
|
|
|
|
|
2011-09-21 17:49:26 -07:00
|
|
|
def send_new_user_email(self):
|
|
|
|
if not self.user.email or not self.send_emails:
|
2011-09-19 08:56:16 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_new_account.txt', locals())
|
|
|
|
html = render_to_string('mail/email_new_account.xhtml', locals())
|
|
|
|
subject = "Welcome to NewsBlur, %s" % (self.user.username)
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
2011-10-28 10:29:11 -07:00
|
|
|
msg.send(fail_silently=True)
|
2011-09-23 18:13:16 -07:00
|
|
|
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending email for new user: %s" % self.user.email)
|
2013-05-23 18:14:21 -07:00
|
|
|
|
|
|
|
def send_opml_export_email(self):
|
|
|
|
if not self.user.email:
|
|
|
|
return
|
|
|
|
|
|
|
|
MSentEmail.objects.get_or_create(receiver_user_id=self.user.pk,
|
|
|
|
email_type='opml_export')
|
|
|
|
|
|
|
|
exporter = OPMLExporter(self.user)
|
|
|
|
opml = exporter.process()
|
2013-04-22 15:24:38 -07:00
|
|
|
|
2013-05-23 18:14:21 -07:00
|
|
|
params = {
|
|
|
|
'feed_count': UserSubscription.objects.filter(user=self.user).count(),
|
|
|
|
}
|
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_opml_export.txt', params)
|
|
|
|
html = render_to_string('mail/email_opml_export.xhtml', params)
|
|
|
|
subject = "Backup OPML file of your NewsBlur sites"
|
|
|
|
filename= 'NewsBlur Subscriptions - %s.xml' % datetime.datetime.now().strftime('%Y-%m-%d')
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
|
|
|
msg.attach(filename, opml, 'text/xml')
|
|
|
|
msg.send(fail_silently=True)
|
|
|
|
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending OPML backup email to: %s" % self.user.email)
|
|
|
|
|
2013-04-22 15:24:38 -07:00
|
|
|
def send_first_share_to_blurblog_email(self, force=False):
|
|
|
|
from apps.social.models import MSocialProfile, MSharedStory
|
|
|
|
|
|
|
|
if not self.user.email:
|
|
|
|
return
|
|
|
|
|
|
|
|
sent_email, created = MSentEmail.objects.get_or_create(receiver_user_id=self.user.pk,
|
|
|
|
email_type='first_share')
|
|
|
|
|
|
|
|
if not created and not force:
|
|
|
|
return
|
|
|
|
|
|
|
|
social_profile = MSocialProfile.objects.get(user_id=self.user.pk)
|
|
|
|
params = {
|
|
|
|
'shared_stories': MSharedStory.objects.filter(user_id=self.user.pk).count(),
|
|
|
|
'blurblog_url': social_profile.blurblog_url,
|
|
|
|
'blurblog_rss': social_profile.blurblog_rss
|
|
|
|
}
|
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_first_share_to_blurblog.txt', params)
|
|
|
|
html = render_to_string('mail/email_first_share_to_blurblog.xhtml', params)
|
|
|
|
subject = "Your shared stories on NewsBlur are available on your Blurblog"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
|
|
|
msg.send(fail_silently=True)
|
|
|
|
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending first share to blurblog email to: %s" % self.user.email)
|
2011-09-19 08:56:16 -07:00
|
|
|
|
2011-10-26 20:09:28 -07:00
|
|
|
def send_new_premium_email(self, force=False):
|
2012-07-05 22:20:49 -07:00
|
|
|
subs = UserSubscription.objects.filter(user=self.user)
|
|
|
|
message = """Woohoo!
|
|
|
|
|
|
|
|
User: %(user)s
|
|
|
|
Feeds: %(feeds)s
|
|
|
|
|
|
|
|
Sincerely,
|
|
|
|
NewsBlur""" % {'user': self.user.username, 'feeds': subs.count()}
|
|
|
|
mail_admins('New premium account', message, fail_silently=True)
|
|
|
|
|
2011-09-21 17:49:26 -07:00
|
|
|
if not self.user.email or not self.send_emails:
|
2011-09-19 08:56:16 -07:00
|
|
|
return
|
|
|
|
|
2012-10-01 17:03:45 -07:00
|
|
|
sent_email, created = MSentEmail.objects.get_or_create(receiver_user_id=self.user.pk,
|
|
|
|
email_type='new_premium')
|
|
|
|
|
|
|
|
if not created and not force:
|
2011-10-26 20:09:28 -07:00
|
|
|
return
|
|
|
|
|
2011-09-19 08:56:16 -07:00
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_new_premium.txt', locals())
|
|
|
|
html = render_to_string('mail/email_new_premium.xhtml', locals())
|
|
|
|
subject = "Thanks for going premium on NewsBlur!"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
2011-10-28 10:29:11 -07:00
|
|
|
msg.send(fail_silently=True)
|
2011-09-23 18:13:16 -07:00
|
|
|
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending email for new premium: %s" % self.user.email)
|
2011-09-22 09:23:42 -07:00
|
|
|
|
|
|
|
def send_forgot_password_email(self, email=None):
|
|
|
|
if not self.user.email and not email:
|
|
|
|
print "Please provide an email address."
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self.user.email and email:
|
|
|
|
self.user.email = email
|
|
|
|
self.user.save()
|
|
|
|
|
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_forgot_password.txt', locals())
|
|
|
|
html = render_to_string('mail/email_forgot_password.xhtml', locals())
|
|
|
|
subject = "Forgot your password on NewsBlur?"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
2011-10-28 10:29:11 -07:00
|
|
|
msg.send(fail_silently=True)
|
2011-09-22 09:23:42 -07:00
|
|
|
|
2011-09-23 18:13:16 -07:00
|
|
|
logging.user(self.user, "~BB~FM~SBSending email for forgotten password: %s" % self.user.email)
|
2013-05-13 18:03:17 -07:00
|
|
|
|
|
|
|
def send_new_user_queue_email(self, force=False):
|
|
|
|
if not self.user.email:
|
|
|
|
print "Please provide an email address."
|
|
|
|
return
|
2011-09-23 18:13:16 -07:00
|
|
|
|
2013-05-13 18:03:17 -07:00
|
|
|
sent_email, created = MSentEmail.objects.get_or_create(receiver_user_id=self.user.pk,
|
|
|
|
email_type='new_user_queue')
|
|
|
|
if not created and not force:
|
|
|
|
return
|
|
|
|
|
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_new_user_queue.txt', locals())
|
|
|
|
html = render_to_string('mail/email_new_user_queue.xhtml', locals())
|
|
|
|
subject = "Your free account is now ready to go on NewsBlur"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
|
|
|
msg.send(fail_silently=True)
|
|
|
|
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending email for new user queue: %s" % self.user.email)
|
|
|
|
|
2012-07-20 19:43:28 -07:00
|
|
|
def send_upload_opml_finished_email(self, feed_count):
|
|
|
|
if not self.user.email:
|
|
|
|
print "Please provide an email address."
|
|
|
|
return
|
|
|
|
|
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_upload_opml_finished.txt', locals())
|
|
|
|
html = render_to_string('mail/email_upload_opml_finished.xhtml', locals())
|
|
|
|
subject = "Your OPML upload is complete. Get going with NewsBlur!"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
|
|
|
msg.send()
|
|
|
|
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending email for OPML upload: %s" % self.user.email)
|
2013-04-02 15:41:50 -07:00
|
|
|
|
|
|
|
def send_import_reader_finished_email(self, feed_count):
|
|
|
|
if not self.user.email:
|
|
|
|
print "Please provide an email address."
|
|
|
|
return
|
|
|
|
|
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_import_reader_finished.txt', locals())
|
|
|
|
html = render_to_string('mail/email_import_reader_finished.xhtml', locals())
|
|
|
|
subject = "Your Google Reader import is complete. Get going with NewsBlur!"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
|
|
|
msg.send()
|
|
|
|
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending email for Google Reader import: %s" % self.user.email)
|
|
|
|
|
|
|
|
def send_import_reader_starred_finished_email(self, feed_count, starred_count):
|
|
|
|
if not self.user.email:
|
|
|
|
print "Please provide an email address."
|
|
|
|
return
|
|
|
|
|
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_import_reader_starred_finished.txt', locals())
|
|
|
|
html = render_to_string('mail/email_import_reader_starred_finished.xhtml', locals())
|
|
|
|
subject = "Your Google Reader starred stories import is complete. Get going with NewsBlur!"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
|
|
|
msg.send()
|
|
|
|
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending email for Google Reader starred stories import: %s" % self.user.email)
|
2012-07-20 19:43:28 -07:00
|
|
|
|
2012-08-09 19:45:08 -07:00
|
|
|
def send_launch_social_email(self, force=False):
|
|
|
|
if not self.user.email or not self.send_emails:
|
2012-08-09 20:42:04 -07:00
|
|
|
logging.user(self.user, "~FM~SB~FRNot~FM sending launch social email for user, %s: %s" % (self.user.email and 'opt-out: ' or 'blank', self.user.email))
|
2012-08-09 19:45:08 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
sent_email, created = MSentEmail.objects.get_or_create(receiver_user_id=self.user.pk,
|
|
|
|
email_type='launch_social')
|
|
|
|
|
|
|
|
if not created and not force:
|
2012-08-09 20:42:04 -07:00
|
|
|
logging.user(self.user, "~FM~SB~FRNot~FM sending launch social email for user, sent already: %s" % self.user.email)
|
2012-08-09 19:45:08 -07:00
|
|
|
return
|
|
|
|
|
|
|
|
delta = datetime.datetime.now() - self.last_seen_on
|
|
|
|
months_ago = delta.days / 30
|
|
|
|
user = self.user
|
|
|
|
data = dict(user=user, months_ago=months_ago)
|
|
|
|
text = render_to_string('mail/email_launch_social.txt', data)
|
|
|
|
html = render_to_string('mail/email_launch_social.xhtml', data)
|
|
|
|
subject = "NewsBlur is now a social news reader"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
|
|
|
msg.send(fail_silently=True)
|
|
|
|
|
2012-08-09 20:42:04 -07:00
|
|
|
logging.user(self.user, "~BB~FM~SBSending launch social email for user: %s months, %s" % (months_ago, self.user.email))
|
2012-12-05 11:56:55 -08:00
|
|
|
|
|
|
|
def send_premium_expire_grace_period_email(self, force=False):
|
|
|
|
if not self.user.email:
|
2013-01-07 17:43:05 -08:00
|
|
|
logging.user(self.user, "~FM~SB~FRNot~FM~SN sending premium expire grace for user: %s" % (self.user))
|
2012-12-05 11:56:55 -08:00
|
|
|
return
|
|
|
|
|
|
|
|
emails_sent = MSentEmail.objects.filter(receiver_user_id=self.user.pk,
|
|
|
|
email_type='premium_expire_grace')
|
|
|
|
day_ago = datetime.datetime.now() - datetime.timedelta(days=360)
|
|
|
|
for email in emails_sent:
|
|
|
|
if email.date_sent > day_ago:
|
2013-01-07 17:43:05 -08:00
|
|
|
logging.user(self.user, "~SN~FMNot sending premium expire grace email, already sent before.")
|
2012-12-05 11:56:55 -08:00
|
|
|
return
|
|
|
|
|
|
|
|
self.premium_expire = datetime.datetime.now()
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
delta = datetime.datetime.now() - self.last_seen_on
|
|
|
|
months_ago = delta.days / 30
|
|
|
|
user = self.user
|
|
|
|
data = dict(user=user, months_ago=months_ago)
|
|
|
|
text = render_to_string('mail/email_premium_expire_grace.txt', data)
|
|
|
|
html = render_to_string('mail/email_premium_expire_grace.xhtml', data)
|
|
|
|
subject = "Your premium account on NewsBlur has one more month!"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
|
|
|
msg.send(fail_silently=True)
|
|
|
|
|
|
|
|
MSentEmail.record(receiver_user_id=self.user.pk, email_type='premium_expire_grace')
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending premium expire grace email for user: %s months, %s" % (months_ago, self.user.email))
|
|
|
|
|
|
|
|
def send_premium_expire_email(self, force=False):
|
|
|
|
if not self.user.email:
|
|
|
|
logging.user(self.user, "~FM~SB~FRNot~FM sending premium expire for user: %s" % (self.user))
|
|
|
|
return
|
|
|
|
|
|
|
|
emails_sent = MSentEmail.objects.filter(receiver_user_id=self.user.pk,
|
|
|
|
email_type='premium_expire')
|
|
|
|
day_ago = datetime.datetime.now() - datetime.timedelta(days=360)
|
|
|
|
for email in emails_sent:
|
|
|
|
if email.date_sent > day_ago:
|
2013-01-07 17:43:05 -08:00
|
|
|
logging.user(self.user, "~FM~SBNot sending premium expire email, already sent before.")
|
2012-12-05 11:56:55 -08:00
|
|
|
return
|
|
|
|
|
|
|
|
delta = datetime.datetime.now() - self.last_seen_on
|
|
|
|
months_ago = delta.days / 30
|
|
|
|
user = self.user
|
|
|
|
data = dict(user=user, months_ago=months_ago)
|
|
|
|
text = render_to_string('mail/email_premium_expire.txt', data)
|
|
|
|
html = render_to_string('mail/email_premium_expire.xhtml', data)
|
|
|
|
subject = "Your premium account on NewsBlur has expired"
|
|
|
|
msg = EmailMultiAlternatives(subject, text,
|
|
|
|
from_email='NewsBlur <%s>' % settings.HELLO_EMAIL,
|
|
|
|
to=['%s <%s>' % (user, user.email)])
|
|
|
|
msg.attach_alternative(html, "text/html")
|
|
|
|
msg.send(fail_silently=True)
|
|
|
|
|
|
|
|
MSentEmail.record(receiver_user_id=self.user.pk, email_type='premium_expire')
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending premium expire email for user: %s months, %s" % (months_ago, self.user.email))
|
2012-08-09 19:45:08 -07:00
|
|
|
|
2011-09-19 09:46:36 -07:00
|
|
|
def autologin_url(self, next=None):
|
|
|
|
return reverse('autologin', kwargs={
|
|
|
|
'username': self.user.username,
|
|
|
|
'secret': self.secret_token
|
|
|
|
}) + ('?' + next + '=1' if next else '')
|
|
|
|
|
2012-04-09 17:20:47 -07:00
|
|
|
|
2012-04-16 11:21:52 -07:00
|
|
|
|
2010-05-11 21:36:17 -04:00
|
|
|
def create_profile(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
Profile.objects.create(user=instance)
|
2010-06-11 20:55:38 -04:00
|
|
|
else:
|
|
|
|
Profile.objects.get_or_create(user=instance)
|
2010-10-16 18:52:52 -04:00
|
|
|
post_save.connect(create_profile, sender=User)
|
|
|
|
|
|
|
|
|
|
|
|
def paypal_signup(sender, **kwargs):
|
|
|
|
ipn_obj = sender
|
2013-01-31 16:55:45 -08:00
|
|
|
try:
|
|
|
|
user = User.objects.get(username__iexact=ipn_obj.custom)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
user = User.objects.get(email__iexact=ipn_obj.payer_email)
|
2011-10-19 09:40:31 -07:00
|
|
|
try:
|
|
|
|
if not user.email:
|
|
|
|
user.email = ipn_obj.payer_email
|
|
|
|
user.save()
|
|
|
|
except:
|
|
|
|
pass
|
2010-10-23 11:20:54 -04:00
|
|
|
user.profile.activate_premium()
|
2010-11-08 12:09:55 -05:00
|
|
|
subscription_signup.connect(paypal_signup)
|
|
|
|
|
2012-12-03 15:17:35 -08:00
|
|
|
def paypal_payment_history_sync(sender, **kwargs):
|
|
|
|
ipn_obj = sender
|
2013-01-31 16:55:45 -08:00
|
|
|
try:
|
|
|
|
user = User.objects.get(username__iexact=ipn_obj.custom)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
user = User.objects.get(email__iexact=ipn_obj.payer_email)
|
2012-12-03 15:17:35 -08:00
|
|
|
try:
|
|
|
|
user.profile.setup_premium_history()
|
|
|
|
except:
|
|
|
|
return {"code": -1, "message": "User doesn't exist."}
|
|
|
|
payment_was_successful.connect(paypal_payment_history_sync)
|
|
|
|
|
2012-02-28 17:39:02 -08:00
|
|
|
def stripe_signup(sender, full_json, **kwargs):
|
2012-07-27 12:46:37 -07:00
|
|
|
stripe_id = full_json['data']['object']['customer']
|
|
|
|
try:
|
|
|
|
profile = Profile.objects.get(stripe_id=stripe_id)
|
|
|
|
profile.activate_premium()
|
|
|
|
except Profile.DoesNotExist:
|
|
|
|
return {"code": -1, "message": "User doesn't exist."}
|
2012-02-27 21:46:34 -08:00
|
|
|
zebra_webhook_customer_subscription_created.connect(stripe_signup)
|
|
|
|
|
2012-12-03 15:17:35 -08:00
|
|
|
def stripe_payment_history_sync(sender, full_json, **kwargs):
|
|
|
|
stripe_id = full_json['data']['object']['customer']
|
|
|
|
try:
|
|
|
|
profile = Profile.objects.get(stripe_id=stripe_id)
|
|
|
|
profile.setup_premium_history()
|
|
|
|
except Profile.DoesNotExist:
|
|
|
|
return {"code": -1, "message": "User doesn't exist."}
|
|
|
|
zebra_webhook_charge_succeeded.connect(stripe_payment_history_sync)
|
|
|
|
|
2013-05-06 15:12:18 -07:00
|
|
|
def change_password(user, old_password, new_password, only_check=False):
|
2010-11-08 12:09:55 -05:00
|
|
|
user_db = authenticate(username=user.username, password=old_password)
|
|
|
|
if user_db is None:
|
2013-03-20 18:36:15 -07:00
|
|
|
blank = blank_authenticate(user.username)
|
2013-05-06 15:12:18 -07:00
|
|
|
if blank and not only_check:
|
|
|
|
user.set_password(new_password or user.username)
|
2013-03-20 18:36:15 -07:00
|
|
|
user.save()
|
|
|
|
if user_db is None:
|
|
|
|
user_db = authenticate(username=user.username, password=user.username)
|
|
|
|
|
|
|
|
if not user_db:
|
2010-11-08 12:09:55 -05:00
|
|
|
return -1
|
|
|
|
else:
|
2013-05-06 15:12:18 -07:00
|
|
|
if not only_check:
|
|
|
|
user_db.set_password(new_password)
|
|
|
|
user_db.save()
|
2012-07-09 13:55:29 -07:00
|
|
|
return 1
|
2013-04-05 19:23:42 -07:00
|
|
|
|
2013-03-20 18:36:15 -07:00
|
|
|
def blank_authenticate(username, password=""):
|
|
|
|
try:
|
2013-05-06 15:12:18 -07:00
|
|
|
user = User.objects.get(username__iexact=username)
|
2013-03-20 18:36:15 -07:00
|
|
|
except User.DoesNotExist:
|
|
|
|
return
|
2012-07-09 13:55:29 -07:00
|
|
|
|
2013-03-20 18:36:15 -07:00
|
|
|
if user.password == "!":
|
|
|
|
return user
|
|
|
|
|
|
|
|
algorithm, salt, hash = user.password.split('$', 2)
|
2013-04-05 19:23:42 -07:00
|
|
|
encoded_blank = hashlib.sha1(salt + password).hexdigest()
|
|
|
|
encoded_username = authenticate(username=username, password=username)
|
|
|
|
if encoded_blank == hash or encoded_username == user:
|
2013-03-20 18:36:15 -07:00
|
|
|
return user
|
|
|
|
|
2012-07-09 13:55:29 -07:00
|
|
|
class MSentEmail(mongo.Document):
|
|
|
|
sending_user_id = mongo.IntField()
|
|
|
|
receiver_user_id = mongo.IntField()
|
|
|
|
email_type = mongo.StringField()
|
|
|
|
date_sent = mongo.DateTimeField(default=datetime.datetime.now)
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
'collection': 'sent_emails',
|
|
|
|
'allow_inheritance': False,
|
2012-08-09 19:45:08 -07:00
|
|
|
'indexes': ['sending_user_id', 'receiver_user_id', 'email_type'],
|
2012-07-09 13:55:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return "%s sent %s email to %s" % (self.sending_user_id, self.email_type, self.receiver_user_id)
|
|
|
|
|
|
|
|
@classmethod
|
2012-12-05 13:10:11 -08:00
|
|
|
def record(cls, email_type, receiver_user_id, sending_user_id=None):
|
2012-07-09 13:55:29 -07:00
|
|
|
cls.objects.create(email_type=email_type,
|
|
|
|
receiver_user_id=receiver_user_id,
|
|
|
|
sending_user_id=sending_user_id)
|
2012-12-03 14:35:21 -08:00
|
|
|
|
|
|
|
class PaymentHistory(models.Model):
|
|
|
|
user = models.ForeignKey(User, related_name='payments')
|
2012-12-03 16:12:13 -08:00
|
|
|
payment_date = models.DateTimeField()
|
2012-12-03 14:35:21 -08:00
|
|
|
payment_amount = models.IntegerField()
|
|
|
|
payment_provider = models.CharField(max_length=20)
|
|
|
|
|
2013-01-31 16:55:45 -08:00
|
|
|
def __unicode__(self):
|
|
|
|
return "[%s] $%s/%s" % (self.payment_date.strftime("%Y-%m-%d"), self.payment_amount,
|
|
|
|
self.payment_provider)
|
2012-12-03 14:35:21 -08:00
|
|
|
class Meta:
|
2012-12-03 15:03:47 -08:00
|
|
|
ordering = ['-payment_date']
|
|
|
|
|
2013-06-12 13:52:43 -07:00
|
|
|
def canonical(self):
|
2012-12-03 15:03:47 -08:00
|
|
|
return {
|
|
|
|
'payment_date': self.payment_date.strftime('%Y-%m-%d'),
|
|
|
|
'payment_amount': self.payment_amount,
|
|
|
|
'payment_provider': self.payment_provider,
|
2013-02-15 13:47:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def report(cls, months=12):
|
|
|
|
total = cls.objects.all().aggregate(sum=Sum('payment_amount'))
|
|
|
|
print "Total: $%s" % total['sum']
|
|
|
|
|
|
|
|
for m in range(months):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
start_date = now - datetime.timedelta(days=(m+1)*30)
|
|
|
|
end_date = now - datetime.timedelta(days=m*30)
|
|
|
|
payments = cls.objects.filter(payment_date__gte=start_date, payment_date__lte=end_date)
|
|
|
|
payments = payments.aggregate(avg=Avg('payment_amount'),
|
|
|
|
sum=Sum('payment_amount'),
|
|
|
|
count=Count('user'))
|
|
|
|
print "%s months ago: avg=$%s sum=$%s users=%s" % (
|
|
|
|
m, payments['avg'], payments['sum'], payments['count'])
|
2013-05-13 16:17:49 -07:00
|
|
|
|
|
|
|
class RNewUserQueue:
|
|
|
|
|
|
|
|
KEY = "new_user_queue"
|
|
|
|
|
2013-05-13 18:03:17 -07:00
|
|
|
@classmethod
|
|
|
|
def activate_next(cls):
|
|
|
|
count = cls.user_count()
|
|
|
|
if not count:
|
|
|
|
return
|
|
|
|
|
|
|
|
user_id = cls.pop_user()
|
|
|
|
user = User.objects.get(pk=user_id)
|
|
|
|
logging.user(user, "~FBActivating free account. %s still in queue." % (count-1))
|
|
|
|
|
|
|
|
user.profile.activate_free()
|
|
|
|
|
2013-05-13 16:17:49 -07:00
|
|
|
@classmethod
|
|
|
|
def add_user(cls, user_id):
|
|
|
|
r = redis.Redis(connection_pool=settings.REDIS_FEED_POOL)
|
|
|
|
now = time.time()
|
|
|
|
|
|
|
|
r.zadd(cls.KEY, user_id, now)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def user_count(cls):
|
|
|
|
r = redis.Redis(connection_pool=settings.REDIS_FEED_POOL)
|
|
|
|
count = r.zcard(cls.KEY)
|
|
|
|
|
|
|
|
return count
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def user_position(cls, user_id):
|
|
|
|
r = redis.Redis(connection_pool=settings.REDIS_FEED_POOL)
|
2013-05-13 18:03:17 -07:00
|
|
|
position = r.zrank(cls.KEY, user_id)
|
|
|
|
if position >= 0:
|
|
|
|
return position + 1
|
2013-05-13 16:17:49 -07:00
|
|
|
|
|
|
|
@classmethod
|
2013-05-13 18:03:17 -07:00
|
|
|
def pop_user(cls):
|
2013-05-13 16:17:49 -07:00
|
|
|
r = redis.Redis(connection_pool=settings.REDIS_FEED_POOL)
|
2013-05-13 18:03:17 -07:00
|
|
|
user = r.zrange(cls.KEY, 0, 0)[0]
|
2013-05-13 16:17:49 -07:00
|
|
|
r.zrem(cls.KEY, user)
|
2013-05-13 18:03:17 -07:00
|
|
|
|
2013-05-13 16:17:49 -07:00
|
|
|
return user
|
2013-07-05 09:31:49 +02:00
|
|
|
|