-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmodels.py
More file actions
117 lines (100 loc) · 4.63 KB
/
models.py
File metadata and controls
117 lines (100 loc) · 4.63 KB
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
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from flask_login import UserMixin
from sqlalchemy.orm import relationship
db = SQLAlchemy()
# FeeRecord Model
class FeeRecord(db.Model):
id = db.Column(db.Integer, primary_key=True)
student_id = db.Column(db.Integer, db.ForeignKey('student.id'), nullable=False)
amount = db.Column(db.Float, nullable=False)
date_paid = db.Column(db.Date, default=datetime.utcnow)
student = relationship('Student', back_populates='fee_records')
# Room Model
class Room(db.Model):
id = db.Column(db.Integer, primary_key=True)
room_number = db.Column(db.Integer, unique=True, nullable=False)
students = relationship('Student', back_populates='room')
# Student Model
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
fee = db.Column(db.Float, nullable=False)
room_id = db.Column(db.Integer, db.ForeignKey('room.id'), nullable=False)
picture = db.Column(db.String(100))
status = db.Column(db.String(20), default='active') # active, inactive, graduated
enrollment_date = db.Column(db.DateTime, default=datetime.utcnow)
last_fee_payment = db.Column(db.DateTime)
# Relationships
room = relationship('Room', back_populates='students')
fee_records = relationship('FeeRecord', back_populates='student')
def __repr__(self):
return f'<Student {self.name}>'
@property
def is_fee_paid(self):
"""Check if the student has paid fees for the current month"""
current_month = datetime.now().month
current_year = datetime.now().year
total_paid = sum(record.amount for record in FeeRecord.query.filter(
FeeRecord.student_id == self.id,
db.extract('month', FeeRecord.date_paid) == current_month,
db.extract('year', FeeRecord.date_paid) == current_year
).all())
return total_paid >= self.fee
@property
def fee_status(self):
"""Get the fee payment status for the current month"""
current_month = datetime.now().month
current_year = datetime.now().year
total_paid = sum(record.amount for record in FeeRecord.query.filter(
FeeRecord.student_id == self.id,
db.extract('month', FeeRecord.date_paid) == current_month,
db.extract('year', FeeRecord.date_paid) == current_year
).all())
if total_paid == 0:
return 'unpaid'
elif total_paid < self.fee:
return 'partial'
else:
return 'paid'
@property
def remaining_fee(self):
"""Calculate remaining fee for the current month"""
current_month = datetime.now().month
current_year = datetime.now().year
total_paid = sum(record.amount for record in FeeRecord.query.filter(
FeeRecord.student_id == self.id,
db.extract('month', FeeRecord.date_paid) == current_month,
db.extract('year', FeeRecord.date_paid) == current_year
).all())
return max(0, self.fee - total_paid)
# Expense Model
class Expense(db.Model):
id = db.Column(db.Integer, primary_key=True)
item_name = db.Column(db.String(100), nullable=False)
price = db.Column(db.Float, nullable=False)
date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) # DateTime for precise date
user_id = db.Column(db.Integer, db.ForeignKey('admin.id'), nullable=False) # Foreign key to Admin
user = relationship('Admin', backref='expenses') # Relationship with Admin model
def __repr__(self):
return f"<Expense {self.item_name} {self.price}>"
# Issue Model
class Issue(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text, nullable=False)
status = db.Column(db.String(20), nullable=False)
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
# Admin Model (UserMixin provides useful methods like is_authenticated)
class Admin(db.Model, UserMixin):
__tablename__ = 'admin'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=True, nullable=False)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), nullable=False, unique=True)
email = db.Column(db.String(150), nullable=False, unique=True)
password = db.Column(db.String(150), nullable=False)