syaikhipin commited on
Commit
0b6522d
·
verified ·
1 Parent(s): 4f8f8d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sqlalchemy import create_engine, Column, Integer, String, JSON, DateTime, ForeignKey, text
2
+ from sqlalchemy.ext.declarative import declarative_base
3
+ from sqlalchemy.orm import sessionmaker, relationship
4
+ from datetime import datetime
5
+ import os
6
+ from dotenv import load_dotenv
7
+ from werkzeug.security import generate_password_hash, check_password_hash
8
+
9
+ load_dotenv()
10
+
11
+ DB_URL = os.getenv('DATABASE_URL', 'postgresql://postgres:postgres@localhost:5432/agridata')
12
+
13
+ engine = create_engine(DB_URL)
14
+ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
15
+ Base = declarative_base()
16
+
17
+ class User(Base):
18
+ __tablename__ = 'users'
19
+
20
+ id = Column(Integer, primary_key=True)
21
+ email = Column(String, unique=True, nullable=False)
22
+ password_hash = Column(String, nullable=False)
23
+ role = Column(String, nullable=False, default='user')
24
+ lands = relationship('Land', back_populates='user')
25
+
26
+ def set_password(self, password):
27
+ self.password_hash = generate_password_hash(password, method='pbkdf2:sha256')
28
+
29
+ def check_password(self, password):
30
+ return check_password_hash(self.password_hash, password)
31
+
32
+ class Land(Base):
33
+ __tablename__ = 'lands'
34
+
35
+ id = Column(Integer, primary_key=True)
36
+ user_id = Column(Integer, ForeignKey('users.id'))
37
+ name = Column(String, nullable=False)
38
+ coordinates = Column(JSON)
39
+ soil_type = Column(String)
40
+ area = Column(Integer)
41
+ user = relationship('User', back_populates='lands')
42
+ recommendations = relationship('Recommendation', back_populates='land')
43
+
44
+ class Recommendation(Base):
45
+ __tablename__ = 'recommendations'
46
+
47
+ id = Column(Integer, primary_key=True)
48
+ land_id = Column(Integer, ForeignKey('lands.id'))
49
+ data = Column(JSON)
50
+ created_at = Column(DateTime, default=datetime.utcnow)
51
+ land = relationship('Land', back_populates='recommendations')
52
+
53
+ def init_database():
54
+ Base.metadata.create_all(bind=engine)
55
+
56
+ def get_user(email):
57
+ db = SessionLocal()
58
+ try:
59
+ return db.query(User).filter(User.email == email).first()
60
+ finally:
61
+ db.close()
62
+
63
+ def create_user(email, password, role='user'):
64
+ db = SessionLocal()
65
+ try:
66
+ user = User(email=email, role=role)
67
+ user.set_password(password)
68
+ db.add(user)
69
+ db.commit()
70
+ db.refresh(user)
71
+ return user
72
+ finally:
73
+ db.close()
74
+
75
+ def sign_in(email, password):
76
+ db = SessionLocal()
77
+ try:
78
+ user = db.query(User).filter(User.email == email).first()
79
+ if user and user.check_password(password):
80
+ return user
81
+ return None
82
+ finally:
83
+ db.close()
84
+
85
+ def save_land(user_id, name, coordinates, soil_type, area):
86
+ db = SessionLocal()
87
+ try:
88
+ land = Land(
89
+ user_id=user_id,
90
+ name=name,
91
+ coordinates=coordinates,
92
+ soil_type=soil_type,
93
+ area=area
94
+ )
95
+ db.add(land)
96
+ db.commit()
97
+ db.refresh(land)
98
+ return land
99
+ finally:
100
+ db.close()
101
+
102
+ def get_user_lands(user_id):
103
+ db = SessionLocal()
104
+ try:
105
+ return db.query(Land).filter(Land.user_id == user_id).all()
106
+ finally:
107
+ db.close()
108
+
109
+ def save_recommendation(land_id, recommendation_data):
110
+ db = SessionLocal()
111
+ try:
112
+ recommendation = Recommendation(
113
+ land_id=land_id,
114
+ data=recommendation_data,
115
+ created_at=datetime.utcnow()
116
+ )
117
+ db.add(recommendation)
118
+ db.commit()
119
+ db.refresh(recommendation)
120
+ return recommendation
121
+ finally:
122
+ db.close()
123
+
124
+ def get_land_recommendations(land_id):
125
+ db = SessionLocal()
126
+ try:
127
+ return db.query(Recommendation)\
128
+ .filter(Recommendation.land_id == land_id)\
129
+ .order_by(Recommendation.created_at.desc())\
130
+ .all()
131
+ finally:
132
+ db.close()