Skip to content

Commit

Permalink
feat(chat): Adicionado chat e message models, atualizacao no settings…
Browse files Browse the repository at this point in the history
….py e consumer comments
  • Loading branch information
Potatoyz908 committed Dec 8, 2024
1 parent 10528be commit cc490b0
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 14 deletions.
Binary file modified API/AcheiUnB/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file modified API/AcheiUnB/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file modified API/AcheiUnB/__pycache__/wsgi.cpython-312.pyc
Binary file not shown.
17 changes: 10 additions & 7 deletions API/AcheiUnB/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,27 @@
ROOT_URLCONF = "AcheiUnB.urls"

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework_simplejwt.authentication.JWTAuthentication",
),
"DEFAULT_RENDERER_CLASSES": [
"rest_framework.renderers.JSONRenderer", # Apenas JSON será usado
],
}

SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
'SIGNING_KEY': SECRET_KEY, # Use a chave secreta do Django
'ALGORITHM': 'HS256',
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=60),
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
"SIGNING_KEY": SECRET_KEY, # Use a chave secreta do Django
"ALGORITHM": "HS256",
}

ASGI_APPLICATION = 'AcheiUnB.asgi.application'
ASGI_APPLICATION = "AcheiUnB.asgi.application"

CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.channels_redis", # Para desenvolvimento local
"BACKEND": "channels.layers.InMemoryChannelLayer",
},
}

Expand Down Expand Up @@ -173,6 +174,8 @@

USE_TZ = True

CORS_ALLOW_ALL_ORIGINS = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
Expand Down
9 changes: 3 additions & 6 deletions API/chat/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,24 @@ async def connect(self):
self.chat_id = self.scope["url_route"]["kwargs"]["chat_id"]
self.chat_group_name = f"chat_{self.chat_id}"

# Join room group
# Join the chat group
await self.channel_layer.group_add(self.chat_group_name, self.channel_name)
await self.accept()

async def disconnect(self, close_code):
# Leave room group
# Leave the chat group
await self.channel_layer.group_discard(self.chat_group_name, self.channel_name)

async def receive(self, text_data):
data = json.loads(text_data)
message = data["message"]
sender = self.scope["user"].username

# Send message to room group
# Send message to the group
await self.channel_layer.group_send(
self.chat_group_name,
{
"type": "chat_message",
"message": message,
"sender": sender,
},
)

Expand All @@ -36,7 +34,6 @@ async def chat_message(self, event):
text_data=json.dumps(
{
"message": event["message"],
"sender": event["sender"],
}
)
)
35 changes: 35 additions & 0 deletions API/chat/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 5.1.3 on 2024-12-08 16:07

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Chat',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('participants', models.ManyToManyField(related_name='chats', to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Message',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content', models.TextField()),
('timestamp', models.DateTimeField(auto_now_add=True)),
('chat', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='messages', to='chat.chat')),
('sender', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Binary file modified API/users/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file modified API/users/__pycache__/admin.cpython-312.pyc
Binary file not shown.
Binary file modified API/users/__pycache__/apps.cpython-312.pyc
Binary file not shown.
Binary file modified API/users/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file modified API/users/__pycache__/views.cpython-312.pyc
Binary file not shown.
Binary file modified API/users/migrations/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion API/users/templates/users/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
<body>
<h1>Perdeu algo no campus?</h1>
<p>A gente te ajuda!</p>
<a href="https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=407427a4-6920-4549-80c6-bc772b8da945&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Faccounts%2Fmicrosoft%2Flogin%2Fcallback%2F&scope=User.Read&response_type=code&state=Zay5NfY4tSn7JgvO&domain=alunos.unb.br" class="btn">Entre com a conta da Microsoft</a>
<a href="https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=407427a4-6920-4549-80c6-bc772b8da945&redirect_uri=http%3A%2F%2Flocalhost%3A5173%2Faccounts%2Fmicrosoft%2Flogin%2Fcallback%2F&scope=User.Read&response_type=code&state=Zay5NfY4tSn7JgvO&domain=alunos.unb.br" class="btn">Entre com a conta da Microsoft</a>
</body>
</html>

0 comments on commit cc490b0

Please sign in to comment.