Skip to content

Commit 39fdc87

Browse files
committed
Added edit geoloc
1 parent ca1c3d1 commit 39fdc87

File tree

3 files changed

+238
-16
lines changed

3 files changed

+238
-16
lines changed

PyMatcha.postman_collection.json

Lines changed: 209 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,182 @@
18841884
}
18851885
},
18861886
"response": []
1887+
},
1888+
{
1889+
"name": "Edit geolocation with ip",
1890+
"event": [
1891+
{
1892+
"listen": "test",
1893+
"script": {
1894+
"id": "39231db4-62ff-4230-8a9f-d6bcda2e91ad",
1895+
"exec": [
1896+
"var response = JSON.parse(responseBody);",
1897+
"",
1898+
"",
1899+
"pm.test(\"Status code is 200\", function () {",
1900+
" pm.response.to.have.status(200);",
1901+
"});",
1902+
"",
1903+
"",
1904+
"tests[\"success is true\"] = response.success == true",
1905+
"tests[\"Message is correct\"] = response.message == \"New location sucessfully saved.\""
1906+
],
1907+
"type": "text/javascript"
1908+
}
1909+
}
1910+
],
1911+
"request": {
1912+
"method": "PUT",
1913+
"header": [],
1914+
"body": {
1915+
"mode": "raw",
1916+
"raw": "{\n\t\"ip\": \"{{user_ip}}\"\n}",
1917+
"options": {
1918+
"raw": {
1919+
"language": "json"
1920+
}
1921+
}
1922+
},
1923+
"url": {
1924+
"raw": "{{host}}/profile/edit/geolocation",
1925+
"host": [
1926+
"{{host}}"
1927+
],
1928+
"path": [
1929+
"profile",
1930+
"edit",
1931+
"geolocation"
1932+
]
1933+
}
1934+
},
1935+
"response": []
1936+
},
1937+
{
1938+
"name": "Get one user after ip geoloc edit",
1939+
"event": [
1940+
{
1941+
"listen": "test",
1942+
"script": {
1943+
"id": "499a0ac6-2355-490c-8f0f-010c397591be",
1944+
"exec": [
1945+
"var response = JSON.parse(responseBody);",
1946+
"",
1947+
"",
1948+
"pm.test(\"Status code is 200\", function () {",
1949+
" pm.response.to.have.status(200);",
1950+
"});",
1951+
"",
1952+
"user_email = pm.collectionVariables.get(\"user_email\");",
1953+
"",
1954+
"tests[\"Is geohash correct\"] = response.geohash == \"ezzweqbd4wuk\""
1955+
],
1956+
"type": "text/javascript"
1957+
}
1958+
}
1959+
],
1960+
"request": {
1961+
"method": "GET",
1962+
"header": [],
1963+
"url": {
1964+
"raw": "{{host}}/users/{{user_email}}",
1965+
"host": [
1966+
"{{host}}"
1967+
],
1968+
"path": [
1969+
"users",
1970+
"{{user_email}}"
1971+
]
1972+
}
1973+
},
1974+
"response": []
1975+
},
1976+
{
1977+
"name": "Edit geolocation with lat lng",
1978+
"event": [
1979+
{
1980+
"listen": "test",
1981+
"script": {
1982+
"id": "6f2ec5c9-96dc-4e1b-af46-96352d0d2135",
1983+
"exec": [
1984+
"var response = JSON.parse(responseBody);",
1985+
"",
1986+
"",
1987+
"pm.test(\"Status code is 200\", function () {",
1988+
" pm.response.to.have.status(200);",
1989+
"});",
1990+
"",
1991+
"",
1992+
"tests[\"success is true\"] = response.success == true",
1993+
"tests[\"Message is correct\"] = response.message == \"New location sucessfully saved.\""
1994+
],
1995+
"type": "text/javascript"
1996+
}
1997+
}
1998+
],
1999+
"request": {
2000+
"method": "PUT",
2001+
"header": [],
2002+
"body": {
2003+
"mode": "raw",
2004+
"raw": "{\n\t\"ip\": \"{{user_ip}}\",\n\t\"lat\": {{user_lat}},\n\t\"lng\": {{user_lng}}\n}",
2005+
"options": {
2006+
"raw": {
2007+
"language": "json"
2008+
}
2009+
}
2010+
},
2011+
"url": {
2012+
"raw": "{{host}}/profile/edit/geolocation",
2013+
"host": [
2014+
"{{host}}"
2015+
],
2016+
"path": [
2017+
"profile",
2018+
"edit",
2019+
"geolocation"
2020+
]
2021+
}
2022+
},
2023+
"response": []
2024+
},
2025+
{
2026+
"name": "Get one user after lat lng edit",
2027+
"event": [
2028+
{
2029+
"listen": "test",
2030+
"script": {
2031+
"id": "14daee88-fcc1-4a60-8100-45eded9ecbd8",
2032+
"exec": [
2033+
"var response = JSON.parse(responseBody);",
2034+
"",
2035+
"",
2036+
"pm.test(\"Status code is 200\", function () {",
2037+
" pm.response.to.have.status(200);",
2038+
"});",
2039+
"",
2040+
"user_email = pm.collectionVariables.get(\"user_email\");",
2041+
"",
2042+
"tests[\"Is geohash correct\"] = response.geohash == \"ezzx4fcq327u\""
2043+
],
2044+
"type": "text/javascript"
2045+
}
2046+
}
2047+
],
2048+
"request": {
2049+
"method": "GET",
2050+
"header": [],
2051+
"url": {
2052+
"raw": "{{host}}/users/{{user_email}}",
2053+
"host": [
2054+
"{{host}}"
2055+
],
2056+
"path": [
2057+
"users",
2058+
"{{user_email}}"
2059+
]
2060+
}
2061+
},
2062+
"response": []
18872063
}
18882064
],
18892065
"protocolProfileBehavior": {}
@@ -1992,94 +2168,112 @@
19922168
],
19932169
"variable": [
19942170
{
1995-
"id": "0b227a65-f661-47b2-8bc4-cbaadf557d17",
2171+
"id": "ba54d237-6790-4b6a-9516-a39706728255",
19962172
"key": "host",
19972173
"value": "http://127.0.0.1:5000",
19982174
"type": "string"
19992175
},
20002176
{
2001-
"id": "cda2689f-841d-40cf-83f7-126c84c175e5",
2177+
"id": "83404c17-bd3c-4c78-bcbe-04fc63afcd2c",
20022178
"key": "user_id",
20032179
"value": "",
20042180
"type": "string"
20052181
},
20062182
{
2007-
"id": "901e9e11-29dd-4cff-9f8f-5ef222000d7d",
2183+
"id": "8f89a062-8871-4338-bf87-315bc90865ac",
20082184
"key": "user_email",
20092185
"value": "foo@example.org",
20102186
"type": "string"
20112187
},
20122188
{
2013-
"id": "873da2ea-7d43-4e13-a409-2a42f5b3fada",
2189+
"id": "7fe38071-c287-4ef7-b7bc-fade4e70ce41",
20142190
"key": "user_username",
20152191
"value": "foo",
20162192
"type": "string"
20172193
},
20182194
{
2019-
"id": "11c9af91-cfc0-4bac-a197-6f3efa0edf42",
2195+
"id": "089c8889-0774-458b-9ff6-1768db6d496d",
20202196
"key": "user_password",
20212197
"value": "admin",
20222198
"type": "string"
20232199
},
20242200
{
2025-
"id": "4bb1d9e2-3eee-49b0-9591-8a87e4b28a05",
2201+
"id": "6049c136-632d-4096-85cf-e07936df0b98",
20262202
"key": "user_firstname",
20272203
"value": "Foo",
20282204
"type": "string"
20292205
},
20302206
{
2031-
"id": "69f58f99-73b4-4fc6-bfe6-46ff80240ce2",
2207+
"id": "2b124a77-82ee-471b-b0da-d67f33b51968",
20322208
"key": "user_lastname",
20332209
"value": "Bar",
20342210
"type": "string"
20352211
},
20362212
{
2037-
"id": "e305f6b4-a16d-48e0-b3df-da36fffd800b",
2213+
"id": "a747331a-a123-4ec5-815a-9a782bb221c2",
20382214
"key": "debug_token",
20392215
"value": "xX69jules69Xx",
20402216
"type": "string"
20412217
},
20422218
{
2043-
"id": "89274e60-1321-42ab-9158-6518061bbb5d",
2219+
"id": "310d251f-935b-4bfa-9009-bfa2c9253fde",
20442220
"key": "user_access_token",
20452221
"value": "",
20462222
"type": "string"
20472223
},
20482224
{
2049-
"id": "998a540c-261c-4202-84ea-fb7734042831",
2225+
"id": "dda8194b-da83-463a-8777-fdf09bd57397",
20502226
"key": "user_refresh_token",
20512227
"value": "",
20522228
"type": "string"
20532229
},
20542230
{
2055-
"id": "3553c278-be1c-4547-80a8-5f2c221a40f9",
2231+
"id": "53a51049-0f79-44ed-94d2-776bff535df0",
20562232
"key": "expired_token",
20572233
"value": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1OTAyNDIyNjQsIm5iZiI6MTU5MDI0MjI2NCwianRpIjoiNTM0ZGI5NzQtOWE4Ni00MGViLWE1NDEtMDg3N2ZmNzQ3NDVhIiwiZXhwIjoxNTkwMjQyMzI0LCJpZGVudGl0eSI6eyJpZCI6NTEyLCJlbWFpbCI6ImZvb0BleGFtcGxlLm9yZyIsInVzZXJuYW1lIjoiYmFyIiwiaXNfb25saW5lIjp0cnVlLCJkYXRlX2xhc3RzZWVuIjoiU2F0LCAyMyBNYXkgMjAyMCAxMzo1Nzo0NCBHTVQifSwiZnJlc2giOnRydWUsInR5cGUiOiJhY2Nlc3MifQ.NTp2P0WNkXDwzuzrcNIQdeAfizQ57HQgkzBLSvY1yHU",
20582234
"type": "string"
20592235
},
20602236
{
2061-
"id": "b4792aa7-be9a-45e7-9420-4ddae8a688b7",
2237+
"id": "6b0d9aef-9b57-4dd3-81d7-57f0eac7e050",
20622238
"key": "user_orientation",
20632239
"value": "heterosexual",
20642240
"type": "string"
20652241
},
20662242
{
2067-
"id": "b4f62225-257f-40dc-b89a-517c054381bd",
2243+
"id": "5c9181e4-9016-4e21-96ea-5dc4068e7f2a",
20682244
"key": "user_bio",
20692245
"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.",
20702246
"type": "string"
20712247
},
20722248
{
2073-
"id": "f4264583-0652-4208-9490-f130eec59dfb",
2249+
"id": "a509cf68-fed7-498e-b193-ec4917ab5b64",
20742250
"key": "user_gender",
20752251
"value": "male",
20762252
"type": "string"
20772253
},
20782254
{
2079-
"id": "6c69f0b0-9c4a-4566-b37f-27c4f445dc6c",
2255+
"id": "94c52bbf-019b-48ec-a981-fe71decd979f",
20802256
"key": "user_birthdate",
20812257
"value": "1590674628",
20822258
"type": "string"
2259+
},
2260+
{
2261+
"id": "aa7335f6-3aa9-4993-bd22-6d2faeb0ef54",
2262+
"key": "user_lat",
2263+
"value": "44.8404",
2264+
"type": "string"
2265+
},
2266+
{
2267+
"id": "5037aa20-cc43-483a-8d0f-5f289e3ed3a0",
2268+
"key": "user_lng",
2269+
"value": "-0.5805",
2270+
"type": "string"
2271+
},
2272+
{
2273+
"id": "21c4e9a3-fc74-4c92-9f6c-f5eb071db1c1",
2274+
"key": "user_ip",
2275+
"value": "77.205.46.181",
2276+
"type": "string"
20832277
}
20842278
],
20852279
"protocolProfileBehavior": {}

backend/PyMatcha/routes/api/profile.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import os
2121

2222
import flask_jwt_extended as fjwt
23+
import Geohash
2324
import PyMatcha.models.user as user
2425
from flask import Blueprint
2526
from flask import request
27+
from ip2geotools.databases.noncommercial import DbIpCity
2628
from PyMatcha.errors import BadRequestError
2729
from PyMatcha.errors import NotFoundError
2830
from PyMatcha.errors import UnauthorizedError
@@ -162,3 +164,28 @@ def edit_password():
162164
f" If you believe it wasn't you, please change it immediatly",
163165
)
164166
return Success("User password successfully updated.")
167+
168+
169+
@profile_bp.route("/profile/edit/geolocation", methods=["PUT"])
170+
@fjwt.jwt_required
171+
@validate_params({"ip": str}, {"lat": float, "lng": float})
172+
def edit_geolocation():
173+
data = request.get_json()
174+
ip = data["ip"]
175+
current_user = fjwt.current_user
176+
try:
177+
lat = data["lat"]
178+
lng = data["lng"]
179+
except KeyError:
180+
lat = None
181+
lng = None
182+
183+
if lat and lng:
184+
current_user.geohash = Geohash.encode(lat, lng)
185+
else:
186+
response = DbIpCity.get(ip, api_key="free")
187+
lat = response.latitude
188+
lng = response.longitude
189+
current_user.geohash = Geohash.encode(lat, lng)
190+
current_user.save()
191+
return Success("New location sucessfully saved.")

backend/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ pre-commit==2.4.0
2626

2727
names==0.3.0
2828
lorem==0.1.1
29-
future==0.18.2
29+
future==0.18.2
30+
ip2geotools

0 commit comments

Comments
 (0)