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

Asynchronous Notification #2388

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ RUN apt-get update && apt-get install -y \

RUN pip install poetry
RUN poetry config virtualenvs.create false
RUN poetry lock --no-update
RUN poetry install

RUN python manage.py migrate
RUN python manage.py loaddata website/fixtures/initial_data.json
# RUN python manage.py collectstatic
RUN python manage.py initsuperuser

28 changes: 28 additions & 0 deletions blt/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
ASGI config for channels_celery_project project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blt.settings")
django.setup()

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

from notification_app.routing import websocket_urlpatterns

application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(URLRouter(websocket_urlpatterns)),
}
)
28 changes: 23 additions & 5 deletions blt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
# Application definition

INSTALLED_APPS = (
"notification_app",
"daphne",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this

"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand Down Expand Up @@ -197,6 +199,15 @@

WSGI_APPLICATION = "blt.wsgi.application"

ASGI_APPLICATION = "blt.asgi.application"

CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer",
},
}


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

Expand Down Expand Up @@ -317,6 +328,7 @@
"0.0.0.0",
"blt.owasp.org",
"." + DOMAIN_NAME_PREVIOUS,
"blt.onrender.com",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this

]

# Static files (CSS, JavaScript, Images)
Expand Down Expand Up @@ -511,11 +523,17 @@

# Twitter

BEARER_TOKEN = os.environ.get("BEARER_TOKEN")
APP_KEY = os.environ.get("APP_KEY")
APP_KEY_SECRET = os.environ.get("APP_KEY_SECRET")
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
ACCESS_TOKEN_SECRET = os.environ.get("ACCESS_TOKEN_SECRET")
# BEARER_TOKEN = os.environ.get("BEARER_TOKEN")
# APP_KEY = os.environ.get("APP_KEY")
# APP_KEY_SECRET = os.environ.get("APP_KEY_SECRET")
# ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN")
# ACCESS_TOKEN_SECRET = os.environ.get("ACCESS_TOKEN_SECRET")
HanilJain marked this conversation as resolved.
Show resolved Hide resolved

BEARER_TOKEN = "AAAAAAAAAAAAAAAAAAAAACY4swEAAAAACCwbesyUMRhqJcZ5IQKJ%2FeAWpYY%3DWhxvFboQKFlJtKIJ1WWlL2fWYzKSfF383NMBsgRFTg9h8Y5jBF"
APP_KEY = "hBSe9kWzsWvrZTjbm5326j9TE"
APP_KEY_SECRET = "mbHK5fNCkIppsO8ErswLzGDXMdRS74XltkHmnFF2WXtxB60AIE"
ACCESS_TOKEN = "1562852714412793857-CHEfVrO4shDhoWJOsBNCN7Z0d0d3Kw"
ACCESS_TOKEN_SECRET = "jKfqv8FaIuYcyYdy6jdGprh2WHJtonR4ziHgETkC81hYq"
HanilJain marked this conversation as resolved.
Show resolved Hide resolved

# USPTO

Expand Down
2 changes: 2 additions & 0 deletions blt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
BugHuntApiViewsetV2,
CompanyViewSet,
DomainViewSet,
FetchNotificationApiView,
FlagIssueApiView,
InviteFriendApiViewset,
IssueViewSet,
Expand Down Expand Up @@ -358,6 +359,7 @@
InviteFriendApiViewset.as_view(),
name="api_invite_friend",
),
path("api/notification/", FetchNotificationApiView.as_view(), name="notification"),
re_path(r"^scoreboard/$", ScoreboardView.as_view(), name="scoreboard"),
re_path(r"^issue/$", IssueCreate.as_view(), name="issue"),
re_path(
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ version: "3"

services:
app:
command: "poetry run python manage.py runserver 0.0.0.0:8000"
command: "poetry run uvicorn blt.asgi:application --host 0.0.0.0 --port 8000"
build: .
volumes:
- .:/blt
ports:
- "8000:8000"
- "8000:8000"
Empty file added notification_app/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions notification_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin

from .models import Notification

admin.site.register(Notification)
6 changes: 6 additions & 0 deletions notification_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class NotificationAppConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "notification_app"
33 changes: 33 additions & 0 deletions notification_app/consumers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json

from asgiref.sync import sync_to_async
from channels.generic.websocket import AsyncWebsocketConsumer

from notification_app.models import Notification


class NotificationConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
self.room_group_name = "notification_%s" % self.room_name

# Join room group
await self.channel_layer.group_add(self.room_group_name, self.channel_name)

await self.accept()

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

async def receive(self, text_data):
data = json.loads(text_data)
notification_id = data["notification_id"]
notification = await sync_to_async(Notification.objects.get)(id=notification_id)
await sync_to_async(notification.delete)()

# Receive message from room group
async def send_notification(self, event):
message = event
# Send message to WebSocket
await self.send(text_data=json.dumps(message))
Empty file.
7 changes: 7 additions & 0 deletions notification_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib.auth.models import User
from django.db import models


class Notification(models.Model):
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
message = models.TextField(null=True, blank=True, max_length=50)
7 changes: 7 additions & 0 deletions notification_app/routing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import re_path

from . import consumers

websocket_urlpatterns = [
re_path(r"ws/notification/(?P<room_name>\w+)/$", consumers.NotificationConsumer.as_asgi()),
]
1 change: 1 addition & 0 deletions notification_app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
1 change: 1 addition & 0 deletions notification_app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your views here.
Loading
Loading