diff --git a/.gitignore b/.gitignore index 68bc17f9..5908fb0c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +py_elog/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/Dockerfile b/Dockerfile index 5c8bdb45..f3d66761 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,9 @@ ARG img_user=ghcr.io/driplineorg ARG img_repo=dripline-python ARG img_tag=v5.1.5 +ARG UID=5001 +ARG GID=5001 + FROM ${img_user}/${img_repo}:${img_tag} COPY . /usr/local/src_dragonfly @@ -9,5 +12,9 @@ COPY . /usr/local/src_dragonfly WORKDIR /usr/local/src_dragonfly RUN pip install docker pymodbus RUN pip install . +RUN pip install Flask +RUN git clone https://github.com/paulscherrerinstitute/py_elog.git /usr/local/py_elog +RUN pip install /usr/local/py_elog WORKDIR / + diff --git a/dragonfly/__init__.py b/dragonfly/__init__.py index eb89dbbe..48b9c8d1 100644 --- a/dragonfly/__init__.py +++ b/dragonfly/__init__.py @@ -11,7 +11,7 @@ def __get_version(): version.parse(pkg_resources.get_distribution('dragonfly').version) version.package = 'project8/dragonfly' version.commit = 'na' - dragonfly.core.add_version('dragonfly', version) + #dragonfly.core.add_version('dragonfly', version) return version version = __get_version() __version__ = version.version diff --git a/dragonfly/checklist_server.py b/dragonfly/checklist_server.py new file mode 100644 index 00000000..ae0d8751 --- /dev/null +++ b/dragonfly/checklist_server.py @@ -0,0 +1,103 @@ +from flask import Flask, redirect, request, render_template +from datetime import datetime +from dragonfly.utility import send_to_elog +import yaml + +with open("/root/checklist.yaml", "r") as open_file: + config = yaml.safe_load( open_file.read() ) + +app = Flask(__name__) + +@app.route('/') +def select_list(): + content = "" + content += "

Please select a Checklist

" + for checklist in config["checklists"]: + label = checklist["name"] + url_name = label.replace(" ", "_") + content += f' {label}
' + return content + +@app.route('/checklist/') +def show_checklist(checklist_name): + checklist = None + for c in config["checklists"]: + if checklist_name == c["name"].replace(" ", "_"): + checklist = c + if checklist is None: + return f'Checklist for {checklist_name} not found. Go back to start page' + + content = """ +

{title}

+

+ + User:

+ {questions} + +

+ + + """ + title = checklist['name'] + questions = "" + checkbox_script = "" + + for i, question in enumerate(checklist["questions"]): + if question["type"] == "text": + if "label_checklist_pre" in question.keys(): + questions += f'{question["label_checklist_pre"]}: ' + questions += f'' + if "label_checklist_post" in question.keys(): + questions += f'' + elif question["type"] == "checkbox": + label = f"{i}" + questions += f' ' + if "label_checklist" in question.keys(): + questions += f'' + checkbox_script += f"""const checkbox_{i} = document.getElementById("question_{i}"); + const hidden_{i} = document.getElementById("hidden_{i}"); """ + checkbox_script += "button.addEventListener('click', () => { hidden_%d.value = checkbox_%d.checked ? 'True' : 'False'; }); "%(i,i) + if "link_page" in question.keys(): + questions += f'Check on this page.' + questions += '

' + return content.format(title=title, checklist_name=checklist_name, questions=questions, checkbox_script=checkbox_script) + + +@app.route('/submit/', methods=["GET",'POST']) +def submit_checklist(checklist_name): + # This function is called with the result of the check list. + # Use the request.form content to generate a nice ELOG message + # That elog message will be posted to the elog + + checklist = None + for c in config["checklists"]: + if checklist_name == c["name"].replace(" ", "_"): + checklist = c + if checklist is None: + return f'Checklist for {checklist_name} not found. Go back to start page' + + + report = "" + try: + report += f"Timestamp: %s\n"%(datetime.now().strftime("%d/%m/%Y %H:%M:%S")) + report += f"User: {request.form['user']}\n" + for i, question in enumerate(checklist["questions"]): + if question["type"] == "text": + report = report + question["label_elog"] + ": " + request.form.get(f"question_{i}") + "\n" + elif question["type"] == "checkbox": + if request.form.get(f"hidden_{i}") == 'True': + report += f"[x] {question['label_elog']}\n" + else: + report += f"[ ] {question['label_elog']}\n" + except Exception as e: + print(e, flush=True) + + print(report, flush=True) + send_to_elog(report, subject=checklist_name, author=config["username"], category="Slow controls", msg_id=None, password=config["password"]) + + # Tell the user that checklist is done. + return '

Checklist successfully submitted. You can go back to the start page .

' diff --git a/dragonfly/utility.py b/dragonfly/utility.py new file mode 100644 index 00000000..030ccb92 --- /dev/null +++ b/dragonfly/utility.py @@ -0,0 +1,12 @@ +import elog +from getpass import getpass + + +def send_to_elog(message, subject="Checklist", author="Rene Reimann", category="Slow Controls", msg_id=None, password=None): + if password is None: + password = getpass() + logbook = elog.open("https://maxwell.npl.washington.edu/elog/project8/P8+Mainz+lab", user=author, password=password) + if msg_id is None: + return logbook.post(message, subject=subject, Author=author, category=category, suppress_email_notification=True) + else: + return logbook.post(message, msg_id=msg_id, subject=subject, Author=author, category=category, suppress_email_notification=True)