-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
117 lines (91 loc) · 3.38 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
from datetime import UTC, datetime
from bson import ObjectId
from dotenv import load_dotenv
from flask import Flask, flash, jsonify, redirect, render_template, request, url_for
from flask_pymongo import PyMongo
# Load environment variables
load_dotenv()
app = Flask(__name__)
# Configuration
app.config["MONGO_URI"] = os.getenv(
"MONGODB_URI", "mongodb://localhost:27017/mongo-notes-flask-app"
)
app.secret_key = os.getenv("SECRET_KEY", "your-secret-key-here")
# Initialize PyMongo
mongo = PyMongo(app)
# Input validation
def validate_note_input(cwid, full_name, content):
errors = []
if not cwid or not cwid.strip():
errors.append("CWID is required")
if not full_name or not full_name.strip():
errors.append("Full name is required")
if not content or not content.strip():
errors.append("Note content is required")
if not cwid.isdigit():
errors.append("CWID must contain only numbers")
if len(cwid) != 9:
errors.append("CWID must be 9 digits")
return errors
@app.route("/")
def home():
try:
notes = mongo.db.notes.find().sort("created_at", -1)
return render_template("home.html", notes=notes)
except Exception as e:
app.logger.error(f"Error retrieving notes: {str(e)}")
flash("Unable to retrieve notes. Please try again later.", "error")
return render_template("home.html", notes=[])
@app.route("/add", methods=["POST"])
def add_note():
try:
cwid = request.form.get("cwid")
full_name = request.form.get("full_name")
content = request.form.get("content")
# Validate input
errors = validate_note_input(cwid, full_name, content)
if errors:
for error in errors:
flash(error, "error")
return redirect(url_for("home"))
note_data = {
"cwid": cwid,
"full_name": full_name.strip(),
"content": content.strip(),
"created_at": datetime.now(UTC),
"updated_at": datetime.now(UTC),
}
mongo.db.notes.insert_one(note_data)
flash("Note added successfully!", "success")
except Exception as e:
app.logger.error(f"Error adding note: {str(e)}")
flash("Unable to add note. Please try again later.", "error")
return redirect(url_for("home"))
@app.route("/delete/<note_id>")
def delete_note(note_id):
try:
if not ObjectId.is_valid(note_id):
flash("Invalid note ID", "error")
return redirect(url_for("home"))
result = mongo.db.notes.delete_one({"_id": ObjectId(note_id)})
if result.deleted_count > 0:
flash("Note deleted successfully!", "success")
else:
flash("Note not found!", "error")
except Exception as e:
app.logger.error(f"Error deleting note: {str(e)}")
flash("Unable to delete note. Please try again later.", "error")
return redirect(url_for("home"))
# API endpoints for potential future use
@app.route("/api/notes", methods=["GET"])
def get_notes():
try:
notes = list(mongo.db.notes.find().sort("created_at", -1))
for note in notes:
note["_id"] = str(note["_id"])
return jsonify({"notes": notes})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=os.getenv("FLASK_DEBUG", "False").lower() == "true")