Skip to content

Commit b23f2d3

Browse files
committed
Update user get functions, and added image schema to swagger
1 parent 1a71d2c commit b23f2d3

File tree

2 files changed

+73
-105
lines changed

2 files changed

+73
-105
lines changed

backend/PyMatcha/models/user.py

Lines changed: 40 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from typing import Optional
2727

2828
import Geohash
29+
from PyMatcha.models.image import Image
2930
from PyMatcha.models.like import Like
3031
from PyMatcha.models.match import Match
3132
from PyMatcha.models.message import Message
@@ -199,6 +200,7 @@ def register(email: str, username: str, password: str, first_name: str, last_nam
199200
def to_dict(self) -> Dict:
200201
returned_dict = super().to_dict()
201202
returned_dict["tags"] = [t.to_dict() for t in self.get_tags()]
203+
returned_dict["images"] = [image.to_dict() for image in self.get_images()]
202204
returned_dict["reports"] = {"sent": [], "received": []}
203205
returned_dict["reports"]["sent"] = [r.to_dict() for r in self.get_reports_sent()]
204206
returned_dict["reports"]["received"] = [r.to_dict() for r in self.get_reports_received()]
@@ -222,128 +224,61 @@ def get_jwt_info(self):
222224
"date_lastseen": self.date_lastseen,
223225
}
224226

227+
def get_images(self):
228+
logging.debug("Getting all images for user {}".format(self.id))
229+
image_list = Image.get_multis(user_id=self.id)
230+
if not image_list:
231+
return []
232+
else:
233+
return image_list
234+
225235
def get_tags(self):
226236
logging.debug("Getting all tags for user {}".format(self.id))
227-
with self.db.cursor() as c:
228-
c.execute(
229-
"""
230-
SELECT tags.id as id, tags.user_id as user_id, tags.name as name
231-
FROM users
232-
INNER JOIN tags on users.id = tags.user_id
233-
WHERE users.id = CAST({} AS UNSIGNED)
234-
""".format(
235-
self.id
236-
)
237-
)
238-
tags = c.fetchall()
239-
tags_list = []
240-
for t in tags:
241-
tags_list.append(Tag(t))
242-
return tags_list
237+
tag_list = Tag.get_multis(user_id=self.id)
238+
if not tag_list:
239+
return []
240+
else:
241+
return tag_list
243242

244243
def get_views(self):
245244
logging.debug("Getting all views for user profile {}".format(self.id))
246-
with self.db.cursor() as c:
247-
c.execute(
248-
"""
249-
SELECT views.id as id, views.profile_id as profile_id,
250-
views.viewer_id as viewer_id, views.dt_seen as dt_seen
251-
FROM users
252-
INNER JOIN views on users.id = views.profile_id
253-
WHERE users.id = CAST({} AS UNSIGNED)
254-
""".format(
255-
self.id
256-
)
257-
)
258-
views = c.fetchall()
259-
views_list = []
260-
for v in views:
261-
views_list.append(View(v))
262-
return views_list
245+
view_list = View.get_multis(profile_id=self.id)
246+
if not view_list:
247+
return []
248+
else:
249+
return view_list
263250

264251
def get_reports_received(self):
265252
logging.debug("Getting all reports received for user {}".format(self.id))
266-
with self.db.cursor() as c:
267-
c.execute(
268-
"""
269-
SELECT reports.id as id, reports.reported_id as reported_id,
270-
reports.reporter_id as reporter_id, reports.dt_reported as dt_reported,
271-
reports.details as details, reports.reason as reason, reports.status as status
272-
FROM users
273-
INNER JOIN reports on users.id = reports.reported_id
274-
WHERE users.id = CAST({} AS UNSIGNED)
275-
""".format(
276-
self.id
277-
)
278-
)
279-
reports = c.fetchall()
280-
reports_list = []
281-
for r in reports:
282-
reports_list.append(Report(r))
283-
return reports_list
253+
reports_received_list = Report.get_multis(reported_id=self.id)
254+
if not reports_received_list:
255+
return []
256+
else:
257+
return reports_received_list
284258

285259
def get_reports_sent(self):
286260
logging.debug("Getting all reports sent for user {}".format(self.id))
287-
with self.db.cursor() as c:
288-
c.execute(
289-
"""
290-
SELECT reports.id as id, reports.reported_id as reported_id,
291-
reports.reporter_id as reporter_id, reports.dt_reported as dt_reported,
292-
reports.details as details, reports.reason as reason, reports.status as status
293-
FROM users
294-
INNER JOIN reports on users.id = reports.reporter_id
295-
WHERE users.id = CAST({} AS UNSIGNED)
296-
""".format(
297-
self.id
298-
)
299-
)
300-
reports = c.fetchall()
301-
reports_list = []
302-
for r in reports:
303-
reports_list.append(Report(r))
304-
return reports_list
261+
reports_sent_list = Report.get_multis(reporter_id=self.id)
262+
if not reports_sent_list:
263+
return []
264+
else:
265+
return reports_sent_list
305266

306267
def get_likes_received(self):
307268
logging.debug("Getting all likes received for user {}".format(self.id))
308-
with self.db.cursor() as c:
309-
c.execute(
310-
"""
311-
SELECT likes.id as id, likes.liked_id as liked_id,
312-
likes.liker_id as liker_id, likes.dt_liked as dt_liked,
313-
likes.is_superlike as is_superlike
314-
FROM users
315-
INNER JOIN likes on users.id = likes.liked_id
316-
WHERE users.id = CAST({} AS UNSIGNED)
317-
""".format(
318-
self.id
319-
)
320-
)
321-
likes = c.fetchall()
322-
like_list = []
323-
for like in likes:
324-
like_list.append(Like(like))
325-
return like_list
269+
likes_received_list = Like.get_multis(liked_id=self.id)
270+
if not likes_received_list:
271+
return []
272+
else:
273+
return likes_received_list
326274

327275
def get_likes_sent(self):
328276
logging.debug("Getting all likes sent for user {}".format(self.id))
329-
with self.db.cursor() as c:
330-
c.execute(
331-
"""
332-
SELECT likes.id as id, likes.liked_id as liked_id,
333-
likes.liker_id as liker_id, likes.dt_liked as dt_liked,
334-
likes.is_superlike as is_superlike
335-
FROM users
336-
INNER JOIN likes on users.id = likes.liker_id
337-
WHERE users.id = CAST({} AS UNSIGNED)
338-
""".format(
339-
self.id
340-
)
341-
)
342-
likes = c.fetchall()
343-
like_list = []
344-
for like in likes:
345-
like_list.append(Like(like))
346-
return like_list
277+
likes_sent_list = Like.get_multis(liker_id=self.id)
278+
if not likes_sent_list:
279+
return []
280+
else:
281+
return likes_sent_list
347282

348283
def already_likes(self, liked_id: int) -> bool:
349284
with self.db.cursor() as c:

backend/schemas/swagger.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,11 @@ components:
13871387
items:
13881388
$ref: '#/components/schemas/Tag'
13891389
uniqueItems: true
1390+
images:
1391+
type: array
1392+
items:
1393+
$ref: '#/components/schemas/Image'
1394+
uniqueItems: true
13901395
Like:
13911396
type: object
13921397
properties:
@@ -1711,6 +1716,11 @@ components:
17111716
items:
17121717
$ref: '#/components/schemas/Tag'
17131718
uniqueItems: true
1719+
images:
1720+
type: array
1721+
items:
1722+
$ref: '#/components/schemas/Image'
1723+
uniqueItems: true
17141724
OnlineUser:
17151725
type: object
17161726
properties:
@@ -1722,3 +1732,26 @@ components:
17221732
type: timestamp
17231733
description: Timestamp of last time the user was online
17241734
example: 1600332058.537065,
1735+
Image:
1736+
type: object
1737+
properties:
1738+
id:
1739+
type: integer
1740+
description: The image id
1741+
example: 1
1742+
user_id:
1743+
type: integer
1744+
description: The user id to whom the image belongs
1745+
example: 1
1746+
link:
1747+
type: string
1748+
description: The image link
1749+
example: https://i.imgur.com/OCqF907.png
1750+
timestamp:
1751+
type: date
1752+
description: When was the image added
1753+
example: Wed, 19 Sep 2020 19:24:02 GMT
1754+
is_primary:
1755+
type: boolean
1756+
description: Is this image the profile picture of the user
1757+
example: true

0 commit comments

Comments
 (0)