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 and _init.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.mysql', # 使用 MySQL
'NAME': 'vm_ets', # 数据库名
'USER': 'root', # 用户名
'PASSWORD': password, # 密码
'HOST': 'localhost', # 数据库地址,本机用127.0.0.1
'PORT': '3306', # MySQL端口,默认3306
'OPTIONS': {
'connect_timeout': 5, # 默认10秒,可以缩短
'charset': 'utf8mb4',
}
}
}
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 = f'mongodb://root:{password}@{host}:27017/'
MINIO_URL = f"s3://admin:{password}@{host}:9001"
MINIO_ROOT = f"http://{host}:9001"
""" Email Options """
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.126.com'
EMAIL_PORT = 25
EMAIL_HOST_USER = "xxxx@126.com"
EMAIL_HOST_PASSWORD = 'CGiKQh5FyQyupQYA'
import pymysql
pymysql.install_as_MySQLdb()
2.2 asgi.py
import os
from pathlib import Path
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
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings' % Path(__file__).resolve().parent.parent.name)
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': URLRouter([
re_path(r'(?P<client>\w+)/$', ValarConsumer.as_asgi()),
])
})
2.3 urls.py
from django.urls import path, include
urlpatterns = [
path('valar/', include('valar.urls')),
]
- 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'
Metadata
Release files for valar 1.3.32
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| valar-1.3.32.tar.gz | 36.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| valar-1.3.32-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 80.0 kB
Release files / valar-1.3.32.tar.gz
| Download URL | valar-1.3.32.tar.gz |
|---|---|
| Size | 36.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f3de7fd693821fe1cbf4c4af6ad380918beffa153e51faa04916106f831ab843
|
|
BLAKE2b-256 checksum How to use checksums |
12f114943140e6079ec27a584380f3f02bcc1f103de17a4574558a76b68f9dbe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.6
|
Release files / valar-1.3.32-py3-none-any.whl
| Download URL | valar-1.3.32-py3-none-any.whl |
|---|---|
| Size | 43.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
24152020d2b0cffd5137e10348d02dfe5e15d056bc97f3c0a592654ad327b7c2
|
|
BLAKE2b-256 checksum How to use checksums |
2922e491b60ed08bc0fca958f29e1f9a6250ef2e1989283cdfcb6758cfcb3c06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.6
|
Security for valar 1.3.32
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.