فهرست منبع

Added uncheck option

allens 1 هفته پیش
والد
کامیت
a69ab646e2
5فایلهای تغییر یافته به همراه55 افزوده شده و 31 حذف شده
  1. 1 0
      .gitignore
  2. 23 11
      checklist/checklist.py
  3. 3 3
      checklist/default_list.py
  4. 18 8
      checklist/templates/index.html
  5. 10 9
      checklist/templates/list.html

+ 1 - 0
.gitignore

@@ -5,3 +5,4 @@ __pycache__/
 env/
 .kdev4/
 *.egg-info/
+instance/

+ 23 - 11
checklist/checklist.py

@@ -1,5 +1,6 @@
-from flask import Flask, render_template, Blueprint, request, redirect, url_for
+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__)
@@ -10,7 +11,7 @@ def save_checklist(list_items):  # Save checklist items
     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.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:
@@ -19,34 +20,45 @@ def save_checklist(list_items):  # Save checklist items
 
 @clbp.route('/', methods=['GET', 'POST'])
 def index():
-    checklist_items = current_app.list['LIST']
+    checklist_items = current_app.config['LIST']
     if request.method == 'POST':
+        checklist_items = current_app.config['LIST']
         item = request.form.get('item')
+        print(checklist_items)
         if item:
-            checklist_items.append({'name': item, 'checked': False})
+            checklist_items.append({'name': item, 'checked': 'no'})
             save_checklist(checklist_items)  # Save to file after adding
-        return redirect(url_for('index'))
+        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.list['LIST']
+    checklist_items = current_app.config['LIST']
     if 0 <= item_id < len(checklist_items):
-        checklist_items[item_id]['checked'] = not checklist_items[item_id]['checked']
+        checklist_items[item_id]['checked'] = 'yes'
         save_checklist(checklist_items)  # Save to file after checking
-    return redirect(url_for('index'))
+    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.list['LIST']
+    checklist_items = current_app.config['LIST']
     checklist_items = []  # Clear the list
     save_checklist(checklist_items)  # Save the empty list
-    return redirect(url_for('index'))
+    return redirect(url_for('checklist.index'))
 
 
 @clbp.route('/list')
 def list():
-    checklist_items = current_app.list['LIST']
+    checklist_items = current_app.config['LIST']
     return render_template('list.html', items=checklist_items)

+ 3 - 3
checklist/default_list.py

@@ -1,13 +1,13 @@
 LIST = [{
     "name": "Do a thing",
-    "checked": true
+    "checked": 'yes'
     },
     {
     "name": "Do another thing",
-    "checked": false
+    "checked": 'no'
     },
     {
     "name": "And that other thing too",
-    "checked": false
+    "checked": 'no'
     }
 ]

+ 18 - 8
checklist/templates/index.html

@@ -47,18 +47,28 @@
         <button type="submit" class="button">Add</button>
     </form>
     <ul>
-        {% for item in items %}
-            <li style="text-decoration: {{ 'line-through' if item.checked else 'none' }};">
-                {{ item.name }}
-                <a href="{{ url_for('check', item_id=loop.index0) }}">[Check]</a>
-            </li>
-        {% endfor %}
+        {% if items %}
+            {% for item in items %}
+                {% if item.checked == 'no' %}
+                    <li style="text-decoration:none;">
+                        {{ item.name }}
+                        <a href="{{ url_for('checklist.check', item_id=loop.index0) }}">[Check]</a>
+                    </li>
+                {% else %}
+                    <li> <span style="text-decoration:line-through;">
+                        {{ item.name }}</span>
+                        <a href="{{ url_for('checklist.uncheck', item_id=loop.index0) }}">[Un-check]</a>
+                    </li>
+                {% endif %}
+            {% endfor %}
+        {% endif %}
     </ul>
-    <form action="{{ url_for('list') }}" method="get" style="display: inline;">
+    <form action="{{ url_for('checklist.list') }}" method="get" style="display: inline;">
         <button type="submit" class="button">View List</button>
     </form>
-    <form action="{{ url_for('clear') }}" method="get" style="display: inline;">
+    <form action="{{ url_for('checklist.clear') }}" method="get" style="display: inline;">
         <button type="submit" class="button">Clear List</button>
     </form>
+    <br><br>
 </body>
 </html>

+ 10 - 9
checklist/templates/list.html

@@ -1,4 +1,3 @@
- 
 <!DOCTYPE html>
 <html lang="en">
 <head>
@@ -14,13 +13,15 @@
     </style>
 </head>
 <body>
-    <h2>Today's to-do List</h2>
-    <ul>
-        {% for item in items %}
-            <li style="text-decoration: {{ 'line-through' if item.checked else 'none' }}; font-weight: bold; font-size: 18px;">
-                {{ item.name }}
-            </li>
-        {% endfor %}
-    </ul>
+    {% if items %}
+        <h2>Today's to-do List</h2>
+        <ul>
+            {% for item in items %}
+                <li style="text-decoration: {{ 'line-through' if item.checked == 'yes' else 'none' }}; font-weight: bold; font-size: 18px;">
+                    {{ item.name }}
+                </li>
+            {% endfor %}
+        </ul>
+    {% endif %}
 </body>
 </html>