liquid_feedback_frontend
changeset 1500:71f54c43d7cb
Added API support interface
author | bsw |
---|---|
date | Tue Mar 24 16:59:32 2020 +0100 (2020-03-24) |
parents | f8a4260841df |
children | caaccdb20d0c 64229f002a47 |
files | app/main/api/support.lua app/main/initiative/_action/add_support.lua env/request/router.lua env/util/add_support.lua |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/app/main/api/support.lua Tue Mar 24 16:59:32 2020 +0100 1.3 @@ -0,0 +1,17 @@ 1.4 +local initiative_id = param.get("initiative_id") 1.5 + 1.6 +local success = util.add_support(initiative_id) 1.7 + 1.8 +slot.set_layout(nil, "application/json") 1.9 + 1.10 +local r = json.array() 1.11 + 1.12 +if success then 1.13 + r.status = "ok" 1.14 +else 1.15 + r.status = "error" 1.16 +end 1.17 + 1.18 +slot.put_into("data", json.export(json.object{ result = r })) 1.19 +slot.put_into("data", "\n") 1.20 +
2.1 --- a/app/main/initiative/_action/add_support.lua Tue Mar 24 16:50:16 2020 +0100 2.2 +++ b/app/main/initiative/_action/add_support.lua Tue Mar 24 16:59:32 2020 +0100 2.3 @@ -1,62 +1,4 @@ 2.4 -local initiative = Initiative:new_selector():add_where{ "id = ?", param.get_id()}:single_object_mode():exec() 2.5 - 2.6 +local initiative_id = param.get_id() 2.7 local draft_id = param.get("draft_id", atom.integer) 2.8 2.9 --- TODO important m1 selectors returning result _SET_! 2.10 -local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec() 2.11 - 2.12 -if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then 2.13 - return execute.view { module = "index", view = "403" } 2.14 -end 2.15 - 2.16 -if issue.closed then 2.17 - slot.put_into("error", _"This issue is already closed.") 2.18 - return false 2.19 -elseif issue.fully_frozen then 2.20 - slot.put_into("error", _"Voting for this issue has already begun.") 2.21 - return false 2.22 -elseif 2.23 - (issue.half_frozen and issue.phase_finished) or 2.24 - (not issue.accepted and issue.phase_finished) 2.25 -then 2.26 - slot.put_into("error", _"Current phase is already closed.") 2.27 - return false 2.28 -end 2.29 - 2.30 -if initiative.revoked then 2.31 - slot.put_into("error", _"This initiative is revoked") 2.32 - return false 2.33 -end 2.34 - 2.35 -local member = app.session.member 2.36 - 2.37 -local supporter = Supporter:by_pk(initiative.id, member.id) 2.38 - 2.39 -local last_draft = Draft:new_selector() 2.40 - :add_where{ "initiative_id = ?", initiative.id } 2.41 - :add_order_by("id DESC") 2.42 - :limit(1) 2.43 - :single_object_mode() 2.44 - :exec() 2.45 - 2.46 -if draft_id and draft_id ~= last_draft.id then 2.47 - slot.select("error", function() 2.48 - ui.tag{ content = _"The initiative draft has been updated again in the meanwhile, support not updated!" } 2.49 - end) 2.50 - return false 2.51 -end 2.52 - 2.53 -if not supporter then 2.54 - supporter = Supporter:new() 2.55 - supporter.member_id = member.id 2.56 - supporter.initiative_id = initiative.id 2.57 - supporter.draft_id = last_draft.id 2.58 - supporter:save() 2.59 -elseif supporter.draft_id ~= last_draft.id then 2.60 - supporter.draft_id = last_draft.id 2.61 - supporter:save() 2.62 - slot.put_into("notice", _"Your support has been updated to the latest draft") 2.63 -else 2.64 - slot.put_into("notice", _"You are already supporting the latest draft") 2.65 -end 2.66 - 2.67 +return util.add_support(initiative_id, draft_id)
3.1 --- a/env/request/router.lua Tue Mar 24 16:50:16 2020 +0100 3.2 +++ b/env/request/router.lua Tue Mar 24 16:59:32 2020 +0100 3.3 @@ -11,6 +11,7 @@ 3.4 settings_info = true, 3.5 settings = true, 3.6 event = true, 3.7 + support = true, 3.8 embed_initiative = true 3.9 } 3.10
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/env/util/add_support.lua Tue Mar 24 16:59:32 2020 +0100 4.3 @@ -0,0 +1,67 @@ 4.4 +function util.add_support(initiative_id, draft_id) 4.5 + 4.6 + local initiative = Initiative:new_selector():add_where{ "id = ?", initiative_id }:single_object_mode():exec() 4.7 + 4.8 + -- TODO important m1 selectors returning result _SET_! 4.9 + local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec() 4.10 + 4.11 + if not app.session.member:has_voting_right_for_unit_id(issue.area.unit_id) then 4.12 + slot.put_into("error", _"No voting rights.") 4.13 + return false 4.14 + end 4.15 + 4.16 + if issue.closed then 4.17 + slot.put_into("error", _"This issue is already closed.") 4.18 + return false 4.19 + elseif issue.fully_frozen then 4.20 + slot.put_into("error", _"Voting for this issue has already begun.") 4.21 + return false 4.22 + elseif 4.23 + (issue.half_frozen and issue.phase_finished) or 4.24 + (not issue.accepted and issue.phase_finished) 4.25 + then 4.26 + slot.put_into("error", _"Current phase is already closed.") 4.27 + return false 4.28 + end 4.29 + 4.30 + if initiative.revoked then 4.31 + slot.put_into("error", _"This initiative is revoked") 4.32 + return false 4.33 + end 4.34 + 4.35 + local member = app.session.member 4.36 + 4.37 + local supporter = Supporter:by_pk(initiative.id, member.id) 4.38 + 4.39 + local last_draft = Draft:new_selector() 4.40 + :add_where{ "initiative_id = ?", initiative.id } 4.41 + :add_order_by("id DESC") 4.42 + :limit(1) 4.43 + :single_object_mode() 4.44 + :exec() 4.45 + 4.46 + if draft_id and draft_id ~= last_draft.id then 4.47 + slot.select("error", function() 4.48 + ui.tag{ content = _"The initiative draft has been updated again in the meanwhile, support not updated!" } 4.49 + end) 4.50 + return false 4.51 + end 4.52 + 4.53 + if not supporter then 4.54 + supporter = Supporter:new() 4.55 + supporter.member_id = member.id 4.56 + supporter.initiative_id = initiative.id 4.57 + supporter.draft_id = last_draft.id 4.58 + supporter:save() 4.59 + elseif supporter.draft_id ~= last_draft.id then 4.60 + supporter.draft_id = last_draft.id 4.61 + supporter:save() 4.62 + slot.put_into("notice", _"Your support has been updated to the latest draft") 4.63 + else 4.64 + slot.put_into("notice", _"You are already supporting the latest draft") 4.65 + end 4.66 + 4.67 + return true 4.68 + 4.69 +end 4.70 +