liquid_feedback_frontend
view env/util/autoapi.lua @ 118:93f4e465b50d
add initiator support in delegation
if a delegation is issued from the initiative view, the initiators
from that one are added to the delegation target list. this makes it easier to delegate to the author without the need to add him to the contact list.
if a delegation is issued from the initiative view, the initiators
from that one are added to the delegation target list. this makes it easier to delegate to the author without the need to add him to the contact list.
| author | Daniel Poelzleithner <poelzi@poelzi.org> |
|---|---|
| date | Mon Sep 20 20:32:04 2010 +0200 (2010-09-20) |
| parents | 733f65c0c0a0 |
| children |
line source
1 function util.autoapi_xml(args)
2 local relation_name = assert(args.relation_name)
3 local selector = assert(args.selector)
4 local fields = assert(args.fields)
5 local rows = selector:exec()
6 slot.set_layout("xml", "application/xml")
7 slot.put("<", relation_name, "_list>\n")
8 for i_row, row in ipairs(rows) do
9 slot.put(" <", relation_name, ">\n")
10 for i_field, field in ipairs(fields) do
11 slot.put(" <", field.name, ">")
12 local value
13 if field.func then
14 value = field.func(row)
15 elseif field.field then
16 value = row[field.name]
17 end
18 if value ~= nil then
19 slot.put(encode.html(tostring(value)))
20 else
21 slot.put("NULL")
22 end
23 slot.put("</", field.name, ">\n")
24 end
25 slot.put(" </", relation_name, ">\n")
26 end
27 slot.put("</", relation_name, "_list>\n")
28 end
30 function util.autoapi_json(args)
31 slot.set_layout("blank", "application/json")
32 local selector = assert(args.selector)
33 local fields = assert(args.fields)
34 local rows = selector:exec()
35 slot.put("[\n")
36 for i_row, row in ipairs(rows) do
37 slot.put(" {\n")
38 for i_field, field in ipairs(fields) do
39 slot.put(" \"", field.name, "\": ")
40 local value
41 if field.func then
42 value = field.func(row)
43 elseif field.field then
44 value = row[field.name]
45 end
46 slot.put(encode.json(value))
47 if i_field < #fields then
48 slot.put(",")
49 end
50 slot.put("\n")
51 end
52 slot.put(" }")
53 if i_row < #rows then
54 slot.put(",")
55 end
56 slot.put("\n")
57 end
58 slot.put("]\n")
59 end
61 function util.autoapi(args)
62 local relation_name = assert(args.relation_name)
63 local selector = assert(args.selector)
64 local fields = assert(args.fields)
65 local api_engine = assert(args.api_engine)
67 selector:reset_fields()
69 for i_field, field in ipairs(fields) do
70 if field.field then
71 selector:add_field(field.field, field.name)
72 end
73 end
75 if api_engine == "xml" then
76 util.autoapi_xml{
77 relation_name = relation_name,
78 selector = selector,
79 fields = fields
80 }
81 elseif api_engine == "json" then
82 util.autoapi_json{
83 selector = selector,
84 fields = fields
85 }
86 end
88 end
