-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodel.lua
271 lines (251 loc) · 8.13 KB
/
model.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
local conf = require('config')
local utils = require('utils')
local url = require("socket.url")
local default = utils.default
local model = {}
-- convert release name to branch name
function model.branchFormat(branch)
return (branch == "edge") and "master" or string.format("%s-stable", branch:sub(2))
end
-- convert branch name to one used in logfiles
function model.logBranchFormat(branch)
return branch:sub(1,1)=="v" and string.gsub(branch:sub(2),"%.","-") or branch
end
function model.packages(pkgs, branch)
local r = {}
for k,v in pairs(pkgs) do
r[k] = {}
r[k].name = {
path=string.format("/package/%s/%s/%s/%s", branch, v.repo, v.arch, v.name),
text=v.name,
title=v.description
}
r[k].version = {
path=string.format("/flag/%s/%s/%s", v.repo, v.origin, v.version),
text=v.version,
title="Flag this package out of date",
class="text-success"
}
r[k].url = {
path=v.url,
text="URL",
title=v.url
}
r[k].license = v.license
r[k].branch = branch
r[k].arch = {
text=v.arch,
title=string.format("Filter packages in the %s architecture", v.arch)
}
r[k].repo = {
text=v.repo,
title=string.format("Filter packages in the %s repository", v.repo)
}
maintainer = v.mname or "None"
r[k].maintainer = {
text=maintainer,
title=string.format("Filter packages maintained by %s", maintainer)
}
r[k].build_time = v.build_time
if (v.flagged) then
r[k].version.title = "Flagged: "..v.flagged
r[k].version.class = "text-danger"
r[k].version.path = "#"
end
if branch == conf.default.branch then r[k].default = true end
end
return r
end
-- multiple selected fields are not valid markup
local function select_placeholder(args)
local res = {}
for _,k in ipairs({"branch", "repo", "arch", "maintainer"}) do
if args[k] == "" then res[k] = "selected" end
end
return res
end
function model.packagesForm(args, distinct)
local m = {}
m.placeholder = select_placeholder(args)
m.name = args.name
m.branch = model.FormSelect({unpack(conf.branches)}, args.branch)
m.repo = model.FormSelect({unpack(conf.repos)}, args.repo)
m.arch = model.FormSelect({unpack(conf.archs)}, args.arch)
m.maintainer = model.FormSelect(distinct.maintainer, args.maintainer)
return m
end
function model.FormSelect(options, selected)
local r = {}
--table.insert(options, 1, "")
for k,v in pairs(options) do
r[k] = {text=v}
if (v==selected) then r[k].selected = "selected" end
end
return r
end
function model.package(pkg)
local r = {}
-- populate default values or None if not set.
for _,v in pairs(conf.index.fields) do
r[v] = default(pkg[v], "None")
end
r.branch = pkg.branch
r.repo = pkg.repo
r.nav = {package="active"}
r.version = {text=pkg.version}
if (pkg.branch == conf.default.branch) then r.flaggable = true end
if pkg.flagged then
r.version.class = "text-danger"
r.version.title = string.format("Flagged: %s", pkg.flagged)
r.version.path = "#"
else
r.version.class = "text-success"
r.version.title = string.format("Flag this package out of date")
r.version.path = string.format("/flag/%s/%s/%s",
pkg.repo, pkg.origin, pkg.version)
end
r.maintainer = pkg.mname or "None"
r.origin = {
path=string.format("/package/%s/%s/%s/%s",
pkg.branch, pkg.repo, pkg.arch, pkg.origin),
text=pkg.origin
}
r.commit = {
path=string.format(conf.git.commit, pkg.commit),
text=pkg.commit
}
r.contents = {
path=string.format("/contents?branch=%s&name=%s&arch=%s&repo=%s",
pkg.branch, url.escape(pkg.name), pkg.arch, pkg.repo),
text="Contents of package"
}
r.git = string.format(conf.git.pkgpath, pkg.repo, pkg.origin,
model.branchFormat(pkg.branch))
r.log = string.format(conf.buildlog, model.logBranchFormat(pkg.branch),
pkg.arch, pkg.repo, pkg.origin, pkg.origin, pkg.version)
return r
end
function model.flagged(pkgs)
local r = {}
for k,v in pairs(pkgs) do
local class = k < (conf.pager.limit/2) and "bottom" or "top"
r[k] = {}
r[k].origin = {
path = ("packages?branch=%s&repo=%s&name=%s"):format(
conf.default.branch, v.repo, url.escape(v.origin)),
text=v.origin,
title=v.description
}
r[k].version = v.version
r[k].new_version = v.new_version
r[k].arch = v.arch
r[k].repo = {
text=v.repo,
title=string.format("Filter packages in the %s repository", v.repo)
}
maintainer = v.mname or "None"
r[k].maintainer = {
text=maintainer,
title=string.format("Filter packages maintained by %s", maintainer)
}
r[k].created = v.created
r[k].message = v.message
r[k].class = "hint--"..class.."-left hint--medium hint--rounded"
end
return r
end
function model.flaggedForm(args, maintainers)
local m = {}
m.placeholder = select_placeholder(args)
m.origin = args.origin
m.repo = model.FormSelect({unpack(conf.repos)}, args.repo)
m.maintainer = model.FormSelect(maintainers, args.maintainer)
return m
end
function model.packageRelations(pkgs)
local r = {}
for _,v in pairs(pkgs) do
local path = string.format("/package/%s/%s/%s/%s", v.branch, v.repo, v.arch, v.name)
table.insert(r, {path=path, text=v.name})
end
return r
end
function model.pagerModel(args, pager)
local result = {}
if pager.last > 1 then
table.insert(pager, 1, "«")
table.insert(pager, "»")
for _,p in ipairs(pager) do
local r = {}
for g,v in pairs(args) do
if (g=="page") then
if p == "«" then
v=1
elseif p == "»" then
v=pager.last
else
v=p
end
end
if v ~= "" then
r[#r+1]=string.format("%s=%s", url.escape(g), url.escape(v))
end
end
local path = table.concat(r, '&')
class = (args.page == p) and "active" or ""
table.insert(result, {args=path, class=class, page=p})
end
end
return result
end
function model.contents(cnt, branch)
local r = {}
for k,v in pairs(cnt) do
r[k] = {}
r[k].branch = branch
r[k].repo = {
text=v.repo,
title=string.format("Filter packages in the %s repository", v.repo)
}
r[k].arch = {
text=v.arch,
title=string.format("Filter packages in the %s architecture", v.arch)
}
r[k].file = string.format("%s/%s", v.path, v.file)
r[k].pkgname = {}
r[k].pkgname.path = string.format("/package/%s/%s/%s/%s", branch, v.repo, v.arch, v.name)
r[k].pkgname.text = v.name
end
return r
end
function model.contentsForm(args)
local m = {}
m.file = args.file
m.path = args.path
m.name = args.name
m.placeholder = select_placeholder(args)
m.branch = model.FormSelect({unpack(conf.branches)}, args.branch)
m.repo = model.FormSelect({unpack(conf.repos)}, args.repo)
m.arch = model.FormSelect({unpack(conf.archs)}, args.arch)
return m
end
function model.flag(pkg, m)
m = m or {}
m.form = m.form or {}
m.nav = {flagged="active"}
m.repo = pkg.repo
m.origin = pkg.origin
m.version = pkg.version
m.sitekey = conf.rc.sitekey
return m
end
function model.flagMail(pkg, args)
return {
maintainer = pkg.mname,
origin = pkg.origin,
from = args.from,
message = args.message,
new_version = args.new_version
}
end
return model