Commit 59305202 authored by Alan Mitchell's avatar Alan Mitchell

Adds Hidden Results Field an Delete Parameters feature.

To the Periodic Script feature.
parent a93f06d2
......@@ -197,6 +197,7 @@ class AlertRecipientAdmin(admin.ModelAdmin):
@admin.register(PeriodicScript)
class SensorAdmin(admin.ModelAdmin):
exclude = ('hidden_script_results', )
list_display = ('script_file_name', 'description', 'period', 'script_parameters')
search_fields = ['script_file_name', 'description', 'script_parameters']
readonly_fields = ('script_results',)
......
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2016-11-02 18:16
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bmsapp', '0012_periodicscript_description'),
]
operations = [
migrations.AddField(
model_name='periodicscript',
name='hidden_script_results',
field=models.TextField(blank=True, verbose_name=b'Hidden Script results in YAML form'),
),
]
......@@ -634,6 +634,10 @@ class PeriodicScript(models.Model):
# Results of the script saved and passed to the next invocation of the script.
script_results = models.TextField('Script results in YAML form', blank=True)
# Results of the Script that are *not* displayed in the Admin interface. Useful
# for storing authorization tokens or other credentials.
hidden_script_results = models.TextField('Hidden Script results in YAML form', blank=True)
def __unicode__(self):
return '%s -- %s' % (self.script_file_name, self.script_parameters.replace('\n', ', '))
......
......@@ -4,6 +4,7 @@ Script to collect sensor readings from an OkoFEN pellet boiler.
import random, time
def run(**kwargs):
results = {'readings': [(None, 'anc_birch_wind', 10.0 * random.random()),
(None, 'anc_birch_temp', 40 + 20 * random.random())]}
results = {'delete_params': ['a', 'x'],
'hidden': {'hidden_info': 99}}
return results
......@@ -80,6 +80,9 @@ class RunScript(threading.Thread):
# results of the script.
results ={'script_start_time': datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")}
# Start a dictionary that holds results that are not shown in the Admin interface.
hidden_results = {}
# get a logger. First strip off any extension from the script
# file name and use that as part of the logger name.
script_mod_base = self.script.script_file_name.split('.')[0]
......@@ -90,6 +93,7 @@ class RunScript(threading.Thread):
# last run of the script. Both of the those sets of parameters are in YAML
# form.
params = yaml.load(self.script.script_parameters)
last_script_results = yaml.load(self.script.script_results)
if type(last_script_results) != dict:
# There may not have been any script results, or the YAML translation
......@@ -97,6 +101,13 @@ class RunScript(threading.Thread):
last_script_results = {}
params.update(last_script_results)
last_hidden_script_results = yaml.load(self.script.hidden_script_results)
if type(last_hidden_script_results) != dict:
# There may not have been any script results, or the YAML translation
# did not produce a dictionary.
last_hidden_script_results = {}
params.update(last_hidden_script_results)
# import the periodic script module, but first strip off any extension that
# the user may have appended
script_mod = importlib.import_module('bmsapp.periodic_scripts.' + script_mod_base)
......@@ -131,6 +142,22 @@ class RunScript(threading.Thread):
results['reading_insert_message'] = insert_msg
logger.debug('Script %s: %s' % (self.script.script_file_name, insert_msg))
# if the results contains a 'hidden' key, then extract that dictionary and save
# for storage in the hidden results field.
if 'hidden' in results:
# Note that you can put binary data into the values fields of the dictionary.
# YAML will represent them correctly.
hidden_results = results.pop('hidden')
# if there is a "delete_params" key, then the script is requesting that those
# input parameters, which are in a list, be deleted from the model object.
# Exmaple use is to delete one-time authorization parameters
if 'delete_params' in results:
params = yaml.load(self.script.script_parameters)
for p in results.pop('delete_params'):
params.pop(p, None) # deletes param and no error if not there
self.script.script_parameters = yaml.dump(params, default_flow_style=False)
except:
# record the traceback of the error in the results variable
results['script_error'] = traceback.format_exc()
......@@ -139,6 +166,7 @@ class RunScript(threading.Thread):
# Store the results back into the model script object so they are
# viewable in the Admin interface and are available for the next call.
self.script.script_results = yaml.dump(results, default_flow_style=False)
self.script.hidden_script_results = yaml.dump(hidden_results, default_flow_style=False)
self.script.save()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment