dockered
This commit is contained in:
0
django/transcendence/__init__.py
Normal file
0
django/transcendence/__init__.py
Normal file
57
django/transcendence/abstract/AbstractRoom.py
Normal file
57
django/transcendence/abstract/AbstractRoom.py
Normal file
@ -0,0 +1,57 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from channels.generic.websocket import WebsocketConsumer
|
||||
|
||||
from .AbstractRoomMember import AbstractRoomMember
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from profiles.models import ProfileModel
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .AbstractRoomManager import AbstractRoomManager
|
||||
|
||||
class AbstractRoom:
|
||||
|
||||
def __init__(self, room_manager: AbstractRoomManager):
|
||||
self._member_list: set[AbstractRoomMember] = set()
|
||||
self._room_manager: AbstractRoomManager = room_manager
|
||||
|
||||
def broadcast(self, detail: str, data: dict = {}, excludes: set[AbstractRoomMember] = set()) -> None:
|
||||
|
||||
members: set[AbstractRoomMember] = self._member_list - excludes
|
||||
|
||||
for member in members:
|
||||
member.send(detail, data)
|
||||
|
||||
def get_member_by_socket(self, socket: WebsocketConsumer) -> AbstractRoomMember | None:
|
||||
|
||||
for member in self._member_list:
|
||||
if member.socket is socket:
|
||||
return member
|
||||
|
||||
def get_member_by_user(self, user: User) -> AbstractRoomMember:
|
||||
|
||||
for member in self._member_list:
|
||||
if member.user == user:
|
||||
return member
|
||||
|
||||
def get_members_profiles(self) -> set[ProfileModel]:
|
||||
return set(member.user.profilemodel for member in self._member_list)
|
||||
|
||||
def get_members(self) -> set[ProfileModel]:
|
||||
return set(member.user for member in self._member_list)
|
||||
|
||||
def append(self, member: AbstractRoomMember) -> None:
|
||||
self._member_list.add(member)
|
||||
|
||||
def remove(self, member: AbstractRoomMember) -> None:
|
||||
self._member_list.remove(member)
|
||||
|
||||
def get_users(self) -> set[User]:
|
||||
return set(member.user for member in self._member_list)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._member_list)
|
12
django/transcendence/abstract/AbstractRoomManager.py
Normal file
12
django/transcendence/abstract/AbstractRoomManager.py
Normal file
@ -0,0 +1,12 @@
|
||||
from .AbstractRoom import AbstractRoom
|
||||
|
||||
class AbstractRoomManager:
|
||||
|
||||
def __init__(self):
|
||||
self._room_list: list[AbstractRoom] = []
|
||||
|
||||
def remove(self, room: AbstractRoom) -> None:
|
||||
self._room_list.remove(room)
|
||||
|
||||
def append(self, room: AbstractRoom) -> None:
|
||||
self._room_list.append(room)
|
16
django/transcendence/abstract/AbstractRoomMember.py
Normal file
16
django/transcendence/abstract/AbstractRoomMember.py
Normal file
@ -0,0 +1,16 @@
|
||||
from channels.generic.websocket import WebsocketConsumer
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
import json
|
||||
|
||||
class AbstractRoomMember:
|
||||
|
||||
def __init__(self, user: User, socket: WebsocketConsumer):
|
||||
self.user: User = user
|
||||
self.socket: WebsocketConsumer = socket
|
||||
|
||||
def send(self, detail: str, data: dict = {}) -> None:
|
||||
raw_data: dict = {"detail": detail}
|
||||
raw_data.update(data)
|
||||
self.socket.send(text_data=json.dumps(raw_data))
|
33
django/transcendence/asgi.py
Normal file
33
django/transcendence/asgi.py
Normal file
@ -0,0 +1,33 @@
|
||||
"""
|
||||
ASGI config for trancendence 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/4.2/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
from channels.routing import ProtocolTypeRouter, URLRouter
|
||||
from channels.auth import AuthMiddlewareStack
|
||||
|
||||
import chat.routing
|
||||
import matchmaking.routing
|
||||
import games.routing
|
||||
import notice.routing
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings')
|
||||
|
||||
application = ProtocolTypeRouter({
|
||||
'http': get_asgi_application(),
|
||||
'websocket': AuthMiddlewareStack(
|
||||
URLRouter(
|
||||
chat.routing.websocket_urlpatterns +
|
||||
matchmaking.routing.websocket_urlpatterns +
|
||||
games.routing.websocket_urlpatterns +
|
||||
notice.routing.websocket_urlpatterns
|
||||
)
|
||||
)
|
||||
})
|
168
django/transcendence/settings.py
Normal file
168
django/transcendence/settings.py
Normal file
@ -0,0 +1,168 @@
|
||||
"""
|
||||
Django settings for transcendence project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 4.2.6.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/4.2/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/4.2/ref/settings/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from pathlib import Path
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'django-insecure-18!@88-wm-!skec9^n-85n(f$my^#mh3!#@f=_e@=*arh_yyjj'
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = ["*"]
|
||||
|
||||
CORS_ORIGIN_ALLOW_ALL = False
|
||||
|
||||
CSRF_TRUSTED_ORIGINS = ["https://django.chauvet.pro"]
|
||||
|
||||
CORS_ORIGIN_WHITELIST = (
|
||||
'http://localhost:8000',
|
||||
)
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'channels',
|
||||
'daphne',
|
||||
|
||||
'matchmaking.apps.MatchmakingConfig',
|
||||
'games.apps.GamesConfig',
|
||||
'accounts.apps.AccountsConfig',
|
||||
'profiles.apps.ProfilesConfig',
|
||||
'frontend.apps.FrontendConfig',
|
||||
'chat.apps.ChatConfig',
|
||||
'notice.apps.NoticeConfig',
|
||||
|
||||
'corsheaders',
|
||||
'rest_framework',
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
]
|
||||
|
||||
ASGI_APPLICATION = 'transcendence.asgi.application'
|
||||
|
||||
CHANNEL_LAYERS = {
|
||||
'default' :{
|
||||
'BACKEND':'channels.layers.InMemoryChannelLayer'
|
||||
}
|
||||
}
|
||||
|
||||
MIDDLEWARE = [
|
||||
'corsheaders.middleware.CorsMiddleware',
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.locale.LocaleMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'transcendence.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.debug',
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'transcendence.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.postgresql',
|
||||
'HOST': 'django-db',
|
||||
'NAME': os.environ['POSTGRES_DB'],
|
||||
'USER': os.environ['POSTGRES_USER'],
|
||||
'PASSWORD': os.environ['POSTGRES_PASSWORD'],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/4.2/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
LANGUAGES = [
|
||||
('en', _('English')),
|
||||
('fr', _('French')),
|
||||
]
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/4.2/howto/static-files/
|
||||
|
||||
STATIC_URL = 'static/'
|
||||
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||
|
||||
# Profile picture upload limit
|
||||
|
||||
PROFILE_PICTURE_MAX_SIZE = 2 * 1024 * 1024 # 2MB
|
29
django/transcendence/urls.py
Normal file
29
django/transcendence/urls.py
Normal file
@ -0,0 +1,29 @@
|
||||
"""
|
||||
URL configuration for trancendence project.
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/4.2/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include, re_path
|
||||
from .views import handler_404_view
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('api/profiles/', include('profiles.urls')),
|
||||
path('api/accounts/', include('accounts.urls')),
|
||||
path('api/chat/', include('chat.urls')),
|
||||
path('api/games/', include('games.urls')),
|
||||
re_path(r'^api/', handler_404_view),
|
||||
path('', include('frontend.urls')),
|
||||
]
|
7
django/transcendence/views.py
Normal file
7
django/transcendence/views.py
Normal file
@ -0,0 +1,7 @@
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework import status
|
||||
|
||||
@api_view(('GET',))
|
||||
def handler_404_view(request):
|
||||
return Response(status=status.HTTP_404_NOT_FOUND);
|
16
django/transcendence/wsgi.py
Normal file
16
django/transcendence/wsgi.py
Normal file
@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for trancendence project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'trancendence.settings')
|
||||
|
||||
application = get_wsgi_application()
|
Reference in New Issue
Block a user