-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
181 lines (131 loc) · 4.91 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
from flask import Flask, request, redirect, url_for, flash,json
from flask.templating import render_template
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, migrate
from models import db, Category , Advert
app = Flask(__name__)
app.debug = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///advertapp.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
from models import db
db.init_app(app)
migrate = Migrate(app, db)
@app.route('/hello')
def check():
return 'Flask is working'
@app.route('/add_category/<name>', methods=['POST']) #kategori ekle
def add_category(name):
category = Category(name=name)
db.session.add(category)
db.session.commit()
return f'Category {name} added successfully'
@app.route('/addAdvert', methods=['POST']) #yeni ilan ekle
def addAdvert():
data = request.json
description = data.get('description')
price = data.get('price')
city = data.get('city')
image = data.get('image')
category_id = data.get('category_id')
advert = Advert(
description=description,
price=price,
city=city,
image=image,
category_id=category_id
)
db.session.add(advert)
db.session.commit()
return f'Advert added successfully'
@app.route('/updateAdvert', methods=['PUT'])
def updateAdvert():
data = request.json
advert_id = data.get('id')
advert = Advert.query.filter_by(id=advert_id).first()
if 'description' in data:
advert.description = data.get('description')
if 'price' in data:
advert.price = data.get('price')
if 'city' in data:
advert.city = data.get('city')
if 'image' in data:
advert.image = data.get('image')
db.session.commit()
return f'Advert with ID {advert_id} updated successfully'
@app.route('/deleteAdvert', methods=['DELETE'])
def deleteAdvert(advert_id):
advert_id = request.args.get('advert_id')
advert = Advert.query.get_or_404(advert_id)
db.session.delete(advert)
db.session.commit()
return f'Advert with ID {advert_id} deleted successfully'
@app.route("/getCategories")
def getCategories():
categories = Category.query.all()
category_list = []
for category in categories:
category_list.append({'id': category.id, 'name': category.name})
return {'categories': category_list}
def get_categories_count(adverts):
categories = {}
for category in Category.query.all():
count_by_category = sum(1 for advert in adverts if advert.category_id == category.id)
if count_by_category > 0:
categories[category.name] = count_by_category
return categories
@app.route('/allAdvert',methods=['GET'])
def getAllAdvert():
all_adverts = Advert.query.all()
adverts_list = []
for advert in all_adverts:
adverts_list.append({
'id': advert.id,
'description': advert.description,
'price': advert.price,
'city': advert.city,
'image': advert.image,
'category_id': advert.category_id
})
return (adverts_list)
@app.route('/', methods=['GET'])
def getAdverts():
adverts = Advert.query.all() # Tüm ilanları veritabanından çekin
advert_list = []
categories = get_categories_count(adverts)
for advert in adverts:
advert_data = {
'id': advert.id,
'description': advert.description,
'price': advert.price,
'city': advert.city,
'image': advert.image,
'category_id': advert.category_id
}
advert_list.append(advert_data)
return render_template('homepage.html', adverts=adverts,categories=categories)
def get_adverts(query=None):
adverts = Advert.query.filter((Advert.description.contains(query)) | (Advert.city.contains(query))).all()
return adverts
@app.route('/search')
def search():
query = request.args.get('query') # Arama sorgusunu al
adverts = get_adverts(query) # Arama sorgusuna göre ilanları getir
categories = get_categories_count(adverts) # Filtrelenmiş ilanların kategorilere göre sayılarını getir
return render_template('searchPage.html', adverts=adverts, categories=categories)
@app.route('/byCategory')
def getAdByCategory():
category_name = request.args.get('category')
category = Category.query.filter_by(name=category_name).first()
adverts = Advert.query.filter(Advert.category == category).all()
categoryAndCount = get_categories_count(adverts)
return render_template('searchPage.html', adverts=adverts, categories=categoryAndCount)
@app.route('/details')
def getDetails():
advert_id = request.args.get('advertId')
advert = Advert.query.filter_by(id=advert_id).first()
return render_template('details.html', advert=advert)
@app.route("/favicon.ico")
def favicon():
return url_for('static', filename='data:,')
if __name__ == '__main__':
app.run()