Skip to content

Instantly share code, notes, and snippets.

@enricobacis
Last active September 9, 2015 23:20
Show Gist options
  • Save enricobacis/76e8dcd79e6173aca4c5 to your computer and use it in GitHub Desktop.
Save enricobacis/76e8dcd79e6173aca4c5 to your computer and use it in GitHub Desktop.
NethServer NetworkPackageManager
<?php
/* /usr/share/nethesis/NethServer/Template/NetworkPackageManager/CreateUpdate.php */
if ($view->getModule()->getIdentifier() == 'update') {
$headerText = 'Update a package `${0}`';
} else {
$headerText = 'Create a new package';
}
echo $view->panel()
->insert($view->header('package')->setAttribute('template', $T($headerText)))
->insert($view->textInput('package', ($view->getModule()->getIdentifier() == 'update' ? $view::STATE_READONLY : 0)))
->insert($view->textInput('Role', ($view->getModule()->getIdentifier() == 'update' ? $view::STATE_READONLY : 0)))
->insert($view->textInput('Rule'))
->insert($view->textInput('Description'));
echo $view->buttonList($view::BUTTON_SUBMIT | $view::BUTTON_CANCEL);
#!/usr/bin/env python
from collections import namedtuple
from subprocess import Popen, PIPE
import json
import sys
Package = namedtuple('Package', ['name', 'role', 'rule', 'description'])
def parse_package(pkg):
return Package(pkg['name'], pkg['props']['Role'], pkg['props']['Rule'], pkg['props']['Description'])
if __name__ == '__main__':
role = sys.argv[1]
result = {'role': role, 'addpkg': [], 'delpkg': []}
try:
db = Popen(['db', 'packages', 'getjson'], stdout=PIPE).communicate()[0]
for pkg in map(parse_package, json.loads(db)):
if pkg.role == role:
result['addpkg' if pkg.rule == 'add' else 'delpkg'].append(str(pkg.name))
except: pass
print result
<?php
/* /usr/share/nethesis/NethServer/Module/NetworkPackageManager.php */
namespace NethServer\Module;
use Nethgui\System\PlatformInterface as Validate;
/**
* Manage Network Packages
*/
class NetworkPackageManager extends \Nethgui\Controller\TableController
{
protected function initializeAttributes(\Nethgui\Module\ModuleAttributesInterface $base)
{
return \Nethgui\Module\SimpleModuleAttributesProvider::extendModuleAttributes($base, 'Administration', 20);
}
public function initialize()
{
$columns = array(
'Key',
'Role',
'Rule',
'Description',
'Actions'
);
$parameterSchema = array(
array('package', Validate::ANYTHING, \Nethgui\Controller\Table\Modify::KEY),
array('Role', Validate::ANYTHING, \Nethgui\Controller\Table\Modify::FIELD),
array('Rule', Validate::ANYTHING, \Nethgui\Controller\Table\Modify::FIELD),
array('Description', Validate::ANYTHING, \Nethgui\Controller\Table\Modify::FIELD),
);
$this
->setTableAdapter($this->getPlatform()->getTableAdapter('packages', 'package'))
->setColumns($columns)
->addTableAction(new \Nethgui\Controller\Table\Modify('create', $parameterSchema, 'NethServer\Template\NetworkPackageManager\CreateUpdate'))
->addTableAction(new \Nethgui\Controller\Table\Help('Help'))
->addRowAction(new \Nethgui\Controller\Table\Modify('update', $parameterSchema, 'NethServer\Template\NetworkPackageManager\CreateUpdate'))
->addRowAction(new \Nethgui\Controller\Table\Modify('delete', $parameterSchema, 'Nethgui\Template\Table\Delete'))
;
parent::initialize();
}
public function onParametersSaved(\Nethgui\Module\ModuleInterface $currentAction, $changes, $parameters)
{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment