|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2009 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """RankerRoot (Model) query functions. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
|
22 ] |
|
23 |
|
24 from google.appengine.api import datastore |
|
25 |
|
26 from ranklist.ranker import Ranker |
|
27 |
|
28 from soc.logic.models import base |
|
29 |
|
30 import soc.models.ranker_root |
|
31 |
|
32 |
|
33 class Logic(base.Logic): |
|
34 """Logic methods for the RankerRoot model. |
|
35 """ |
|
36 |
|
37 def __init__(self, model=soc.models.ranker_root.RankerRoot, |
|
38 base_model=None, scope_logic=None): |
|
39 """Defines the name, key_name and model for this entity. |
|
40 """ |
|
41 |
|
42 super(Logic, self).__init__(model=model, base_model=base_model, |
|
43 scope_logic=scope_logic) |
|
44 |
|
45 def create(self, name, scope, scores, branching_factor): |
|
46 """Creates a new RankerRoot with a new Ranker. |
|
47 |
|
48 Args: |
|
49 name: the Link ID of the ranker root |
|
50 scope: the entity owning the ranker |
|
51 score_range: A list showing the range of valid scores, in the form: |
|
52 [most_significant_score_min, most_significant_score_max, |
|
53 less_significant_score_min, less_significant_score_max, ...] |
|
54 Ranges are [inclusive, exclusive) |
|
55 branching_factor: The branching factor of the tree. The number of |
|
56 datastore Gets is Theta(1/log(branching_factor)), and the amount of data |
|
57 returned by each Get is Theta(branching_factor). |
|
58 |
|
59 """ |
|
60 ranker = Ranker.Create(scores, branching_factor) |
|
61 |
|
62 fields = {'link_id': name, |
|
63 'scope': scope, |
|
64 'scope_path': scope.key().name(), |
|
65 'root': ranker.rootkey} |
|
66 |
|
67 key_name = self.getKeyNameFromFields(fields) |
|
68 self.updateOrCreateFromKeyName(fields, key_name) |
|
69 |
|
70 def getRootFromEntity(self, entity): |
|
71 """Returns a Ranker object created from a RankerRoot entity. |
|
72 |
|
73 Args: |
|
74 entity: A RankerRoot entity which the root should be retrieved of |
|
75 """ |
|
76 |
|
77 return Ranker(datastore.Get(entity.key())['root']) |
|
78 |
|
79 |
|
80 logic = Logic() |