-
Notifications
You must be signed in to change notification settings - Fork 52
Feature/geolocation #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/geolocation #193
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #193 +/- ##
========================================
Coverage 95.40% 95.41%
========================================
Files 79 79
Lines 3549 3575 +26
========================================
+ Hits 3386 3411 +25
- Misses 163 164 +1
Continue to review full report at Codecov.
|
app/routers/event.py
Outdated
def get_location_coordinates( | ||
address: str, | ||
timeout: float = LOCATION_TIMEOUT | ||
) -> Tuple[float, float, str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type hinting is incorrect
@@ -25,6 +25,20 @@ <h1>{{event.title}}</h1> | |||
<!-- <address>LOCATION / <a href="#">VC URL</a></address>--> | |||
</div> | |||
|
|||
{% if event.latitude is not none and event.longitude is not none %} | |||
<div style="width: 100%"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use inline styles
@@ -1,4 +1,4 @@ | |||
<div class="event_info_row title" style="border-bottom: 4px solid {{event.color}}"> | |||
<div class="event_info_row title" style="border-bottom: 4px solid '{{event.color}}'"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use inline styles
Typo issues resolved. |
app/database/models.py
Outdated
@@ -57,6 +57,8 @@ class Event(Base): | |||
end = Column(DateTime, nullable=False) | |||
content = Column(String) | |||
location = Column(String) | |||
latitude = Column(String) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can they be null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, they do. Thanks.
app/routers/event.py
Outdated
@@ -40,17 +46,26 @@ async def create_new_event(request: Request, session=Depends(get_db)): | |||
end = datetime.strptime(data['end_date'] + ' ' + data['end_time'], | |||
'%Y-%m-%d %H:%M') | |||
user = session.query(User).filter_by(id=1).first() | |||
user = user if user else create_user("u", "p", "e@mail.com", session) | |||
user = user if user else create_user( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a function get_user_or_create_default
?
app/routers/event.py
Outdated
return location.latitude, location.longitude, acc_address | ||
except (GeocoderTimedOut, GeocoderUnavailable) as e: | ||
logger.exception(str(e)) | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can return (None, None, location), and then you could remove if location_details is not None:
in line 64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea 👍
@@ -50,10 +50,6 @@ div.event_info_row, | |||
margin-block-end: 0.2em; | |||
} | |||
|
|||
.title { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No one uses that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was used in view_event_details_tab.html and nowhere else.
The event color was set to blue by default in class .title.
I removed the class and switched it with a class for each color, according to the events color in the db.
@@ -43,6 +43,7 @@ def fake_user_events(session): | |||
create_event( | |||
db=session, | |||
title='Cool today event', | |||
color='red', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I got that, are the colors related to your feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. While coding, 30 tests were failed because "color" column added to User schema and was pushed but wasn't supported by all tests written so far. Fixed it and added colors to some tests to make sure everything works.
tests/test_geolocation.py
Outdated
|
||
|
||
class TestGeolocation: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove blank line in line 7
tests/test_geolocation.py
Outdated
assert response.ok | ||
url = event_test_client.app.url_path_for('eventview', event_id=1) | ||
response = event_test_client.get(url) | ||
location = get_location_coordinates( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe _, loaction_name, _ = ..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
tests/test_geolocation.py
Outdated
assert response.ok | ||
url = event_test_client.app.url_path_for('eventview', event_id=1) | ||
response = event_test_client.get(url) | ||
location = get_location_coordinates( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great progress! :) We're almost there
app/routers/event.py
Outdated
@@ -108,6 +117,15 @@ def by_id(db: Session, event_id: int) -> Event: | |||
return event | |||
|
|||
|
|||
def get_user_or_create_default(user: User, session: Session) -> User: | |||
"""Check if the user is logged in, else create a fictive user |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a blank line after the docstring's summary line :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to remove the method, after it was edited by someone else.
app/static/event/eventview.css
Outdated
} | ||
|
||
.google_maps_object { | ||
width: 100% |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
; (You missed it in 4 places :))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad! :)
…into feature/geolocation
…into feature/geolocation
…into feature/geolocation
…endar into feature/geolocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added few insights :)
app/internal/event.py
Outdated
"""Return location coordinates and accurate | ||
address of the specified location.""" | ||
Location = namedtuple('Location', 'latitude, longitude, location') | ||
geolocator = Nominatim(user_agent="calendar", timeout=timeout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fortunately, this module does support work with async.
Use async with
app/internal/event.py
Outdated
if geolocation is not None: | ||
location = Location(geolocation.latitude, | ||
geolocation.longitude, | ||
geolocation.raw["display_name"]) | ||
return location |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these lines can raise an exception? If not, put them outside the try/except
app/internal/event.py
Outdated
@@ -1,15 +1,28 @@ | |||
from collections import namedtuple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not using this anymore, please remove.
app/static/event/eventview.css
Outdated
} | ||
|
||
.google_maps_object { | ||
width: 100%; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please set the indentation in the CSS to 2 spaces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, my bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still not resolved :)
@@ -58,6 +59,7 @@ MarkupSafe==1.1.1 | |||
mccabe==0.6.1 | |||
mypy==0.790 | |||
mypy-extensions==0.4.3 | |||
nest-asyncio==1.5.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You sure you need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. My tests encounter event loop errors if I don't use it.
tests/__init__.py
Outdated
@@ -0,0 +1,2 @@ | |||
import nest_asyncio |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to conftest.py
tests/test_geolocation.py
Outdated
async def test_get_location_coordinates_correct(location): | ||
# Test geolocation search using valid locations. | ||
location = await get_location_coordinates(location) | ||
assert all(list(location)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
list
is redundant
<!-- <span class="icon">PRIVACY</span>--> | ||
</div> | ||
<div class="event_info_row title_color_{{event.color}}" > | ||
<div class="event_info_row_start"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer 2 spaces, not 4
…into feature/geolocation
…into feature/geolocation
…into feature/geolocation
…into feature/geolocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great progress! I've added few further insights
app/internal/event.py
Outdated
) -> Union[Location, str]: | ||
"""Return location coordinates and accurate | ||
address of the specified location.""" | ||
Location = namedtuple("Location", "latitude, longitude, location") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define outside, use typing.NamedTuple
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, my bad. Fixed.
app/routers/event.py
Outdated
from fastapi import APIRouter, Depends, HTTPException, Request | ||
from pydantic import BaseModel | ||
from sqlalchemy.exc import SQLAlchemyError | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound | ||
from sqlalchemy.sql.elements import Null | ||
from starlette import status | ||
from starlette.responses import RedirectResponse, Response | ||
from starlette.templating import _TemplateResponse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines should appear on the 2nd paragraph. Please use git precommit hooks to order it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, precommit hooks didn't fix it automatically. I separated the paragraphs.
app/routers/event.py
Outdated
|
||
if vc_link: | ||
raise_if_zoom_link_invalid(vc_link) | ||
else: | ||
location_details = await get_location_coordinates(location) | ||
if type(location_details) is not str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not isinstance(location_details, str)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Fixed.
app/static/event/eventview.css
Outdated
} | ||
|
||
.google_maps_object { | ||
width: 100%; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still not resolved :)
…into feature/geolocation
app/routers/event.py
Outdated
import json | ||
from datetime import datetime as dt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reorder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Issue #185 : Events geolocation support: