-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
260 lines (208 loc) · 8.56 KB
/
app.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from flask import Flask,render_template,request,session,redirect,url_for,flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from werkzeug.security import generate_password_hash,check_password_hash
from flask_login import login_user,logout_user,login_manager,LoginManager
from flask_login import login_required,current_user
import os
# MY db connection
local_server= True
app = Flask(__name__)
app.secret_key='sujitnirmal'
# app.secret_key = os.environ.get('SECRET_KEY')
# this is for getting unique user access
login_manager=LoginManager(app)
login_manager.login_view='login'
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
# app.config['SQLALCHEMY_DATABASE_URL']='mysql://username:password@localhost/databas_table_name'
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:@localhost/farmers'
db=SQLAlchemy(app)
# app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("DATABASE_URL")
# db = SQLAlchemy(app)
# here we will create db models that is tables
class Test(db.Model):
id=db.Column(db.Integer,primary_key=True)
name=db.Column(db.String(100))
class Farming(db.Model):
fid=db.Column(db.Integer,primary_key=True)
farmingtype=db.Column(db.String(100))
class Addagroproducts(db.Model):
username=db.Column(db.String(50))
email=db.Column(db.String(50))
pid=db.Column(db.Integer,primary_key=True)
productname=db.Column(db.String(100))
productdesc=db.Column(db.String(300))
price=db.Column(db.Integer)
class Trig(db.Model):
id=db.Column(db.Integer,primary_key=True)
fid=db.Column(db.String(100))
action=db.Column(db.String(100))
timestamp=db.Column(db.String(100))
class User(UserMixin,db.Model):
id=db.Column(db.Integer,primary_key=True)
username=db.Column(db.String(50))
email=db.Column(db.String(50),unique=True)
password=db.Column(db.String(1000))
class Register(db.Model):
rid=db.Column(db.Integer,primary_key=True)
farmername=db.Column(db.String(50))
adharnumber=db.Column(db.String(50))
age=db.Column(db.Integer)
gender=db.Column(db.String(50))
phonenumber=db.Column(db.String(50))
address=db.Column(db.String(50))
farming=db.Column(db.String(50))
@app.route('/')
def index():
return render_template('index.html')
@app.route('/farmerdetails')
@login_required
def farmerdetails():
# query=db.engine.execute(f"SELECT * FROM `register`")
query=Register.query.all()
return render_template('farmerdetails.html',query=query)
@app.route('/agroproducts')
def agroproducts():
# query=db.engine.execute(f"SELECT * FROM `addagroproducts`")
query=Addagroproducts.query.all()
return render_template('agroproducts.html',query=query)
@app.route('/addagroproduct',methods=['POST','GET'])
@login_required
def addagroproduct():
if request.method=="POST":
username=request.form.get('username')
email=request.form.get('email')
productname=request.form.get('productname')
productdesc=request.form.get('productdesc')
price=request.form.get('price')
products=Addagroproducts(username=username,email=email,productname=productname,productdesc=productdesc,price=price)
db.session.add(products)
db.session.commit()
flash("Product Added","info")
return redirect('/agroproducts')
return render_template('addagroproducts.html')
@app.route('/triggers')
@login_required
def triggers():
# query=db.engine.execute(f"SELECT * FROM `trig`")
query=Trig.query.all()
return render_template('triggers.html',query=query)
@app.route('/addfarming',methods=['POST','GET'])
@login_required
def addfarming():
if request.method=="POST":
farmingtype=request.form.get('farming')
query=Farming.query.filter_by(farmingtype=farmingtype).first()
if query:
flash("Farming Type Already Exist","warning")
return redirect('/addfarming')
dep=Farming(farmingtype=farmingtype)
db.session.add(dep)
db.session.commit()
flash("Farming Addes","success")
return render_template('farming.html')
@app.route("/delete/<string:rid>",methods=['POST','GET'])
@login_required
def delete(rid):
# db.engine.execute(f"DELETE FROM `register` WHERE `register`.`rid`={rid}")
post=Register.query.filter_by(rid=rid).first()
db.session.delete(post)
db.session.commit()
flash("Slot Deleted Successful","warning")
return redirect('/farmerdetails')
@app.route("/edit/<string:rid>",methods=['POST','GET'])
@login_required
def edit(rid):
# farming=db.engine.execute("SELECT * FROM `farming`")
if request.method=="POST":
farmername=request.form.get('farmername')
adharnumber=request.form.get('adharnumber')
age=request.form.get('age')
gender=request.form.get('gender')
phonenumber=request.form.get('phonenumber')
address=request.form.get('address')
farmingtype=request.form.get('farmingtype')
# query=db.engine.execute(f"UPDATE `register` SET `farmername`='{farmername}',`adharnumber`='{adharnumber}',`age`='{age}',`gender`='{gender}',`phonenumber`='{phonenumber}',`address`='{address}',`farming`='{farmingtype}'")
post=Register.query.filter_by(rid=rid).first()
print(post.farmername)
post.farmername=farmername
post.adharnumber=adharnumber
post.age=age
post.gender=gender
post.phonenumber=phonenumber
post.address=address
post.farming=farmingtype
db.session.commit()
flash("Slot is Updates","success")
return redirect('/farmerdetails')
posts=Register.query.filter_by(rid=rid).first()
farming=Farming.query.all()
return render_template('edit.html',posts=posts,farming=farming)
@app.route('/signup',methods=['POST','GET'])
def signup():
if request.method == "POST":
username=request.form.get('username')
email=request.form.get('email')
password=request.form.get('password')
print(username,email,password)
user=User.query.filter_by(email=email).first()
if user:
flash("Email Already Exist","warning")
return render_template('/signup.html')
# encpassword=generate_password_hash(password)
# new_user=db.engine.execute(f"INSERT INTO `user` (`username`,`email`,`password`) VALUES ('{username}','{email}','{encpassword}')")
# this is method 2 to save data in db
newuser=User(username=username,email=email,password=password)
db.session.add(newuser)
db.session.commit()
flash("Signup Succes Please Login","success")
return render_template('login.html')
return render_template('signup.html')
@app.route('/login',methods=['POST','GET'])
def login():
if request.method == "POST":
email=request.form.get('email')
password=request.form.get('password')
user=User.query.filter_by(email=email).first()
if user and user.password == password:
login_user(user)
flash("Login Success","primary")
return redirect(url_for('index'))
else:
flash("invalid credentials","warning")
return render_template('login.html')
return render_template('login.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
flash("Logout SuccessFul","warning")
return redirect(url_for('login'))
@app.route('/register',methods=['POST','GET'])
@login_required
def register():
farming=Farming.query.all()
if request.method=="POST":
farmername=request.form.get('farmername')
adharnumber=request.form.get('adharnumber')
age=request.form.get('age')
gender=request.form.get('gender')
phonenumber=request.form.get('phonenumber')
address=request.form.get('address')
farmingtype=request.form.get('farmingtype')
query=Register(farmername=farmername,adharnumber=adharnumber,age=age,gender=gender,phonenumber=phonenumber,address=address,farming=farmingtype)
db.session.add(query)
db.session.commit()
# query=db.engine.execute(f"INSERT INTO `register` (`farmername`,`adharnumber`,`age`,`gender`,`phonenumber`,`address`,`farming`) VALUES ('{farmername}','{adharnumber}','{age}','{gender}','{phonenumber}','{address}','{farmingtype}')")
# flash("Your Record Has Been Saved","success")
return redirect('/farmerdetails')
return render_template('farmer.html',farming=farming)
@app.route('/test')
def test():
try:
Test.query.all()
return 'My database is Connected'
except:
return 'My db is not Connected'