Skip to content

Commit a028889

Browse files
committed
WIP on views
1 parent 62e2dd5 commit a028889

File tree

7 files changed

+314
-25
lines changed

7 files changed

+314
-25
lines changed

PyMatcha.postman_collection.json

Lines changed: 164 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,8 +1949,6 @@
19491949
" pm.response.to.have.status(200);",
19501950
"});",
19511951
"",
1952-
"user_email = pm.collectionVariables.get(\"user_email\");",
1953-
"",
19541952
"tests[\"Is geohash correct\"] = response.geohash == \"ezzweqbd4wuk\""
19551953
],
19561954
"type": "text/javascript"
@@ -2037,8 +2035,6 @@
20372035
" pm.response.to.have.status(200);",
20382036
"});",
20392037
"",
2040-
"user_email = pm.collectionVariables.get(\"user_email\");",
2041-
"",
20422038
"tests[\"Is geohash correct\"] = response.geohash == \"ezzx4fcq327u\""
20432039
],
20442040
"type": "text/javascript"
@@ -2060,6 +2056,103 @@
20602056
}
20612057
},
20622058
"response": []
2059+
},
2060+
{
2061+
"name": "Create random views",
2062+
"event": [
2063+
{
2064+
"listen": "prerequest",
2065+
"script": {
2066+
"id": "545bcc8e-5159-4ce2-9a28-11efc5dfbf62",
2067+
"exec": [
2068+
""
2069+
],
2070+
"type": "text/javascript"
2071+
}
2072+
},
2073+
{
2074+
"listen": "test",
2075+
"script": {
2076+
"id": "90d417c9-84fa-457d-adbb-93c453b15a29",
2077+
"exec": [
2078+
"var response = JSON.parse(responseBody);",
2079+
"",
2080+
"",
2081+
"pm.test(\"Status code is 200\", function () {",
2082+
" pm.response.to.have.status(200);",
2083+
"});",
2084+
"",
2085+
"tests[\"JSON return code is 200\"] = response.code == 200",
2086+
"",
2087+
"tests[\"success is true\"] = response.success == true"
2088+
],
2089+
"type": "text/javascript"
2090+
}
2091+
}
2092+
],
2093+
"request": {
2094+
"method": "POST",
2095+
"header": [
2096+
{
2097+
"key": "debug-auth-token",
2098+
"value": "xX69jules69Xx",
2099+
"type": "text"
2100+
}
2101+
],
2102+
"url": {
2103+
"raw": "{{host}}/debug/views/78",
2104+
"host": [
2105+
"{{host}}"
2106+
],
2107+
"path": [
2108+
"debug",
2109+
"views",
2110+
"78"
2111+
]
2112+
}
2113+
},
2114+
"response": []
2115+
},
2116+
{
2117+
"name": "Get profile views",
2118+
"event": [
2119+
{
2120+
"listen": "test",
2121+
"script": {
2122+
"id": "dade5edf-80bc-4e3c-8509-d56281228062",
2123+
"exec": [
2124+
"var response = JSON.parse(responseBody);",
2125+
"",
2126+
"",
2127+
"pm.test(\"Status code is 200\", function () {",
2128+
" pm.response.to.have.status(200);",
2129+
"});",
2130+
"",
2131+
"tests[\"JSON return code is 200\"] = response.code == 200",
2132+
"",
2133+
"tests[\"success is true\"] = response.success == true",
2134+
"",
2135+
"tests[\"View count\"] = response.views.length == 78"
2136+
],
2137+
"type": "text/javascript"
2138+
}
2139+
}
2140+
],
2141+
"request": {
2142+
"method": "GET",
2143+
"header": [],
2144+
"url": {
2145+
"raw": "{{host}}/profile/views",
2146+
"host": [
2147+
"{{host}}"
2148+
],
2149+
"path": [
2150+
"profile",
2151+
"views"
2152+
]
2153+
}
2154+
},
2155+
"response": []
20632156
}
20642157
],
20652158
"protocolProfileBehavior": {}
@@ -2115,11 +2208,59 @@
21152208
},
21162209
"response": []
21172210
},
2211+
{
2212+
"name": "Reset views table",
2213+
"event": [
2214+
{
2215+
"listen": "test",
2216+
"script": {
2217+
"id": "0e26bd80-adcd-472d-a239-4b7cdc4403fb",
2218+
"exec": [
2219+
"pm.test(\"Status code is 200\", function () {",
2220+
" pm.response.to.have.status(204);",
2221+
"});",
2222+
""
2223+
],
2224+
"type": "text/javascript"
2225+
}
2226+
}
2227+
],
2228+
"request": {
2229+
"auth": {
2230+
"type": "noauth"
2231+
},
2232+
"method": "DELETE",
2233+
"header": [
2234+
{
2235+
"key": "debug-auth-token",
2236+
"type": "text",
2237+
"value": "{{debug_token}}"
2238+
}
2239+
],
2240+
"url": {
2241+
"raw": "{{host}}/debug/views",
2242+
"host": [
2243+
"{{host}}"
2244+
],
2245+
"path": [
2246+
"debug",
2247+
"views"
2248+
]
2249+
}
2250+
},
2251+
"response": []
2252+
},
21182253
{
21192254
"name": "DEBUG redis view all keys",
21202255
"request": {
21212256
"method": "GET",
2122-
"header": [],
2257+
"header": [
2258+
{
2259+
"key": "debug-auth-token",
2260+
"value": "xX69jules69Xx",
2261+
"type": "text"
2262+
}
2263+
],
21232264
"url": {
21242265
"raw": "{{host}}/debug/redis",
21252266
"host": [
@@ -2168,109 +2309,109 @@
21682309
],
21692310
"variable": [
21702311
{
2171-
"id": "ba54d237-6790-4b6a-9516-a39706728255",
2312+
"id": "9ee0b458-242a-42c6-b5f8-e696eb8a0729",
21722313
"key": "host",
21732314
"value": "http://127.0.0.1:5000",
21742315
"type": "string"
21752316
},
21762317
{
2177-
"id": "83404c17-bd3c-4c78-bcbe-04fc63afcd2c",
2318+
"id": "6c597c98-eb07-40c7-8bd8-e8f2c7015b1b",
21782319
"key": "user_id",
21792320
"value": "",
21802321
"type": "string"
21812322
},
21822323
{
2183-
"id": "8f89a062-8871-4338-bf87-315bc90865ac",
2324+
"id": "532e183c-9ce0-4f3e-a516-3bd255e358ee",
21842325
"key": "user_email",
21852326
"value": "foo@example.org",
21862327
"type": "string"
21872328
},
21882329
{
2189-
"id": "7fe38071-c287-4ef7-b7bc-fade4e70ce41",
2330+
"id": "7abbc682-23eb-47b2-9892-bf5db23af497",
21902331
"key": "user_username",
21912332
"value": "foo",
21922333
"type": "string"
21932334
},
21942335
{
2195-
"id": "089c8889-0774-458b-9ff6-1768db6d496d",
2336+
"id": "982c50f4-bc18-4c6d-8938-282e36ba34de",
21962337
"key": "user_password",
21972338
"value": "admin",
21982339
"type": "string"
21992340
},
22002341
{
2201-
"id": "6049c136-632d-4096-85cf-e07936df0b98",
2342+
"id": "e8c39bfa-cf48-48fe-b231-501aff995bc4",
22022343
"key": "user_firstname",
22032344
"value": "Foo",
22042345
"type": "string"
22052346
},
22062347
{
2207-
"id": "2b124a77-82ee-471b-b0da-d67f33b51968",
2348+
"id": "4c7b2449-9f4f-4f74-8c85-489b5cd5937e",
22082349
"key": "user_lastname",
22092350
"value": "Bar",
22102351
"type": "string"
22112352
},
22122353
{
2213-
"id": "a747331a-a123-4ec5-815a-9a782bb221c2",
2354+
"id": "7a863c36-4345-4a6a-b0ad-a0b097cfef5f",
22142355
"key": "debug_token",
22152356
"value": "xX69jules69Xx",
22162357
"type": "string"
22172358
},
22182359
{
2219-
"id": "310d251f-935b-4bfa-9009-bfa2c9253fde",
2360+
"id": "47d8d8f4-c900-469e-8a7b-5a14ee64ad57",
22202361
"key": "user_access_token",
22212362
"value": "",
22222363
"type": "string"
22232364
},
22242365
{
2225-
"id": "dda8194b-da83-463a-8777-fdf09bd57397",
2366+
"id": "db2500a3-5115-4ef9-a554-cf6fda284240",
22262367
"key": "user_refresh_token",
22272368
"value": "",
22282369
"type": "string"
22292370
},
22302371
{
2231-
"id": "53a51049-0f79-44ed-94d2-776bff535df0",
2372+
"id": "90c8271c-0619-4735-bcaa-0ca95aa4f8ab",
22322373
"key": "expired_token",
22332374
"value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1OTAyNDIyNjQsIm5iZiI6MTU5MDI0MjI2NCwianRpIjoiNTM0ZGI5NzQtOWE4Ni00MGViLWE1NDEtMDg3N2ZmNzQ3NDVhIiwiZXhwIjoxNTkwMjQyMzI0LCJpZGVudGl0eSI6eyJpZCI6NTEyLCJlbWFpbCI6ImZvb0BleGFtcGxlLm9yZyIsInVzZXJuYW1lIjoiYmFyIiwiaXNfb25saW5lIjp0cnVlLCJkYXRlX2xhc3RzZWVuIjoiU2F0LCAyMyBNYXkgMjAyMCAxMzo1Nzo0NCBHTVQifSwiZnJlc2giOnRydWUsInR5cGUiOiJhY2Nlc3MifQ.NTp2P0WNkXDwzuzrcNIQdeAfizQ57HQgkzBLSvY1yHU",
22342375
"type": "string"
22352376
},
22362377
{
2237-
"id": "6b0d9aef-9b57-4dd3-81d7-57f0eac7e050",
2378+
"id": "ce0863fb-3310-40cb-82ff-5a7a0a4e4857",
22382379
"key": "user_orientation",
22392380
"value": "heterosexual",
22402381
"type": "string"
22412382
},
22422383
{
2243-
"id": "5c9181e4-9016-4e21-96ea-5dc4068e7f2a",
2384+
"id": "94603d88-df49-4037-8355-ff595d910444",
22442385
"key": "user_bio",
22452386
"value": "Lorem Ipsum is the single greatest threat. We are not - we are not keeping up with other websites. Lorem Ipsum best not make any more threats to your website. It will be met with fire and fury like the world has never seen. Does everybody know that pig named Lorem Ipsum? An ‘extremely credible source’ has called my office and told me that Barack Obama’s placeholder text is a fraud.",
22462387
"type": "string"
22472388
},
22482389
{
2249-
"id": "a509cf68-fed7-498e-b193-ec4917ab5b64",
2390+
"id": "9eaf6774-eb9a-4f29-8755-edb8905b625e",
22502391
"key": "user_gender",
22512392
"value": "male",
22522393
"type": "string"
22532394
},
22542395
{
2255-
"id": "94c52bbf-019b-48ec-a981-fe71decd979f",
2396+
"id": "396db636-caf7-43e7-9e8c-cf70cd250c36",
22562397
"key": "user_birthdate",
22572398
"value": "1590674628",
22582399
"type": "string"
22592400
},
22602401
{
2261-
"id": "aa7335f6-3aa9-4993-bd22-6d2faeb0ef54",
2402+
"id": "d268c770-a5e1-4933-91d1-9fcdd0ef575e",
22622403
"key": "user_lat",
22632404
"value": "44.8404",
22642405
"type": "string"
22652406
},
22662407
{
2267-
"id": "5037aa20-cc43-483a-8d0f-5f289e3ed3a0",
2408+
"id": "d4fa040b-8348-4edd-839c-e4dfe2eec8d6",
22682409
"key": "user_lng",
22692410
"value": "-0.5805",
22702411
"type": "string"
22712412
},
22722413
{
2273-
"id": "21c4e9a3-fc74-4c92-9f6c-f5eb071db1c1",
2414+
"id": "6d890f0d-9211-4da8-b036-66c1dc204de0",
22742415
"key": "user_ip",
22752416
"value": "77.205.46.181",
22762417
"type": "string"

backend/PyMatcha/models/user.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from PyMatcha.errors import ConflictError
3232
from PyMatcha.errors import NotFoundError
3333
from PyMatcha.models.tag import Tag
34+
from PyMatcha.models.view import View
3435
from PyMatcha.utils import create_user_table
3536
from PyMatcha.utils import hash_password
3637
from PyMatcha.utils.orm import Field
@@ -281,6 +282,26 @@ def get_tags(self):
281282
tags_list.append(Tag(t))
282283
return tags_list
283284

285+
def get_views(self):
286+
logging.debug("Getting all views for user profile {}".format(self.id))
287+
with self.db.cursor() as c:
288+
c.execute(
289+
"""
290+
SELECT views.id as id, views.profile_id as profile_id,
291+
views.viewer_id as viewer_id, views.dt_seen as dt_seen
292+
FROM users
293+
INNER JOIN views on users.id = views.profile_id
294+
WHERE users.id = CAST({} AS UNSIGNED)
295+
""".format(
296+
self.id
297+
)
298+
)
299+
views = c.fetchall()
300+
views_list = []
301+
for v in views:
302+
views_list.append(View(v))
303+
return views_list
304+
284305

285306
def get_user(uid: Any[int, str]) -> Optional[User]:
286307
not_found = 0

backend/PyMatcha/models/view.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
PyMatcha - A Python Dating Website
3+
Copyright (C) 2018-2019 jlasne/gmorer
4+
<jlasne@student.42.fr> - <gmorer@student.42.fr>
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
"""
19+
from __future__ import annotations
20+
21+
import datetime
22+
import logging
23+
from typing import Dict
24+
25+
from PyMatcha.utils import create_views_table
26+
from PyMatcha.utils.orm import Field
27+
from PyMatcha.utils.orm import Model
28+
29+
30+
class View(Model):
31+
table_name = "views"
32+
33+
id = Field(int, modifiable=False)
34+
profile_id = Field(int)
35+
viewer_id = Field(int)
36+
dt_seen = Field(datetime.datetime)
37+
38+
def before_init(self, data):
39+
pass
40+
41+
@staticmethod
42+
def create(profile_id: int, viewer_id: int, dt_seen: datetime.datetime = datetime.datetime.utcnow()) -> View:
43+
new_view = View(profile_id=profile_id, viewer_id=viewer_id, dt_seen=dt_seen)
44+
new_view.save()
45+
logging.debug("Creating new view")
46+
return new_view
47+
48+
def get_all_info(self) -> Dict:
49+
return {"id": self.id, "profile_id": self.profile_id, "viewer_id": self.viewer_id, "dt_seen": self.dt_seen}
50+
51+
@classmethod
52+
def create_table(cls):
53+
create_views_table(cls.db)

0 commit comments

Comments
 (0)