Skip to content

Commit b705922

Browse files
committed
fix: make roles dropdown work Issue #33
1 parent 477fee2 commit b705922

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

html/classhelper.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const CSS_FILE_NAME = "@@file/classhelper.css";
2424

2525
const CLASSHELPER_TAG_NAME = "roundup-classhelper";
2626

27+
const ALTERNATIVE_DROPDOWN_PATHNAMES = {
28+
"roles": "/rest/roles"
29+
}
30+
2731
/**
2832
* This is a custom web component(user named html tag) that wraps a helpurl link
2933
* and provides additional functionality.
@@ -296,7 +300,15 @@ class ClassHelper extends HTMLElement {
296300
const segments = param.split("[]");
297301
param = segments[0];
298302
const sortOrder = segments[1];
299-
let url = `${this.trackerBaseURL}/rest/data/${param}?@fields=id,name`;
303+
304+
let url = this.trackerBaseURL;
305+
if (ALTERNATIVE_DROPDOWN_PATHNAMES[param]) {
306+
url += ALTERNATIVE_DROPDOWN_PATHNAMES[param];
307+
} else {
308+
url += `/rest/data/${param}`;
309+
}
310+
url += "?@verbose=2";
311+
300312
if (sortOrder) {
301313
url += `&@sort=${sortOrder}`;
302314
}
@@ -317,10 +329,20 @@ class ClassHelper extends HTMLElement {
317329
}
318330

319331
let list = new Map();
320-
for (let entry of json.data.collection) {
321-
list.set(entry.id, entry.name);
322-
}
323332

333+
if (json.data.collection.length > 0) {
334+
let idKey = "id";
335+
let valueKey = Object.keys(json.data.collection[0]).find(key => key !== "id" && key !== "link");
336+
337+
if (!valueKey) {
338+
throw new Error("No value key found in dropdown data for: " + url);
339+
}
340+
341+
for (let entry of json.data.collection) {
342+
list.set(entry[idKey], entry[valueKey]);
343+
}
344+
345+
}
324346
this.dropdowns[param] = list;
325347
}
326348
}

interfaces.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from roundup.rest import Routing, RestfulInstance, _data_decorator
2+
3+
class RestfulInstance:
4+
5+
@Routing.route("/roles", 'GET')
6+
@_data_decorator
7+
def get_roles(self, input):
8+
"""Return all defined roles. The User class property
9+
roles is a string but simulate it as a MultiLink
10+
to an actual Roles class.
11+
"""
12+
return 200, {"collection": [ {"id": rolename, "name": rolename}
13+
for rolename in list(self.db.security.role.keys())]}

0 commit comments

Comments
 (0)