-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
80 lines (60 loc) · 3.18 KB
/
models.py
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
import uuid
from database import Base
from sqlalchemy import Column, String, Float, Integer, Date, ForeignKey, Enum
from sqlalchemy.orm import relationship
from passlib.context import CryptContext
# Initialize password hashing context
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
class CommunityCentre(Base):
__tablename__ = "community_centres"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
name = Column(String(255), nullable=False)
address = Column(String(255), nullable=False)
latitude = Column(Float, nullable=False)
longitude = Column(Float, nullable=False)
contact = Column(String(20), unique=True, nullable=False)
email = Column(String(100), unique=True, nullable=False) # New Email field
password = Column(String(255), nullable=False) # Hashed Password field
requirements = relationship("Requirement", back_populates="community_centre")
class User(Base):
__tablename__ = "users"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
name = Column(String(100), nullable=False)
address = Column(String(255), nullable=False)
contact = Column(String(20), nullable=False, unique=True)
email = Column(String(100), unique=True, nullable=False)
password = Column(String(255), nullable=False)
token_count = Column(Integer, default=0)
@staticmethod
def hash_password(plain_password: str) -> str:
"""Hashes the password before storing it."""
return pwd_context.hash(plain_password)
def verify_password(self, plain_password: str) -> bool:
"""Verifies the password during login."""
return pwd_context.verify(plain_password, self.password)
# ✅ Add relationship to FoodItem
food_items = relationship("FoodItem", back_populates="user")
class Requirement(Base):
__tablename__ = "requirements"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
community_centre_id = Column(String(36), ForeignKey("community_centres.id"), nullable=False)
servings = Column(Integer, nullable=False)
date = Column(Date, nullable=False)
meal_type = Column(String(50), nullable=False)
status = Column(String(50), nullable=False)
community_centre = relationship("CommunityCentre", back_populates="requirements")
# ✅ Fix relationship name to match `FoodItem`
food_items = relationship("FoodItem", back_populates="requirement")
class FoodItem(Base):
__tablename__ = "food_items"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
image = Column(String, nullable=False)
title = Column(String, nullable=False)
description = Column(String, nullable=False)
servings = Column(Integer, nullable=False)
request_id = Column(String(36), ForeignKey("requirements.id"), nullable=False)
user_id = Column(String(36), ForeignKey("users.id"), nullable=False)
status = Column(Enum("Open", "Approved", "In Transit", "Received", "Not fulfilled"), default="Open")
# ✅ Fix relationship name to match `Requirement`
requirement = relationship("Requirement", back_populates="food_items")
user = relationship("User", back_populates="food_items")