app/soc/logic/models/notification.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Tue, 27 Jan 2009 21:56:32 +0000
changeset 1015 b9d51be5104a
parent 997 d4ba019f1f8b
child 1106 e14b0995cf29
permissions -rw-r--r--
Add profiling support to Melange. By assigning profile_main_as_logs or profile_main_as_html to main variable you can turn on profiling. profile_main_as_logs will log profile data to App Engine console logs, profile_main_as_html will show profile data as html at the bottom of the page. If you want to profile app on deployed app just set the profiling function and deploy it. Patch by: Pawel Solyga Reviewed by: to-be-reviewed
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
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
997
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    61
    # flush the sidebar cache
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    62
    sidebar.flush(entity.scope.account)
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    63
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    64
  def _updateField(self, entity, name, value):
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
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    68
    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
    69
      # 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
    70
      sidebar.flush(entity.scope.account)
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    71
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    72
    return True
d4ba019f1f8b Added flushing the sidebar when dealing with notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 819
diff changeset
    73
819
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    74
08a79c3e8817 Fixed whitespace and EOL damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 757
diff changeset
    75
logic = Logic()