- from flask import Flask, jsonify, request, render_template, redirect
- import smtplib, ssl
- from flask_sqlalchemy import SQLAlchemy
- from models import Task
- from models.Task import app, db
- """shows all tasks from db"""
- @app.route("/")
- def get_all(methods=["GET"]):
- tasks = Task.query.all()
- return render_template('index.html', tasks=tasks)
- """insert task"""
- @app.route("/add", methods=["POST"])
- def add():
- title = request.form.get('title')
- new_task = Task(title=title)
- db.create_all()
- db.session.add(new_task)
- db.session.commit()
- return jsonify({'message': 'task added'})
- """marks task as completed"""
- @app.route("/update/<int:id>")
- def update(id):
- task = Task.query.get(id)
- task.complete = not task.complete
- db.session.commit()
- return jsonify({'message': 'task completed'})
- """finds a task by id"""
- @app.route("/<int:id>")
- def find(id):
- task = Task.query.get(id)
- if task:
- return render_template('task_detail.html', task=task)
- else:
- return f"Task with ID {id} not found.", 404
- """delete task"""
- @app.route("/delete/<int:id>")
- def delete(id, methods=["DELETE"]):
- task = Task.query.get(id)
- db.session.delete(task)
- db.session.commit()
- return jsonify({'message': 'task deleted'})
- """send mail"""
- @app.route("/send-email/<int:id>")
- def send_email(id, methods=["GET"]):
- context = ssl.create_default_context()
- try:
- server = smtplib.SMTP(smtp_server,port)
- server.ehlo()
- server.starttls(context=context)
- server.ehlo()
- server.login(sender_email, password)
- # TODO: Send email here
- server.sendmail(sender_email, '[email protected]', 'test')
- return jsonify({'message': 'email sent'})
- except Exception as e:
- return jsonify({'error': str(e)}), 500
- finally:
- server.quit()
- if __name__ == '__main__':
- app.run(host='localhost', port=5001)
[text] task managment main
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- task managment main
- Paste Code
- 17 Apr-2024
- 1.96 Kb
You can Share it:
Latest Code Pastes