123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- from flask import Flask, render_template, Blueprint, request, redirect, url_for, current_app
- from datetime import timezone
- import json
- import os
- clbp = Blueprint('checklist', __name__)
- def save_checklist(list_items): # Save checklist items
- new_list = json.dumps(list_items, indent=4)
- list_file = os.path.join(current_app.instance_path, 'list.py')
- try:
- with open(list_file, 'w') as f:
- f.write(f'LIST = {new_list}')
- f.close
- current_app.config.from_pyfile('list.py', silent=True) # Reread the list into the app
- except Exception as sclerror:
- current_app.logger.error(f'Couldn\'t save list.py: {sclerror.args[0]}')
- @clbp.route('/', methods=['GET', 'POST'])
- def index():
- checklist_items = current_app.config['LIST']
- if request.method == 'POST':
- checklist_items = current_app.config['LIST']
- item = request.form.get('item')
- if item:
- checklist_items.append({'name': item, 'checked': 'no'})
- save_checklist(checklist_items) # Save to file after adding
- return redirect(url_for('checklist.index'))
- return render_template('index.html', items=checklist_items)
- @clbp.route('/check/<int:item_id>')
- def check(item_id):
- checklist_items = current_app.config['LIST']
- if 0 <= item_id < len(checklist_items):
- checklist_items[item_id]['checked'] = 'yes'
- save_checklist(checklist_items) # Save to file after checking
- return redirect(url_for('checklist.index'))
- @clbp.route('/uncheck/<int:item_id>')
- def uncheck(item_id):
- checklist_items = current_app.config['LIST']
- if 0 <= item_id < len(checklist_items):
- checklist_items[item_id]['checked'] = 'no'
- save_checklist(checklist_items) # Save to file after checking
- return redirect(url_for('checklist.index'))
- @clbp.route('/clear')
- def clear():
- checklist_items = current_app.config['LIST']
- checklist_items = [] # Clear the list
- save_checklist(checklist_items) # Save the empty list
- return redirect(url_for('checklist.index'))
- @clbp.route('/list')
- def list():
- checklist_items = current_app.config['LIST']
- return render_template('list.html', items=checklist_items)
|