Skip to content
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

Community #7

Merged
merged 3 commits into from
Apr 1, 2024
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
Binary file modified backend/backend/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file modified backend/backend/__pycache__/settings.cpython-310.pyc
Binary file not shown.
Binary file removed backend/backend/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file modified backend/backend/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file removed backend/backend/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified backend/backend/__pycache__/wsgi.cpython-310.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

INSTALLED_APPS = [
'to_do_list',
'community',
'rest_framework',
'corsheaders',
'django.contrib.admin',
Expand Down
3 changes: 2 additions & 1 deletion backend/backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
urlpatterns = [
path('admin/', admin.site.urls),
path('to_do_list/', include('to_do_list.urls')),
path('chat/', include('chat.urls'))
path('chat/', include('chat.urls')),
path('community/', include('community.urls'))
]
Empty file added backend/community/__init__.py
Empty file.
Binary file not shown.
Binary file added backend/community/__pycache__/admin.cpython-311.pyc
Binary file not shown.
Binary file added backend/community/__pycache__/apps.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added backend/community/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions backend/community/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions backend/community/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CommunityConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'community'
47 changes: 47 additions & 0 deletions backend/community/authentication.py
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}')
12 changes: 12 additions & 0 deletions backend/community/db_GetPostData.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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 fetch_post():
client = get_supabase_client()
data = client.table('posts').select('*').execute()
return data
Empty file.
Binary file not shown.
14 changes: 14 additions & 0 deletions backend/community/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone # Import timezone

class Posts(models.Model):
post_id = models.CharField(max_length=255, unique=True)
post_title = models.CharField(max_length=255)
post_content = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='postList')
likes = models.IntegerField(default=0)
posted_at = models.DateTimeField(default=timezone.now) # Set default to the current time

def __str__(self):
return self.title
3 changes: 3 additions & 0 deletions backend/community/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
6 changes: 6 additions & 0 deletions backend/community/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from .views import PostListCreate

urlpatterns = [
path('postList/', PostListCreate.as_view(), name='post-list-create'),
]
56 changes: 56 additions & 0 deletions backend/community/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django.http import JsonResponse
import json
from django.shortcuts import render
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_GetPostData import fetch_post #HERE DO FUNCTIONS IG.
from .models import Posts
from django.contrib.auth.models import User

#def post(request):
# all_posts = Posts.objects.all
# return render(request, '../../web/src/components/post.js')

class PostListCreate(APIView):
permission_classes = [IsAuthenticated]

def get(self, request):
user_uuid = request.user.username

try:
response = fetch_post()
postList = []

if response.data:
for post_data in response.data:
user, _ = User.objects.get_or_create(username=post_data['post_id'])
post = Posts.objects.update_or_create(
post_id = post_data['post_id'],
defaults={
'post_title': post_data['post_title'],
'posted_at': post_data['posted_at'],
'post_content': post_data['post_content'],
'likes':post_data['likes'],
'user': user,
}
)

postList.append({
'post_id': post.post_id,
'post_title': post.post_title,
'post_content': post.post_content,
'likes': post.likes,
'posted_at': post.posted_at
})

print(len(postList), "tasks found")

return JsonResponse({'tasks': postList}, safe=False)

except Exception as e:
print("Error syncing tasks")
print(e)
return JsonResponse({'error': 'Failed to fetch and sync tasks.'}, status=500)
Binary file modified backend/to_do_list/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file modified backend/to_do_list/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file modified backend/to_do_list/__pycache__/authentication.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file modified backend/to_do_list/__pycache__/db_service.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file modified backend/to_do_list/__pycache__/models.cpython-310.pyc
Binary file not shown.
Binary file modified backend/to_do_list/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file removed backend/to_do_list/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified backend/to_do_list/__pycache__/views.cpython-310.pyc
Binary file not shown.
Binary file removed backend/to_do_list/__pycache__/views.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
67 changes: 62 additions & 5 deletions web/src/components/post.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,68 @@
import React from 'react'
import React, {useState, useEffect, useCallback} from "react";
import "../css/post.css"
function post() {
import UserIcon from "../assets/images/UserIcon.png"
function Post() {
const [postList, setPosts] = useState([]);

const fetchPosts = useCallback(async () => {
const response = await fetch('/community/postList', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
//'Authorization': `Bearer ${token.session.access_token}`,
},
});
const data = await response.json();
if (data && data.postList) {
setPosts(data.postList);
} else {
// Handle any errors or empty responses
console.error('Failed to fetch tasks or no tasks available');
}
}, );
useEffect(() => {
fetchPosts();
}, [fetchPosts]);



const [liked, setLike] = useState(false);

const handleLike = () => {
setLike(!liked);
};

const [flagged, setFlag] = useState(false);

const handleFlag = () => {
setFlag(!flagged);
};
return (
<div>

<div className="postss">
<button className="navbar-button" onClick={fetchPosts}>Fetch posts</button>

{postList.map((postList, index) => (
<div key={index} className='PostHolder'>
{/*Insert users profile picture here */}<div className="imgHolder"><img src={UserIcon} /></div>
<div className="Postinfo">
{/*Insert post title here */} <h3 className="PostTitle">{postList.post_title}</h3>
<div className="infoContent"><p>{postList.post_content}</p></div>
</div>
{/*Insert users Name here */} <h3 className="Name">{postList.user}</h3>
<button className={`likeButton ${liked ? 'clicked' : ''}`} onClick={handleLike}>
</button>
<button className={`flag ${flagged ? 'clicked' : ''}`} onClick={handleFlag}>
</button>
{/*Insert date posted here */} <h4 className="PostDate">{postList.posted_at}</h4>
</div>
))}
{/*<div className = "PostHolder"> </div> */}




</div>
)
}

export default post
export default Post
33 changes: 33 additions & 0 deletions web/src/components/postspare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { useState } from 'react';
import "../css/post.css"
import UserIcon from "../assets/images/UserIcon.png"
function Post() {
const [liked, setLike] = useState(false);

const handleLike = () => {
setLike(!liked);
};

const [flagged, setFlag] = useState(false);

const handleFlag = () => {
setFlag(!flagged);
};
return (
<div className = "PostHolder">
{/*Insert users profile picture here */}<div className="imgHolder"><img src={UserIcon} /></div>
<div className="Postinfo">
{/*Insert post title here */} <h3 className="PostTitle">Happiness</h3>
<div className="infoContent"><p>dgsi fodj sdjfids jiofnioa hg iohparuiogbuah bpaoiidfsn nje fnsjfknsndfj nsk fnsn fdk slnfd kskd fnsanfdknas kdfna dfknasdkf nsdanf ksadn fknas dkfnask nfdksnf kdnsafkl nas;lf nads;n fdas nfksadn fn;asdnf;aldfn;ansfd ndnfasndfla ndfln asnf asnfd ;lasnf d;nasdfk; adfdklasn f;las flk;andsfkl nasdnf kas f;ldnaks nfd;ak nsflkdan s;kfldn aklsdnf ;asdn fl;aks nf u phg iowah nuifp hnaw uibhefubqwu brigf ehrifje hgbrgui bg uihqe ugta ehig prqhuiri hh rp ibaeur buiarh angrjg nabgur aug boib g oa</p></div>
</div>
{/*Insert users Name here */} <h3 className="Name">Jane</h3>
<button className={`likeButton ${liked ? 'clicked' : ''}`} onClick={handleLike}>
</button>
<button className={`flag ${flagged ? 'clicked' : ''}`} onClick={handleFlag}>
</button>
{/*Insert date posted here */} <h4 className="PostDate">01/04/24</h4>
</div>
)
}

export default Post
28 changes: 28 additions & 0 deletions web/src/css/community.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
border-radius: 140px;
background: rgba(143, 143, 143, 0.50);
box-shadow: 2px 4px 4px 0px rgba(0, 0, 0, 0.25);
display: flex;
flex-direction: column;
align-items: center;
}
.Community .postsWrapper::-webkit-scrollbar {
display: none; /* Hide scrollbar */
Expand All @@ -63,4 +66,29 @@
margin-top: 2vh;
margin-left: 15vw;
width: 50vw;
}

.Community .modal {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
z-index: 1000;
}

/* Style for the overlay/background behind the modal */
.Community .overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 900;
}
Loading
Loading