import os

from flask import Flask, render_template, request
from yaml import safe_load as yaml_load

app = Flask(__name__)

@app.route('/')
def root():
    return render_template('index.html', jobs=list(all_jobs()))

@app.route('/jobs/<slug>', methods=('GET', 'POST'))
def job(slug):
    job = Job(slug)

    if request.method == 'POST':
        job.name = request.form['name']

    return render_template('job.html', job=job)

@app.route('/jobs/<slug>/edit', methods=('GET',))
def job_edit(slug):
    return render_template('job_edit.html', job=Job(slug))

def load_on_access(var_name):
    """
    Property that tries to call .load if the value is not set
    """
    def getter(self):
        try:
            value = getattr(self, var_name)
        except AttributeError:
            self.load()
            value = getattr(self, var_name)
        return value

    def setter(self, value):
        return setattr(self, var_name, value)

    return property(getter, setter, None)

class Job(object):
    def __init__(self, slug=None):
        self.slug = slug

    name = load_on_access('_name')

    def load(self):
        """
        Fill the object from the job file
        """
        with open('data/jobs/%s.yaml' % self.slug) as fh:
            data = yaml_load(fh)
            self.name = data.get('name', self.slug)

def all_jobs():
    """
    Get the list of jobs
    """
    for fn in os.listdir('data/jobs'):
        if os.path.isfile('data/jobs/%s' % fn) and fn.endswith('.yaml'):
            job = Job(fn[:-5])
            yield job

def setup_data():
    """
    Setup the data dirs for storing jobs/etc
    """
    os.makedirs('data/jobs', exist_ok=True)

if __name__ == "__main__":
    setup_data()
    app.run()

@@ -93,17 +93,36 @@
 aml_load
+, dump as yaml_dump
 %0A
-
 %0Aapp = F
@@ -390,16 +390,35 @@
 %5B'name'%5D
+%0A        job.save()
 %0A%0A    re
@@ -1363,16 +1363,389 @@
 .slug)%0A%0A
+    def save(self):%0A        %22%22%22%0A        Save the job data%0A        %22%22%22%0A        yaml_data = yaml_dump(self.as_dict(), default_flow_style=True)%0A        with open('data/jobs/%25s.yaml' %25 self.slug, 'w') as fh:%0A            fh.write(yaml_data)%0A%0A    def as_dict(self):%0A        %22%22%22%0A        Serialize to dict%0A        %22%22%22%0A        return %7Batt: getattr(self, att) for att in ('name',)%7D%0A%0A
 def all_
@@ -2127,16 +2127,16 @@
 _data()%0A
-
     app.
@@ -2139,10 +2139,20 @@
 app.run(
+debug=True
 )%0A

Implement job edit persistence