-
Notifications
You must be signed in to change notification settings - Fork 52
Add basic update event #100
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
yammesicka
merged 19 commits into
PythonFreeCourse:develop
from
efratush:feature/update_event
Jan 27, 2021
Merged
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b5d29ad
Add basic update event
efratush 0eedacf
Fix bug
efratush 56d0f3d
Minor repairs
efratush 1aef91c
Merge branch 'develop' into feature/update_event
efratush e2e21bd
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
efratush 69aacde
Improve the update event
efratush 8d949d8
Deleting an unnecessary model
efratush 953d6c2
Improving the presentation of parameters in tests
efratush ab68a68
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
efratush 801a7de
Add package to requirements
efratush fb00604
Add package into requirements.txt
efratush 35ab946
Merge branch 'develop' into feature/update_event
efratush 48bd843
Merge branch 'develop' of https://github.com/PythonFreeCourse/calenda…
efratush 936b4af
Rename the get_event_by_id function
efratush fcd2583
Merge branch 'feature/update_event' of https://github.com/efratush/ca…
efratush cf52bf2
Merge branch 'develop' into feature/update_event
yammesicka 7e821b9
Split the update_event func and rename the validate_dates fun
efratush 97d6929
Fix flake8
efratush eea0a99
Merge branch 'develop' into feature/update_event
yammesicka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
from fastapi import APIRouter, Request | ||
from datetime import datetime | ||
from typing import Dict, Optional, Set | ||
|
||
from app.database.models import Event | ||
from app.dependencies import templates | ||
from fastapi import APIRouter, Request | ||
from sqlalchemy import exc | ||
from sqlalchemy.orm import Session | ||
|
||
router = APIRouter( | ||
prefix="/event", | ||
|
@@ -19,3 +24,73 @@ async def eventedit(request: Request): | |
async def eventview(request: Request, id: int): | ||
return templates.TemplateResponse("event/eventview.html", | ||
{"request": request, "event_id": id}) | ||
|
||
|
||
def get_event_by_id(db: Session, event_id: int) -> Event: | ||
"""Select event by id""" | ||
return db.query(Event).filter(Event.id == event_id).first() | ||
|
||
|
||
# copy from michael ben david | ||
def check_validation(start_time: datetime, end_time: datetime) -> bool: | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Check if the start time is earlier than the end time""" | ||
|
||
if start_time < end_time: | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return True | ||
return False | ||
|
||
|
||
def get_column_names_event(db: Session) -> Set: | ||
result = db.execute("select * from events") | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return {col for col in result.keys()} | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def check_date_valitation(old_event: Event, event_update: Event): | ||
""" Checks if the time change is possible""" | ||
try: | ||
if not check_validation(event_update.start, event_update.end): | ||
return False | ||
except TypeError: | ||
try: | ||
if event_update.start and not event_update.end: | ||
if not check_validation(event_update.start, old_event.end): | ||
return False | ||
elif not check_validation(old_event.start, event_update.end): | ||
return False | ||
except TypeError: | ||
return False | ||
return True | ||
|
||
|
||
def update_event(event_id: int, event_items: Dict, db: Session | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) -> Optional[Event]: | ||
# To do: Check if the user is the owner of the event. | ||
if not bool(event_items): | ||
return None | ||
event_items.pop('id', None) | ||
event_items.pop('owner_id', None) | ||
|
||
try: | ||
old_event = get_event_by_id(db=db, event_id=event_id) | ||
except AttributeError: # Problem connecting to db | ||
return None | ||
|
||
if old_event is None: # No such event number. | ||
return None | ||
|
||
column_names = get_column_names_event(db) | ||
for key in event_items.keys(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can leave out the |
||
if key not in column_names: | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return None | ||
|
||
update = Event(**event_items) | ||
if not check_date_valitation(old_event, update): | ||
return None | ||
try: | ||
db.query(Event).filter(Event.id == event_id).update( | ||
event_items, synchronize_session=False) | ||
db.commit() | ||
# To do: Sending emails and reset. | ||
except (AttributeError, exc.SQLAlchemyError): | ||
return None | ||
return get_event_by_id(db=db, event_id=event_id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,64 @@ | ||
from fastapi.testclient import TestClient | ||
from datetime import datetime | ||
|
||
import pytest | ||
from app.database.models import Event | ||
from app.main import app | ||
from app.routers.event import update_event | ||
|
||
# this is fallback for those who's not getting | ||
# the client in the function - FYI YAM. | ||
from fastapi.testclient import TestClient | ||
client = TestClient(app) | ||
|
||
DATA_UPDATE_OPTIONS = [ | ||
{}, {"test": "test"}, {"start": "20.01.2020"}, | ||
{"start": datetime(2020, 2, 2), "end": datetime(2020, 1, 1)}, | ||
{"start": datetime(2030, 2, 2)}, {"end": datetime(1990, 1, 1)}, | ||
{"start": "2020-02-02", "end": "2021-12-21"}, | ||
] | ||
|
||
|
||
def test_eventedit(): | ||
def test_eventedit(client): | ||
response = client.get("/event/edit") | ||
assert response.status_code == 200 | ||
assert b"Edit Event" in response.content | ||
|
||
|
||
def test_eventview_with_id(): | ||
def test_eventview_with_id(client): | ||
response = client.get("/event/view/1") | ||
assert response.status_code == 200 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use starlette.status for status code, also below |
||
assert b"View Event" in response.content | ||
|
||
|
||
def test_eventview_without_id(): | ||
def test_eventview_without_id(client): | ||
response = client.get("/event/view") | ||
assert response.status_code == 404 | ||
|
||
|
||
@pytest.mark.parametrize("data", DATA_UPDATE_OPTIONS) | ||
def test_invalid_update(data, event, session): | ||
assert update_event(1, data, session) is None | ||
efratush marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_successful_update(event, session): | ||
data = { | ||
"title": "successful", | ||
"start": datetime(2021, 1, 20), | ||
"end": datetime(2021, 1, 21), | ||
} | ||
assert type(update_event(1, data, session)) == Event | ||
assert "successful" in update_event(1, data, session).title | ||
|
||
|
||
def test_update_db_close(event): | ||
data = { | ||
"title": "Problem connecting to db", | ||
} | ||
assert update_event(1, data, db=None) is None | ||
|
||
|
||
def test_update_event_does_not_exist(event, session): | ||
data = { | ||
"content": "An update test for an event does not exist" | ||
} | ||
assert update_event(5, data, db=session) is None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.