Skip to content

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

Merged
merged 47 commits into from
Feb 24, 2021
Merged

Conversation

Eliory09
Copy link

@Eliory09 Eliory09 commented Feb 3, 2021

Issue #185 : Events geolocation support:

  • Calendar suggest an address and coordinates according to the user's input.
  • Event schema updated - latitude, longitude.
  • Google Maps object in the event view page.

@Eliory09 Eliory09 changed the base branch from main to develop February 3, 2021 01:47
@codecov-io
Copy link

codecov-io commented Feb 3, 2021

Codecov Report

Merging #193 (9d4e1c3) into develop (f85eb84) will increase coverage by 0.00%.
The diff coverage is 93.10%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #193   +/-   ##
========================================
  Coverage    95.40%   95.41%           
========================================
  Files           79       79           
  Lines         3549     3575   +26     
========================================
+ Hits          3386     3411   +25     
- Misses         163      164    +1     
Impacted Files Coverage Δ
app/internal/event.py 95.71% <89.47%> (-2.37%) ⬇️
app/database/models.py 97.24% <100.00%> (+0.02%) ⬆️
app/routers/event.py 97.09% <100.00%> (+0.43%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f85eb84...9d4e1c3. Read the comment docs.

def get_location_coordinates(
address: str,
timeout: float = LOCATION_TIMEOUT
) -> Tuple[float, float, str]:
Copy link
Member

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%">
Copy link
Member

@yammesicka yammesicka Feb 3, 2021

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}}'">
Copy link
Member

@yammesicka yammesicka Feb 3, 2021

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

@Eliory09
Copy link
Author

Eliory09 commented Feb 5, 2021

Typo issues resolved.
Event-color support added.

@@ -57,6 +57,8 @@ class Event(Base):
end = Column(DateTime, nullable=False)
content = Column(String)
location = Column(String)
latitude = Column(String)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can they be null?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, they do. Thanks.

@@ -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(
Copy link
Contributor

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?

return location.latitude, location.longitude, acc_address
except (GeocoderTimedOut, GeocoderUnavailable) as e:
logger.exception(str(e))
return None
Copy link
Contributor

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

Copy link
Author

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No one uses that?

Copy link
Author

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',
Copy link
Contributor

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?

Copy link
Author

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.



class TestGeolocation:

Copy link
Contributor

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

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe _, loaction_name, _ = ..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here :)

@Eliory09 Eliory09 requested a review from yammesicka February 6, 2021 08:54
Copy link
Member

@yammesicka yammesicka left a 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

@@ -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
Copy link
Member

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 :)

Copy link
Author

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.

}

.google_maps_object {
width: 100%
Copy link
Member

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 :))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad! :)

@Eliory09 Eliory09 requested a review from yammesicka February 8, 2021 08:39
@Eliory09 Eliory09 requested a review from yammesicka February 14, 2021 14:07
Copy link
Member

@yammesicka yammesicka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added few insights :)

"""Return location coordinates and accurate
address of the specified location."""
Location = namedtuple('Location', 'latitude, longitude, location')
geolocator = Nominatim(user_agent="calendar", timeout=timeout)
Copy link
Member

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

Comment on lines 112 to 116
if geolocation is not None:
location = Location(geolocation.latitude,
geolocation.longitude,
geolocation.raw["display_name"])
return location
Copy link
Member

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

@@ -1,15 +1,28 @@
from collections import namedtuple
Copy link
Member

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.

}

.google_maps_object {
width: 100%;
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, my bad.

Copy link
Member

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
Copy link
Member

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?

Copy link
Author

@Eliory09 Eliory09 Feb 20, 2021

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.

@@ -0,0 +1,2 @@
import nest_asyncio
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to conftest.py

async def test_get_location_coordinates_correct(location):
# Test geolocation search using valid locations.
location = await get_location_coordinates(location)
assert all(list(location))
Copy link
Member

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">
Copy link
Member

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

@Eliory09 Eliory09 requested a review from yammesicka February 22, 2021 02:03
Copy link
Member

@yammesicka yammesicka left a 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

) -> Union[Location, str]:
"""Return location coordinates and accurate
address of the specified location."""
Location = namedtuple("Location", "latitude, longitude, location")
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, my bad. Fixed.

Comment on lines 21 to 29
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
Copy link
Member

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.

Copy link
Author

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.


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:
Copy link
Member

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)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Fixed.

}

.google_maps_object {
width: 100%;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not resolved :)

@Eliory09 Eliory09 requested a review from yammesicka February 22, 2021 21:50
import json
from datetime import datetime as dt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reorder

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@yammesicka yammesicka merged commit b947751 into PythonFreeCourse:develop Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants