# HG changeset patch # User bsw # Date 1632412078 -7200 # Node ID 446d2798f424c7d91d200836f76ecfb1c60f9e07 # Parent 97ff2a26c84ea2183ece0490e8d33d6c390f7d8c Automatically switch to single unit mode for users having only one unit diff -r 97ff2a26c84e -r 446d2798f424 app/main/_filter/20_session.lua --- a/app/main/_filter/20_session.lua Thu Sep 23 14:30:18 2021 +0200 +++ b/app/main/_filter/20_session.lua Thu Sep 23 17:47:58 2021 +0200 @@ -66,4 +66,13 @@ } end +app.single_unit_id = config.single_unit_id + +if app.session.member then + local member_single_unit_id = app.session.member:get_single_unit_id() or config.single_unit_id + if member_single_unit_id then + app.single_unit_id = member_single_unit_id + end +end + execute.inner() diff -r 97ff2a26c84e -r 446d2798f424 app/main/_filter/21_auth.lua --- a/app/main/_filter/21_auth.lua Thu Sep 23 14:30:18 2021 +0200 +++ b/app/main/_filter/21_auth.lua Thu Sep 23 17:47:58 2021 +0200 @@ -113,8 +113,8 @@ end if app.session:has_access("anonymous") and not app.session.member_id and auth_needed and module == "index" and view == "index" then - if config.single_unit_id then - request.redirect{ module = "unit", view = "show", id = config.single_unit_id } + if app.single_unit_id then + request.redirect{ module = "unit", view = "show", id = app.single_unit_id } else request.redirect{ module = "unit", view = "list" } end diff -r 97ff2a26c84e -r 446d2798f424 app/main/delegation/show.lua --- a/app/main/delegation/show.lua Thu Sep 23 14:30:18 2021 +0200 +++ b/app/main/delegation/show.lua Thu Sep 23 17:47:58 2021 +0200 @@ -156,12 +156,12 @@ local unit_delegation = Delegation:by_pk(app.session.member_id, issue.area.unit_id) if unit_delegation then delegate_name = unit_delegation.trustee.name - scope = config.single_unit_id and _"global" or _"unit" + scope = app.single_unit_id and _"global" or _"unit" end end local text_apply local text_abandon - if config.single_unit_id then + if app.single_unit_id then text_apply = _("Apply global or area delegation for this issue (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope }) text_abandon = _"Abandon unit and area delegations for this issue" else @@ -179,11 +179,11 @@ local unit_delegation = Delegation:by_pk(app.session.member_id, area.unit_id) if unit_delegation then delegate_name = unit_delegation.trustee.name - scope = config.single_unit_id and _"global" or _"unit" + scope = app.single_unit_id and _"global" or _"unit" end local text_apply local text_abandon - if config.single_unit_id then + if app.single_unit_id then text_apply = _("Apply global delegation for this area (Currently: #{delegate_name} [#{scope}])", { delegate_name = delegate_name, scope = scope }) text_abandon = _"Abandon global delegation for this area" else diff -r 97ff2a26c84e -r 446d2798f424 app/main/index/_head.lua --- a/app/main/index/_head.lua Thu Sep 23 14:30:18 2021 +0200 +++ b/app/main/index/_head.lua Thu Sep 23 17:47:58 2021 +0200 @@ -1,4 +1,4 @@ -local unit_id = config.single_unit_id or request.get_param{ name = "unit" } +local unit_id = app.single_unit_id or request.get_param{ name = "unit" } local area_id = config.single_area_id or request.get_param{ name = "area" } local initiative = param.get("initiative", "table") diff -r 97ff2a26c84e -r 446d2798f424 app/main/issue/_filters.lua --- a/app/main/issue/_filters.lua Thu Sep 23 14:30:18 2021 +0200 +++ b/app/main/issue/_filters.lua Thu Sep 23 17:47:58 2021 +0200 @@ -5,7 +5,7 @@ local member = param.get("member", "table") local phase = request.get_param{ name = "phase" } -local selected_unit_id = config.single_unit_id or request.get_param{ name = "unit" } +local selected_unit_id = app.single_unit_id or request.get_param{ name = "unit" } if selected_unit_id == "all" then selected_unit_id = nil end @@ -38,7 +38,7 @@ -- units - if not config.single_unit_id then + if not app.single_unit_id then local units_selector @@ -177,7 +177,7 @@ selector_modifier = function() end } - if member and not for_unit and not for_area and not config.single_unit_id then + if member and not for_unit and not for_area and not app.single_unit_id then filter[#filter+1] = { name = "my_units", label = _"in my units", diff -r 97ff2a26c84e -r 446d2798f424 app/main/issue/_head.lua --- a/app/main/issue/_head.lua Thu Sep 23 14:30:18 2021 +0200 +++ b/app/main/issue/_head.lua Thu Sep 23 17:47:58 2021 +0200 @@ -5,7 +5,7 @@ ui.title ( function () - if not config.single_unit_id then + if not app.single_unit_id then ui.link { attr = { class = "unit" }, content = function() diff -r 97ff2a26c84e -r 446d2798f424 app/main/issue/_list.lua --- a/app/main/issue/_list.lua Thu Sep 23 14:30:18 2021 +0200 +++ b/app/main/issue/_list.lua Thu Sep 23 17:47:58 2021 +0200 @@ -15,8 +15,8 @@ mode = "timeline" end -if config.single_unit_id then - for_unit = Unit:by_id(config.single_unit_id) +if app.single_unit_id then + for_unit = Unit:by_id(app.single_unit_id) end local selector @@ -201,8 +201,8 @@ if not for_issue and not for_initiative then ui.container{ attr = { class = "mdl-card__title mdl-card--has-fab mdl-card--border card-issue" }, content = function() ui.container{ attr = { class = "contextlinks" }, content = function() - if not (config.single_unit_id and config.single_area_id) then - if not config.single_unit_id then + if not (app.single_unit_id and config.single_area_id) then + if not app.single_unit_id then ui.icon("group") slot.put(" ") ui.link{ @@ -210,7 +210,7 @@ attr = { class = "unit" }, content = issue.area.unit.name } end if not config.single_area_id then - if not config.single_unit_id then + if not app.single_unit_id then slot.put("     ") end ui.icon("category") diff -r 97ff2a26c84e -r 446d2798f424 model/member.lua --- a/model/member.lua Thu Sep 23 14:30:18 2021 +0200 +++ b/model/member.lua Thu Sep 23 17:47:58 2021 +0200 @@ -837,6 +837,19 @@ return self.__units_with_polling_right_hash[unit_id] and true or false end +function Member.object:get_single_unit_id() + populate_units_with_voting_right_hash(self) + local single_unit_id + local count = 0 + for unit_id in pairs(self.__units_with_voting_right_hash) do + single_unit_id = unit_id + count = count + 1 + end + if count == 1 then + return single_unit_id + end +end + function Member.object:get_delegatee_member(unit_id, area_id, issue_id) local selector = Member:new_selector() if unit_id then