You've already forked bthlabs-jsonrpc
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:
@@ -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)
|
||||
@@ -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'
|
||||
@@ -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',
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -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,
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user