app/soc/logic/models/notification.py
author Lennard de Rijk <ljvderijk@gmail.com>
Tue, 20 Jan 2009 14:09:39 +0000
changeset 829 595b34a71cbb
parent 819 08a79c3e8817
child 997 d4ba019f1f8b
permissions -rw-r--r--
Users now have the right to delete their own notifications. Patch by: Lennard de Rijk
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
#
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
     3
# Copyright 2008 the Melange authors.
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
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    27
from soc.logic.helper import notifications
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    28
from soc.logic.models import base
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    29
from soc.logic.models import user as user_logic
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    30
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    31
import soc.models.notification
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    32
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
class Logic(base.Logic):
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    35
  """Logic methods for the Notification model.
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    36
  """
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
  def __init__(self):
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    39
    """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
    40
    """
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    41
    super(Logic, self).__init__(model=soc.models.notification.Notification,
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    42
         base_model=None, scope_logic=user_logic)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    43
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    44
  def _onCreate(self, entity):
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    45
    """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
    46
    """
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
    # 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
    49
    query = db.Query(self._model)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    50
    query.filter('scope =', entity.scope)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    51
    query.filter('unread = ', True)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    52
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    53
    # 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
    54
    unread_count = query.count(2)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    55
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    56
    if unread_count == 1:
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    57
      # 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
    58
      notifications.sendNewNotificationMessage(entity)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    59
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    60
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    61
logic = Logic()