Skip to content

fix: event backend and frontend creation #329

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 3 commits into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Event(Base):
content = Column(String)
location = Column(String, nullable=True)
is_google_event = Column(Boolean, default=False)
vc_link = Column(String)
vc_link = Column(String, nullable=True)
color = Column(String, nullable=True)
all_day = Column(Boolean, default=False)
invitees = Column(String)
Expand Down
67 changes: 42 additions & 25 deletions app/internal/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,55 @@

from app.database.models import Event

ZOOM_REGEX = re.compile(r'https://.*?\.zoom.us/[a-z]/.[^.,\b\s]+')
ZOOM_REGEX = re.compile(r"https://.*?\.zoom.us/[a-z]/.[^.,\b\s]+")


def raise_if_zoom_link_invalid(vc_link):
if ZOOM_REGEX.search(vc_link) is None:
raise HTTPException(status_code=HTTP_400_BAD_REQUEST,
detail="VC type with no valid zoom link")
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
detail="VC type with no valid zoom link",
)


def get_invited_emails(invited_from_form: str) -> List[str]:
invited_emails = []
for invited_email in invited_from_form.split(','):
if not invited_from_form:
return [""]
for invited_email in invited_from_form.split(","):
invited_email = invited_email.strip()
try:
validate_email(invited_email, check_deliverability=False)
except EmailSyntaxError:
logging.exception(f'{invited_email} is not a valid email address')
continue
invited_emails.append(invited_email)
logging.exception(
f"{invited_email} is not a valid email address",
)
else:
invited_emails.append(invited_email)

return invited_emails


def get_uninvited_regular_emails(session: Session,
owner_id: int,
title: str,
invited_emails: List[str]) -> Set[str]:
def get_uninvited_regular_emails(
session: Session,
owner_id: int,
title: str,
invited_emails: List[str],
) -> Set[str]:
invitees_query = session.query(Event).with_entities(Event.invitees)
similar_events_invitees = invitees_query.filter(Event.owner_id == owner_id,
Event.title == title).all()
similar_events_invitees = invitees_query.filter(
Event.owner_id == owner_id,
Event.title == title,
).all()
regular_invitees = set()
for record in similar_events_invitees:
if record:
regular_invitees.update(record[0].split(','))
regular_invitees.update(record[0].split(","))

return regular_invitees - set(invited_emails)


def check_diffs(checked_event: Event,
all_events: List[Event]):
def check_diffs(checked_event: Event, all_events: List[Event]):
"""Returns the repeated events and the week difference"""
diffs = []
for event in all_events:
Expand All @@ -65,22 +74,30 @@ def check_diffs(checked_event: Event,


def find_pattern(session, event):
all_events_with_same_name = session.query(Event).filter(
Event.owner_id == event.owner_id, Event.title == event.title).all()
all_events_with_same_name = (
session.query(Event)
.filter(Event.owner_id == event.owner_id, Event.title == event.title)
.all()
)

return check_diffs(event, all_events_with_same_name)


def get_messages(session: Session,
event: Event,
uninvited_contacts: Set[str]) -> List[str]:
def get_messages(
session: Session,
event: Event,
uninvited_contacts: Set[str],
) -> List[str]:
messages = []
if uninvited_contacts:
messages.append(f'Forgot to invite '
f'{", ".join(uninvited_contacts)} maybe?')
messages.append(
f"Forgot to invite " f'{", ".join(uninvited_contacts)} maybe?',
)

pattern = find_pattern(session, event)
for weeks_diff in pattern:
messages.append(f'Same event happened {weeks_diff} weeks before too. '
f'Want to create another one {weeks_diff} after too?')
messages.append(
f"Same event happened {weeks_diff} weeks before too. "
f"Want to create another one {weeks_diff} after too?",
)
return messages
6 changes: 3 additions & 3 deletions app/routers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def create_event_api(event: EventModel, session=Depends(get_db)):
db=session,
title=event.title,
start=event.start,
end=event.start,
end=event.end,
content=event.content,
owner_id=event.owner_id,
location=event.location,
Expand Down Expand Up @@ -117,7 +117,7 @@ async def create_new_event(
location = data["location"]
all_day = data["event_type"] and data["event_type"] == "on"

vc_link = data["vc_link"]
vc_link = data.get("vc_link")
category_id = data.get("category_id")
privacy = data["privacy"]
privacy_kinds = [kind.name for kind in PrivacyKinds]
Expand All @@ -132,7 +132,7 @@ async def create_new_event(
invited_emails,
)

if vc_link is not None:
if vc_link:
raise_if_zoom_link_invalid(vc_link)

event = create_event(
Expand Down
31 changes: 17 additions & 14 deletions app/templates/partials/calendar/event/edit_event_details_tab.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,22 @@
</div>

<div class="form_row">
<label for="location_type">Location Type:</label>
<select id="location_type" name="location_type" required>
<option value="" disabled selected>Type</option>
<option value="vc_url">VC URL</option>
<option value="address">Address</option>
</select>
<input type="text" name="location" placeholder="VC URL/Location">
<label for="location">Location </label>
<input type="text" id="location" name="location" placeholder="Location">
</div>

<div class="form_row textarea">
<textarea id="say" name="description" placeholder="Description"></textarea>
<div class="form_row">
<label for="vc_link">VC link: </label>
<input type="text" id="vc_link" name="vc_link" placeholder="VC URL">

Choose a reason for hiding this comment

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

Use dashes in HTML ids: vc-link instead of vc_link.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is really problematic.
I've looked this through and though you are right this is a problem it exists all over the project in different tags and affects many many files and not related to the purpose of this PR (fixing create event).
Maybe it's necessary to open a new PR to fix this throughout the project . @yammesicka What do you think?

</div>

<div class="form_row">
<label for="event_type">All-day:</label>
<select id="event_type" name="event_type" required>
<option value="on">Yes</option>
<option value="off" selected>No</option>
</select>
<label for="invited">Invited emails: </label>
<input type="text" id="invited" name="invited" placeholder="Invited emails, separated by commas">
</div>

<div class="form_row textarea">
<textarea id="say" name="description" placeholder="Description"></textarea>
</div>

<div class="form_row">
Expand All @@ -72,6 +69,12 @@
</div>

<div class="form_row_end">
<label for="event_type">All-day:</label>
<select id="event_type" name="event_type" required>
<option value="on">Yes</option>
<option value="off" selected>No</option>
</select>

<label for="availability">Availability:</label>
<select id="availability" name="availability">
<option value="free">Free</option>
Expand Down