projekt_paradygmaty_lidia_w.../app.py
Lidia Wojciechowska 7397020d64 Dodano nowe pliki
2024-06-17 10:52:23 +02:00

126 lines
4.3 KiB
Python

from flask import Flask, render_template, request, redirect, url_for
import pyodbc
import configparser
from datetime import datetime
app = Flask(__name__, template_folder='.',static_folder='')
class TodoList:
def __init__(self):
config = configparser.ConfigParser()
config.read('dane.ini')
mssql_config = config['mssql']
self.conn = pyodbc.connect(
f"DRIVER={mssql_config['driver']};"
f"SERVER={mssql_config['server']};"
f"DATABASE={mssql_config['database']};"
f"UID={mssql_config['uid']};"
f"PWD={mssql_config['pwd']}"
)
self.create_table()
def create_table(self):
with self.conn.cursor() as cursor:
cursor.execute('''
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='tasks' and xtype='U')
CREATE TABLE tasks (
id INT PRIMARY KEY IDENTITY(1,1),
description NVARCHAR(255) NOT NULL
)
''')
self.conn.commit()
def add_task(self, description):
description_with_timestamp = self.add_timestamp(description)
with self.conn.cursor() as cursor:
cursor.execute('INSERT INTO tasks (description) VALUES (?)', (description_with_timestamp,))
self.conn.commit()
def remove_task(self, task_id):
with self.conn.cursor() as cursor:
cursor.execute('DELETE FROM tasks WHERE id = ?', (task_id,))
self.conn.commit()
def edit_task(self, task_id, new_description):
new_description_with_timestamp = self.add_timestamp(new_description)
with self.conn.cursor() as cursor:
cursor.execute('UPDATE tasks SET description = ? WHERE id = ?', (new_description_with_timestamp, task_id))
self.conn.commit()
def get_tasks(self):
with self.conn.cursor() as cursor:
cursor.execute('SELECT id, description FROM tasks')
tasks = []
for row in cursor.fetchall():
task_dict = {
'id': row.id,
'description': row.description
}
tasks.append(task_dict)
return tasks
def get_task(self, task_id):
with self.conn.cursor() as cursor:
cursor.execute('SELECT id, description FROM tasks WHERE id = ?', (task_id,))
return cursor.fetchone()
def add_timestamp(self, description):
return f"{description} (added/edited on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')})"
def __del__(self):
self.conn.close()
todo_list = TodoList()
@app.route('/')
def index():
tasks = todo_list.get_tasks()
return render_template('todo_list.html', tasks=tasks)
@app.route('/add', methods=['GET', 'POST'])
def add_task():
if request.method == 'POST':
description = request.form['description']
todo_list.add_task(description)
return redirect(url_for('index'))
return render_template('task_add.html')
@app.route('/delete/<int:task_id>')
def delete_task(task_id):
todo_list.remove_task(task_id)
return redirect(url_for('index'))
@app.route('/search', methods=['GET', 'POST'])
def search_tasks():
if request.method == 'POST':
keyword = request.form['keyword'].lower()
tasks = todo_list.get_tasks()
matching_tasks = [task for task in tasks if keyword in task['description'].lower()]
return render_template('task_search.html', tasks=matching_tasks, keyword=request.form['keyword'])
return render_template('task_search.html')
@app.route('/edit/<int:task_id>', methods=['GET', 'POST'])
def edit_task(task_id):
task = todo_list.get_task(task_id)
if request.method == 'POST':
new_description = request.form['description']
todo_list.edit_task(task_id, new_description)
return redirect(url_for('index'))
return render_template('task_edit.html', task=task)
@app.route('/long')
def long_tasks():
tasks = todo_list.get_tasks()
long_tasks = list(filter(lambda task: len(task['description']) > 60, tasks))
return render_template('long.html', tasks=long_tasks)
if __name__ == '__main__':
app.run('localhost', 4449, debug=True)