|
|
|
|
@@ -11,12 +11,12 @@ from django import forms
|
|
|
|
|
from django.http import HttpRequest
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
from djarea.client import ServerFunction, client
|
|
|
|
|
from djarea.channels import ReactChannel
|
|
|
|
|
from djarea.setup.registry import register, register_form, register_as
|
|
|
|
|
from djarea.channels import register as register_channel
|
|
|
|
|
from djarea.forms import DjareaFormMixin, DjareaFormMeta
|
|
|
|
|
from djarea.jwt import jwt_obtain, jwt_refresh
|
|
|
|
|
from mizan.client import ServerFunction, client
|
|
|
|
|
from mizan.channels import ReactChannel
|
|
|
|
|
from mizan.setup.registry import register, register_form, register_as
|
|
|
|
|
from mizan.channels import register as register_channel
|
|
|
|
|
from mizan.forms import mizanFormMixin, mizanFormMeta
|
|
|
|
|
from mizan.jwt import jwt_obtain, jwt_refresh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# =============================================================================
|
|
|
|
|
@@ -57,9 +57,9 @@ class WhoamiOutput(BaseModel):
|
|
|
|
|
@client(auth=True)
|
|
|
|
|
def whoami(request: HttpRequest) -> WhoamiOutput:
|
|
|
|
|
return WhoamiOutput(
|
|
|
|
|
user_id=getattr(request.user, 'id', None),
|
|
|
|
|
email=getattr(request.user, 'email', ''),
|
|
|
|
|
is_staff=getattr(request.user, 'is_staff', False),
|
|
|
|
|
user_id=getattr(request.user, "id", None),
|
|
|
|
|
email=getattr(request.user, "email", ""),
|
|
|
|
|
is_staff=getattr(request.user, "is_staff", False),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -197,18 +197,20 @@ register_channel(PresenceChannel, "presence")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --- Staff-only ---
|
|
|
|
|
@client(auth='staff')
|
|
|
|
|
@client(auth="staff")
|
|
|
|
|
def staff_only(request: HttpRequest) -> EchoOutput:
|
|
|
|
|
return EchoOutput(message=f"staff:{request.user.email}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register(staff_only, "staff_only")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --- Superuser-only ---
|
|
|
|
|
@client(auth='superuser')
|
|
|
|
|
@client(auth="superuser")
|
|
|
|
|
def superuser_only(request: HttpRequest) -> EchoOutput:
|
|
|
|
|
return EchoOutput(message=f"superuser:{request.user.email}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register(superuser_only, "superuser_only")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -216,12 +218,14 @@ register(superuser_only, "superuser_only")
|
|
|
|
|
def check_verified_email(request):
|
|
|
|
|
if not request.user.is_authenticated:
|
|
|
|
|
return False
|
|
|
|
|
return getattr(request.user, 'email', '').endswith('@verified.com')
|
|
|
|
|
return getattr(request.user, "email", "").endswith("@verified.com")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@client(auth=check_verified_email)
|
|
|
|
|
def verified_only(request: HttpRequest) -> EchoOutput:
|
|
|
|
|
return EchoOutput(message="verified")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register(verified_only, "verified_only")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -235,7 +239,8 @@ class CurrentUserOutput(BaseModel):
|
|
|
|
|
email: str
|
|
|
|
|
is_staff: bool
|
|
|
|
|
|
|
|
|
|
@client(context='global')
|
|
|
|
|
|
|
|
|
|
@client(context="global")
|
|
|
|
|
def current_user(request: HttpRequest) -> CurrentUserOutput:
|
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
|
return CurrentUserOutput(
|
|
|
|
|
@@ -245,16 +250,19 @@ def current_user(request: HttpRequest) -> CurrentUserOutput:
|
|
|
|
|
)
|
|
|
|
|
return CurrentUserOutput(authenticated=False, email="", is_staff=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register(current_user, "current_user")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GreetOutput(BaseModel):
|
|
|
|
|
greeting: str
|
|
|
|
|
|
|
|
|
|
@client(context='local')
|
|
|
|
|
|
|
|
|
|
@client(context="local")
|
|
|
|
|
def greet(request: HttpRequest, name: str) -> GreetOutput:
|
|
|
|
|
return GreetOutput(greeting=f"Hello, {name}!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register(greet, "greet")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -267,9 +275,11 @@ class MultiplyInput(BaseModel):
|
|
|
|
|
x: int
|
|
|
|
|
y: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MultiplyOutput(BaseModel):
|
|
|
|
|
product: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@register_as("multiply")
|
|
|
|
|
class Multiply(ServerFunction):
|
|
|
|
|
Input = MultiplyInput
|
|
|
|
|
@@ -288,6 +298,7 @@ class Multiply(ServerFunction):
|
|
|
|
|
def not_implemented_fn(request: HttpRequest) -> EchoOutput:
|
|
|
|
|
raise NotImplementedError("This feature is not yet implemented")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register(not_implemented_fn, "not_implemented_fn")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -295,6 +306,7 @@ register(not_implemented_fn, "not_implemented_fn")
|
|
|
|
|
def buggy_fn(request: HttpRequest) -> EchoOutput:
|
|
|
|
|
raise RuntimeError("Unexpected internal failure")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register(buggy_fn, "buggy_fn")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -304,6 +316,7 @@ def permission_check_fn(request: HttpRequest, secret: str) -> EchoOutput:
|
|
|
|
|
raise PermissionError("Wrong secret")
|
|
|
|
|
return EchoOutput(message="access granted")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register(permission_check_fn, "permission_check_fn")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -315,21 +328,22 @@ register(permission_check_fn, "permission_check_fn")
|
|
|
|
|
@client(websocket=True, auth=True)
|
|
|
|
|
def ws_whoami(request: HttpRequest) -> WhoamiOutput:
|
|
|
|
|
return WhoamiOutput(
|
|
|
|
|
user_id=getattr(request.user, 'id', None),
|
|
|
|
|
email=getattr(request.user, 'email', ''),
|
|
|
|
|
is_staff=getattr(request.user, 'is_staff', False),
|
|
|
|
|
user_id=getattr(request.user, "id", None),
|
|
|
|
|
email=getattr(request.user, "email", ""),
|
|
|
|
|
is_staff=getattr(request.user, "is_staff", False),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register(ws_whoami, "ws_whoami")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# =============================================================================
|
|
|
|
|
# DjareaFormMixin Forms
|
|
|
|
|
# mizanFormMixin Forms
|
|
|
|
|
# =============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ContactForm(DjareaFormMixin, forms.Form):
|
|
|
|
|
djarea = DjareaFormMeta(
|
|
|
|
|
class ContactForm(mizanFormMixin, forms.Form):
|
|
|
|
|
mizan = mizanFormMeta(
|
|
|
|
|
name="contact",
|
|
|
|
|
title="Contact Us",
|
|
|
|
|
subtitle="We'd love to hear from you",
|
|
|
|
|
@@ -351,8 +365,8 @@ class ContactForm(DjareaFormMixin, forms.Form):
|
|
|
|
|
# =============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ItemForm(DjareaFormMixin, forms.Form):
|
|
|
|
|
djarea = DjareaFormMeta(
|
|
|
|
|
class ItemForm(mizanFormMixin, forms.Form):
|
|
|
|
|
mizan = mizanFormMeta(
|
|
|
|
|
name="item",
|
|
|
|
|
title="Items",
|
|
|
|
|
submit_label="Save Items",
|
|
|
|
|
@@ -363,7 +377,10 @@ class ItemForm(DjareaFormMixin, forms.Form):
|
|
|
|
|
quantity = forms.IntegerField(min_value=1, label="Quantity")
|
|
|
|
|
|
|
|
|
|
def on_submit_success(self, request):
|
|
|
|
|
return {"label": self.cleaned_data["label"], "qty": self.cleaned_data["quantity"]}
|
|
|
|
|
return {
|
|
|
|
|
"label": self.cleaned_data["label"],
|
|
|
|
|
"qty": self.cleaned_data["quantity"],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# =============================================================================
|
|
|
|
|
@@ -376,11 +393,12 @@ class PrivateChannel(ReactChannel):
|
|
|
|
|
text: str
|
|
|
|
|
|
|
|
|
|
def authorize(self, params=None):
|
|
|
|
|
return getattr(self.user, 'is_authenticated', False)
|
|
|
|
|
return getattr(self.user, "is_authenticated", False)
|
|
|
|
|
|
|
|
|
|
def group(self, params=None):
|
|
|
|
|
return "private_global"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
register_channel(PrivateChannel, "private")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|