Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from flask import Flask, render_template, redirect, url_for, flash, request, session
|
| 2 |
from flask_sqlalchemy import SQLAlchemy
|
| 3 |
from werkzeug.security import generate_password_hash, check_password_hash
|
|
@@ -76,139 +77,28 @@ with app.app_context():
|
|
| 76 |
db.session.add(admin_user)
|
| 77 |
db.session.commit()
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
def
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
# Login Route
|
| 96 |
-
@app.route('/login', methods=['GET', 'POST'])
|
| 97 |
-
def login():
|
| 98 |
-
if request.method == 'POST':
|
| 99 |
-
username = request.form['username']
|
| 100 |
-
password = request.form['password']
|
| 101 |
-
user = User.query.filter_by(username=username).first()
|
| 102 |
-
if user and user.check_password(password):
|
| 103 |
-
session['user_id'] = user.id
|
| 104 |
-
# Redirect to password change page if it's the employee's first login
|
| 105 |
-
if user.role.name != 'Admin' and not user.password_changed:
|
| 106 |
-
return redirect(url_for('change_password'))
|
| 107 |
-
flash('Logged in successfully!', 'success')
|
| 108 |
-
return redirect(url_for('index'))
|
| 109 |
-
else:
|
| 110 |
-
flash('Invalid username or password.', 'danger')
|
| 111 |
-
return render_template('login.html')
|
| 112 |
-
|
| 113 |
-
# Logout Route
|
| 114 |
-
@app.route('/logout')
|
| 115 |
-
def logout():
|
| 116 |
-
session.pop('user_id', None)
|
| 117 |
-
flash('Logged out successfully!', 'success')
|
| 118 |
-
return redirect(url_for('index'))
|
| 119 |
-
|
| 120 |
-
# Home Page (Protected)
|
| 121 |
-
@app.route('/')
|
| 122 |
-
@login_required()
|
| 123 |
-
def index():
|
| 124 |
-
return render_template('index.html')
|
| 125 |
-
|
| 126 |
-
# Project Management (Admin Only)
|
| 127 |
-
@app.route('/projects', methods=['GET', 'POST'])
|
| 128 |
-
@login_required(role='Admin')
|
| 129 |
-
def projects():
|
| 130 |
-
if request.method == 'POST':
|
| 131 |
-
name = request.form['name']
|
| 132 |
-
description = request.form['description']
|
| 133 |
-
budget = request.form['budget']
|
| 134 |
-
new_project = Project(name=name, description=description, budget=budget)
|
| 135 |
-
db.session.add(new_project)
|
| 136 |
-
db.session.commit()
|
| 137 |
-
flash('Project added successfully!', 'success')
|
| 138 |
-
return redirect(url_for('projects'))
|
| 139 |
-
projects = Project.query.all()
|
| 140 |
-
return render_template('projects.html', projects=projects)
|
| 141 |
-
|
| 142 |
-
# Inventory Management (Materials Manager Only)
|
| 143 |
-
@app.route('/inventory', methods=['GET', 'POST'])
|
| 144 |
-
@login_required(role='Materials Manager')
|
| 145 |
-
def inventory():
|
| 146 |
-
if request.method == 'POST':
|
| 147 |
-
item_name = request.form['item_name']
|
| 148 |
-
quantity = request.form['quantity']
|
| 149 |
-
unit_price = request.form['unit_price']
|
| 150 |
-
new_item = Inventory(item_name=item_name, quantity=quantity, unit_price=unit_price)
|
| 151 |
-
db.session.add(new_item)
|
| 152 |
-
db.session.commit()
|
| 153 |
-
flash('Inventory item added successfully!', 'success')
|
| 154 |
-
return redirect(url_for('inventory'))
|
| 155 |
-
inventory_items = Inventory.query.all()
|
| 156 |
-
return render_template('inventory.html', inventory_items=inventory_items)
|
| 157 |
-
|
| 158 |
-
# Employee Management (Admin Only)
|
| 159 |
-
@app.route('/employees', methods=['GET', 'POST'])
|
| 160 |
-
@login_required(role='Admin')
|
| 161 |
-
def employees():
|
| 162 |
-
if request.method == 'POST':
|
| 163 |
-
name = request.form['name']
|
| 164 |
-
role = request.form['role']
|
| 165 |
-
salary = request.form['salary']
|
| 166 |
-
username = request.form['username']
|
| 167 |
-
password = request.form['password']
|
| 168 |
-
|
| 169 |
-
# Create a new employee
|
| 170 |
-
new_employee = Employee(name=name, role=role, salary=salary)
|
| 171 |
-
db.session.add(new_employee)
|
| 172 |
-
|
| 173 |
-
# Create a new user for the employee
|
| 174 |
-
employee_role = Role.query.filter_by(name=role).first()
|
| 175 |
-
if not employee_role:
|
| 176 |
-
flash(f'Role "{role}" does not exist.', 'danger')
|
| 177 |
-
return redirect(url_for('employees'))
|
| 178 |
-
|
| 179 |
-
new_user = User(username=username, role_id=employee_role.id)
|
| 180 |
-
new_user.set_password(password) # Set initial password
|
| 181 |
-
db.session.add(new_user)
|
| 182 |
-
|
| 183 |
-
db.session.commit()
|
| 184 |
-
flash('Employee added successfully!', 'success')
|
| 185 |
-
return redirect(url_for('employees'))
|
| 186 |
-
|
| 187 |
-
employees = Employee.query.all()
|
| 188 |
-
return render_template('employees.html', employees=employees)
|
| 189 |
-
|
| 190 |
-
# Password Change Route (For Employees)
|
| 191 |
-
@app.route('/change_password', methods=['GET', 'POST'])
|
| 192 |
-
@login_required()
|
| 193 |
-
def change_password():
|
| 194 |
-
if request.method == 'POST':
|
| 195 |
-
new_password = request.form['new_password']
|
| 196 |
-
confirm_password = request.form['confirm_password']
|
| 197 |
-
|
| 198 |
-
if new_password != confirm_password:
|
| 199 |
-
flash('Passwords do not match.', 'danger')
|
| 200 |
-
return redirect(url_for('change_password'))
|
| 201 |
-
|
| 202 |
-
user = User.query.get(session['user_id'])
|
| 203 |
-
user.set_password(new_password)
|
| 204 |
-
user.password_changed = True # Mark password as changed
|
| 205 |
-
db.session.commit()
|
| 206 |
-
|
| 207 |
-
flash('Password changed successfully!', 'success')
|
| 208 |
-
return redirect(url_for('index'))
|
| 209 |
-
|
| 210 |
-
return render_template('change_password.html')
|
| 211 |
-
|
| 212 |
-
# Run the application
|
| 213 |
if __name__ == '__main__':
|
| 214 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from flask import Flask, render_template, redirect, url_for, flash, request, session
|
| 3 |
from flask_sqlalchemy import SQLAlchemy
|
| 4 |
from werkzeug.security import generate_password_hash, check_password_hash
|
|
|
|
| 77 |
db.session.add(admin_user)
|
| 78 |
db.session.commit()
|
| 79 |
|
| 80 |
+
# Streamlit App
|
| 81 |
+
def main():
|
| 82 |
+
st.title("Construction ERP System")
|
| 83 |
+
st.sidebar.title("Navigation")
|
| 84 |
+
|
| 85 |
+
# Navigation
|
| 86 |
+
page = st.sidebar.radio("Go to", ["Home", "Projects", "Inventory", "Employees", "Change Password"])
|
| 87 |
+
|
| 88 |
+
if page == "Home":
|
| 89 |
+
st.write("Welcome to the Construction ERP System!")
|
| 90 |
+
elif page == "Projects":
|
| 91 |
+
st.write("Projects Management")
|
| 92 |
+
# Add Streamlit components for projects
|
| 93 |
+
elif page == "Inventory":
|
| 94 |
+
st.write("Inventory Management")
|
| 95 |
+
# Add Streamlit components for inventory
|
| 96 |
+
elif page == "Employees":
|
| 97 |
+
st.write("Employee Management")
|
| 98 |
+
# Add Streamlit components for employees
|
| 99 |
+
elif page == "Change Password":
|
| 100 |
+
st.write("Change Password")
|
| 101 |
+
# Add Streamlit components for changing password
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
if __name__ == '__main__':
|
| 104 |
+
main()
|