|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 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 """Sponsor (Model) query functions. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
|
22 ] |
|
23 |
|
24 |
|
25 from soc.logic import key_name |
|
26 from soc.logic import out_of_band |
|
27 |
|
28 import soc.models.sponsor |
|
29 import soc.logic.model |
|
30 |
|
31 |
|
32 def doesLinkNameExist(link_name=None): |
|
33 """Returns True if link name exists in the Datastore. |
|
34 |
|
35 Args: |
|
36 link_name: link name used in URLs to identify Sponsor |
|
37 """ |
|
38 if getSponsorFromLinkName(link_name): |
|
39 return True |
|
40 else: |
|
41 return False |
|
42 |
|
43 |
|
44 def getSponsorFromLinkName(link_name): |
|
45 """Returns Sponsor entity for a given link name, or None if not found. |
|
46 |
|
47 Args: |
|
48 link_name: a link name of the Sponsor that uniquely identifies it |
|
49 """ |
|
50 # lookup by Sponsor key name |
|
51 sponsor_key_name = getSponsorKeyNameForLinkName(link_name) |
|
52 |
|
53 if key_name: |
|
54 sponsor = soc.models.sponsor.Sponsor.get_by_key_name(sponsor_key_name) |
|
55 else: |
|
56 sponsor = None |
|
57 |
|
58 return sponsor |
|
59 |
|
60 |
|
61 def getSponsorIfLinkName(link_name=None): |
|
62 """Returns Sponsor entity for supplied link name if one exists. |
|
63 |
|
64 Args: |
|
65 link_name: a link name of the Sponsor that uniquely identifies it |
|
66 |
|
67 Returns: |
|
68 * None if link name is false. |
|
69 * Sponsor entity for supplied linkname |
|
70 |
|
71 Raises: |
|
72 out_of_band.ErrorResponse if link name is not false, but no Sponsor entity |
|
73 with the supplied link name exists in the Datastore |
|
74 """ |
|
75 if not link_name: |
|
76 # exit without error, to let view know that link_name was not supplied |
|
77 return None |
|
78 |
|
79 linkname_sponsor = getSponsorFromLinkName(link_name=link_name) |
|
80 |
|
81 if linkname_sponsor: |
|
82 # a Sponsor exist for this linkname, so return that Sponsor entity |
|
83 return linkname_sponsor |
|
84 |
|
85 # else: a linkname was supplied, but there is no Sponsor that has it |
|
86 raise out_of_band.ErrorResponse( |
|
87 'There is no sponsor with a "link name" of "%s".' % link_name, status=404) |
|
88 |
|
89 |
|
90 def getSponsorKeyNameForLinkName(link_name): |
|
91 """Return a Datastore key_name for a Sponsor from the link name. |
|
92 |
|
93 Args: |
|
94 link_name: a link name of the Sponsor that uniquely identifies it |
|
95 """ |
|
96 if not link_name: |
|
97 return None |
|
98 |
|
99 return key_name.nameSponsor(link_name) |
|
100 |
|
101 |
|
102 def getSponsorsForOffsetAndLimit(offset=0, limit=0): |
|
103 """Returns Sponsors entities for given offset and limit or None if not found. |
|
104 |
|
105 Args: |
|
106 offset: offset in entities list which defines first entity to return |
|
107 limit: max amount of entities to return |
|
108 """ |
|
109 query = soc.models.sponsor.Sponsor.all() |
|
110 |
|
111 # Fetch one more to see if there should be a 'next' link |
|
112 return query.fetch(limit+1, offset) |
|
113 |
|
114 |
|
115 def updateOrCreateSponsorFromLinkName(sponsor_link_name, **sponsor_properties): |
|
116 """Update existing Sponsor entity, or create new one with supplied properties. |
|
117 |
|
118 Args: |
|
119 sponsor_name: a linkname of the Sponsor that uniquely identifies it |
|
120 **sponsor_properties: keyword arguments that correspond to Sponsor entity |
|
121 properties and their values |
|
122 |
|
123 Returns: |
|
124 the Sponsor entity corresponding to the path, with any supplied |
|
125 properties changed, or a new Sponsor entity now associated with the |
|
126 supplied path and properties. |
|
127 """ |
|
128 # attempt to retrieve the existing Sponsor |
|
129 sponsor = getSponsorFromLinkName(sponsor_link_name) |
|
130 |
|
131 if not sponsor: |
|
132 # sponsor did not exist, so create one in a transaction |
|
133 sponsor_key_name = getSponsorKeyNameForLinkName(sponsor_link_name) |
|
134 sponsor = soc.models.sponsor.Sponsor.get_or_insert( |
|
135 sponsor_key_name, **sponsor_properties) |
|
136 |
|
137 # there is no way to be sure if get_or_insert() returned a new Sponsor or |
|
138 # got an existing one due to a race, so update with sponsor_properties anyway, |
|
139 # in a transaction |
|
140 return soc.logic.model.updateModelProperties(sponsor, **sponsor_properties) |