Upload files to "/"

This commit is contained in:
s490133 2024-06-21 00:13:27 +02:00
commit 67901d5593
5 changed files with 170 additions and 0 deletions

135
main.py Normal file
View File

@ -0,0 +1,135 @@
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
import datetime
import os
import subprocess
app = Flask(__name__)
# Function to create a new database and table if they don't exist
def initialize_database():
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS tasks
(id INTEGER PRIMARY KEY AUTOINCREMENT,
task TEXT NOT NULL,
due_date TEXT,
completed INTEGER DEFAULT 0)''')
conn.commit()
conn.close()
# Function to add a new task to the database
def add_task(task, due_date=None):
conn = sqlite3.connect('todo.db')
c = conn.cursor()
if due_date is None:
due_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
c.execute("INSERT INTO tasks (task, due_date) VALUES (?, ?)", (task, due_date))
conn.commit()
conn.close()
# Function to delete a task from the database
def delete_task(task_id):
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("DELETE FROM tasks WHERE id=?", (task_id,))
conn.commit()
conn.close()
# Function to view tasks in the database
def view_tasks():
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("SELECT * FROM tasks WHERE completed=0")
tasks = c.fetchall()
conn.close()
return tasks
def view_tasks_complited():
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("SELECT * FROM tasks WHERE completed=1")
tasks = c.fetchall()
conn.close()
return tasks
# Function to update a task's details (task or due date)
def update_task(task_id, task=None, due_date=None):
conn = sqlite3.connect('todo.db')
c = conn.cursor()
if task:
c.execute("UPDATE tasks SET task=? WHERE id=?", (task, task_id))
if due_date:
if due_date.lower() == 'now':
due_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
c.execute("UPDATE tasks SET due_date=? WHERE id=?", (due_date, task_id))
conn.commit()
conn.close()
# Function to mark a task as completed
def complete_task(task_id):
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("UPDATE tasks SET completed=? WHERE id=?", (1, task_id))
conn.commit()
conn.close()
# Initialize the database
initialize_database()
# Route to display tasks
@app.route('/')
def index():
tasks = view_tasks()
completed_tasks = view_tasks_complited()
return render_template('index.html', tasks=tasks, completed_tasks=completed_tasks)
# Route to add new task
@app.route('/add', methods=['POST'])
def add():
task = request.form['task']
due_date = request.form['due_date']
add_task(task, due_date)
return redirect(url_for('index'))
# Route to update task
@app.route('/update/<int:task_id>', methods=['POST'])
def update(task_id):
task = request.form['task']
due_date = request.form['due_date']
update_task(task_id, task, due_date, time_left)
return redirect(url_for('index'))
# Route to delete task
@app.route('/delete/<int:task_id>', methods=['POST'])
def delete(task_id):
delete_task(task_id)
return redirect(url_for('index'))
# Route to mark task as completed
@app.route('/complete/<int:task_id>', methods=['POST'])
def complete(task_id):
complete_task(task_id)
print(view_tasks())
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
Flask==2.0.2
sqlite3

10
run.bat Normal file
View File

@ -0,0 +1,10 @@
@echo off
start http://127.0.0.1:5000
REM
pip install -r requirements.txt
REM
python main.py
REM
pause

3
run.sh Normal file
View File

@ -0,0 +1,3 @@
pip install -r requirements.txt
python main.py
xdg-open http://127.0.0.1:5000

20
time.hs Normal file
View File

@ -0,0 +1,20 @@
import Data.Time
import Data.Time.Format
import Data.Time.Clock
import System.Environment (getArgs)
import Control.Monad (unless)
main :: IO ()
main = do
args <- getArgs
unless (length args == 2) $ putStrLn "Usage: runhaskell time.hs <time1> <time2>" >> return ()
let [arg1, arg2] = args
format = "%Y-%m-%dT%H:%M"
parseTimeOrError' = parseTimeM True defaultTimeLocale format :: String -> Maybe UTCTime
case (parseTimeOrError' arg1, parseTimeOrError' arg2) of
(Just time1, Just time2) -> do
let diff = diffUTCTime time2 time1
putStrLn $ show diff