Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,494 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
import json
|
4 |
-
from io import BytesIO
|
5 |
|
6 |
-
from flask import Flask, jsonify, render_template, request, send_file
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
API_TOKEN = os.getenv("BIG_GAN_TOKEN")
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
@app.route("/")
|
19 |
-
def index():
|
20 |
-
return render_template("index.html")
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
@app.route("/infer_biggan")
|
24 |
-
def biggan():
|
25 |
-
input = request.args.get("input")
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
)
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
|
37 |
-
@app.route("/infer_t5")
|
38 |
-
def t5():
|
39 |
-
input = request.args.get("input")
|
40 |
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
|
|
44 |
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
start = request.args.get("start")
|
49 |
-
end = request.args.get("end")
|
50 |
|
51 |
-
|
52 |
-
|
|
|
|
|
53 |
|
54 |
-
|
55 |
|
56 |
-
return jsonify({"output": output})
|
57 |
|
58 |
|
59 |
-
if __name__ ==
|
60 |
-
app.
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
|
2 |
+
from flask_sqlalchemy import SQLAlchemy
|
3 |
+
from sqlalchemy import select, Column, exists, or_, Table, ForeignKey, Integer, String, DateTime, Text, and_, alias
|
4 |
+
from sqlalchemy.orm import aliased, relationship
|
5 |
+
from flask_login import UserMixin, LoginManager, login_user, login_required, logout_user, current_user
|
6 |
+
from flask_wtf.csrf import generate_csrf
|
7 |
+
from werkzeug.security import generate_password_hash, check_password_hash
|
8 |
+
from werkzeug.utils import secure_filename
|
9 |
+
from datetime import datetime
|
10 |
+
from flask_migrate import Migrate
|
11 |
+
import uuid
|
12 |
import os
|
13 |
+
from sqlalchemy import or_, func
|
|
|
|
|
14 |
|
|
|
15 |
|
16 |
+
app = Flask("Simplz")
|
17 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
|
18 |
+
app.config['SECRET_KEY'] = 'your_secret_key'
|
19 |
+
db = SQLAlchemy(app)
|
20 |
+
migrate = Migrate(app, db)
|
21 |
|
22 |
+
app.config['UPLOAD_FOLDER'] = 'static/users/uploaded_images'
|
23 |
+
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'}
|
|
|
24 |
|
25 |
+
followers = db.Table(
|
26 |
+
'followers',
|
27 |
+
db.Column('follower_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
|
28 |
+
db.Column('followed_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
|
29 |
+
extend_existing=True
|
30 |
+
)
|
31 |
|
32 |
+
class Comment(db.Model):
|
33 |
+
id = db.Column(db.Integer, primary_key=True)
|
34 |
+
content = db.Column(db.Text, nullable=False)
|
35 |
+
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
|
36 |
+
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
37 |
+
post_id = db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
|
38 |
+
author = db.relationship('User', backref='comments', lazy=True)
|
39 |
|
|
|
|
|
|
|
40 |
|
41 |
+
class Like(db.Model):
|
42 |
+
id = db.Column(db.Integer, primary_key=True)
|
43 |
+
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
44 |
+
post_id = db.Column(db.Integer, db.ForeignKey('post.id', ondelete='CASCADE'), nullable=False)
|
45 |
+
liked_at = db.Column(db.DateTime, default=datetime.utcnow)
|
46 |
+
|
47 |
+
|
48 |
+
class Follow(db.Model):
|
49 |
+
__tablename__ = 'followers'
|
50 |
+
follower_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
|
51 |
+
followed_id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
|
52 |
+
|
53 |
+
follower = db.relationship('User', foreign_keys=[follower_id], backref='following')
|
54 |
+
followed = db.relationship('User', foreign_keys=[followed_id], backref='followers')
|
55 |
+
|
56 |
+
__table_args__ = {'extend_existing': True}
|
57 |
+
|
58 |
+
class Message(db.Model):
|
59 |
+
id = db.Column(db.Integer, primary_key=True)
|
60 |
+
sender_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
61 |
+
recipient_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
62 |
+
content = db.Column(db.Text, nullable=False)
|
63 |
+
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
|
64 |
+
sender = db.relationship('User', foreign_keys=[sender_id], backref='sent_messages')
|
65 |
+
recipient = db.relationship('User', foreign_keys=[recipient_id], backref='received_messages')
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
class User(UserMixin, db.Model):
|
70 |
+
id = db.Column(db.Integer, primary_key=True)
|
71 |
+
username = db.Column(db.String(100), unique=True, nullable=False)
|
72 |
+
email = db.Column(db.String(100), unique=True, nullable=False)
|
73 |
+
password = db.Column(db.String(100), nullable=False)
|
74 |
+
first_name = db.Column(db.String(50), default="Per")
|
75 |
+
last_name = db.Column(db.String(50), default="Son")
|
76 |
+
profession = db.Column(db.String(100), default="Person")
|
77 |
+
location = db.Column(db.String(100), default="Earth")
|
78 |
+
bio = db.Column(db.Text, default="Hello there...")
|
79 |
+
profile_pic = db.Column(db.String(100), default='default.jpg')
|
80 |
+
posts = db.relationship('Post', backref='author', lazy=True)
|
81 |
+
|
82 |
+
following_list = db.relationship(
|
83 |
+
'User',
|
84 |
+
secondary=followers,
|
85 |
+
primaryjoin=(followers.c.follower_id == id),
|
86 |
+
secondaryjoin=(followers.c.followed_id == id),
|
87 |
+
backref=db.backref('followers_of', lazy='dynamic'),
|
88 |
+
lazy='dynamic',
|
89 |
+
overlaps="follower,followers_of,following,following_list"
|
90 |
+
)
|
91 |
+
|
92 |
+
followers_list = db.relationship(
|
93 |
+
'User',
|
94 |
+
secondary=followers,
|
95 |
+
primaryjoin=(followers.c.followed_id == id),
|
96 |
+
secondaryjoin=(followers.c.follower_id == id),
|
97 |
+
backref=db.backref('following_by', lazy='dynamic'),
|
98 |
+
lazy='dynamic',
|
99 |
+
overlaps="followed,followers,followers_of,following_list"
|
100 |
+
)
|
101 |
+
|
102 |
+
def is_following(self, user):
|
103 |
+
return self.following_list.filter_by(id=user.id).first() is not None
|
104 |
+
|
105 |
+
def follow(self, user):
|
106 |
+
if not self.is_following(user) and self.id != user.id:
|
107 |
+
self.following_list.append(user)
|
108 |
+
db.session.commit()
|
109 |
+
|
110 |
+
def unfollow(self, user):
|
111 |
+
if self.is_following(user):
|
112 |
+
self.following_list.remove(user)
|
113 |
+
db.session.commit()
|
114 |
+
|
115 |
+
def like_post(self, post):
|
116 |
+
if not self.has_liked_post(post):
|
117 |
+
like = Like(user_id=self.id, post_id=post.id)
|
118 |
+
db.session.add(like)
|
119 |
+
db.session.commit()
|
120 |
+
|
121 |
+
def unlike_post(self, post):
|
122 |
+
like = Like.query.filter_by(user_id=self.id, post_id=post.id).first()
|
123 |
+
if like:
|
124 |
+
db.session.delete(like)
|
125 |
+
db.session.commit()
|
126 |
+
|
127 |
+
def has_liked_post(self, post):
|
128 |
+
return Like.query.filter_by(user_id=self.id, post_id=post.id).count() > 0
|
129 |
+
|
130 |
+
def add_comment(self, post, content):
|
131 |
+
comment = Comment(user_id=self.id, post_id=post.id, content=content)
|
132 |
+
db.session.add(comment)
|
133 |
+
db.session.commit()
|
134 |
+
|
135 |
+
def delete_comment(self, comment):
|
136 |
+
if comment.user_id == self.id:
|
137 |
+
db.session.delete(comment)
|
138 |
+
db.session.commit()
|
139 |
|
|
|
|
|
|
|
140 |
|
141 |
+
|
142 |
+
class Post(db.Model):
|
143 |
+
id = db.Column(db.Integer, primary_key=True)
|
144 |
+
content = db.Column(db.Text, nullable=False)
|
145 |
+
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
146 |
+
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
147 |
+
filename = db.Column(db.String(100))
|
148 |
+
comments = db.relationship('Comment', backref='post', lazy=True, cascade='all, delete-orphan')
|
149 |
+
likes = db.relationship('Like', backref='post', lazy=True)
|
150 |
+
@property
|
151 |
+
def likes_count(self):
|
152 |
+
return Like.query.filter_by(post_id=self.id).count()
|
153 |
+
|
154 |
+
|
155 |
+
login_manager = LoginManager(app)
|
156 |
+
login_manager.login_view = 'login'
|
157 |
+
|
158 |
+
def allowed_file(filename):
|
159 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
|
160 |
+
|
161 |
+
@login_manager.user_loader
|
162 |
+
def load_user(user_id):
|
163 |
+
return db.session.get(User, int(user_id))
|
164 |
+
|
165 |
+
|
166 |
+
@app.route('/')
|
167 |
+
@login_required
|
168 |
+
def index():
|
169 |
+
followed_user = aliased(User, name='followed_user')
|
170 |
+
|
171 |
+
own_posts_query = Post.query.filter_by(user_id=current_user.id)
|
172 |
+
|
173 |
+
followed_posts_query = Post.query.join(
|
174 |
+
Follow,
|
175 |
+
or_(
|
176 |
+
and_(Post.user_id == Follow.followed_id, Follow.follower_id == current_user.id),
|
177 |
+
and_(Post.user_id == Follow.follower_id, Follow.followed_id == current_user.id)
|
178 |
+
)
|
179 |
)
|
180 |
|
181 |
+
combined_posts_query = own_posts_query.union_all(followed_posts_query)
|
182 |
+
posts = combined_posts_query.order_by(Post.created_at.desc()).all()
|
183 |
+
|
184 |
+
csrf_token = request.environ.get('HTTP_X_CSRFTOKEN')
|
185 |
+
|
186 |
+
return render_template('index.html', posts=posts, csrf_token=csrf_token)
|
187 |
+
|
188 |
+
|
189 |
+
|
190 |
+
@app.route('/register', methods=['GET', 'POST'])
|
191 |
+
def register():
|
192 |
+
if request.method == 'POST':
|
193 |
+
email = request.form['email']
|
194 |
+
username = request.form['username']
|
195 |
+
password = request.form['password']
|
196 |
+
password2 = request.form['password2']
|
197 |
+
|
198 |
+
if password != password2:
|
199 |
+
flash('Passwords do not match. Please try again.', 'error')
|
200 |
+
return render_template('register.html')
|
201 |
+
|
202 |
+
existing_user_with_username = User.query.filter_by(username=username).first()
|
203 |
+
existing_user_with_email = User.query.filter_by(email=email).first()
|
204 |
+
|
205 |
+
if existing_user_with_username:
|
206 |
+
flash('Username already exists. Please choose a different username.', 'error')
|
207 |
+
return render_template('register.html')
|
208 |
+
|
209 |
+
if existing_user_with_email:
|
210 |
+
flash('Email address already registered. Please use a different email.', 'error')
|
211 |
+
return render_template('register.html')
|
212 |
+
|
213 |
+
hashed_password = generate_password_hash(password, method='scrypt')
|
214 |
+
new_user = User(username=username, email=email, password=hashed_password)
|
215 |
+
db.session.add(new_user)
|
216 |
+
db.session.commit()
|
217 |
+
|
218 |
+
flash('Account created successfully! Please log in.', 'success')
|
219 |
+
return redirect(url_for('login'))
|
220 |
+
|
221 |
+
return render_template('register.html')
|
222 |
+
|
223 |
+
@app.route('/login', methods=['GET', 'POST'])
|
224 |
+
def login():
|
225 |
+
if request.method == 'POST':
|
226 |
+
username = request.form['username']
|
227 |
+
password = request.form['password']
|
228 |
+
user = User.query.filter_by(username=username).first()
|
229 |
+
if user and check_password_hash(user.password, password):
|
230 |
+
login_user(user)
|
231 |
+
return redirect(url_for('index'))
|
232 |
+
else:
|
233 |
+
flash('Invalid username or password. Fields are case sensitive.', 'error')
|
234 |
+
return render_template('login.html')
|
235 |
+
|
236 |
+
@app.route('/logout')
|
237 |
+
@login_required
|
238 |
+
def logout():
|
239 |
+
logout_user()
|
240 |
+
return redirect(url_for('index'))
|
241 |
+
|
242 |
+
@app.route('/create_post', methods=['POST'])
|
243 |
+
@login_required
|
244 |
+
def create_post():
|
245 |
+
content = request.form['content']
|
246 |
+
image = request.files['image']
|
247 |
+
|
248 |
+
if image and allowed_file(image.filename):
|
249 |
+
filename = str(uuid.uuid4()) + secure_filename(image.filename)
|
250 |
+
image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
251 |
+
else:
|
252 |
+
filename = None
|
253 |
+
|
254 |
+
new_post = Post(content=content, author=current_user, filename=filename)
|
255 |
+
db.session.add(new_post)
|
256 |
+
db.session.commit()
|
257 |
+
return redirect(url_for('index'))
|
258 |
+
|
259 |
+
@app.route('/delete_post/<int:post_id>', methods=['POST'])
|
260 |
+
@login_required
|
261 |
+
def delete_post(post_id):
|
262 |
+
post = db.session.get(Post, post_id)
|
263 |
+
if post and post.author == current_user:
|
264 |
+
if post.filename:
|
265 |
+
try:
|
266 |
+
os.remove(os.path.join(app.config['UPLOAD_FOLDER'], post.filename))
|
267 |
+
except Exception as e:
|
268 |
+
flash(f"Error deleting image file: {str(e)}", "error")
|
269 |
+
|
270 |
+
|
271 |
+
Like.query.filter_by(post_id=post_id).delete()
|
272 |
+
db.session.delete(post)
|
273 |
+
db.session.commit()
|
274 |
+
return redirect(url_for('index'))
|
275 |
+
|
276 |
+
|
277 |
+
|
278 |
+
|
279 |
+
@app.route('/follow/<int:user_id>', methods=['POST'])
|
280 |
+
@login_required
|
281 |
+
def follow(user_id, page):
|
282 |
+
user_to_follow = User.query.get(user_id)
|
283 |
+
if user_to_follow is None:
|
284 |
+
flash('User not found.', 'error')
|
285 |
+
return redirect(url_for('explore'))
|
286 |
+
|
287 |
+
if current_user.is_following(user_to_follow):
|
288 |
+
flash('You are already following this user.', 'info')
|
289 |
+
return redirect(url_for('view_profile', user_id=user_id))
|
290 |
+
|
291 |
+
try:
|
292 |
+
current_user.follow(user_to_follow)
|
293 |
+
except Exception as e:
|
294 |
+
flash('Failed to follow the user. Please try again.', 'error')
|
295 |
+
print(f"Error: {str(e)}")
|
296 |
+
return redirect(url_for('view_profile', user_id=user_id))
|
297 |
+
|
298 |
+
flash(f"You are now following {user_to_follow.username}.", 'success')
|
299 |
+
if page == 'profile':
|
300 |
+
return redirect(url_for('view_profile', user_id=user_id))
|
301 |
+
elif page == 'search':
|
302 |
+
return redirect(url_for('search_results', query=request.form['query']))
|
303 |
+
else:
|
304 |
+
return redirect(url_for('view_profile', user_id=user_id))
|
305 |
+
|
306 |
+
|
307 |
+
@app.route('/unfollow/<int:user_id>', methods=['POST'])
|
308 |
+
@login_required
|
309 |
+
def unfollow(user_id, page):
|
310 |
+
user_to_unfollow = User.query.get(user_id)
|
311 |
+
if user_to_unfollow is None:
|
312 |
+
flash('User not found.', 'danger')
|
313 |
+
return redirect(url_for('index'))
|
314 |
+
|
315 |
+
if current_user.is_following(user_to_unfollow):
|
316 |
+
current_user.unfollow(user_to_unfollow)
|
317 |
+
flash('You have unfollowed {}.'.format(user_to_unfollow.username), 'success')
|
318 |
+
else:
|
319 |
+
flash('You are not following this user.', 'info')
|
320 |
+
if page == 'profile':
|
321 |
+
return redirect(url_for('view_profile', user_id=user_id))
|
322 |
+
elif page == 'search':
|
323 |
+
return redirect(url_for('search_results', query=request.form['query']))
|
324 |
+
else:
|
325 |
+
return redirect(url_for('view_profile', user_id=user_id))
|
326 |
+
|
327 |
+
|
328 |
+
@app.route('/account', methods=['GET', 'POST'])
|
329 |
+
@login_required
|
330 |
+
def account():
|
331 |
+
if request.method == 'POST':
|
332 |
+
current_user.username = request.form['username']
|
333 |
+
current_user.email = request.form['email']
|
334 |
+
current_user.first_name = request.form['first_name']
|
335 |
+
current_user.last_name = request.form['last_name']
|
336 |
+
current_user.bio = request.form['bio']
|
337 |
+
current_user.location = request.form['location']
|
338 |
+
current_user.profession = request.form['profession']
|
339 |
+
|
340 |
+
profile_picture = request.files['profile_picture']
|
341 |
+
if profile_picture and allowed_file(profile_picture.filename):
|
342 |
+
filename = str(uuid.uuid4()) + secure_filename(profile_picture.filename)
|
343 |
+
profile_picture.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
344 |
+
|
345 |
+
if current_user.profile_pic is not None and current_user.profile_pic != 'default.jpg':
|
346 |
+
old_profile_picture_path = os.path.join(app.config['UPLOAD_FOLDER'], current_user.profile_pic)
|
347 |
+
os.remove(old_profile_picture_path)
|
348 |
+
|
349 |
+
current_user.profile_pic = filename
|
350 |
+
|
351 |
+
new_password = request.form['new_password']
|
352 |
+
if new_password:
|
353 |
+
current_user.password = generate_password_hash(new_password, method='scrypt')
|
354 |
+
|
355 |
+
db.session.commit()
|
356 |
+
flash('Profile updated successfully!', 'success')
|
357 |
+
return redirect(url_for('account'))
|
358 |
+
|
359 |
+
return render_template('account.html')
|
360 |
+
|
361 |
+
@app.route('/search_user', methods=['GET'])
|
362 |
+
def search_user():
|
363 |
+
search_query = request.args.get('search_query', '')
|
364 |
+
users = User.query.filter(
|
365 |
+
|
366 |
+
or_(
|
367 |
+
User.username.ilike(f'%{search_query}%'),
|
368 |
+
User.first_name.ilike(f'%{search_query}%'),
|
369 |
+
User.last_name.ilike(f'%{search_query}%'),
|
370 |
+
(User.first_name + User.last_name).like(f'%{search_query}%')
|
371 |
+
)
|
372 |
+
).all()
|
373 |
+
|
374 |
+
|
375 |
+
#users = User.query.filter(User.username.ilike(f'%{search_query}%')).all()
|
376 |
+
csrf_token = generate_csrf() # Generate CSRF token
|
377 |
+
return render_template('search_results.html', users=users, csrf_token=csrf_token, searchq=search_query )
|
378 |
+
|
379 |
+
@app.route('/user/<int:user_id>')
|
380 |
+
@login_required
|
381 |
+
def view_profile(user_id):
|
382 |
+
user = User.query.get(user_id)
|
383 |
+
if not user:
|
384 |
+
flash('User not found.', 'error')
|
385 |
+
return redirect(url_for('search_user'))
|
386 |
+
|
387 |
+
posts = Post.query.filter_by(user_id=user.id).order_by(Post.created_at.desc()).all()
|
388 |
+
csrf_token = generate_csrf()
|
389 |
+
return render_template('user_profile.html', user=user, posts=posts, csrf_token=csrf_token, profile_user_id=user_id)
|
390 |
+
|
391 |
+
@app.route('/add_comment/<int:post_id>', methods=['POST'])
|
392 |
+
@login_required
|
393 |
+
def add_comment(post_id):
|
394 |
+
content = request.form['content']
|
395 |
+
post = Post.query.get(post_id)
|
396 |
+
if not post:
|
397 |
+
flash('Post not found.', 'error')
|
398 |
+
return redirect(url_for('index'))
|
399 |
+
current_user.add_comment(post, content)
|
400 |
+
|
401 |
+
flash('Comment added successfully.', 'success')
|
402 |
+
return redirect(url_for('index'))
|
403 |
+
|
404 |
+
@login_required
|
405 |
+
@app.route('/like_post/<int:post_id>', methods=['POST'])
|
406 |
+
def like_post(post_id):
|
407 |
+
post = Post.query.get(post_id)
|
408 |
+
if not post:
|
409 |
+
flash('Post not found.', 'error')
|
410 |
+
return redirect(url_for('index'))
|
411 |
+
|
412 |
+
if current_user.has_liked_post(post):
|
413 |
+
current_user.unlike_post(post)
|
414 |
+
db.session.commit()
|
415 |
+
liked = False
|
416 |
+
else:
|
417 |
+
current_user.like_post(post)
|
418 |
+
db.session.commit()
|
419 |
+
liked = True
|
420 |
+
|
421 |
+
likes_count = post.likes_count
|
422 |
+
response_data = {'likes_count': likes_count, 'liked': liked}
|
423 |
+
return jsonify(response_data)
|
424 |
+
|
425 |
+
|
426 |
+
@app.route('/send_message', methods=['POST'])
|
427 |
+
@login_required
|
428 |
+
def send_message():
|
429 |
+
recipient_username = request.form['recipient_username']
|
430 |
+
message_content = request.form['message_content']
|
431 |
+
|
432 |
+
recipient = User.query.filter_by(username=recipient_username).first()
|
433 |
+
if not recipient:
|
434 |
+
flash('Recipient not found.', 'error')
|
435 |
+
return redirect(url_for('conversations'))
|
436 |
+
|
437 |
+
new_message = Message(sender_id=current_user.id, recipient_id=recipient.id, content=message_content)
|
438 |
+
db.session.add(new_message)
|
439 |
+
db.session.commit()
|
440 |
+
|
441 |
+
flash('Message sent successfully!', 'success')
|
442 |
+
return redirect(url_for('conversation', recipient_id=recipient.id))
|
443 |
+
|
444 |
+
|
445 |
+
@app.route('/conversations')
|
446 |
+
@login_required
|
447 |
+
def conversations():
|
448 |
+
conversations = db.session.query(User).join(
|
449 |
+
Message, or_(
|
450 |
+
and_(Message.sender_id == current_user.id, Message.recipient_id == User.id),
|
451 |
+
and_(Message.sender_id == User.id, Message.recipient_id == current_user.id)
|
452 |
+
)
|
453 |
+
).distinct(User.id).all()
|
454 |
+
|
455 |
+
return render_template('conversations.html', conversations=conversations)
|
456 |
+
|
457 |
+
@app.route('/new_conversation', methods=['GET'])
|
458 |
+
@login_required
|
459 |
+
def new_conversation():
|
460 |
+
return render_template('new_conversation.html')
|
461 |
|
462 |
|
|
|
|
|
|
|
463 |
|
464 |
+
@app.route('/conversation/<int:recipient_id>', methods=['GET', 'POST'])
|
465 |
+
@login_required
|
466 |
+
def conversation(recipient_id):
|
467 |
+
recipient = User.query.get(recipient_id)
|
468 |
+
if not recipient:
|
469 |
+
flash('Recipient not found.', 'error')
|
470 |
+
return redirect(url_for('conversations'))
|
471 |
|
472 |
+
if request.method == 'POST':
|
473 |
+
message_content = request.form['message_content']
|
474 |
|
475 |
+
new_message = Message(sender_id=current_user.id, recipient_id=recipient.id, content=message_content)
|
476 |
+
db.session.add(new_message)
|
477 |
+
db.session.commit()
|
478 |
|
479 |
+
flash('Message sent successfully!', 'success')
|
480 |
+
return redirect(url_for('conversation', recipient_id=recipient_id))
|
|
|
|
|
481 |
|
482 |
+
messages = Message.query.filter(or_(
|
483 |
+
and_(Message.sender_id == current_user.id, Message.recipient_id == recipient_id),
|
484 |
+
and_(Message.sender_id == recipient_id, Message.recipient_id == current_user.id)
|
485 |
+
)).order_by(Message.timestamp).all()
|
486 |
|
487 |
+
return render_template('conversation.html', recipient=recipient, messages=messages)
|
488 |
|
|
|
489 |
|
490 |
|
491 |
+
if __name__ == '__main__':
|
492 |
+
with app.app_context():
|
493 |
+
db.create_all()
|
494 |
+
app.run(debug=True, use_reloader=False)
|