Project Documentation
Project Documentation
FOR
INDUSTRIAL TRAINING PROJECT
AppNote is a Flask web application designed to help users manage their notes. It
allows users to create, view, and delete notes with a user-friendly interface.
Features
1. User Authentication:
Users can sign up for an account with a unique email and password.
Existing users can log in to their accounts.
2. Note Management:
Authenticated users can create new notes with a simple text interface.
Existing notes are displayed on the home page.
Users can delete notes they no longer need.
Technologies Used
AppNote/
|-- website/
| |-- __init__.py
| |-- auth.py
| |-- models.py
| |-- views.py
| |-- static/
| | |-- styles.css
| | |-- script.js
| |-- templates/
| |-- base.html
| |-- home.html
| |-- login.html
| |-- sign_up.html
|-- venv/ (Virtual Environment)
|-- main.py
|-- config.py
|-- .gitignore
Go to http://127.0.0.1:5000
main.py
from website import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
{% block javascript %}
<script type="text/javascript">
function deleteNote(noteId) {
fetch("/delete-note", {
method: "POST",
body: JSON.stringify({ noteId: noteId }),
}).then((_res) => {
window.location.href = "/";
});
}
</script>
{% endblock %}
</body>
</html>
Home.html: Displays the user's notes, provides a form for adding new notes, and includes
JavaScript for deleting notes asynchronously.
{% extends "base.html" %}
{% block title %}
Home
{% endblock %}
{% block content %}
<h1 align="center">Notes</h1>
<ul class="list-group list-group-flush" id="notes">
{% for note in user.notes %}
<li class="list-group-item">
{{ note.data }}
<button type="button" class="close" onclick="deleteNote('{{
note.id }}')">
<span aria-hidden="true">×</span>
</button>
</li>
{% endfor %}
</ul>
<script>
function deleteNote(noteId) {
fetch("/delete-note", {
method: "POST",
body: JSON.stringify({ noteId: noteId }),
}).then((_res) => {
window.location.href = "/";
});
}
</script>
{% endblock %}
__init__.py
db = SQLAlchemy()
DB_NAME = "database.db"
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hjshjhdjah kjshkjdhjs'
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
db.init_app(app)
app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/')
with app.app_context():
db.create_all()
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
return app
def create_database(app):
if not path.exists('website/' + DB_NAME):
db.create_all(app=app)
print('Created Database!')
auth.py
user = User.query.filter_by(email=email).first()
if user:
if check_password_hash(user.password, password):
flash('Logged in successfully!', category='success')
login_user(user, remember=True)
return redirect(url_for('views.home'))
else:
flash('Incorrect password, try again.', category='error')
else:
flash('Email does not exist.', category='error')
@auth.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('auth.login'))
@auth.route('/sign-up', methods=['GET', 'POST'])
def sign_up():
if request.method == 'POST':
email = request.form.get('email')
first_name = request.form.get('firstName')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
user = User.query.filter_by(email=email).first()
if user:
flash('Email already exists.', category='error')
elif len(email) < 4:
flash('Email must be greater than 3 characters.',
category='error')
elif len(first_name) < 2:
flash('First name must be greater than 1 character.',
category='error')
elif password1 != password2:
flash('Passwords don\'t match.', category='error')
elif len(password1) < 7:
flash('Password must be at least 7 characters.', category='error')
else:
new_user = User(email=email,
first_name=first_name,password=generate_password_hash(password1,
method='pbkdf2:sha256'))
db.session.add(new_user)
db.session.commit()
login_user(new_user, remember=True)
flash('Account created!', category='success')
return redirect(url_for('views.home'))
models.py
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
class Note(db.Model):
id = db.Column(db.Integer, primary_key=True)
data = db.Column(db.String(10000))
date = db.Column(db.DateTime(timezone=True), default=func.now())
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
views.py
# views.py
@views.route('/add-note', methods=['POST'])
def add_note():
# Add logic to handle adding a note
pass
@views.route('/delete-note', methods=['POST'])
def delete_note():
note = json.loads(request.data)
noteId = note['noteId']
note = Note.query.get(noteId)
if note:
if note.user_id == current_user.id:
db.session.delete(note)
db.session.commit()
return jsonify({})