valar for morghulis
Project description
valar for morghulis
1. installation
- you need to install valar in a django project
pip install valar
2. root app
- you only need 3 files in your root app
- settings.py
- asgi.py
- urls.py
2.1 settings.py
from pathlib import Path
""" Compulsory settings """
DEBUG = True
BASE_DIR = Path(__file__).resolve().parent.parent
BASE_APP = str(BASE_DIR.name)
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
SECRET_KEY = 'django-insecure-of@tfouoq^_f$l!yki#m=6j7)@&kjri$1_$!mca-=%7=+@f@5^'
""" Minimized compulsory settings """
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
INSTALLED_APPS = [
'django.contrib.sessions',
"corsheaders",
'channels',
'valar.apps.ValarConfig',
]
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'valar.auth.Middleware.ValarMiddleware'
]
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer"
}
}
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
ROOT_URLCONF = "%s.urls" % BASE_APP
ASGI_APPLICATION = "%s.asgi.application" % BASE_APP
""" Optional settings """
ALLOWED_HOSTS = ['*']
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_TZ = False
SESSION_SAVE_EVERY_REQUEST = True
SESSION_COOKIE_AGE = 60 * 60
FILE_UPLOAD_MAX_MEMORY_SIZE = 1024 * 1024 * 100
DATA_UPLOAD_MAX_MEMORY_SIZE = 1024 * 1024 * 100
""" Valar Options """
HANDLER_MAPPING = "%s.urls.channel_mapping" % BASE_APP
MONGO_URI = 'mongodb://username:password@host:27017'
MINIO_URL = "s3://username:password@host:9000"
MINIO_ROOT = "https://host:9001"
""" Email Options """
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'xxx.xx.xx.xx'
EMAIL_PORT = 465
EMAIL_DOMAIN = 'http://xx.xx.xx'
EMAIL_HOST_USER = "xxx@xx.xx.xx"
EMAIL_HOST_PASSWORD = '*******'
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
2.2 asgi.py
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import re_path
from valar.channels.consumer import ValarConsumer
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': URLRouter([
re_path(r'(?P<client>\w+)/$', ValarConsumer.as_asgi()),
])
})
2.3 urls.py
- no need to provide urls for Valar, Valar will auto set urlpatterns for Morghulis ( see https://www.npmjs.com/package/morghulis)
- go to section 4 to see how to register channel handlers (a Vue - Django async communication tool) in urls.
3. migrate
- no need to makemigrations and migrate for valar, valar will auto migration
4. how to register a channel handler for Morghulis async methods
4.1 create a handler
import time
from valar.channels.sender import ValarChannelSender
from valar.channels.counter import Counter
def valar_test_handler(sender: ValarChannelSender):
data = sender.data
length = data.get('length', 100)
counter = Counter(length)
for i in range(length):
time.sleep(0.1)
tick = counter.tick()
tick.update({'name': 'test1'})
sender.load(tick)
4.2 create a dict (e.g. using the name 'channel_mapping') to save your handler
- I'd like to put it in the root urls.py, you can put it anywhere
channel_mapping = {
'test': valar_test_handler,
}
4.3 register the channel_mapping in the settings.py
HANDLER_MAPPING = "%s.urls.channel_mapping" % BASE_APP
4.4 you can copy the following codes to your urls.py
import json
from django.urls import path
from valar.classes.valar_response import ValarResponse
from valar.views.handler import valar_test_handler
def test_request(request):
body = json.loads(request.body)
return ValarResponse(body)
urlpatterns = [
path('test', test_request),
]
channel_mapping = {
'test_handler': valar_test_handler,
}
5. create an orm model extends the AbstractUser class, enable the authentication of Valar
class User(AbstractUser):
"""
any fields you want
"""
description = models.TextField(null=True, verbose_name='备注')
class Meta:
verbose_name = 'User'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
valar-1.3.16.tar.gz
(34.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
valar-1.3.16-py3-none-any.whl
(45.2 kB
view details)
File details
Details for the file valar-1.3.16.tar.gz.
File metadata
- Download URL: valar-1.3.16.tar.gz
- Upload date:
- Size: 34.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57280b31805e5bc7db028b2562052726f2d05b42ab4c2f0e2b763b421718217c
|
|
| MD5 |
f41eeff0da446e431735ee1b07dab989
|
|
| BLAKE2b-256 |
bc7e6d26463dd9136214861015db81b8b39316e65ebb0a7e929c238ba6ebd159
|
File details
Details for the file valar-1.3.16-py3-none-any.whl.
File metadata
- Download URL: valar-1.3.16-py3-none-any.whl
- Upload date:
- Size: 45.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92f107d10571f8e5542b95b16e92f2d1f060221c247b1b23f550128028eba022
|
|
| MD5 |
bdef0dd58fca97248cc5c2e1fc04688c
|
|
| BLAKE2b-256 |
fcd729e1dd16f015a4d7773086309de96af88ce1226cb88d5a986527e468eb99
|