app/soc/logic/models/ranker_root.py
author Mario Ferraro <fadinlight@gmail.com>
Sun, 15 Nov 2009 22:12:20 +0100
changeset 3093 d1be59b6b627
parent 2160 3f9dd37d98a8
permissions -rw-r--r--
GMaps related JS changed to use new google namespace. Google is going to change permanently in the future the way to load its services, so better stay safe. Also this commit shows uses of the new melange.js module. Fixes Issue 634.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1607
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""RankerRoot (Model) query functions.
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
from google.appengine.api import datastore
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    25
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    26
from ranklist.ranker import Ranker
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
from soc.logic.models import base
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
1623
8b70d6bb3f8f Renamed rankerroot to ranker_root and made some changes due to comments.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1607
diff changeset
    30
import soc.models.ranker_root
1607
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
class Logic(base.Logic):
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
  """Logic methods for the RankerRoot model.
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
  """
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
1623
8b70d6bb3f8f Renamed rankerroot to ranker_root and made some changes due to comments.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1607
diff changeset
    37
  def __init__(self, model=soc.models.ranker_root.RankerRoot, 
1607
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
               base_model=None, scope_logic=None):
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
    """Defines the name, key_name and model for this entity.
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
    """
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
    super(Logic, self).__init__(model=model, base_model=base_model,
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
                                scope_logic=scope_logic)
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
  def create(self, name, scope, scores, branching_factor):
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
    """Creates a new RankerRoot with a new Ranker.
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    48
    Args:
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    49
      name: the Link ID of the ranker root
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    50
      scope: the entity owning the ranker
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    51
      score_range: A list showing the range of valid scores, in the form:
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    52
          [most_significant_score_min, most_significant_score_max,
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    53
          less_significant_score_min, less_significant_score_max, ...]
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
          Ranges are [inclusive, exclusive)
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
      branching_factor: The branching factor of the tree.  The number of
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    56
          datastore Gets is Theta(1/log(branching_factor)), and the amount of data
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
          returned by each Get is Theta(branching_factor). 
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    58
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    59
    """
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    60
    ranker = Ranker.Create(scores, branching_factor)
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    61
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
    fields = {'link_id': name,
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
        'scope': scope,
2160
3f9dd37d98a8 Use key().id_or_name() instead of key().name()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1623
diff changeset
    64
        'scope_path': scope.key().id_or_name(),
1607
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
        'root': ranker.rootkey}
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    67
    key_name = self.getKeyNameFromFields(fields)
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
    self.updateOrCreateFromKeyName(fields, key_name)
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    70
  def getRootFromEntity(self, entity):
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    71
    """Returns a Ranker object created from a RankerRoot entity.
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    73
    Args:
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    74
      entity: A RankerRoot entity which the root should be retrieved of
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
    """
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    76
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
    return Ranker(datastore.Get(entity.key())['root'])
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    78
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    79
4ef383aa54c4 Added RankerRoot model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    80
logic = Logic()