-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
591 additions
and
48 deletions.
There are no files selected for viewing
Empty file.
Binary file not shown.
Binary file not shown.
This file contains 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,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains 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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class AuthenticationConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'authentication' |
This file contains 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,47 @@ | ||
from django.contrib.auth.models import User | ||
from django.conf import settings | ||
from rest_framework import authentication, exceptions | ||
import jwt | ||
|
||
class JWTAuthentication(authentication.BaseAuthentication): | ||
def authenticate(self, request): | ||
auth_header = authentication.get_authorization_header(request).decode('utf-8') | ||
if not auth_header or not auth_header.startswith('Bearer '): | ||
print("No JWT token found in request headers") | ||
return None | ||
|
||
token = auth_header.split(' ')[1] | ||
try: | ||
payload = jwt.decode(token, settings.SUPABASE_SECRET_KEY, algorithms=['HS256'], audience='authenticated') | ||
user_id = payload['sub'] | ||
email = payload.get('email', '') | ||
first_name = payload.get('user_metadata', {}).get('first_name', '') | ||
last_name = payload.get('user_metadata', {}).get('last_name', '') | ||
|
||
# Check if the user exists and update/create accordingly | ||
user, created = User.objects.get_or_create(username=user_id, defaults={ | ||
'first_name': first_name, | ||
'last_name': last_name, | ||
'email': email | ||
}) | ||
|
||
# If the user was not created (i.e., it already exists), update its details | ||
if not created: | ||
user.first_name = first_name | ||
user.last_name = last_name | ||
user.email = email | ||
user.save() | ||
|
||
if created: | ||
print("\nNew user authenticated and created") | ||
else: | ||
print("User authenticated") | ||
|
||
return (user, token) | ||
|
||
except jwt.ExpiredSignatureError: | ||
raise exceptions.AuthenticationFailed('Token expired, login again') | ||
except jwt.InvalidTokenError: | ||
raise exceptions.AuthenticationFailed('Invalid token') | ||
except Exception as e: | ||
raise exceptions.AuthenticationFailed(f'Unexpected error during authentication: {e}') |
Empty file.
This file contains 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,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains 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,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains 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,3 @@ | ||
from django.shortcuts import render | ||
|
||
# Create your views here. |
Binary file not shown.
Binary file not shown.
This file contains 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 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
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains 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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ChatConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'chat' |
This file contains 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,48 @@ | ||
#this file is a copy from the file in authentication app. this file is not needed but im too scared to remove it. | ||
from django.contrib.auth.models import User | ||
from django.conf import settings | ||
from rest_framework import authentication, exceptions | ||
import jwt | ||
|
||
class JWTAuthentication(authentication.BaseAuthentication): | ||
def authenticate(self, request): | ||
auth_header = authentication.get_authorization_header(request).decode('utf-8') | ||
if not auth_header or not auth_header.startswith('Bearer '): | ||
print("No JWT token found in request headers") | ||
return None | ||
|
||
token = auth_header.split(' ')[1] | ||
try: | ||
payload = jwt.decode(token, settings.SUPABASE_SECRET_KEY, algorithms=['HS256'], audience='authenticated') | ||
user_id = payload['sub'] | ||
email = payload.get('email', '') | ||
first_name = payload.get('user_metadata', {}).get('first_name', '') | ||
last_name = payload.get('user_metadata', {}).get('last_name', '') | ||
|
||
# Check if the user exists and update/create accordingly | ||
user, created = User.objects.get_or_create(username=user_id, defaults={ | ||
'first_name': first_name, | ||
'last_name': last_name, | ||
'email': email | ||
}) | ||
|
||
# If the user was not created (i.e., it already exists), update its details | ||
if not created: | ||
user.first_name = first_name | ||
user.last_name = last_name | ||
user.email = email | ||
user.save() | ||
|
||
if created: | ||
print("\nNew user authenticated and created") | ||
else: | ||
print("User authenticated") | ||
|
||
return (user, token) | ||
|
||
except jwt.ExpiredSignatureError: | ||
raise exceptions.AuthenticationFailed('Token expired, login again') | ||
except jwt.InvalidTokenError: | ||
raise exceptions.AuthenticationFailed('Invalid token') | ||
except Exception as e: | ||
raise exceptions.AuthenticationFailed(f'Unexpected error during authentication: {e}') |
This file contains 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,42 @@ | ||
from supabase import create_client, Client | ||
from django.conf import settings | ||
|
||
def get_supabase_client() -> Client: | ||
url: str = settings.SUPABASE_URL | ||
key: str = settings.SUPABASE_KEY | ||
return create_client(url, key) | ||
|
||
def check_user_type(user_id): | ||
client = get_supabase_client() | ||
# Attempt to fetch the user from the 'mhp' table | ||
response = client.table('mhp').select('*').eq('mhp_id', user_id).execute() | ||
print(response) | ||
|
||
if response.data: | ||
print("User found as a MHP.") | ||
return "mhp" | ||
else: | ||
print("User not found as MHP, marked as employee.") | ||
return "employee" | ||
|
||
def fetch_chats(user_id): | ||
client = get_supabase_client() | ||
user_type = check_user_type(user_id) | ||
|
||
if user_type == "employee": | ||
response = client.table('chats').select('*').eq('user_id', user_id).execute() | ||
return response | ||
elif user_type == "mhp": | ||
response = client.table('chats').select('*').eq('mhp_id', user_id).execute() | ||
return response | ||
else: | ||
response = client.table('chats').select('*').eq('user_id', user_id).execute() | ||
print("user type unknown") | ||
return response | ||
return None | ||
|
||
def fetch_messages(chat_id): | ||
client = get_supabase_client() | ||
|
||
response = client.table('messages').select('*').eq('chat_id', chat_id).execute() | ||
return response |
Empty file.
This file contains 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,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains 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,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains 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,7 @@ | ||
from django.urls import path | ||
from .views import GetChats, GetMessages | ||
|
||
urlpatterns = [ | ||
path('getChats/', GetChats.as_view(), name='get-chats'), | ||
path('getMessages/', GetMessages.as_view(), name='get-messages'), | ||
] |
This file contains 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,66 @@ | ||
from django.http import JsonResponse | ||
import json | ||
from rest_framework.views import APIView | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from django.shortcuts import get_object_or_404 | ||
from .db_service import fetch_chats, fetch_messages | ||
from django.contrib.auth.models import User | ||
|
||
class GetChats(APIView): | ||
permission_classes = [IsAuthenticated] | ||
|
||
def get(self, request): | ||
user_id = request.user.username | ||
|
||
try: | ||
response = fetch_chats(user_id) | ||
chats = [] | ||
|
||
if response.data: | ||
for chat in response.data: | ||
chats.append({ | ||
'chat_id': chat['chat_id'], | ||
'created_at': chat['created_at'], | ||
'user_id': chat['user_id'], | ||
'mhp_id': chat['mhp_id'], | ||
}) | ||
|
||
print(len(chats), "chats found") | ||
return JsonResponse({'chats': chats}, safe=False) | ||
|
||
except Exception as e: | ||
print("Error fetching chats") | ||
print(e) | ||
return JsonResponse({'error': 'Failed to fetch chats'}, status=500) | ||
|
||
class GetMessages(APIView): | ||
permission_classes = [IsAuthenticated] | ||
|
||
def get(self, request, *args, **kwargs): | ||
user_id = request.user.username | ||
chat_id = request.GET.get('chat_id', None) | ||
|
||
try: | ||
if chat_id is not None: | ||
messages = [] | ||
response = fetch_messages(chat_id) | ||
for message in response.data: | ||
messages.append({ | ||
'message_id': message['message_id'], | ||
'sent_at': message['sent_at'], | ||
'sender_id': message['sender_id'], | ||
'receiver_id': message['receiver_id'], | ||
'content': message['content'], | ||
'chat_id': message['chat_id'], | ||
}) | ||
|
||
print(len(messages), "messages found") | ||
return JsonResponse({'messages': messages}, safe=False) | ||
|
||
except Exception as e: | ||
print("Error fethcing messages") | ||
print(e) | ||
return JsonResponse({'error': 'Failed to fetch messages'}, status=500) | ||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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 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 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
|
||
from django.urls import path | ||
from .views import TaskListCreate, TaskUpdate | ||
from .views import TaskListCreate, TaskUpdate, TaskCreate | ||
|
||
urlpatterns = [ | ||
path('tasks/', TaskListCreate.as_view(), name='task-list-create'), | ||
path('tasks/update/<str:task_id>/', TaskUpdate.as_view(), name='task-update'), | ||
path('tasks/create/', TaskCreate.as_view(), name='task-create'), | ||
] |
Oops, something went wrong.