app/soc/logic/models/notification.py
author Sverre Rabbelier <srabbelier@gmail.com>
Tue, 03 Mar 2009 19:27:55 +0000
changeset 1600 0aa3de1b2acc
parent 1556 c81109a8a1a1
child 2708 db544114b158
permissions -rw-r--r--
Fix access bug due to forgotten normalization Prevent this from happening again by wrapping all occurences of users.get_current_account with accounts.getCurrentAccount, which will always normalize (unless explicitly told not to). Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
819
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
     1
#!/usr/bin/python2.5
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
819
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
     4
#
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
     6
# you may not use this file except in compliance with the License.
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
     7
# You may obtain a copy of the License at
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
     8
#
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    10
#
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    14
# See the License for the specific language governing permissions and
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    15
# limitations under the License.
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    16
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    17
"""Notification (Model) query functions.
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    18
"""
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    19
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    20
__authors__ = [
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    22
  ]
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    23
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    24
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    25
from google.appengine.ext import db
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    26
997
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    27
from soc.cache import sidebar
819
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    28
from soc.logic.helper import notifications
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    29
from soc.logic.models import base
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    30
from soc.logic.models import user as user_logic
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    31
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    32
import soc.models.notification
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    33
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    34
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    35
class Logic(base.Logic):
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    36
  """Logic methods for the Notification model.
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    37
  """
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    38
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    39
  def __init__(self):
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    40
    """Defines the name, key_name and model for this entity.
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    41
    """
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    42
    super(Logic, self).__init__(model=soc.models.notification.Notification,
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    43
         base_model=None, scope_logic=user_logic)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    44
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    45
  def _onCreate(self, entity):
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    46
    """Sends out a message if there is only one unread notification.
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    47
    """
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    48
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    49
    # create a special query on which we can call count
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    50
    query = db.Query(self._model)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    51
    query.filter('scope =', entity.scope)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    52
    query.filter('unread = ', True)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    53
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    54
    # count the number of results with a maximum of two
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    55
    unread_count = query.count(2)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    56
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    57
    if unread_count == 1:
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    58
      # there is only one unread notification so send out an email
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    59
      notifications.sendNewNotificationMessage(entity)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    60
1556
c81109a8a1a1 Flush the receiver's sidebar when sending a notification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    61
    sidebar.flush(entity.scope.account)
1106
e14b0995cf29 Flush sidebar when creating a new entity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 997
diff changeset
    62
    super(Logic, self)._onCreate(entity)
997
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    63
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    64
  def _updateField(self, entity, entity_properties, name):
997
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    65
    """If unread changes we flush the sidebar cache.
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    66
    """
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    67
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    68
    value = entity_properties[name]
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    69
997
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    70
    if (name == 'unread') and (entity.unread != value):
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    71
      # in case that the unread value changes we flush the sidebar.
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    72
      sidebar.flush(entity.scope.account)
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    73
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    74
    return True
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    75
819
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    76
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    77
logic = Logic()