From 5ef64692408249e51e3593eee0f36ae88c9bacd4 Mon Sep 17 00:00:00 2001 From: Noam <63012023+noam-y@users.noreply.github.com> Date: Thu, 11 Feb 2021 14:48:40 +0200 Subject: [PATCH 01/26] basic restoration of code from a deleted branch. includes- adding is_public attr to event model, a function that adds a user to existing event and test for that function. --- app/database/models.py | 2 +- app/routers/event.py | 25 +++++++++++++++++++++++++ tests/test_event.py | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/app/database/models.py b/app/database/models.py index 9f0f7f1a..19a62241 100644 --- a/app/database/models.py +++ b/app/database/models.py @@ -59,7 +59,7 @@ class Event(Base): color = Column(String, nullable=True) emotion = Column(String, nullable=True) invitees = Column(String) - + is_public = Column(Boolean, default=False) owner_id = Column(Integer, ForeignKey("users.id")) color = Column(String, nullable=True) category_id = Column(Integer, ForeignKey("categories.id")) diff --git a/app/routers/event.py b/app/routers/event.py index d0d4a06e..b5b5b05b 100644 --- a/app/routers/event.py +++ b/app/routers/event.py @@ -300,3 +300,28 @@ def add_new_event(values: dict, db: Session) -> Optional[Event]: except (AssertionError, AttributeError, TypeError) as e: logger.exception(e) return None + + +def add_user_to_event( + session: Session, user_id: int, event_id: int +): """this function gets an existing event and a user and adds the user to the participant list. + function won't add the user to the event if the user is already participates in it. + Function will return: + True if the connection was set successfuly + False if it didn't""" + user_already_connected = session.query(UserEvent).filter_by(event_id == event_id, user_id == user_id).all() + if user_already_connected: + """ if user is not registered to the event, the system will add him""" + try: + create_model( + db, UserEvent, + user_id=owner_id, + event_id=event.id + ) + return True + except: + return False + else: + """if the user has a connection to the event, + the function will recognize the duplicate and return false.""" + return False \ No newline at end of file diff --git a/tests/test_event.py b/tests/test_event.py index e8c4b588..dc498ccf 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -99,6 +99,29 @@ ] +#------------------------------------------------- +# fixtures +#------------------------------------------------- + +@pytest.fixture +def new_event(): + """this func is a valid event, made in order to test event functions""" + return add_new_event(TestApp.event_test_data, session) + + +#------------------------------------------------- +# tests +#------------------------------------------------- + +def test_joining_public_event(): + """test in order to make sure user is added the first time he asks to join event, + yet won't join the same user twice""" + first_join = add_user_to_event(session,new_user.id,new_event.id) + assert first_join + second_join = add_user_to_event(session,new_user.id,new_event.id) + assert not second_join + + def test_eventedit(event_test_client): response = event_test_client.get("/event/edit") assert response.ok From 885a84349c4ef48ccf053067cd049287444810ac Mon Sep 17 00:00:00 2001 From: Noam <63012023+noam-y@users.noreply.github.com> Date: Thu, 11 Feb 2021 14:54:46 +0200 Subject: [PATCH 02/26] added 2 functions- one to send an adaptable email, with email body content given as a var, and also a func that sends email to all participants in event. --- app/internal/email.py | 41 +++++++++++++++++++++++++++++++++++++++++ app/routers/email.py | 14 ++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/app/internal/email.py b/app/internal/email.py index 87092f7f..29a53733 100644 --- a/app/internal/email.py +++ b/app/internal/email.py @@ -52,6 +52,47 @@ def send( return True +def send_email_with_body( + session: Session, event_used: int, user_to_send: int, + title: str, body_content: str, background_tasks: BackgroundTasks = BackgroundTasks +) -> bool: + """This function is the altered version of the function above, + that allows adaptable email content. + It takes an event, user and email content and sends the event to the user. + + Args: + session(Session): The session to redirect to the database. + title (str): Title of the email that is being sent. + event_used (int): Id number of the event that is used. + user_to_send (int): Id number of user that we want to notify. + body_content (string): the content of the email sent to the user. + background_tasks (BackgroundTasks): Function from fastapi that lets + you apply tasks in the background. + + + Returns: + bool: Returns True if the email was sent, else returns False. + """ + event_used = session.query(Event).filter( + Event.id == event_used).first() + user_to_send = session.query(User).filter( + User.id == user_to_send).first() + if not user_to_send or not event_used: + return False + if not verify_email_pattern(user_to_send.email): + return False + + subject = f"{title} {event_used.title}" + recipients = {"email": [user_to_send.email]}.get("email") + body = f"begins at:{event_used.start} : {event_used.content} \n {body_content}" + + background_tasks.add_task(send_internal, + subject=subject, + recipients=recipients, + body=body) + return True + + def send_email_invitation(sender_name: str, recipient_name: str, recipient_mail: str, diff --git a/app/routers/email.py b/app/routers/email.py index 908c35a2..378e50b1 100644 --- a/app/routers/email.py +++ b/app/routers/email.py @@ -69,3 +69,17 @@ def send_invitation(invitation: InvitationParams, background_tasks=background_task): raise HTTPException(status_code=422, detail="Couldn't send the email!") return RedirectResponse(invitation.send_to, status_code=303) + + +def send_email_to_event_participants( + session: Session, + event_id: int, + email_content: str, + title: str): + """ this function sents emails to all of the event participants. + the function gets the event id and the content of the email, then sends emails. + even if the sending of one of the email fails the function will keep running.""" + all_participants = session.query(UserEvent).filter(event_id == event_id).all() + for person in all_participants: + send_email_with_body(session, event_id, person.user_id, title, email_content) + From 1f4b0844d186d3b65d7b477f05fa78ae09fbb779 Mon Sep 17 00:00:00 2001 From: Noam <63012023+noam-y@users.noreply.github.com> Date: Thu, 11 Feb 2021 15:00:03 +0200 Subject: [PATCH 03/26] added front end button to allow users to join event, not connected to backend yet... --- app/templates/event/eventview.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/templates/event/eventview.html b/app/templates/event/eventview.html index 708195da..291d411a 100644 --- a/app/templates/event/eventview.html +++ b/app/templates/event/eventview.html @@ -44,6 +44,9 @@