Merge branch 'modules' into 'master'
Modules See merge request medlabboulder/modpol!19
This commit is contained in:
		| @@ -71,9 +71,10 @@ dofile (topdir .. "/api.lua") | |||||||
|  |  | ||||||
| -- =================================================================== | -- =================================================================== | ||||||
| -- Final checks | -- Final checks | ||||||
|  |  | ||||||
| for id, org in ipairs(modpol.orgs.array) do | for id, org in ipairs(modpol.orgs.array) do | ||||||
|     setmetatable(org, modpol.orgs) |     if type(org) == 'table' then  | ||||||
|  |         setmetatable(org, modpol.orgs) | ||||||
|  |     end | ||||||
| end | end | ||||||
|  |  | ||||||
| -- create instance if not present | -- create instance if not present | ||||||
|   | |||||||
| @@ -7,6 +7,27 @@ modpol.orgs = modpol.orgs or | |||||||
| -- sets modpol.orgs as its own fallback | -- sets modpol.orgs as its own fallback | ||||||
| modpol.orgs.__index = modpol.orgs | modpol.orgs.__index = modpol.orgs | ||||||
|  |  | ||||||
|  | function temp_org() | ||||||
|  |     return { | ||||||
|  |         id = nil, | ||||||
|  |         name = nil, | ||||||
|  |         policies = {}, | ||||||
|  |         processes = {}, | ||||||
|  |         requests = {}, | ||||||
|  |         request_count = 0, | ||||||
|  |         members = {}, | ||||||
|  |         parent = nil, | ||||||
|  |         children = {} | ||||||
|  |     } | ||||||
|  | end | ||||||
|  |  | ||||||
|  | modpol.orgs.request_params = { | ||||||
|  |     add_org = 1, | ||||||
|  |     delete = 0, | ||||||
|  |     add_member = 1, | ||||||
|  |     remove_member = 1 | ||||||
|  | } | ||||||
|  |  | ||||||
| -- ================================================== | -- ================================================== | ||||||
| -- returns org when given its id or name | -- returns org when given its id or name | ||||||
| function modpol.orgs.get_org(arg) | function modpol.orgs.get_org(arg) | ||||||
| @@ -71,22 +92,14 @@ end | |||||||
| function modpol.orgs.init_instance() | function modpol.orgs.init_instance() | ||||||
|     local error_msg |     local error_msg | ||||||
|     if modpol.orgs.array[1] then |     if modpol.orgs.array[1] then | ||||||
|         error_msg = 'Error: instance has already been initialized' |         modpol.ocutil.log('Error: instance has already been initialized') | ||||||
|         modpol.ocutil.log(error_msg) |         return false | ||||||
|         return false, error_msg |  | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     local instance = { |     local instance = temp_org() | ||||||
|         id = 1, |     instance.id = 1 | ||||||
|         name = "instance", |     instance.name = "instance" | ||||||
|         policies = {}, |      | ||||||
|         members = {}, |  | ||||||
|         ledger = {}, |  | ||||||
|         parent = nil, |  | ||||||
|         children = {}, |  | ||||||
|         properties = {}, |  | ||||||
|         old_names = {} |  | ||||||
|     } |  | ||||||
|     setmetatable(instance, modpol.orgs) |     setmetatable(instance, modpol.orgs) | ||||||
|  |  | ||||||
|     -- adding instance to org list |     -- adding instance to org list | ||||||
| @@ -114,14 +127,15 @@ function modpol.orgs:record(msg, entry_type) | |||||||
|     if type(msg) == 'string' and not(modpol.ocutil.str_empty(msg)) then |     if type(msg) == 'string' and not(modpol.ocutil.str_empty(msg)) then | ||||||
|         entry.action_msg = msg |         entry.action_msg = msg | ||||||
|     else |     else | ||||||
|         print('Error: msg must be a non empty string') |         modpol.ocutil.log('Error: msg must be a non empty string') | ||||||
|         return false |         return false | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     if type(entry_type) == 'string' and not(modpol.ocutil.str_empty(entry_type)) then |     if type(entry_type) == 'string' and not(modpol.ocutil.str_empty(entry_type)) then | ||||||
|         entry.entry_type = entry_type |         entry.entry_type = entry_type | ||||||
|     else |     else | ||||||
|         print('Error: entry_type must be a non empty string') |         modpol.ocutil.log('Error: entry_type must be a non empty string') | ||||||
|  |         print(msg, entry_type) | ||||||
|         return false |         return false | ||||||
|     end |     end | ||||||
|  |  | ||||||
| @@ -138,33 +152,27 @@ end | |||||||
| -- ex: instance:add_org('town hall') | -- ex: instance:add_org('town hall') | ||||||
| function modpol.orgs:add_org(name) | function modpol.orgs:add_org(name) | ||||||
|     if self.id == nil then |     if self.id == nil then | ||||||
|         error_msg = 'Error: add_org can only be called by another org' |         modpol.ocutil.log('Error: add_org can only be called by another org') | ||||||
|         modpol.ocutil.log(error_msg) |         return false | ||||||
|         return false, error_msg |  | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     if modpol.ocutil.str_empty(name) then |     if modpol.ocutil.str_empty(name) then | ||||||
|         error_msg = 'Error: org name is required' |         modpol.ocutil.log('Error: org name is required') | ||||||
|         modpol.ocutil.log(error_msg) |         return false | ||||||
|         return false, error_msg |  | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     if modpol.orgs.get_org(name) then |     if modpol.orgs.get_org(name) then | ||||||
|         error_msg = 'Error: org name is already being used' |         modpol.ocutil.log('Error: org name is already being used') | ||||||
|         modpol.ocutil.log(error_msg) |         return false | ||||||
|         return false, error_msg |  | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     -- creating the child sub org |     -- creating the child sub org | ||||||
|     modpol.orgs.count = modpol.orgs.count + 1 |     modpol.orgs.count = modpol.orgs.count + 1 | ||||||
|     local child_org = { |     local child_org = temp_org() | ||||||
|         id = modpol.orgs.count, |     child_org.id = modpol.orgs.count | ||||||
|         name = name, |     child_org.name = name | ||||||
|         policies = {}, |     child_org.parent = self.id | ||||||
|         members = {}, |      | ||||||
|         parent = self.id, |  | ||||||
|         children = {}, |  | ||||||
|     } |  | ||||||
|     setmetatable(child_org, modpol.orgs) |     setmetatable(child_org, modpol.orgs) | ||||||
|  |  | ||||||
|     -- adding child id to list of children |     -- adding child id to list of children | ||||||
| @@ -174,6 +182,7 @@ function modpol.orgs:add_org(name) | |||||||
|     modpol.orgs.array[child_org.id] = child_org |     modpol.orgs.array[child_org.id] = child_org | ||||||
|  |  | ||||||
|     self:record('created sub org ' .. name, 'add_org') |     self:record('created sub org ' .. name, 'add_org') | ||||||
|  |     modpol.ocutil.log('Created sub org ' .. name) | ||||||
|  |  | ||||||
|     return child_org |     return child_org | ||||||
| end | end | ||||||
| @@ -184,7 +193,8 @@ end | |||||||
| -- note: "reason" param was removed, can be added back | -- note: "reason" param was removed, can be added back | ||||||
| function modpol.orgs:delete() | function modpol.orgs:delete() | ||||||
|     if self.id == 1 then |     if self.id == 1 then | ||||||
|         return false, 'Error: cannot delete instance' |         modpol.ocutil.log('Error: cannot delete instance') | ||||||
|  |         return false | ||||||
|     end |     end | ||||||
|      |      | ||||||
|     if #self.children > 0 then |     if #self.children > 0 then | ||||||
| @@ -196,7 +206,7 @@ function modpol.orgs:delete() | |||||||
|     end |     end | ||||||
|  |  | ||||||
|     modpol.orgs.array[self.id] = 'removed' |     modpol.orgs.array[self.id] = 'removed' | ||||||
|     print('Removed ' .. self.name .. ': ' .. self.id) |     modpol.ocutil.log('Removed ' .. self.name .. ': ' .. self.id) | ||||||
|  |  | ||||||
|     self:record('Deleted ' .. self.name .. ' and all child orgs', 'del_org') |     self:record('Deleted ' .. self.name .. ' and all child orgs', 'del_org') | ||||||
|  |  | ||||||
| @@ -224,7 +234,8 @@ function modpol.orgs:add_member(user) | |||||||
|         -- adds to end if no empty spots |         -- adds to end if no empty spots | ||||||
|         table.insert(self.members, user) |         table.insert(self.members, user) | ||||||
|     end |     end | ||||||
|     self.record('Added member ' .. user, 'add_member') |     self:record('Added member ' .. user, 'add_member') | ||||||
|  |  | ||||||
| end | end | ||||||
|  |  | ||||||
| -- ======================================= | -- ======================================= | ||||||
| @@ -236,7 +247,7 @@ function modpol.orgs:remove_member(user) | |||||||
|     if user_index then |     if user_index then | ||||||
|         self.members[user_index] = '' |         self.members[user_index] = '' | ||||||
|     end |     end | ||||||
|     self.record('Removed member ' .. user, 'del_member') |     self:record('Removed member ' .. user, 'del_member') | ||||||
| end | end | ||||||
|  |  | ||||||
| -- =========================================== | -- =========================================== | ||||||
| @@ -263,3 +274,57 @@ function modpol.orgs:list_member() | |||||||
|     end |     end | ||||||
|     return str |     return str | ||||||
| end | end | ||||||
|  |  | ||||||
|  | -- =========================== | ||||||
|  | -- compares to requests to see if they are identical | ||||||
|  | function modpol.orgs.comp_req(req1, req2) | ||||||
|  |     -- compares request type | ||||||
|  |     if req1.type ~= req2.type then | ||||||
|  |         return false | ||||||
|  |     else | ||||||
|  |         -- comparing parameters | ||||||
|  |         -- we can assume the number of params is the same as this is checked in the make_request func | ||||||
|  |         for k, v in ipairs(req1.params) do | ||||||
|  |             if v ~= req2.params[k] then | ||||||
|  |                 return false | ||||||
|  |             end | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |     return true | ||||||
|  | end | ||||||
|  |  | ||||||
|  | -- ================================ | ||||||
|  | -- tries to make a request to the org | ||||||
|  | function modpol.orgs:make_request(request) | ||||||
|  |     -- makes sure the request has the valid number of parameters | ||||||
|  |     local num_params = modpol.orgs.request_params[request.type] | ||||||
|  |  | ||||||
|  |     if num_params == nil then  | ||||||
|  |         modpol.ocutil.log("Error: request type is invalid") | ||||||
|  |         return false  | ||||||
|  |     end | ||||||
|  |     | ||||||
|  |     for k, v in ipairs(request.params) do | ||||||
|  |         num_params = num_params - 1 | ||||||
|  |     end  | ||||||
|  |  | ||||||
|  |     if num_params ~= 0 then | ||||||
|  |         modpol.ocutil.log("Error: request has invalid number of parameters")  | ||||||
|  |         return false  | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     -- checking to see if identical request already exists | ||||||
|  |     for k, v in ipairs(self.requests) do | ||||||
|  |         if self.comp_req(request, v) == true then | ||||||
|  |             modpol.ocutil.log("Error: request has already been made") | ||||||
|  |             return false | ||||||
|  |         end | ||||||
|  |     end | ||||||
|  |  | ||||||
|  |     -- use lazy deletion here, not very clean | ||||||
|  |     -- table.insert(self.requests, request) | ||||||
|  |     self.request_count = self.request_count + 1 | ||||||
|  |     self.requests[self.request_count] = request | ||||||
|  |     return self.request_count | ||||||
|  |  | ||||||
|  | end | ||||||
| @@ -117,9 +117,9 @@ local load_orgs   = function() | |||||||
|         modpol.orgs = func() |         modpol.orgs = func() | ||||||
|  |  | ||||||
|         -- this block resets the metatable after being loaded in so that the class functions work |         -- this block resets the metatable after being loaded in so that the class functions work | ||||||
|         for id, org in ipairs(modpol.orgs.array) do |         -- for id, org in ipairs(modpol.orgs.array) do | ||||||
|             setmetatable(org, modpol.orgs) |         --     setmetatable(org, modpol.orgs) | ||||||
|         end |         -- end | ||||||
|  |  | ||||||
|         local nn  = modpol.ocutil.table_length (modpol.orgs.array) |         local nn  = modpol.ocutil.table_length (modpol.orgs.array) | ||||||
|         local str = "entries" |         local str = "entries" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user