add user prefrence for insite notifications
authorRodney Ewing <ewing.rj@gmail.com>
Thu, 8 Aug 2013 22:07:07 +0000 (15:07 -0700)
committerRodney Ewing <ewing.rj@gmail.com>
Tue, 20 Aug 2013 15:10:50 +0000 (08:10 -0700)
mediagoblin/db/migrations.py
mediagoblin/db/models.py
mediagoblin/edit/forms.py
mediagoblin/edit/views.py
mediagoblin/notifications/__init__.py

index 374ab4c8f6b6bbba969d2b8ccf399cdb62931207..d542d7b935e573df66444a36527d6e832c23760c 100644 (file)
@@ -425,7 +425,7 @@ class RequestToken_v0(declarative_base()):
     callback = Column(Unicode, nullable=False, default=u"oob")
     created = Column(DateTime, nullable=False, default=datetime.datetime.now)
     updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
-    
+
 class AccessToken_v0(declarative_base()):
     """
         Model for representing the access tokens
@@ -438,7 +438,7 @@ class AccessToken_v0(declarative_base()):
     request_token = Column(Unicode, ForeignKey(RequestToken_v0.token))
     created = Column(DateTime, nullable=False, default=datetime.datetime.now)
     updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
+
 
 class NonceTimestamp_v0(declarative_base()):
     """
@@ -460,3 +460,15 @@ def create_oauth1_tables(db):
     NonceTimestamp_v0.__table__.create(db.bind)
 
     db.commit()
+
+
+@RegisterMigration(15, MIGRATIONS)
+def wants_notifications(db):
+    """Add a wants_notifications field to User model"""
+    metadata = MetaData(bind=db.bind)
+    user_table = inspect_table(metadata, "core__users")
+
+    col = Column('wants_notifications', Boolean, default=True)
+    col.create(user_table)
+
+    db.commit()
index 9cb39ff4821c4a8e622034d3a4341d0c2febec28..4341e086fe922782cf2c39beae662e1332b3688e 100644 (file)
@@ -69,6 +69,7 @@ class User(Base, UserMixin):
     # Intented to be nullable=False, but migrations would not work for it
     # set to nullable=True implicitly.
     wants_comment_notification = Column(Boolean, default=True)
+    wants_notifications = Column(Boolean, default=True)
     license_preference = Column(Unicode)
     is_admin = Column(Boolean, default=False, nullable=False)
     url = Column(Unicode)
index 85c243a0dcb1d5277a7447ff48db94865522f557..5de1bf96ec98d198fa2eed1c5e7b5feca2840561 100644 (file)
@@ -67,6 +67,8 @@ class EditAccountForm(wtforms.Form):
          normalize_user_or_email_field(allow_user=False)])
     wants_comment_notification = wtforms.BooleanField(
         description=_("Email me when others comment on my media"))
+    wants_notifications = wtforms.BooleanField(
+        description=_("Enable/Disable insite notifications"))
     license_preference = wtforms.SelectField(
         _('License preference'),
         [
index 6aa2acd94d5d3d5b1019b87968193f076af80e25..a11cb93287d75ff2285c965ec0ce02c8e122dd53 100644 (file)
@@ -228,10 +228,12 @@ def edit_account(request):
     user = request.user
     form = forms.EditAccountForm(request.form,
         wants_comment_notification=user.wants_comment_notification,
-        license_preference=user.license_preference)
+        license_preference=user.license_preference,
+        wants_notifications=user.wants_notifications)
 
     if request.method == 'POST' and form.validate():
         user.wants_comment_notification = form.wants_comment_notification.data
+        user.wants_notifications = form.wants_notifications.data
 
         user.license_preference = form.license_preference.data
 
index ed9f8d7896b6b4f1a36da93224ce13c31241cb15..b6f9f4789fe7c3da570d16a5c67a208e41001bd6 100644 (file)
@@ -17,7 +17,8 @@
 import logging
 
 from mediagoblin.db.models import Notification, \
-        CommentNotification, CommentSubscription
+        CommentNotification, CommentSubscription, User
+from mediagoblin.notifications.task import email_notification_task
 from mediagoblin.notifications.tools import generate_comment_message
 
 _log = logging.getLogger(__name__)
@@ -121,6 +122,12 @@ NOTIFICATION_FETCH_LIMIT = 100
 
 def get_notifications(user_id, only_unseen=True):
     query = Notification.query.filter_by(user_id=user_id)
+    wants_notifications = User.query.filter_by(id=user_id).first()\
+        .wants_notifications
+
+    # If the user does not want notifications, don't return any
+    if not wants_notifications:
+        return None
 
     if only_unseen:
         query = query.filter_by(seen=False)
@@ -130,12 +137,19 @@ def get_notifications(user_id, only_unseen=True):
 
     return notifications
 
+
 def get_notification_count(user_id, only_unseen=True):
     query = Notification.query.filter_by(user_id=user_id)
+    wants_notifications = User.query.filter_by(id=user_id).first()\
+        .wants_notifications
 
     if only_unseen:
         query = query.filter_by(seen=False)
 
-    count = query.count()
+    # If the user doesn't want notifications, don't show any
+    if not wants_notifications:
+        count = None
+    else:
+        count = query.count()
 
     return count