rename channel module + update user creation + fix read channel permission

This commit is contained in:
rmanach 2023-09-24 16:33:08 +02:00
parent 861d18484f
commit 33ac6f9ad6
5 changed files with 13 additions and 54 deletions

View File

@ -4,3 +4,8 @@ from django.apps import AppConfig
class DeploymentConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "deployment"
def ready(self) -> None:
import deployment.signals # noqa
return super().ready()

View File

@ -1,48 +0,0 @@
from django.contrib.auth.models import User
from django_eventstream import send_event
from django_eventstream.channelmanager import DefaultChannelManager
from deployment.models import Deployment
def parse_channel(channel: str) -> tuple[str] | None:
parts = channel.lstrip("deployment_").split("_")
if len_part := len(parts):
if len_part == 1:
return (int(parts[0]), "")
if len_part == 2:
return (int(parts[0]), parts[1])
return
class DeploymentChannelManager(DefaultChannelManager):
def can_read_channel(self, user: User, channel: str):
match parse_channel(channel):
case (user_id, ""):
return user_id == user.id or user.is_superuser
case (user_id, _):
# TODO(rmanach): check if the deployment belongs to the user
return user_id == user.id or user.is_superuser
return False
class Event:
@staticmethod
def send_details(deployment: Deployment, progress: int):
send_event(
f"deployment_{deployment.user.id}_{deployment.id}",
"message",
{
"id": deployment.id,
"status": deployment.status,
"progress": progress,
},
)
@staticmethod
def send(deployment: Deployment):
send_event(
f"deployment_{deployment.user.id}",
"message",
{"id": deployment.id, "status": deployment.status},
)

View File

@ -5,7 +5,7 @@ from uuid import UUID
from celery import shared_task
from celery.contrib.abortable import AbortableTask
from deployment.channel import Event
from deployment.channels import Event
from deployment.models import Deployment, Type, Status

View File

@ -13,7 +13,7 @@ from django.http import (
)
from django.shortcuts import render, get_object_or_404
from deployment.channel import Event
from deployment.channels import Event
from deployment.forms import DeploymentForm
from deployment.models import Deployment, Status
from deployment.tasks import deploy as launch_deploy
@ -30,7 +30,7 @@ def check_user_credits(user: User) -> bool:
return True
deployments = Deployment.objects.filter(user=user)
return not len(deployments) >= dep_user.credits
return dep_user.credits > len(deployments)
return True
@ -135,8 +135,10 @@ def create(request):
form = DeploymentForm(request.POST)
if not form.is_valid():
return HttpResponseBadRequest(f"deployment creation inputs are invalid: {form.errors}")
return HttpResponseBadRequest(
f"deployment creation inputs are invalid: {form.errors}"
)
try:
Deployment.objects.create(
user=request.user, id=uuid4(), **form.cleaned_data

View File

@ -137,6 +137,6 @@ LOGIN_REDIRECT_URL = "home"
LOGOUT_REDIRECT_URL = "home"
GRIP_URL = "http://pushpin:5561"
EVENTSTREAM_CHANNELMANAGER_CLASS = "deployment.channel.DeploymentChannelManager"
EVENTSTREAM_CHANNELMANAGER_CLASS = "deployment.channels.DeploymentChannelManager"
CSRF_TRUSTED_ORIGINS = [HOST] if IS_PROD else ["http://localhost:8080"]