db = SQLAlchemy()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///inventory.db'
db.init_app(app)
return app
A Saefullah <asaefullah303@gmail.com>
12.54 (0 menit yang lalu)
kepada saya
from flask import Blueprint, render_template, redirect, url_for, request, flash
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import login_user, logout_user, login_required
from models import User
from database import db
auth = Blueprint('auth', __name__)
@auth.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password, password):
login_user(user)
return redirect(url_for('main.index'))
else:
flash('Login Unsuccessful. Please check username and password', 'danger')
return render_template('login.html')
@auth.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
hashed_password = generate_password_hash(password, method='sha256')
new_user = User(username=username, password=hashed_password)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('auth.login'))
return render_template('register.html')
@auth.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('auth.login'))
from flask import Blueprint, render_template, request, redirect, url_for
from flask_login import login_required
from models import Item
from database import db
inventory = Blueprint('inventory', __name__)
@inventory.route('/')
@login_required
def index():
return render_template('index.html')
@inventory.route('/add', methods=['GET', 'POST'])
@login_required
def add_item():
if request.method == 'POST':
name = request.form['name']
quantity = request.form['quantity']
description = request.form['description']
new_item = Item(name=name, quantity=quantity, description=description)
db.session.add(new_item)
db.session.commit()
return redirect(url_for('inventory.view_items'))
return render_template('add_item.html')
@inventory.route('/view')
@login_required
def view_items():
items = Item.query.all()
return render_template('view_items.html', items=items)
@inventory.route('/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_item(id):
item = Item.query.get_or_404(id)
if request.method == 'POST':
item.name = request.form['name']
item.quantity = request.form['quantity']
item.description = request.form['description']
db.session.commit()
return redirect(url_for('inventory.view_items'))
return render_template('edit_item.html', item=item)
@inventory.route('/delete/<int:id>')
@login_required
def delete_item(id):
item = Item.query.get_or_404(id)
db.session.delete(item)
db.session.commit()
return redirect(url_for('inventory.view_items'))
from flask import Flask
from flask_login import LoginManager
from database import create_app, db
from models import User
from auth import auth as auth_blueprint
from inventory import inventory as inventory_blueprint
app = create_app()
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
app.register_blueprint(auth_blueprint)
app.register_blueprint(inventory_blueprint)
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)
<!doctype html>
<html>
<head>
<title>Inventory System</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<nav>
<a href="{{ url_for('inventory.index') }}">Home</a>
{% if current_user.is_authenticated %}
<a href="{{ url_for('auth.logout') }}">Logout</a>
{% else %}
<a href="{{ url_for('auth.login') }}">Login</a>
<a href="{{ url_for('auth.register') }}">Register</a>
{% endif %}
</nav>
<div class="container">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="flash {{ category }}">{{ message }}</div>
{% endfor %}
{% endif %}
{% endwith %}
{% block content %}{% endblock %}
</div>
</body>
</html>
{% extends "base.html" %}
{% block content %}
<h1>Welcome to Inventory System</h1>
<a href="{{ url_for('inventory.add_item') }}">Add Item</a>
<a href="{{ url_for('inventory.view_items') }}">View Items</a>
{% endblock %}