-
Notifications
You must be signed in to change notification settings - Fork 52
feat: adding holidays to calendar as events #165
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
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
307e584
feat: adding holidays to calendar as events
f8f4d5d
fix: unused import and long lines
a064a89
fix: add os to get folder location for test
468d18b
fix: add line break
a1fec2b
fix: another try for pytest relative folder for my test
af814ad
fix: another try for pytest relative folder for my test
94f5ede
fix: another try for pytest relative folder for my test
6597f7a
fix: fixed merge request comments
bcc34a2
fix: fixed merge request comments
d67a4e3
fix: fixed merge request comments
d2d1d52
fix: fixed awaited test failure
e2ff48e
fix: fixed blank line
1d7b2b7
fix: removed unused import
c38afb1
fix: renamed 128 into variable
ec85764
fix: fix line too long
dfbd98c
fix: removed not needed code
6292b0c
fix: fixed mr comments - added one more test and minor fixes
53c3b00
fix: fixed new line at end of file
3388982
fix: added type annotations
b79af9c
Merge remote-tracking branch 'upstream/develop' into holiday
edfe81e
fix: solve merge conflict
b042297
fix: added type annotations
410aae1
Merge branch 'develop' into holiday
018f82d
fix: merge conflict
e42f6be
fix: fixed type annotation for test
65e8507
fix: fixed mr comments
c4d6b30
Merge branch 'develop' into holiday
43d1344
fix: merge conflict
b712b4b
fix: solve mr comments
b29f6f9
fix: solve mr comments
191d8e7
Merge branch 'develop' into holiday
3d1ad20
fix: solve mr comments
b9c6402
fix: moved import holidays to internal folder
995d423
fix: mr comments
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import re | ||
from datetime import datetime, timedelta | ||
|
||
from app.database.models import User, Event, UserEvent | ||
from sqlalchemy.orm import Session | ||
from typing import List, Match | ||
|
||
REGEX_EXTRACT_HOLIDAYS = re.compile( | ||
r'SUMMARY:(?P<title>.*)(\n.*){1,8}DTSTAMP:(?P<date>\w{8})', | ||
re.MULTILINE) | ||
|
||
|
||
def get_holidays_from_file(file: List[Event], session: Session) -> List[Event]: | ||
""" | ||
This function using regex to extract holiday title | ||
and date from standrd ics file | ||
:param file:standard ics file | ||
:param session:current connection | ||
:return:list of holidays events | ||
""" | ||
parsed_holidays = REGEX_EXTRACT_HOLIDAYS.finditer(file) | ||
holidays = [] | ||
for holiday in parsed_holidays: | ||
holiday_event = create_holiday_event( | ||
holiday, session.query(User).filter_by(id=1).first().id) | ||
holidays.append(holiday_event) | ||
return holidays | ||
|
||
|
||
def create_holiday_event(holiday: Match[str], owner_id: int) -> Event: | ||
valid_ascii_chars_range = 128 | ||
title = holiday.groupdict()['title'].strip() | ||
title_to_save = ''.join(i if ord(i) < valid_ascii_chars_range | ||
else '' for i in title) | ||
date = holiday.groupdict()['date'].strip() | ||
format_string = '%Y%m%d' | ||
holiday = Event( | ||
title=title_to_save, | ||
start=datetime.strptime(date, format_string), | ||
end=datetime.strptime(date, format_string) + timedelta(days=1), | ||
content='holiday', | ||
owner_id=owner_id | ||
) | ||
return holiday | ||
|
||
|
||
def save_holidays_to_db(holidays: List[Event], session: Session): | ||
""" | ||
this function saves holiday list into database. | ||
:param holidays: list of holidays events | ||
:param session: current connection | ||
""" | ||
session.add_all(holidays) | ||
session.commit() | ||
session.flush(holidays) | ||
userevents = [] | ||
for holiday in holidays: | ||
userevent = UserEvent( | ||
user_id=holiday.owner_id, | ||
event_id=holiday.id | ||
) | ||
userevents.append(userevent) | ||
session.add_all(userevents) | ||
session.commit() |
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 |
---|---|---|
|
@@ -64,3 +64,7 @@ body { | |
border: none; | ||
background-color: whitesmoke; | ||
} | ||
|
||
.subtitle { | ||
font-size: 20px; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{% extends "base.html" %} | ||
|
||
|
||
{% block content %} | ||
|
||
<form class="m-3" action="update_holidays" method="post" enctype="multipart/form-data"> | ||
<h1 class="title">Import holidays using ics file</h1> | ||
yammesicka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<p class="subtitle">Use the upload button to choose ics file</p> | ||
<a href="https://www.feiertagskalender.ch/export.php?geo=3542&hl=en#" target="_blank">Check this website to export holidays by country</a> | ||
<div class="input-group"> | ||
<label for="file"></label> | ||
<input type="file" class="form-control-sm" accept=".ics" id="file"> | ||
</div> | ||
<div class="mt-4"> | ||
<button type="submit" class="btn btn-sm btn-success">Add events from file to calender</button> | ||
</div> | ||
</form> | ||
|
||
{% endblock %} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
STATUS:CONFIRMED | ||
DTSTAMP:20210225T000000 | ||
DTSTART;VALUE=DATE:20210225 | ||
UID:feiertag2021-0323-3542-fcal.ch | ||
CATEGORIES:Public holidays | ||
END:VEVENT | ||
BEGIN:VEVENT | ||
SUMMARY:Purim | ||
DESCRIPTION:Feiertagskalender.ch - free data for private /internal use only. | ||
STATUS:CONFIRMED | ||
DTSTAMP:20210226T000000 | ||
DTSTART;VALUE=DATE:20210226 | ||
UID:feiertag2021-0324-3542-fcal.ch | ||
CATEGORIES:Public holidays | ||
END:VEVENT | ||
BEGIN:VEVENT | ||
SUMMARY:Pesach | ||
DESCRIPTION:Feiertagskalender.ch - free data for private /internal use only. | ||
STATUS:CONFIRMED | ||
DTSTAMP:20210328T000000 | ||
DTSTART;VALUE=DATE:20210328 | ||
UID:feiertag2021-0336-3542-fcal.ch | ||
CATEGORIES:Public holidays | ||
END:VEVENT | ||
BEGIN:VEVENT | ||
SUMMARY:Pesach | ||
DESCRIPTION:Feiertagskalender.ch - free data for private /internal use only. | ||
STATUS:CONFIRMED | ||
DTSTAMP:20210329T010000 | ||
DTSTART;VALUE=DATE:20210329 | ||
UID:feiertag2021-0336-3542-fcal.ch | ||
CATEGORIES:Public holidays | ||
END:VEVENT | ||
BEGIN:VEVENT | ||
SUMMARY:One More Holiday For Test | ||
DTSTAMP:20210101T010000 |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
STATUS:CONFIRMED | ||
DTSTAMP:20210225T000000 | ||
DTSTART;VALUE=DATE:20210225 | ||
UID:feiertag2021-0323-3542-fcal.ch | ||
CATEGORIES:Public holidays | ||
END:VEVENT | ||
BEGIN:VEVENT | ||
DESCRIPTION:Feiertagskalender.ch - free data for private /internal use only. | ||
STATUS:CONFIRMED | ||
DTSTAMP:20210226T000000 | ||
DTSTART;VALUE=DATE:20210226 | ||
UID:feiertag2021-0324-3542-fcal.ch | ||
CATEGORIES:Public holidays | ||
END:VEVENT | ||
BEGIN:VEVENT | ||
SUMMARY:Pesach | ||
DESCRIPTION:Feiertagskalender.ch - free data for private /internal use only. | ||
STATUS:CONFIRMED | ||
DTSTART;VALUE=DATE:20210328 | ||
UID:feiertag2021-0336-3542-fcal.ch | ||
CATEGORIES:Public holidays | ||
END:VEVENT | ||
BEGIN:VEVENT | ||
SUMMARY:Pesach | ||
DESCRIPTION:Feiertagskalender.ch - free data for private /internal use only. |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
from app.database.models import Event, User | ||
galzunited marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from app.routers import profile | ||
from sqlalchemy.orm import Session | ||
|
||
|
||
class TestHolidaysImport: | ||
HOLIDAYS = '/profile/holidays/import' | ||
|
||
@staticmethod | ||
def test_import_holidays_page_exists(client): | ||
galzunited marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resp = client.get(TestHolidaysImport.HOLIDAYS) | ||
assert resp.ok | ||
assert b'Import holidays using ics file' in resp.content | ||
|
||
def test_get_holidays(self, session: Session, user: User): | ||
current_folder = os.path.dirname(os.path.realpath(__file__)) | ||
resource_folder = os.path.join(current_folder, 'resources') | ||
test_file = os.path.join(resource_folder, 'ics_example.txt') | ||
with open(test_file) as file: | ||
ics_content = file.read() | ||
holidays = profile.get_holidays_from_file(ics_content, session) | ||
profile.save_holidays_to_db(holidays, session) | ||
assert len(session.query(Event).all()) == 4 | ||
|
||
def test_wrong_file_get_holidays(self, session: Session, user: User): | ||
current_folder = os.path.dirname(os.path.realpath(__file__)) | ||
resource_folder = os.path.join(current_folder, 'resources') | ||
test_file = os.path.join(resource_folder, 'wrong_ics_example.txt') | ||
with open(test_file) as file: | ||
ics_content = file.read() | ||
holidays = profile.get_holidays_from_file(ics_content, session) | ||
profile.save_holidays_to_db(holidays, session) | ||
assert len(session.query(Event).all()) == 0 |
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.