-
Notifications
You must be signed in to change notification settings - Fork 52
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/export #213
Merged
yammesicka
merged 13 commits into
PythonFreeCourse:develop
from
IdanPelled:feature/export
Feb 14, 2021
Merged
Feature/export #213
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6aa5279
feat: add export calendar
IdanPelled ad81594
Merge branch 'develop' into feature/export
IdanPelled 98b49dc
feat: flaek8
IdanPelled 8729589
Merge branch 'develop' into feature/export
IdanPelled daaf5de
fix: setting bug
IdanPelled b9c60af
Merge branch 'develop' into feature/export
IdanPelled 4c24eca
Merge branch 'develop' into feature/export
IdanPelled f2d1e89
feat: add peer review changes
IdanPelled 43af21c
Merge branch 'develop' into feature/export
IdanPelled fc68f95
feat: add peer review changes
IdanPelled 6c210fe
fix: minor testing bug
IdanPelled 22802d1
add: peer review changes
IdanPelled fc1c55c
Merge branch 'develop' into feature/export
IdanPelled File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
from datetime import datetime | ||
from typing import List | ||
|
||
import pytz | ||
from icalendar import Calendar, vCalAddress, vText | ||
from icalendar import Event as IcalEvent | ||
from sqlalchemy.orm import Session | ||
|
||
from app.config import DOMAIN, ICAL_VERSION, PRODUCT_ID | ||
from app.database.models import Event | ||
from app.internal.email import verify_email_pattern | ||
from app.routers.event import get_attendees_email | ||
|
||
|
||
def generate_id(event: Event) -> bytes: | ||
"""Creates an unique id.""" | ||
return ( | ||
str(event.id) | ||
+ event.start.strftime('%Y%m%d') | ||
+ event.end.strftime('%Y%m%d') | ||
+ f'@{DOMAIN}' | ||
).encode() | ||
|
||
|
||
def create_ical_calendar(): | ||
"""Creates an ical calendar, and adds the required information""" | ||
cal = Calendar() | ||
cal.add('version', ICAL_VERSION) | ||
cal.add('prodid', PRODUCT_ID) | ||
|
||
return cal | ||
|
||
|
||
def add_optional(user_event, data): | ||
"""Adds an optional field if it exists.""" | ||
if user_event.location: | ||
data.append(('location', user_event.location)) | ||
|
||
if user_event.content: | ||
data.append(('description', user_event.content)) | ||
|
||
return data | ||
|
||
|
||
def create_ical_event(user_event): | ||
"""Creates an ical event, and adds the event information""" | ||
ievent = IcalEvent() | ||
data = [ | ||
('organizer', add_attendee(user_event.owner.email, organizer=True)), | ||
('uid', generate_id(user_event)), | ||
('dtstart', user_event.start), | ||
('dtstamp', datetime.now(tz=pytz.utc)), | ||
('dtend', user_event.end), | ||
('summary', user_event.title), | ||
] | ||
|
||
data = add_optional(user_event, data) | ||
|
||
for param in data: | ||
ievent.add(*param) | ||
|
||
return ievent | ||
|
||
|
||
def add_attendee(email, organizer=False): | ||
IdanPelled marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Adds an attendee to the event.""" | ||
attendee = vCalAddress(f'MAILTO:{email}') | ||
if organizer: | ||
attendee.params['partstat'] = vText('ACCEPTED') | ||
attendee.params['role'] = vText('CHAIR') | ||
else: | ||
attendee.params['partstat'] = vText('NEEDS-ACTION') | ||
attendee.params['role'] = vText('PARTICIPANT') | ||
|
||
return attendee | ||
|
||
|
||
def add_attendees( | ||
ievent: IcalEvent, | ||
attendees: List[str], | ||
): | ||
"""Adds attendees for the event.""" | ||
for email in attendees: | ||
if verify_email_pattern(email): | ||
ievent.add( | ||
'attendee', | ||
add_attendee(email), | ||
encode=0 | ||
) | ||
|
||
return ievent | ||
|
||
|
||
def event_to_ical(user_event: Event, attendees: List[str]) -> bytes: | ||
"""Returns an ical event, given an | ||
"Event" instance and a list of email.""" | ||
ical = create_ical_calendar() | ||
ievent = create_ical_event(user_event) | ||
ievent = add_attendees(ievent, attendees) | ||
ical.add_component(ievent) | ||
|
||
return ical.to_ical() | ||
|
||
|
||
def export_calendar(session: Session, events: List[Event]) -> bytes: | ||
"""Returns an icalendar, given an list of | ||
"Event" instances and a list of email.""" | ||
ical = create_ical_calendar() | ||
for event in events: | ||
ievent = create_ical_event(event) | ||
|
||
attendees = get_attendees_email(session, event) | ||
attendees.remove((event.owner.email,)) | ||
|
||
ievent = add_attendees(ievent, attendees) | ||
ical.add_component(ievent) | ||
|
||
return ical.to_ical() |
This file contains 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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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 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,103 +1,37 @@ | ||
from datetime import datetime | ||
from typing import List | ||
|
||
from icalendar import Calendar, Event, vCalAddress, vText | ||
import pytz | ||
|
||
from app.config import DOMAIN, ICAL_VERSION, PRODUCT_ID | ||
from app.database.models import Event as UserEvent | ||
|
||
|
||
def generate_id(event: UserEvent) -> bytes: | ||
"""Creates an unique id.""" | ||
|
||
return ( | ||
str(event.id) | ||
+ event.start.strftime('%Y%m%d') | ||
+ event.end.strftime('%Y%m%d') | ||
+ f'@{DOMAIN}' | ||
).encode() | ||
|
||
|
||
def create_ical_calendar(): | ||
"""Creates an ical calendar, | ||
and adds the required information""" | ||
|
||
cal = Calendar() | ||
cal.add('version', ICAL_VERSION) | ||
cal.add('prodid', PRODUCT_ID) | ||
|
||
return cal | ||
|
||
|
||
def add_optional(user_event, data): | ||
"""Adds an optional field if it exists.""" | ||
|
||
if user_event.location: | ||
data.append(('location', user_event.location)) | ||
|
||
if user_event.content: | ||
data.append(('description', user_event.content)) | ||
|
||
return data | ||
|
||
|
||
def create_ical_event(user_event): | ||
"""Creates an ical event, | ||
and adds the event information""" | ||
|
||
ievent = Event() | ||
data = [ | ||
('organizer', add_attendee(user_event.owner.email, organizer=True)), | ||
('uid', generate_id(user_event)), | ||
('dtstart', user_event.start), | ||
('dtstamp', datetime.now(tz=pytz.utc)), | ||
('dtend', user_event.end), | ||
('summary', user_event.title), | ||
] | ||
|
||
data = add_optional(user_event, data) | ||
|
||
for param in data: | ||
ievent.add(*param) | ||
|
||
return ievent | ||
|
||
|
||
def add_attendee(email, organizer=False): | ||
"""Adds an attendee to the event.""" | ||
|
||
attendee = vCalAddress(f'MAILTO:{email}') | ||
if organizer: | ||
attendee.params['partstat'] = vText('ACCEPTED') | ||
attendee.params['role'] = vText('CHAIR') | ||
else: | ||
attendee.params['partstat'] = vText('NEEDS-ACTION') | ||
attendee.params['role'] = vText('PARTICIPANT') | ||
|
||
return attendee | ||
|
||
|
||
def add_attendees(ievent, attendees: list): | ||
"""Adds attendees for the event.""" | ||
|
||
for email in attendees: | ||
ievent.add( | ||
'attendee', | ||
add_attendee(email), | ||
encode=0 | ||
) | ||
|
||
return ievent | ||
|
||
|
||
def event_to_ical(user_event: UserEvent, attendees: List[str]) -> bytes: | ||
"""Returns an ical event, given an | ||
"UserEvent" instance and a list of email.""" | ||
|
||
ical = create_ical_calendar() | ||
ievent = create_ical_event(user_event) | ||
ievent = add_attendees(ievent, attendees) | ||
ical.add_component(ievent) | ||
|
||
return ical.to_ical() | ||
from datetime import date | ||
from io import BytesIO | ||
from typing import Union | ||
|
||
from fastapi import APIRouter, Depends | ||
from sqlalchemy.orm import Session | ||
from starlette.responses import StreamingResponse | ||
|
||
from app.dependencies import get_db | ||
from app.internal.agenda_events import get_events_in_time_frame | ||
from app.internal.export import export_calendar | ||
|
||
router = APIRouter( | ||
prefix="/export", | ||
tags=["export"], | ||
responses={404: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@router.get("/") | ||
def export( | ||
start_date: Union[date, str], # date or an empty string | ||
end_date: Union[date, str], | ||
db: Session = Depends(get_db), | ||
) -> StreamingResponse: | ||
|
||
user_id = 1 | ||
events = get_events_in_time_frame(start_date, end_date, user_id, db) | ||
file = BytesIO(export_calendar(db, list(events))) | ||
return StreamingResponse( | ||
content=file, | ||
media_type="text/calendar", | ||
headers={ | ||
# change filename to "pylendar.ics" | ||
"Content-Disposition": "attachment;filename=pylendar.ics" | ||
}, | ||
) |
This file contains 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 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 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
Oops, something went wrong.
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.
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.
Blank line after this line