liquid_feedback_frontend
view model/issue.lua @ 549:42d070777353
Optimized fetching of calculated state_time_left attribute of issue
author | bsw |
---|---|
date | Tue May 29 20:52:20 2012 +0200 (2012-05-29) |
parents | 8af806af86a0 |
children | c1dc3b14a4f3 |
line source
1 Issue = mondelefant.new_class()
2 Issue.table = 'issue'
4 local new_selector = Issue.new_selector
6 function Issue:new_selector()
7 local selector = new_selector(self)
8 selector:add_field("coalesce(issue.fully_frozen + issue.voting_time, issue.half_frozen + issue.verification_time, issue.accepted + issue.discussion_time, issue.created + issue.admission_time) - now()", "state_time_left")
9 return selector
10 end
12 Issue:add_reference{
13 mode = 'm1',
14 to = "Area",
15 this_key = 'area_id',
16 that_key = 'id',
17 ref = 'area',
18 }
20 Issue:add_reference{
21 mode = 'm1',
22 to = "Policy",
23 this_key = 'policy_id',
24 that_key = 'id',
25 ref = 'policy',
26 }
28 Issue:add_reference{
29 mode = '1m',
30 to = "Initiative",
31 this_key = 'id',
32 that_key = 'issue_id',
33 ref = 'initiatives',
34 back_ref = 'issue'
35 }
37 Issue:add_reference{
38 mode = '1m',
39 to = "Interest",
40 this_key = 'id',
41 that_key = 'issue_id',
42 ref = 'interests',
43 back_ref = 'issue',
44 default_order = '"id"'
45 }
47 Issue:add_reference{
48 mode = '1m',
49 to = "Supporter",
50 this_key = 'id',
51 that_key = 'issue_id',
52 ref = 'supporters',
53 back_ref = 'issue',
54 default_order = '"id"'
55 }
57 Issue:add_reference{
58 mode = '1m',
59 to = "DirectVoter",
60 this_key = 'id',
61 that_key = 'issue_id',
62 ref = 'direct_voters',
63 back_ref = 'issue',
64 default_order = '"member_id"'
65 }
67 Issue:add_reference{
68 mode = '1m',
69 to = "Vote",
70 this_key = 'id',
71 that_key = 'issue_id',
72 ref = 'votes',
73 back_ref = 'issue',
74 default_order = '"member_id", "initiative_id"'
75 }
77 Issue:add_reference{
78 mode = '1m',
79 to = "Delegation",
80 this_key = 'id',
81 that_key = 'issue_id',
82 ref = 'delegations',
83 back_ref = 'issue'
84 }
86 Issue:add_reference{
87 mode = 'mm',
88 to = "Member",
89 this_key = 'id',
90 that_key = 'id',
91 connected_by_table = 'interest',
92 connected_by_this_key = 'issue_id',
93 connected_by_that_key = 'member_id',
94 ref = 'members'
95 }
97 Issue:add_reference{
98 mode = 'mm',
99 to = "Member",
100 this_key = 'id',
101 that_key = 'id',
102 connected_by_table = 'direct_interest_snapshot',
103 connected_by_this_key = 'issue_id',
104 connected_by_that_key = 'member_id',
105 ref = 'interested_members_snapshot'
106 }
108 Issue:add_reference{
109 mode = 'mm',
110 to = "Member",
111 this_key = 'id',
112 that_key = 'id',
113 connected_by_table = 'direct_voter',
114 connected_by_this_key = 'issue_id',
115 connected_by_that_key = 'member_id',
116 ref = 'direct_voters'
117 }
119 Issue:add_reference{
120 mode = "11",
121 to = mondelefant.class_prototype,
122 this_key = "id",
123 that_key = "issue_id",
124 ref = "member_info",
125 back_ref = "issue",
126 selector_generator = function(list, options)
127 assert(options.member_id, "member_id mandatory for member_info")
128 local ids = { sep = ", " }
129 for i, object in ipairs(list) do
130 local id = object.id
131 if id ~= nil then
132 ids[#ids+1] = {"?", id}
133 end
134 end
135 local sub_selector = Issue:get_db_conn():new_selector()
136 if #ids == 0 then
137 return sub_selector:empty_list_mode()
138 end
139 sub_selector:from("issue")
140 sub_selector:add_field("issue.id", "issue_id")
141 sub_selector:add_field{ '(delegation_info(?, null, null, issue.id, ?)).*', options.member_id, options.trustee_id }
142 sub_selector:add_where{ 'issue.id IN ($)', ids }
144 local selector = Issue:get_db_conn():new_selector()
145 selector:add_from("issue")
146 selector:join(sub_selector, "delegation_info", "delegation_info.issue_id = issue.id")
147 selector:left_join("member", "first_trustee", "first_trustee.id = delegation_info.first_trustee_id")
148 selector:left_join("member", "other_trustee", "other_trustee.id = delegation_info.other_trustee_id")
149 selector:add_field("delegation_info.*")
150 selector:add_field("first_trustee.name", "first_trustee_name")
151 selector:add_field("other_trustee.name", "other_trustee_name")
152 selector:left_join("direct_voter", nil, { "direct_voter.issue_id = issue.id AND direct_voter.member_id = ?", options.member_id })
153 selector:add_field("direct_voter.member_id NOTNULL", "direct_voted")
154 return selector
155 end
156 }
158 function Issue.list:load_everything_for_member_id(member_id)
159 local areas = self:load("area")
160 areas:load("unit")
161 self:load("policy")
162 self:load("member_info", { member_id = member_id })
163 end
165 function Issue.object:load_everything_for_member_id(member_id)
166 local areas = self:load("area")
167 areas:load("unit")
168 self:load("policy")
169 self:load("member_info", { member_id = member_id })
170 end
172 function Issue:get_state_name_for_state(value)
173 local state_name_table = {
174 new = _"New",
175 accepted = _"Discussion",
176 frozen = _"Frozen",
177 voting = _"Voting",
178 finished = _"Finished",
179 cancelled = _"Cancelled"
180 }
181 return state_name_table[value] or value or ''
182 end
184 function Issue:get_search_selector(search_string)
185 return self:new_selector()
186 :join('"initiative"', nil, '"initiative"."issue_id" = "issue"."id"')
187 :join('"draft"', nil, '"draft"."initiative_id" = "initiative"."id"')
188 :add_where{ '"initiative"."text_search_data" @@ "text_search_query"(?) OR "draft"."text_search_data" @@ "text_search_query"(?)', search_string, search_string }
189 :add_group_by('"issue"."id"')
190 :add_group_by('"issue"."state"')
191 :add_group_by('"issue"."area_id"')
192 :add_group_by('"issue"."policy_id"')
193 :add_group_by('"issue"."created"')
194 :add_group_by('"issue"."accepted"')
195 :add_group_by('"issue"."half_frozen"')
196 :add_group_by('"issue"."fully_frozen"')
197 :add_group_by('"issue"."closed"')
198 :add_group_by('"issue"."ranks_available"')
199 :add_group_by('"issue"."status_quo_schulze_rank"')
200 :add_group_by('"issue"."cleaned"')
201 :add_group_by('"issue"."snapshot"')
202 :add_group_by('"issue"."latest_snapshot_event"')
203 :add_group_by('"issue"."population"')
204 :add_group_by('"issue"."voter_count"')
205 :add_group_by('"issue"."admission_time"')
206 :add_group_by('"issue"."discussion_time"')
207 :add_group_by('"issue"."verification_time"')
208 :add_group_by('"issue"."voting_time"')
209 :add_group_by('"_interest"."member_id"')
210 :add_group_by("_delegating_interest.delegate_member_ids")
211 --:set_distinct()
212 end
214 function Issue:modify_selector_for_state(initiatives_selector, state)
215 if state == "new" then
216 initiatives_selector:add_where("issue.accepted ISNULL AND issue.closed ISNULL")
217 elseif state == "accepted" then
218 initiatives_selector:add_where("issue.accepted NOTNULL AND issue.half_frozen ISNULL AND issue.closed ISNULL")
219 elseif state == "frozen" then
220 initiatives_selector:add_where("issue.half_frozen NOTNULL AND issue.fully_frozen ISNULL AND issue.closed ISNULL")
221 elseif state == "voting" then
222 initiatives_selector:add_where("issue.fully_frozen NOTNULL AND issue.closed ISNULL")
223 elseif state == "finished" then
224 initiatives_selector:add_where("issue.fully_frozen NOTNULL AND issue.closed NOTNULL")
225 elseif state == "cancelled" then
226 initiatives_selector:add_where("issue.fully_frozen ISNULL AND issue.closed NOTNULL")
227 else
228 error("Invalid state")
229 end
230 end
232 function Issue.object_get:state()
233 if self.closed then
234 if self.fully_frozen then
235 return "finished"
236 else
237 return "cancelled"
238 end
239 elseif self.fully_frozen then
240 return "voting"
241 elseif self.half_frozen then
242 return "frozen"
243 elseif self.accepted then
244 return "accepted"
245 else
246 return "new"
247 end
249 end
251 function Issue.object_get:state_name()
252 return Issue:get_state_name_for_state(self.state)
253 end
255 function Issue.object_get:next_states()
256 local state = self.state
257 local next_states
258 if state == "new" then
259 next_states = { "accepted", "cancelled" }
260 elseif state == "accepted" then
261 next_states = { "frozen" }
262 elseif state == "frozen" then
263 next_states = { "voting" }
264 elseif state == "voting" then
265 next_states = { "finished" }
266 end
267 return next_states
268 end
270 function Issue.object_get:next_states_names()
271 local next_states = self.next_states
272 if not next_states then
273 return
274 end
275 local state_names = {}
276 for i, state in ipairs(self.next_states) do
277 state_names[#state_names+1] = Issue:get_state_name_for_state(state)
278 end
279 return table.concat(state_names, ", ")
280 end
282 function Issue.object_get:etherpad_url()
283 return config.etherpad.base_url .. "p/" .. config.etherpad.group_id .. "$Issue" .. self.id
284 end