From b912bb21ad7470569e2c847606765561a15eb219 Mon Sep 17 00:00:00 2001 From: NarayanBavisetti Date: Thu, 7 Dec 2023 11:52:48 +0530 Subject: [PATCH 1/5] chore: removed the print statement --- apiserver/plane/app/views/config.py | 56 +---------------------------- 1 file changed, 1 insertion(+), 55 deletions(-) diff --git a/apiserver/plane/app/views/config.py b/apiserver/plane/app/views/config.py index 9ed82a573..fb32e8570 100644 --- a/apiserver/plane/app/views/config.py +++ b/apiserver/plane/app/views/config.py @@ -20,60 +20,6 @@ class ConfigurationEndpoint(BaseAPIView): ] def get(self, request): - print( - get_configuration_value( - [ - { - "key": "GOOGLE_CLIENT_ID", - "default": os.environ.get("GOOGLE_CLIENT_ID", None), - }, - { - "key": "GITHUB_CLIENT_ID", - "default": os.environ.get("GITHUB_CLIENT_ID", None), - }, - { - "key": "GITHUB_APP_NAME", - "default": os.environ.get("GITHUB_APP_NAME", None), - }, - { - "key": "EMAIL_HOST_USER", - "default": os.environ.get("EMAIL_HOST_USER", None), - }, - { - "key": "EMAIL_HOST_PASSWORD", - "default": os.environ.get("EMAIL_HOST_PASSWORD", None), - }, - { - "key": "ENABLE_MAGIC_LINK_LOGIN", - "default": os.environ.get("ENABLE_MAGIC_LINK_LOGIN", "1"), - }, - { - "key": "ENABLE_EMAIL_PASSWORD", - "default": os.environ.get("ENABLE_EMAIL_PASSWORD", "1"), - }, - { - "key": "SLACK_CLIENT_ID", - "default": os.environ.get("SLACK_CLIENT_ID", "1"), - }, - { - "key": "POSTHOG_API_KEY", - "default": os.environ.get("POSTHOG_API_KEY", "1"), - }, - { - "key": "POSTHOG_HOST", - "default": os.environ.get("POSTHOG_HOST", "1"), - }, - { - "key": "UNSPLASH_ACCESS_KEY", - "default": os.environ.get("UNSPLASH_ACCESS_KEY", "1"), - }, - { - "key": "OPENAI_API_KEY", - "default": os.environ.get("OPENAI_API_KEY", "1"), - }, - ] - ) - ) # Get all the configuration ( @@ -141,7 +87,7 @@ class ConfigurationEndpoint(BaseAPIView): }, ] ) - print("hewllo") + data = {} # Authentication data["google_client_id"] = GOOGLE_CLIENT_ID From 5a364855e8d42364b2e70cac0c7dc9b21419e90c Mon Sep 17 00:00:00 2001 From: NarayanBavisetti Date: Thu, 7 Dec 2023 12:42:02 +0530 Subject: [PATCH 2/5] chore: changed config variables --- apiserver/plane/app/views/auth_extended.py | 57 ++++++----- apiserver/plane/app/views/authentication.py | 42 ++++----- apiserver/plane/app/views/external.py | 10 +- apiserver/plane/app/views/oauth.py | 40 ++++---- apiserver/plane/app/views/workspace.py | 17 ++-- .../plane/bgtasks/event_tracking_task.py | 94 ++++++++++++------- 6 files changed, 140 insertions(+), 120 deletions(-) diff --git a/apiserver/plane/app/views/auth_extended.py b/apiserver/plane/app/views/auth_extended.py index 660829f4b..049e5aab9 100644 --- a/apiserver/plane/app/views/auth_extended.py +++ b/apiserver/plane/app/views/auth_extended.py @@ -387,16 +387,15 @@ class EmailCheckEndpoint(BaseAPIView): ) # Send event - if settings.POSTHOG_API_KEY and settings.POSTHOG_HOST: - auth_events.delay( - user=user.id, - email=email, - user_agent=request.META.get("HTTP_USER_AGENT"), - ip=request.META.get("REMOTE_ADDR"), - event_name="SIGN_IN", - medium="MAGIC_LINK", - first_time=True, - ) + auth_events.delay( + user=user.id, + email=email, + user_agent=request.META.get("HTTP_USER_AGENT"), + ip=request.META.get("REMOTE_ADDR"), + event_name="SIGN_IN", + medium="MAGIC_LINK", + first_time=True, + ) key, token, current_attempt = generate_magic_token(email=email) if not current_attempt: return Response( @@ -420,16 +419,15 @@ class EmailCheckEndpoint(BaseAPIView): status=status.HTTP_400_BAD_REQUEST, ) - if settings.POSTHOG_API_KEY and settings.POSTHOG_HOST: - auth_events.delay( - user=user.id, - email=email, - user_agent=request.META.get("HTTP_USER_AGENT"), - ip=request.META.get("REMOTE_ADDR"), - event_name="SIGN_IN", - medium="MAGIC_LINK", - first_time=False, - ) + auth_events.delay( + user=user.id, + email=email, + user_agent=request.META.get("HTTP_USER_AGENT"), + ip=request.META.get("REMOTE_ADDR"), + event_name="SIGN_IN", + medium="MAGIC_LINK", + first_time=False, + ) # Generate magic token key, token, current_attempt = generate_magic_token(email=email) @@ -449,16 +447,15 @@ class EmailCheckEndpoint(BaseAPIView): status=status.HTTP_200_OK, ) else: - if settings.POSTHOG_API_KEY and settings.POSTHOG_HOST: - auth_events.delay( - user=user.id, - email=email, - user_agent=request.META.get("HTTP_USER_AGENT"), - ip=request.META.get("REMOTE_ADDR"), - event_name="SIGN_IN", - medium="EMAIL", - first_time=False, - ) + auth_events.delay( + user=user.id, + email=email, + user_agent=request.META.get("HTTP_USER_AGENT"), + ip=request.META.get("REMOTE_ADDR"), + event_name="SIGN_IN", + medium="EMAIL", + first_time=False, + ) # User should enter password to login return Response( diff --git a/apiserver/plane/app/views/authentication.py b/apiserver/plane/app/views/authentication.py index 2a94b5f1f..811eeb959 100644 --- a/apiserver/plane/app/views/authentication.py +++ b/apiserver/plane/app/views/authentication.py @@ -73,7 +73,7 @@ class SignUpEndpoint(BaseAPIView): # get configuration values # Get configuration values - ENABLE_SIGNUP = get_configuration_value( + ENABLE_SIGNUP, = get_configuration_value( [ { "key": "ENABLE_SIGNUP", @@ -173,7 +173,7 @@ class SignInEndpoint(BaseAPIView): # Create the user else: - ENABLE_SIGNUP = get_configuration_value( + ENABLE_SIGNUP, = get_configuration_value( [ { "key": "ENABLE_SIGNUP", @@ -269,16 +269,15 @@ class SignInEndpoint(BaseAPIView): workspace_member_invites.delete() project_member_invites.delete() # Send event - if settings.POSTHOG_API_KEY and settings.POSTHOG_HOST: - auth_events.delay( - user=user.id, - email=email, - user_agent=request.META.get("HTTP_USER_AGENT"), - ip=request.META.get("REMOTE_ADDR"), - event_name="SIGN_IN", - medium="EMAIL", - first_time=False, - ) + auth_events.delay( + user=user.id, + email=email, + user_agent=request.META.get("HTTP_USER_AGENT"), + ip=request.META.get("REMOTE_ADDR"), + event_name="SIGN_IN", + medium="EMAIL", + first_time=False, + ) access_token, refresh_token = get_tokens_for_user(user) data = { @@ -352,16 +351,15 @@ class MagicSignInEndpoint(BaseAPIView): status=status.HTTP_403_FORBIDDEN, ) # Send event - if settings.POSTHOG_API_KEY and settings.POSTHOG_HOST: - auth_events.delay( - user=user.id, - email=email, - user_agent=request.META.get("HTTP_USER_AGENT"), - ip=request.META.get("REMOTE_ADDR"), - event_name="SIGN_IN", - medium="MAGIC_LINK", - first_time=False, - ) + auth_events.delay( + user=user.id, + email=email, + user_agent=request.META.get("HTTP_USER_AGENT"), + ip=request.META.get("REMOTE_ADDR"), + event_name="SIGN_IN", + medium="MAGIC_LINK", + first_time=False, + ) user.is_active = True user.is_email_verified = True diff --git a/apiserver/plane/app/views/external.py b/apiserver/plane/app/views/external.py index 448234fc1..97d509c1e 100644 --- a/apiserver/plane/app/views/external.py +++ b/apiserver/plane/app/views/external.py @@ -89,16 +89,16 @@ class ReleaseNotesEndpoint(BaseAPIView): class UnsplashEndpoint(BaseAPIView): def get(self, request): - UNSPLASH_ACESS_KEY = get_configuration_value( + UNSPLASH_ACCESS_KEY, = get_configuration_value( [ { - "key": "UNSPLASH_ACESS_KEY", + "key": "UNSPLASH_ACCESS_KEY", "default": os.environ.get("UNSPLASH_ACCESS_KEY"), } ] ) # Check unsplash access key - if not UNSPLASH_ACESS_KEY: + if not UNSPLASH_ACCESS_KEY: return Response([], status=status.HTTP_200_OK) # Query parameters @@ -107,9 +107,9 @@ class UnsplashEndpoint(BaseAPIView): per_page = request.GET.get("per_page", 20) url = ( - f"https://api.unsplash.com/search/photos/?client_id={UNSPLASH_ACESS_KEY}&query={query}&page=${page}&per_page={per_page}" + f"https://api.unsplash.com/search/photos/?client_id={UNSPLASH_ACCESS_KEY}&query={query}&page=${page}&per_page={per_page}" if query - else f"https://api.unsplash.com/photos/?client_id={UNSPLASH_ACESS_KEY}&page={page}&per_page={per_page}" + else f"https://api.unsplash.com/photos/?client_id={UNSPLASH_ACCESS_KEY}&page={page}&per_page={per_page}" ) headers = { diff --git a/apiserver/plane/app/views/oauth.py b/apiserver/plane/app/views/oauth.py index 0ffb76db2..e12cba2ae 100644 --- a/apiserver/plane/app/views/oauth.py +++ b/apiserver/plane/app/views/oauth.py @@ -280,16 +280,15 @@ class OauthEndpoint(BaseAPIView): ) # Send event - if settings.POSTHOG_API_KEY and settings.POSTHOG_HOST: - auth_events.delay( - user=user.id, - email=email, - user_agent=request.META.get("HTTP_USER_AGENT"), - ip=request.META.get("REMOTE_ADDR"), - event_name="SIGN_IN", - medium=medium.upper(), - first_time=False, - ) + auth_events.delay( + user=user.id, + email=email, + user_agent=request.META.get("HTTP_USER_AGENT"), + ip=request.META.get("REMOTE_ADDR"), + event_name="SIGN_IN", + medium=medium.upper(), + first_time=False, + ) access_token, refresh_token = get_tokens_for_user(user) @@ -300,7 +299,7 @@ class OauthEndpoint(BaseAPIView): return Response(data, status=status.HTTP_200_OK) except User.DoesNotExist: - ENABLE_SIGNUP = get_configuration_value( + ENABLE_SIGNUP, = get_configuration_value( [ { "key": "ENABLE_SIGNUP", @@ -412,16 +411,15 @@ class OauthEndpoint(BaseAPIView): project_member_invites.delete() # Send event - if settings.POSTHOG_API_KEY and settings.POSTHOG_HOST: - auth_events.delay( - user=user.id, - email=email, - user_agent=request.META.get("HTTP_USER_AGENT"), - ip=request.META.get("REMOTE_ADDR"), - event_name="SIGN_IN", - medium=medium.upper(), - first_time=True, - ) + auth_events.delay( + user=user.id, + email=email, + user_agent=request.META.get("HTTP_USER_AGENT"), + ip=request.META.get("REMOTE_ADDR"), + event_name="SIGN_IN", + medium=medium.upper(), + first_time=True, + ) SocialLoginConnection.objects.update_or_create( medium=medium, diff --git a/apiserver/plane/app/views/workspace.py b/apiserver/plane/app/views/workspace.py index 5e50f28f0..ed72dbcf1 100644 --- a/apiserver/plane/app/views/workspace.py +++ b/apiserver/plane/app/views/workspace.py @@ -408,15 +408,14 @@ class WorkspaceJoinEndpoint(BaseAPIView): workspace_invite.delete() # Send event - if settings.POSTHOG_API_KEY and settings.POSTHOG_HOST: - workspace_invite_event.delay( - user=user.id if user is not None else None, - email=email, - user_agent=request.META.get("HTTP_USER_AGENT"), - ip=request.META.get("REMOTE_ADDR"), - event_name="MEMBER_ACCEPTED", - accepted_from="EMAIL", - ) + workspace_invite_event.delay( + user=user.id if user is not None else None, + email=email, + user_agent=request.META.get("HTTP_USER_AGENT"), + ip=request.META.get("REMOTE_ADDR"), + event_name="MEMBER_ACCEPTED", + accepted_from="EMAIL", + ) return Response( {"message": "Workspace Invitation Accepted"}, diff --git a/apiserver/plane/bgtasks/event_tracking_task.py b/apiserver/plane/bgtasks/event_tracking_task.py index 25479d3ee..7d26dd4ab 100644 --- a/apiserver/plane/bgtasks/event_tracking_task.py +++ b/apiserver/plane/bgtasks/event_tracking_task.py @@ -1,50 +1,78 @@ import uuid +import os -from posthog import Posthog -from django.conf import settings - -#third party imports +# third party imports from celery import shared_task from sentry_sdk import capture_exception +from posthog import Posthog + +# module imports +from plane.license.utils.instance_value import get_configuration_value + + +def posthogConfiguration(): + POSTHOG_API_KEY, POSTHOG_HOST = get_configuration_value( + [ + { + "key": "POSTHOG_API_KEY", + "default": os.environ.get("POSTHOG_API_KEY", None), + }, + { + "key": "POSTHOG_HOST", + "default": os.environ.get("POSTHOG_HOST", None), + }, + ] + ) + if POSTHOG_API_KEY and POSTHOG_HOST: + return POSTHOG_API_KEY, POSTHOG_HOST + else: + return None, None @shared_task def auth_events(user, email, user_agent, ip, event_name, medium, first_time): try: - posthog = Posthog(settings.POSTHOG_API_KEY, host=settings.POSTHOG_HOST) - posthog.capture( - email, - event=event_name, - properties={ - "event_id": uuid.uuid4().hex, - "user": {"email": email, "id": str(user)}, - "device_ctx": { - "ip": ip, - "user_agent": user_agent, - }, - "medium": medium, - "first_time": first_time - } - ) + POSTHOG_API_KEY, POSTHOG_HOST = posthogConfiguration() + + if POSTHOG_API_KEY and POSTHOG_HOST: + posthog = Posthog(POSTHOG_API_KEY, host=POSTHOG_HOST) + posthog.capture( + email, + event=event_name, + properties={ + "event_id": uuid.uuid4().hex, + "user": {"email": email, "id": str(user)}, + "device_ctx": { + "ip": ip, + "user_agent": user_agent, + }, + "medium": medium, + "first_time": first_time + } + ) except Exception as e: capture_exception(e) + @shared_task def workspace_invite_event(user, email, user_agent, ip, event_name, accepted_from): try: - posthog = Posthog(settings.POSTHOG_API_KEY, host=settings.POSTHOG_HOST) - posthog.capture( - email, - event=event_name, - properties={ - "event_id": uuid.uuid4().hex, - "user": {"email": email, "id": str(user)}, - "device_ctx": { - "ip": ip, - "user_agent": user_agent, - }, - "accepted_from": accepted_from - } - ) + POSTHOG_API_KEY, POSTHOG_HOST = posthogConfiguration() + + if POSTHOG_API_KEY and POSTHOG_HOST: + posthog = Posthog(POSTHOG_API_KEY, host=POSTHOG_HOST) + posthog.capture( + email, + event=event_name, + properties={ + "event_id": uuid.uuid4().hex, + "user": {"email": email, "id": str(user)}, + "device_ctx": { + "ip": ip, + "user_agent": user_agent, + }, + "accepted_from": accepted_from + } + ) except Exception as e: capture_exception(e) \ No newline at end of file From 279044e895213727577c437e7e56c5f458d6f3e3 Mon Sep 17 00:00:00 2001 From: NarayanBavisetti Date: Thu, 7 Dec 2023 12:51:10 +0530 Subject: [PATCH 3/5] chore: cleanup --- apiserver/plane/license/utils/instance_value.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/apiserver/plane/license/utils/instance_value.py b/apiserver/plane/license/utils/instance_value.py index 53481108a..92fc6e6ac 100644 --- a/apiserver/plane/license/utils/instance_value.py +++ b/apiserver/plane/license/utils/instance_value.py @@ -9,12 +9,6 @@ from plane.license.models import InstanceConfiguration from plane.license.utils.encryption import decrypt_data - -def create_lookup_dict(list_of_dicts): - return {item['key']: item for item in list_of_dicts} - - - # Helper function to return value from the passed key def get_configuration_value(keys): environment_list = [] From 1fd7372c1852cbb32d72f162880a2f119a4a2645 Mon Sep 17 00:00:00 2001 From: NarayanBavisetti Date: Thu, 7 Dec 2023 13:58:57 +0530 Subject: [PATCH 4/5] chore: added SKIP_ENV_VAR --- .../plane/license/utils/instance_value.py | 43 ++++--------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/apiserver/plane/license/utils/instance_value.py b/apiserver/plane/license/utils/instance_value.py index 92fc6e6ac..58a16b4f4 100644 --- a/apiserver/plane/license/utils/instance_value.py +++ b/apiserver/plane/license/utils/instance_value.py @@ -29,20 +29,17 @@ def get_configuration_value(keys): break else: environment_list.append(key.get("default")) + else: + # Get the configuration from os + for key in keys: + environment_list.append(os.environ.get(key.get("key"), key.get("default"))) return tuple(environment_list) def get_email_configuration(): - if settings.SKIP_ENV_VAR: - ( - EMAIL_HOST_USER, - EMAIL_HOST_PASSWORD, - EMAIL_HOST, - EMAIL_FROM, - EMAIL_USE_TLS, - EMAIL_PORT, - ) = get_configuration_value( + return ( + get_configuration_value( [ { "key": "EMAIL_HOST_USER", @@ -58,7 +55,7 @@ def get_email_configuration(): }, { "key": "EMAIL_FROM", - "default": os.environ.get("EMAIL_FROM", None), + "default": os.environ.get("EMAIL_FROM", "Team Plane "), }, { "key": "EMAIL_USE_TLS", @@ -70,29 +67,5 @@ def get_email_configuration(): }, ] ) - return ( - EMAIL_HOST, - EMAIL_HOST_USER, - EMAIL_HOST_PASSWORD, - EMAIL_PORT, - EMAIL_USE_TLS, - EMAIL_FROM, - ) + ) - else: - # Get email configuration directly from os - EMAIL_HOST = os.environ.get("EMAIL_HOST") - EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER") - EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD") - EMAIL_PORT = os.environ.get("EMAIL_PORT", 587) - EMAIL_USE_TLS = os.environ.get("EMAIL_USE_TLS", "1") - EMAIL_FROM = os.environ.get("EMAIL_FROM", "Team Plane ") - - return ( - EMAIL_HOST, - EMAIL_HOST_USER, - EMAIL_HOST_PASSWORD, - EMAIL_PORT, - EMAIL_USE_TLS, - EMAIL_FROM, - ) From bd25013feefb9c2a19c3a821ea24be47bdf7ed99 Mon Sep 17 00:00:00 2001 From: NarayanBavisetti Date: Thu, 7 Dec 2023 14:07:52 +0530 Subject: [PATCH 5/5] chore: changed the EMAIL_FROM --- apiserver/plane/license/utils/instance_value.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apiserver/plane/license/utils/instance_value.py b/apiserver/plane/license/utils/instance_value.py index 58a16b4f4..f26724183 100644 --- a/apiserver/plane/license/utils/instance_value.py +++ b/apiserver/plane/license/utils/instance_value.py @@ -54,16 +54,16 @@ def get_email_configuration(): "default": os.environ.get("EMAIL_HOST", None), }, { - "key": "EMAIL_FROM", - "default": os.environ.get("EMAIL_FROM", "Team Plane "), + "key": "EMAIL_PORT", + "default": os.environ.get("EMAIL_PORT", 587), }, { "key": "EMAIL_USE_TLS", "default": os.environ.get("EMAIL_USE_TLS", "1"), }, { - "key": "EMAIL_PORT", - "default": os.environ.get("EMAIL_PORT", 587), + "key": "EMAIL_FROM", + "default": os.environ.get("EMAIL_FROM", "Team Plane "), }, ] )