Skip to content

Commit 776301b

Browse files
committed
CR fixes
1 parent 49c2c06 commit 776301b

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

app/internal/event.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
import re
22

3+
from email_validator import validate_email, EmailSyntaxError
34
from fastapi import HTTPException
45

56
from starlette.status import HTTP_400_BAD_REQUEST
67

8+
from app.database.models import Event
9+
710
ZOOM_REGEX = re.compile(r'https://.*?\.zoom.us/[a-z]/.[^.,\b\s]+')
811

912

1013
def validate_zoom_link(location):
1114
if ZOOM_REGEX.search(location) is None:
1215
raise HTTPException(status_code=HTTP_400_BAD_REQUEST,
1316
detail="VC type with no valid zoom link")
17+
18+
19+
def get_invited_emails(invited_from_form):
20+
invited_emails = []
21+
for invited_email in invited_from_form.split(','):
22+
invited_email = invited_email.strip()
23+
try:
24+
validate_email(invited_email, check_deliverability=False)
25+
invited_emails.append(invited_email)
26+
except EmailSyntaxError:
27+
pass
28+
29+
return invited_emails
30+
31+
32+
def get_uninvited_regular_emails(session, owner_id, title, invited_emails):
33+
regular_invitees = set()
34+
for record in session.query(Event).\
35+
with_entities(Event.invitees).\
36+
filter(Event.owner_id == owner_id, Event.title == title).all():
37+
for email in record[0].split(','):
38+
regular_invitees.add(email)
39+
40+
return regular_invitees.difference(set(invited_emails))

app/routers/event.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import re
21
from datetime import datetime as dt
32
from operator import attrgetter
43
from typing import List
@@ -13,12 +12,10 @@
1312
from app.database.models import User
1413
from app.database.models import UserEvent
1514
from app.dependencies import templates
16-
from app.internal.event import validate_zoom_link
15+
from app.internal.event import validate_zoom_link, get_invited_emails, get_uninvited_regular_emails
1716
from app.internal.utils import create_model
1817
from app.routers.user import create_user
1918

20-
VALID_MAIL_REGEX = re.compile(r'^\S+@\S+\.\S+$')
21-
2219

2320
router = APIRouter(
2421
prefix="/event",
@@ -49,27 +46,17 @@ async def create_new_event(request: Request, session=Depends(get_db)):
4946
is_zoom = location_type == 'vc_url'
5047
location = data['location']
5148

52-
invitees = []
53-
for invited_mail in data['invited'].split(','):
54-
invited_mail = invited_mail.strip()
55-
if VALID_MAIL_REGEX.fullmatch(invited_mail):
56-
invitees.append(invited_mail)
57-
58-
regular_invitees = set()
59-
for record in session.query(Event).\
60-
with_entities(Event.invitees).\
61-
filter(Event.owner_id == owner_id, Event.title == title).all():
62-
for email in record[0].split(','):
63-
regular_invitees.add(email)
64-
65-
uninvited_contacts = regular_invitees.difference(set(invitees))
49+
invited_emails = get_invited_emails(data['invited'])
50+
uninvited_contacts = get_uninvited_regular_emails(session, owner_id, title, invited_emails)
6651

6752
if is_zoom:
6853
validate_zoom_link(location)
6954

7055
event = create_event(session, title, start, end, owner_id, content,
71-
location, invitees)
72-
message = f'Forgot to invite {", ".join(uninvited_contacts)} maybe?'
56+
location, invited_emails)
57+
message = ''
58+
if uninvited_contacts:
59+
message = f'Forgot to invite {", ".join(uninvited_contacts)} maybe?'
7360
return RedirectResponse(f'/event/view/{event.id}?message={message}',
7461
status_code=HTTP_303_SEE_OTHER)
7562

@@ -86,10 +73,9 @@ def create_event(db, title, start, end, owner_id, content=None,
8673
location=None, invitees=None):
8774
"""Creates an event and an association."""
8875

76+
invitees_concatenated = ''
8977
if invitees:
90-
invitees_str = ','.join(invitees)
91-
else:
92-
invitees_str = ''
78+
invitees_concatenated = ','.join(invitees)
9379

9480
event = create_model(
9581
db, Event,
@@ -99,7 +85,7 @@ def create_event(db, title, start, end, owner_id, content=None,
9985
content=content,
10086
owner_id=owner_id,
10187
location=location,
102-
invitees=invitees_str
88+
invitees=invitees_concatenated
10389
)
10490
create_model(
10591
db, UserEvent,

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ chardet==4.0.0
77
click==7.1.2
88
colorama==0.4.4
99
coverage==5.3.1
10+
email-validator==1.1.2
1011
fastapi==0.63.0
1112
fastapi_mail==0.3.3.1
1213
faker==5.6.2
@@ -49,4 +50,4 @@ uvicorn==0.13.3
4950
watchgod==0.6
5051
websockets==8.1
5152
wsproto==1.0.0
52-
zipp==3.4.0
53+
zipp==3.4.0

tests/test_event.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def test_eventedit_post_correct(self, event_test_client, user):
6363
response = event_test_client.post("/event/edit",
6464
data=CORRECT_EVENT_FORM_DATA)
6565
assert response.status_code == HTTP_303_SEE_OTHER
66-
assert '/event/view/' in response.headers['location']
66+
assert event_test_client.app.url_path_for('eventview', id=1) in \
67+
response.headers['location']
6768

6869
def test_eventedit_post_wrong(self, event_test_client, user):
6970
response = event_test_client.post("/event/edit",

0 commit comments

Comments
 (0)