Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,250 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from flask import Flask
|
| 3 |
-
from flask_sqlalchemy import SQLAlchemy
|
| 4 |
-
from werkzeug.security import generate_password_hash, check_password_hash
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
# Initialize Flask app
|
| 8 |
-
app = Flask(__name__)
|
| 9 |
-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///erp_construction.db'
|
| 10 |
-
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
| 11 |
-
db = SQLAlchemy(app)
|
| 12 |
-
|
| 13 |
-
# Define models (User, Role, Project, Inventory, Employee)
|
| 14 |
-
class Role(db.Model):
|
| 15 |
-
id = db.Column(db.Integer, primary_key=True)
|
| 16 |
-
name = db.Column(db.String(50), unique=True, nullable=False)
|
| 17 |
-
|
| 18 |
-
class User(db.Model):
|
| 19 |
-
id = db.Column(db.Integer, primary_key=True)
|
| 20 |
-
username = db.Column(db.String(50), unique=True, nullable=False)
|
| 21 |
-
password_hash = db.Column(db.String(128), nullable=False)
|
| 22 |
-
role_id = db.Column(db.Integer, db.ForeignKey('role.id'), nullable=False)
|
| 23 |
-
role = db.relationship('Role', backref='users')
|
| 24 |
-
password_changed = db.Column(db.Boolean, default=False)
|
| 25 |
-
|
| 26 |
-
def set_password(self, password):
|
| 27 |
-
self.password_hash = generate_password_hash(password)
|
| 28 |
-
|
| 29 |
-
def check_password(self, password):
|
| 30 |
-
return check_password_hash(self.password_hash, password)
|
| 31 |
-
|
| 32 |
-
class Project(db.Model):
|
| 33 |
-
id = db.Column(db.Integer, primary_key=True)
|
| 34 |
-
name = db.Column(db.String(100), nullable=False)
|
| 35 |
-
description = db.Column(db.String(500), nullable=False)
|
| 36 |
-
budget = db.Column(db.Float, nullable=False)
|
| 37 |
-
|
| 38 |
-
class Inventory(db.Model):
|
| 39 |
-
id = db.Column(db.Integer, primary_key=True)
|
| 40 |
-
item_name = db.Column(db.String(100), nullable=False)
|
| 41 |
-
quantity = db.Column(db.Integer, nullable=False)
|
| 42 |
-
unit_price = db.Column(db.Float, nullable=False)
|
| 43 |
-
|
| 44 |
-
class Employee(db.Model):
|
| 45 |
-
id = db.Column(db.Integer, primary_key=True)
|
| 46 |
-
name = db.Column(db.String(100), nullable=False)
|
| 47 |
-
role = db.Column(db.String(100), nullable=False)
|
| 48 |
-
salary = db.Column(db.Float, nullable=False)
|
| 49 |
-
|
| 50 |
-
# Initialize the database
|
| 51 |
-
def init_db():
|
| 52 |
-
with app.app_context():
|
| 53 |
-
db.create_all()
|
| 54 |
-
# Add default roles if they don't exist
|
| 55 |
-
if not Role.query.filter_by(name='Admin').first():
|
| 56 |
-
admin_role = Role(name='Admin')
|
| 57 |
-
db.session.add(admin_role)
|
| 58 |
-
if not Role.query.filter_by(name='Materials Manager').first():
|
| 59 |
-
materials_role = Role(name='Materials Manager')
|
| 60 |
-
db.session.add(materials_role)
|
| 61 |
-
if not Role.query.filter_by(name='Procurement Manager').first():
|
| 62 |
-
procurement_role = Role(name='Procurement Manager')
|
| 63 |
-
db.session.add(procurement_role)
|
| 64 |
-
if not Role.query.filter_by(name='HR Manager').first():
|
| 65 |
-
hr_role = Role(name='HR Manager')
|
| 66 |
-
db.session.add(hr_role)
|
| 67 |
-
db.session.commit()
|
| 68 |
-
|
| 69 |
-
# Add default admin user if it doesn't exist
|
| 70 |
-
if not User.query.filter_by(username='Admin').first():
|
| 71 |
-
admin_role = Role.query.filter_by(name='Admin').first()
|
| 72 |
-
admin_user = User(username='Admin', role_id=admin_role.id)
|
| 73 |
-
admin_user.set_password('Admin') # Set default password
|
| 74 |
-
db.session.add(admin_user)
|
| 75 |
-
db.session.commit()
|
| 76 |
-
|
| 77 |
-
# Login Page
|
| 78 |
-
def login():
|
| 79 |
-
st.title("Login")
|
| 80 |
-
username = st.text_input("Username", key="login_username")
|
| 81 |
-
password = st.text_input("Password", type="password", key="login_password")
|
| 82 |
-
if st.button("Login", key="login_button"):
|
| 83 |
-
with app.app_context():
|
| 84 |
-
user = User.query.filter_by(username=username).first()
|
| 85 |
-
if user and user.check_password(password):
|
| 86 |
-
st.session_state['user_id'] = user.id
|
| 87 |
-
st.session_state['role'] = user.role.name
|
| 88 |
-
st.success("Logged in successfully!")
|
| 89 |
-
if user.role.name != 'Admin' and not user.password_changed:
|
| 90 |
-
st.session_state['page'] = 'Change Password'
|
| 91 |
-
else:
|
| 92 |
-
st.session_state['page'] = 'Home'
|
| 93 |
-
else:
|
| 94 |
-
st.error("Invalid username or password.")
|
| 95 |
-
|
| 96 |
-
# Home Page
|
| 97 |
-
def home():
|
| 98 |
-
st.title("Home")
|
| 99 |
-
st.write("Welcome to the Construction ERP System!")
|
| 100 |
-
st.write("Use the sidebar to navigate to different sections.")
|
| 101 |
-
|
| 102 |
-
# Projects Page (Admin Only)
|
| 103 |
-
def projects():
|
| 104 |
-
st.title("Projects Management")
|
| 105 |
-
if st.session_state['role'] != 'Admin':
|
| 106 |
-
st.error("You do not have permission to access this page.")
|
| 107 |
-
return
|
| 108 |
-
|
| 109 |
-
name = st.text_input("Project Name", key="project_name")
|
| 110 |
-
description = st.text_area("Description", key="project_description")
|
| 111 |
-
budget = st.number_input("Budget", step=0.01, key="project_budget")
|
| 112 |
-
if st.button("Add Project", key="add_project_button"):
|
| 113 |
-
with app.app_context():
|
| 114 |
-
new_project = Project(name=name, description=description, budget=budget)
|
| 115 |
-
db.session.add(new_project)
|
| 116 |
-
db.session.commit()
|
| 117 |
-
st.success("Project added successfully!")
|
| 118 |
-
|
| 119 |
-
with app.app_context():
|
| 120 |
-
projects = Project.query.all()
|
| 121 |
-
st.write("### Project List")
|
| 122 |
-
for project in projects:
|
| 123 |
-
st.write(f"**Name:** {project.name}, **Description:** {project.description}, **Budget:** ${project.budget}")
|
| 124 |
-
|
| 125 |
-
# Inventory Page (Materials Manager Only)
|
| 126 |
-
def inventory():
|
| 127 |
-
st.title("Inventory Management")
|
| 128 |
-
if st.session_state['role'] != 'Materials Manager':
|
| 129 |
-
st.error("You do not have permission to access this page.")
|
| 130 |
-
return
|
| 131 |
-
|
| 132 |
-
item_name = st.text_input("Item Name", key="item_name")
|
| 133 |
-
quantity = st.number_input("Quantity", step=1, key="item_quantity")
|
| 134 |
-
unit_price = st.number_input("Unit Price", step=0.01, key="item_unit_price")
|
| 135 |
-
if st.button("Add Item", key="add_item_button"):
|
| 136 |
-
with app.app_context():
|
| 137 |
-
new_item = Inventory(item_name=item_name, quantity=quantity, unit_price=unit_price)
|
| 138 |
-
db.session.add(new_item)
|
| 139 |
-
db.session.commit()
|
| 140 |
-
st.success("Inventory item added successfully!")
|
| 141 |
-
|
| 142 |
-
with app.app_context():
|
| 143 |
-
inventory_items = Inventory.query.all()
|
| 144 |
-
st.write("### Inventory List")
|
| 145 |
-
for item in inventory_items:
|
| 146 |
-
st.write(f"**Item Name:** {item.item_name}, **Quantity:** {item.quantity}, **Unit Price:** ${item.unit_price}")
|
| 147 |
-
|
| 148 |
-
# Employees Page (Admin Only)
|
| 149 |
-
def employees():
|
| 150 |
-
st.title("Employee Management")
|
| 151 |
-
if st.session_state['role'] != 'Admin':
|
| 152 |
-
st.error("You do not have permission to access this page.")
|
| 153 |
-
return
|
| 154 |
-
|
| 155 |
-
name = st.text_input("Employee Name", key="employee_name")
|
| 156 |
-
role = st.selectbox("Role", ["Materials Manager", "Procurement Manager", "HR Manager"], key="employee_role")
|
| 157 |
-
salary = st.number_input("Salary", step=0.01, key="employee_salary")
|
| 158 |
-
username = st.text_input("Username", key="employee_username")
|
| 159 |
-
password = st.text_input("Password", type="password", key="employee_password")
|
| 160 |
-
if st.button("Add Employee", key="add_employee_button"):
|
| 161 |
-
with app.app_context():
|
| 162 |
-
new_employee = Employee(name=name, role=role, salary=salary)
|
| 163 |
-
db.session.add(new_employee)
|
| 164 |
-
|
| 165 |
-
employee_role = Role.query.filter_by(name=role).first()
|
| 166 |
-
if not employee_role:
|
| 167 |
-
st.error(f'Role "{role}" does not exist.')
|
| 168 |
-
return
|
| 169 |
-
|
| 170 |
-
new_user = User(username=username, role_id=employee_role.id)
|
| 171 |
-
new_user.set_password(password) # Set initial password
|
| 172 |
-
db.session.add(new_user)
|
| 173 |
-
|
| 174 |
-
db.session.commit()
|
| 175 |
-
st.success("Employee added successfully!")
|
| 176 |
-
|
| 177 |
-
with app.app_context():
|
| 178 |
-
employees = Employee.query.all()
|
| 179 |
-
st.write("### Employee List")
|
| 180 |
-
for employee in employees:
|
| 181 |
-
st.write(f"**Name:** {employee.name}, **Role:** {employee.role}, **Salary:** ${employee.salary}")
|
| 182 |
-
|
| 183 |
-
# Change Password Page
|
| 184 |
-
def change_password():
|
| 185 |
-
st.title("Change Password")
|
| 186 |
-
new_password = st.text_input("New Password", type="password", key="new_password")
|
| 187 |
-
confirm_password = st.text_input("Confirm Password", type="password", key="confirm_password")
|
| 188 |
-
if st.button("Change Password", key="change_password_button"):
|
| 189 |
-
if new_password != confirm_password:
|
| 190 |
-
st.error("Passwords do not match.")
|
| 191 |
-
return
|
| 192 |
-
|
| 193 |
-
with app.app_context():
|
| 194 |
-
user = User.query.get(st.session_state['user_id'])
|
| 195 |
-
user.set_password(new_password)
|
| 196 |
-
user.password_changed = True # Mark password as changed
|
| 197 |
-
db.session.commit()
|
| 198 |
-
|
| 199 |
-
st.success("Password changed successfully!")
|
| 200 |
-
st.session_state['page'] = 'Home'
|
| 201 |
-
|
| 202 |
-
# Main App
|
| 203 |
-
def main():
|
| 204 |
-
if 'user_id' not in st.session_state:
|
| 205 |
-
st.session_state['user_id'] = None
|
| 206 |
-
if 'role' not in st.session_state:
|
| 207 |
-
st.session_state['role'] = None
|
| 208 |
-
if 'page' not in st.session_state:
|
| 209 |
-
st.session_state['page'] = 'Login'
|
| 210 |
-
|
| 211 |
-
# Initialize the database
|
| 212 |
-
init_db()
|
| 213 |
-
|
| 214 |
-
# Navigation
|
| 215 |
-
if st.session_state['page'] == 'Login':
|
| 216 |
-
login()
|
| 217 |
-
elif st.session_state['page'] == 'Home':
|
| 218 |
-
home()
|
| 219 |
-
elif st.session_state['page'] == 'Projects':
|
| 220 |
-
projects()
|
| 221 |
-
elif st.session_state['page'] == 'Inventory':
|
| 222 |
-
inventory()
|
| 223 |
-
elif st.session_state['page'] == 'Employees':
|
| 224 |
-
employees()
|
| 225 |
-
elif st.session_state['page'] == 'Change Password':
|
| 226 |
-
change_password()
|
| 227 |
-
|
| 228 |
-
# Sidebar Navigation
|
| 229 |
-
if st.session_state['user_id']:
|
| 230 |
-
st.sidebar.title("Navigation")
|
| 231 |
-
if st.sidebar.button("Home", key="sidebar_home"):
|
| 232 |
-
st.session_state['page'] = 'Home'
|
| 233 |
-
if st.session_state['role'] == 'Admin':
|
| 234 |
-
if st.sidebar.button("Projects", key="sidebar_projects"):
|
| 235 |
-
st.session_state['page'] = 'Projects'
|
| 236 |
-
if st.sidebar.button("Employees", key="sidebar_employees"):
|
| 237 |
-
st.session_state['page'] = 'Employees'
|
| 238 |
-
if st.session_state['role'] == 'Materials Manager':
|
| 239 |
-
if st.sidebar.button("Inventory", key="sidebar_inventory"):
|
| 240 |
-
st.session_state['page'] = 'Inventory'
|
| 241 |
-
if st.sidebar.button("Change Password", key="sidebar_change_password"):
|
| 242 |
-
st.session_state['page'] = 'Change Password'
|
| 243 |
-
if st.sidebar.button("Logout", key="sidebar_logout"):
|
| 244 |
-
st.session_state['user_id'] = None
|
| 245 |
-
st.session_state['role'] = None
|
| 246 |
-
st.session_state['page'] = 'Login'
|
| 247 |
-
st.experimental_rerun()
|
| 248 |
-
|
| 249 |
-
if __name__ == '__main__':
|
| 250 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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