1
0

Initial public releases

* `bthlabs-jsonrpc-aiohttp` v1.0.0
* `bthlabs-jsonrpc-core` v1.0.0
* `bthlabs-jsonrpc-django` v1.0.0
This commit is contained in:
2022-06-04 10:41:53 +02:00
commit c75ea4ea9d
111 changed files with 7193 additions and 0 deletions

View File

@@ -0,0 +1 @@
/db.sqlite3

View File

@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
"""
ASGI config for django_jsonrpc_django_example 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.0/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault(
'DJANGO_SETTINGS_MODULE',
'bthlabs_jsonrpc_django_example.settings.production',
)
application = get_asgi_application()

View File

@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from django.contrib.auth.middleware import RemoteUserMiddleware
class CustomHeaderRemoteUserMiddleware(RemoteUserMiddleware):
header = 'HTTP_X_USER'

View File

@@ -0,0 +1 @@
/local.py

View File

@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent.parent
SECRET_KEY = None
DEBUG = False
ALLOWED_HOSTS = []
INSTALLED_APPS = [
# Django apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 3rd party apps
'bthlabs_jsonrpc_django',
# Project apps
'bthlabs_jsonrpc_django_example.things',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'bthlabs_jsonrpc_django_example.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 = 'bthlabs_jsonrpc_django_example.wsgi.application'
DATABASES = {
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
JSONRPC_METHOD_MODULES = [
'bthlabs_jsonrpc_django_example.things.rpc_methods',
]

View File

@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
# django-jsonrpc-django | (c) 2022-present Tomek Wójcik | MIT License
from django.contrib import admin
from bthlabs_jsonrpc_django_example.things import models
class ThingAdmin(admin.ModelAdmin):
list_display = ('pk', 'name', 'created_at', 'owner', 'is_active')
admin.site.register(models.Thing, ThingAdmin)

View File

@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
# django-jsonrpc-django | (c) 2022-present Tomek Wójcik | MIT License
from django.apps import AppConfig
class ThingsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'bthlabs_jsonrpc_django_example.things'
label = 'things'
verbose_name = 'Things'

View File

@@ -0,0 +1,33 @@
# Generated by Django 4.0.4 on 2022-05-12 06:15
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Thing',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('content', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('modified_at', models.DateTimeField(auto_now=True)),
('is_active', models.BooleanField(db_index=True, default=True)),
('owner', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'thing',
'verbose_name_plural': 'things',
},
),
]

View File

@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# django-jsonrpc-django | (c) 2022-present Tomek Wójcik | MIT License
from django.db import models
class Thing(models.Model):
name = models.CharField(max_length=255)
content = models.TextField()
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True, auto_now_add=False)
is_active = models.BooleanField(default=True, db_index=True)
owner = models.ForeignKey(
'auth.User', null=True, blank=True, on_delete=models.CASCADE,
)
class Meta:
verbose_name = 'thing'
verbose_name_plural = 'things'
def to_rpc(self):
return {
'id': self.pk,
'name': self.name,
'content': self.content,
'created_at': self.created_at,
'modified_at': self.modified_at,
'is_active': self.is_active,
'owner_id': self.owner_id,
}

View File

@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-
# django-jsonrpc-django | (c) 2022-present Tomek Wójcik | MIT License
from bthlabs_jsonrpc_core import JSONRPCAccessDeniedError, register_method
from bthlabs_jsonrpc_django_example.things.models import Thing
@register_method('things.list')
def list_things(request):
return Thing.objects.filter(
is_active=True,
owner__isnull=True,
)
@register_method('things.list', namespace='private')
def private_list_things(request):
return Thing.objects.filter(
is_active=True,
owner=request.user,
)
@register_method('things.create', namespace='private')
def private_create_thing(request, name, content):
return Thing.objects.create(
name=name,
content=content,
is_active=True,
owner=request.user,
)
@register_method('things.update', namespace='private')
def private_update_thing(request, thing_id, name=None, content=None):
thing = Thing.objects.get(pk=thing_id, is_active=True)
if thing.owner != request.user:
raise JSONRPCAccessDeniedError("You can't access this thing.")
if name is not None:
thing.name = name
if content is not None:
thing.content = content
thing.save()
return thing
@register_method('things.list', namespace='admin')
def admin_list_things(request):
return Thing.objects.all()
@register_method('things.create', namespace='admin')
def admin_create_thing(request, name, content, is_active, owner_id):
return Thing.objects.create(
name=name,
content=content,
is_active=is_active,
owner_id=owner_id,
)
@register_method('things.update', namespace='admin')
def admin_update_thing(request,
thing_id,
name=None,
content=None,
is_active=None,
owner_id=None):
thing = Thing.objects.get(pk=thing_id)
if name is not None:
thing.name = name
if content is not None:
thing.content = content
if is_active is not None:
thing.is_active = is_active
if owner_id is not None:
thing.owner_id = owner_id
thing.save()
return thing

View File

@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from django.contrib import admin
from django.urls import path
from bthlabs_jsonrpc_django import JSONRPCView, is_authenticated, is_staff
urlpatterns = [
path('admin/', admin.site.urls),
path(
'rpc/admin',
JSONRPCView.as_view(
auth_checks=[is_authenticated, is_staff],
namespace='admin',
),
),
path(
'rpc/private',
JSONRPCView.as_view(
auth_checks=[is_authenticated],
namespace='private',
),
),
path('rpc', JSONRPCView.as_view()),
]

View File

@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
"""
WSGI config for django_jsonrpc_django_example 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.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault(
'DJANGO_SETTINGS_MODULE',
'bthlabs_jsonrpc_django_example.settings.production',
)
application = get_wsgi_application()

View File

@@ -0,0 +1,27 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault(
'DJANGO_SETTINGS_MODULE',
'bthlabs_jsonrpc_django_example.settings.local',
)
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
),
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()