app/soc/logic/models/notification.py
author Daniel Hans <Daniel.M.Hans@gmail.com>
Fri, 07 Aug 2009 01:27:24 +0200
changeset 2736 8f3935f0f4ba
parent 2708 db544114b158
permissions -rw-r--r--
Argument store added to updateEntityProperties. This argument determines if an entity should be stored in the data model after its properties are updated. It may be useful, for example, along with tasks (Task Queue API). One may want to make some modifications to an entity during execution of a task, but the developer is sure that at least one new task, which also wants to modify the entity, will be queued, so he or she can just update the entity without saving the changes to the data model, set the entity in memcache and the following task (which is to be executed very shortly) is to retrive the current entity from the memcache (without any expensive calls to the actual data model).
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
2708
db544114b158 Notification logic in main SoC is capable of getting constructor parameters.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1556
diff changeset
    39
  def __init__(self, model=soc.models.notification.Notification,
db544114b158 Notification logic in main SoC is capable of getting constructor parameters.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1556
diff changeset
    40
               base_model=None, scope_logic=user_logic):
819
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    41
    """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
    42
    """
2708
db544114b158 Notification logic in main SoC is capable of getting constructor parameters.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1556
diff changeset
    43
    super(Logic, self).__init__(model=model, base_model=base_model,
db544114b158 Notification logic in main SoC is capable of getting constructor parameters.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 1556
diff changeset
    44
                                scope_logic=scope_logic)
819
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    45
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    46
  def _onCreate(self, entity):
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    47
    """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
    48
    """
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    49
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    50
    # 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
    51
    query = db.Query(self._model)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    52
    query.filter('scope =', entity.scope)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    53
    query.filter('unread = ', True)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    54
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    55
    # 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
    56
    unread_count = query.count(2)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    57
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    58
    if unread_count == 1:
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    59
      # 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
    60
      notifications.sendNewNotificationMessage(entity)
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    61
1556
c81109a8a1a1 Flush the receiver's sidebar when sending a notification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1517
diff changeset
    62
    sidebar.flush(entity.scope.account)
1106
e14b0995cf29 Flush sidebar when creating a new entity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 997
diff changeset
    63
    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
    64
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    65
  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
    66
    """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
    67
    """
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    68
1517
a467d13e34ea Pass _updateField the entity_properties dict instead of just the value
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    69
    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
    70
997
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    71
    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
    72
      # 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
    73
      sidebar.flush(entity.scope.account)
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    74
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    75
    return True
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    76
819
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    77
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    78
logic = Logic()