gluon-web: split out model support into a separate package

This commit is contained in:
Matthias Schiffer 2018-02-25 18:14:24 +01:00
parent 83a6847fbd
commit c3e4ceed28
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
28 changed files with 173 additions and 101 deletions

View File

@ -15,7 +15,7 @@ define Package/gluon-setup-mode
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
TITLE:=Setup mode TITLE:=Setup mode
DEPENDS:=+gluon-core +gluon-web +ubus +uhttpd +dnsmasq DEPENDS:=+gluon-core +gluon-web-model +ubus +uhttpd +dnsmasq
endef endef
define Package/gluon-setup-mode/description define Package/gluon-setup-mode/description

View File

@ -0,0 +1,36 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=gluon-web-model
PKG_VERSION:=1
include ../gluon.mk
PKG_CONFIG_DEPENDS += $(GLUON_I18N_CONFIG)
define Package/gluon-web-model
SECTION:=gluon
CATEGORY:=Gluon
TITLE:=Minimal Lua web framework derived from LuCI (model support)
DEPENDS:=+gluon-web
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
endef
define Build/Compile
$(call GluonBuildI18N,gluon-web-model,i18n)
$(call GluonSrcDiet,./luasrc,$(PKG_BUILD_DIR)/luadest/)
endef
define Package/gluon-web-model/install
$(CP) ./files/* $(1)/
$(CP) $(PKG_BUILD_DIR)/luadest/* $(1)/
$(call GluonInstallI18N,gluon-web-model,$(1))
endef
$(eval $(call BuildPackage,gluon-web-model))

View File

@ -0,0 +1,23 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"PO-Revision-Date: 2013-03-29 12:13+0200\n"
"Last-Translator: Matthias Schiffer <mschiffer@universe-factory.net>\n"
"Language-Team: German\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
msgid "One or more fields contain invalid values!"
msgstr "Ein oder mehrere Felder enthalten ungültige Werte!"
msgid "One or more required fields have no value!"
msgstr "Ein oder mehr benötigte Felder sind nicht ausgefüllt!"
msgid "Reset"
msgstr "Zurücksetzen"
msgid "Save"
msgstr "Speichern"

View File

@ -0,0 +1,23 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"PO-Revision-Date: 2013-12-22 17:11+0200\n"
"Last-Translator: goofy <pierre.gaufillet@gmail.com>\n"
"Language-Team: French\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
msgid "One or more fields contain invalid values!"
msgstr "Un ou plusieurs champs contiennent des valeurs incorrectes !"
msgid "One or more required fields have no value!"
msgstr "Un ou plusieurs champs n'ont pas de valeur !"
msgid "Reset"
msgstr "Remise à zéro"
msgid "Save"
msgstr "Soumettre"

View File

@ -0,0 +1,14 @@
msgid ""
msgstr "Content-Type: text/plain; charset=UTF-8"
msgid "One or more fields contain invalid values!"
msgstr ""
msgid "One or more required fields have no value!"
msgstr ""
msgid "Reset"
msgstr ""
msgid "Save"
msgstr ""

View File

@ -0,0 +1,68 @@
-- Copyright 2008 Steven Barth <steven@midlink.org>
-- Copyright 2017-2018 Matthias Schiffer <mschiffer@universe-factory.net>
-- Licensed to the public under the Apache License 2.0.
module('gluon.web.model', package.seeall)
local fs = require 'nixio.fs'
local classes = require 'gluon.web.model.classes'
local util = require 'gluon.web.util'
local instanceof = util.instanceof
-- Loads a model from given file, creating an environment and returns it
local function load(filename, i18n)
local func = assert(loadfile(filename))
setfenv(func, setmetatable({}, {__index =
function(tbl, key)
return classes[key] or i18n[key] or _G[key]
end
}))
local models = { func() }
for k, model in ipairs(models) do
if not instanceof(model, classes.Node) then
error("model definition returned an invalid model object")
end
model.index = k
end
return models
end
return function(config, http, renderer, name, pkg)
local hidenav = false
local modeldir = config.base_path .. '/model/'
local filename = modeldir..name..'.lua'
if not fs.access(filename) then
error("Model '" .. name .. "' not found!")
end
local i18n = setmetatable({
i18n = renderer.i18n
}, {
__index = renderer.i18n(pkg)
})
local maps = load(filename, i18n)
for _, map in ipairs(maps) do
map:parse(http)
end
for _, map in ipairs(maps) do
map:handle()
hidenav = hidenav or map.hidenav
end
renderer.render('layout', {
content = 'model/wrapper',
env = {
maps = maps,
},
hidenav = hidenav,
})
end

View File

@ -1,12 +1,11 @@
-- Copyright 2008 Steven Barth <steven@midlink.org> -- Copyright 2008 Steven Barth <steven@midlink.org>
-- Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net> -- Copyright 2017-2018 Matthias Schiffer <mschiffer@universe-factory.net>
-- Licensed to the public under the Apache License 2.0. -- Licensed to the public under the Apache License 2.0.
module("gluon.web.model", package.seeall) module("gluon.web.model.classes", package.seeall)
local util = require "gluon.web.util" local util = require "gluon.web.util"
local fs = require "nixio.fs"
local datatypes = require "gluon.web.model.datatypes" local datatypes = require "gluon.web.model.datatypes"
local class = util.class local class = util.class
local instanceof = util.instanceof local instanceof = util.instanceof
@ -15,41 +14,6 @@ FORM_NODATA = 0
FORM_VALID = 1 FORM_VALID = 1
FORM_INVALID = -1 FORM_INVALID = -1
-- Loads a model from given file, creating an environment and returns it
function load(config, name, renderer, pkg)
local modeldir = config.base_path .. "/model/"
if not fs.access(modeldir..name..".lua") then
error("Model '" .. name .. "' not found!")
end
local func = assert(loadfile(modeldir..name..".lua"))
local i18n = setmetatable({
i18n = renderer.i18n
}, {
__index = renderer.i18n(pkg)
})
setfenv(func, setmetatable({}, {__index =
function(tbl, key)
return _M[key] or i18n[key] or _G[key]
end
}))
local models = { func() }
for k, model in ipairs(models) do
if not instanceof(model, Node) then
error("model definition returned an invalid model object")
end
model.index = k
end
return models
end
local function parse_datatype(code) local function parse_datatype(code)
local match, arg, arg2 local match, arg, arg2
@ -86,7 +50,7 @@ function Node:__init__(title, description, name)
self.name = name self.name = name
self.index = nil self.index = nil
self.parent = nil self.parent = nil
self.package = 'gluon-web' self.package = 'gluon-web-model'
end end
function Node:append(obj) function Node:append(obj)

View File

@ -16,7 +16,7 @@ define Package/gluon-web-theme
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
TITLE:=gluon-web theme TITLE:=gluon-web theme
DEPENDS:=+gluon-core +gluon-web DEPENDS:=+gluon-core +gluon-web-model
endef endef
define Build/Prepare define Build/Prepare

View File

@ -142,7 +142,7 @@ You may obtain a copy of the License at
<% <%
ok, err = pcall(renderer.render, content, env, pkg) ok, err = pcall(renderer.render, content, env, pkg)
if not ok then if not ok then
renderer.render('error500', {message = err}, 'gluon-web') renderer.render('error/500', {message = err}, 'gluon-web')
end end
%> %>

View File

@ -3,7 +3,6 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=gluon-web PKG_NAME:=gluon-web
PKG_VERSION:=1 PKG_VERSION:=1
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
include ../gluon.mk include ../gluon.mk

View File

@ -16,18 +16,6 @@ msgstr "Interner Serverfehler"
msgid "Not Found" msgid "Not Found"
msgstr "Nicht Gefunden" msgstr "Nicht Gefunden"
msgid "One or more fields contain invalid values!"
msgstr "Ein oder mehrere Felder enthalten ungültige Werte!"
msgid "One or more required fields have no value!"
msgstr "Ein oder mehr benötigte Felder sind nicht ausgefüllt!"
msgid "Reset"
msgstr "Zurücksetzen"
msgid "Save"
msgstr "Speichern"
msgid "Sorry, the object you requested was not found." msgid "Sorry, the object you requested was not found."
msgstr "Entschuldigung, das anfgeforderte Objekt wurde nicht gefunden." msgstr "Entschuldigung, das anfgeforderte Objekt wurde nicht gefunden."

View File

@ -16,18 +16,6 @@ msgstr "Erreur Serveur Interne"
msgid "Not Found" msgid "Not Found"
msgstr "Pas trouvé" msgstr "Pas trouvé"
msgid "One or more fields contain invalid values!"
msgstr "Un ou plusieurs champs contiennent des valeurs incorrectes !"
msgid "One or more required fields have no value!"
msgstr "Un ou plusieurs champs n'ont pas de valeur !"
msgid "Reset"
msgstr "Remise à zéro"
msgid "Save"
msgstr "Soumettre"
msgid "Sorry, the object you requested was not found." msgid "Sorry, the object you requested was not found."
msgstr "Désolé, l'objet que vous avez demandé n'as pas été trouvé." msgstr "Désolé, l'objet que vous avez demandé n'as pas été trouvé."

View File

@ -7,18 +7,6 @@ msgstr ""
msgid "Not Found" msgid "Not Found"
msgstr "" msgstr ""
msgid "One or more fields contain invalid values!"
msgstr ""
msgid "One or more required fields have no value!"
msgstr ""
msgid "Reset"
msgstr ""
msgid "Save"
msgstr ""
msgid "Sorry, the object you requested was not found." msgid "Sorry, the object you requested was not found."
msgstr "" msgstr ""

View File

@ -146,26 +146,7 @@ local function dispatch(config, http, request)
model = function(name) model = function(name)
local pkg = _pkg local pkg = _pkg
return function() return function()
local hidenav = false require('gluon.web.model')(config, http, renderer, name, pkg)
local model = require "gluon.web.model"
local maps = model.load(config, name, renderer, pkg)
for _, map in ipairs(maps) do
map:parse(http)
end
for _, map in ipairs(maps) do
map:handle()
hidenav = hidenav or map.hidenav
end
renderer.render("layout", {
content = "model/wrapper",
env = {
maps = maps,
},
hidenav = hidenav,
})
end end
end, end,
@ -198,7 +179,7 @@ local function dispatch(config, http, request)
if not node or not node.target then if not node or not node.target then
http:status(404, "Not Found") http:status(404, "Not Found")
renderer.render("layout", { renderer.render("layout", {
content = "error404", content = "error/404",
env = { env = {
message = message =
"No page is registered at '/" .. table.concat(request, "/") .. "'.\n" .. "No page is registered at '/" .. table.concat(request, "/") .. "'.\n" ..
@ -215,7 +196,7 @@ local function dispatch(config, http, request)
if not ok then if not ok then
http:status(500, "Internal Server Error") http:status(500, "Internal Server Error")
renderer.render("layout", { renderer.render("layout", {
content = "error500", content = "error/500",
env = { env = {
message = message =
"Failed to execute dispatcher target for entry '/" .. table.concat(request, "/") .. "'.\n" .. "Failed to execute dispatcher target for entry '/" .. table.concat(request, "/") .. "'.\n" ..