2010-06-30 13:36:51 -04:00
|
|
|
import datetime
|
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
|
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
|
|
|
|
from celery.task import Task
|
2010-10-16 23:24:55 -04:00
|
|
|
from apps.reader.models import UserSubscription
|
2010-10-29 09:02:54 -04:00
|
|
|
from apps.rss_feeds.models import Feed
|
2011-01-30 23:56:51 -05:00
|
|
|
from apps.rss_feeds.tasks import NewFeeds
|
2010-10-23 12:29:18 -04:00
|
|
|
from utils import log as logging
|
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
|
|
|
|
from vendor.paypal.standard.ipn.signals import subscription_signup
|
2012-02-28 17:37:01 -08:00
|
|
|
from zebra.signals import zebra_webhook_customer_subscription_created
|
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)
|
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="[]")
|
2011-05-12 18:15:15 -04:00
|
|
|
feed_pane_size = models.IntegerField(default=240)
|
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-08-07 21:38:09 -07:00
|
|
|
hide_mobile = models.BooleanField(default=False)
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2010-10-23 11:20:54 -04:00
|
|
|
def activate_premium(self):
|
2011-10-26 20:09:28 -07:00
|
|
|
self.send_new_premium_email()
|
|
|
|
|
2010-10-23 11:20:54 -04:00
|
|
|
self.is_premium = True
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
subs = UserSubscription.objects.filter(user=self.user)
|
|
|
|
for sub in subs:
|
|
|
|
sub.active = True
|
2010-10-25 20:20:59 -04:00
|
|
|
try:
|
|
|
|
sub.save()
|
|
|
|
sub.feed.setup_feed_for_premium_subscribers()
|
2010-10-29 09:02:54 -04:00
|
|
|
except IntegrityError, Feed.DoesNotExist:
|
2010-10-25 20:20:59 -04:00
|
|
|
pass
|
2010-10-23 11:20:54 -04:00
|
|
|
|
2011-01-30 23:56:51 -05:00
|
|
|
self.queue_new_feeds()
|
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()))
|
2010-10-23 12:29:18 -04:00
|
|
|
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)
|
2010-10-23 11:20:54 -04: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
|
|
|
|
publisher = Task.get_publisher(exchange="new_feeds")
|
|
|
|
for t in (new_feeds[pos:pos + size] for pos in xrange(0, len(new_feeds), size)):
|
|
|
|
NewFeeds.apply_async(args=(t,), queue="new_feeds", publisher=publisher)
|
|
|
|
publisher.connection.close()
|
|
|
|
|
|
|
|
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
|
|
|
|
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)
|
2011-09-19 08:56:16 -07:00
|
|
|
|
2011-10-26 20:09:28 -07:00
|
|
|
def send_new_premium_email(self, force=False):
|
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
|
|
|
|
|
2011-10-26 20:09:28 -07:00
|
|
|
if self.is_premium and not force:
|
|
|
|
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
|
|
|
|
2012-03-11 13:26:57 -07:00
|
|
|
user.set_password('')
|
|
|
|
user.save()
|
|
|
|
|
2011-09-23 18:13:16 -07:00
|
|
|
logging.user(self.user, "~BB~FM~SBSending email for forgotten password: %s" % self.user.email)
|
2012-04-11 17:13:54 -07:00
|
|
|
|
|
|
|
def send_social_beta_email(self):
|
|
|
|
from apps.social.models import MRequestInvite
|
|
|
|
if not self.user.email:
|
|
|
|
print "Please provide an email address."
|
|
|
|
return
|
2011-09-23 18:13:16 -07:00
|
|
|
|
2012-04-11 17:13:54 -07:00
|
|
|
user = self.user
|
|
|
|
text = render_to_string('mail/email_social_beta.txt', locals())
|
|
|
|
html = render_to_string('mail/email_social_beta.xhtml', locals())
|
2012-04-11 17:21:33 -07:00
|
|
|
subject = "Psst, you're in..."
|
2012-04-11 17:13:54 -07:00
|
|
|
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()
|
|
|
|
|
2012-04-16 14:43:36 -07:00
|
|
|
invites = MRequestInvite.objects.filter(username__iexact=self.user.username)
|
2012-04-11 17:13:54 -07:00
|
|
|
if not invites:
|
2012-04-16 14:43:36 -07:00
|
|
|
invites = MRequestInvite.objects.filter(username__iexact=self.user.email)
|
2012-04-11 17:13:54 -07:00
|
|
|
if not invites:
|
|
|
|
print "User not on invite list"
|
|
|
|
else:
|
|
|
|
for invite in invites:
|
|
|
|
print "Invite listed as: %s" % invite.username
|
|
|
|
invite.email_sent = True
|
|
|
|
invite.save()
|
|
|
|
|
|
|
|
logging.user(self.user, "~BB~FM~SBSending email for social beta: %s" % self.user.email)
|
|
|
|
|
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
|
2010-10-16 23:23:15 -04:00
|
|
|
user = User.objects.get(username=ipn_obj.custom)
|
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-02-28 17:39:02 -08:00
|
|
|
def stripe_signup(sender, full_json, **kwargs):
|
|
|
|
profile = Profile.objects.get(stripe_id=full_json['data']['object']['customer'])
|
2012-02-28 13:14:45 -08:00
|
|
|
profile.activate_premium()
|
2012-02-27 21:46:34 -08:00
|
|
|
zebra_webhook_customer_subscription_created.connect(stripe_signup)
|
|
|
|
|
2010-11-08 12:09:55 -05:00
|
|
|
def change_password(user, old_password, new_password):
|
|
|
|
user_db = authenticate(username=user.username, password=old_password)
|
|
|
|
if user_db is None:
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
user_db.set_password(new_password)
|
|
|
|
user_db.save()
|
|
|
|
return 1
|