liquid_feedback_frontend

annotate model/issue.lua @ 548:8af806af86a0

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

Impressum / About Us