This commit is contained in:
parent
ef8ac09e48
commit
b7b6c56d0e
0
apps/core/__init__.py
Normal file
0
apps/core/__init__.py
Normal file
3
apps/core/admin.py
Normal file
3
apps/core/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
51
apps/core/api.py
Normal file
51
apps/core/api.py
Normal file
@ -0,0 +1,51 @@
|
||||
from ninja import NinjaAPI
|
||||
from ninja import Schema
|
||||
import datetime
|
||||
from ..accounts.models import User
|
||||
from .models import ThingEdit, Thing as ThingModel
|
||||
|
||||
api = NinjaAPI()
|
||||
|
||||
|
||||
class Thing(Schema):
|
||||
id: int
|
||||
data: dict
|
||||
type: str
|
||||
|
||||
|
||||
def get_user_from_key(key: str):
|
||||
# TODO: something real
|
||||
return User.objects.get(username=key)
|
||||
|
||||
|
||||
@api.post("/thing/")
|
||||
def sync_thing(request, key: str, thing: Thing):
|
||||
user = get_user_from_key(key)
|
||||
action = "none"
|
||||
try:
|
||||
old_thing = ThingModel.objects.get(user=user, thing_id=thing.id)
|
||||
if old_thing.data != thing.data:
|
||||
ThingEdit.objects.create(
|
||||
thing=old_thing,
|
||||
data=old_thing.data,
|
||||
timestamp=old_thing.synced_at,
|
||||
)
|
||||
old_thing.data = thing.data
|
||||
old_thing.synced_at = datetime.datetime.utcnow()
|
||||
old_thing.save()
|
||||
thing = old_thing
|
||||
action = "updated"
|
||||
except ThingModel.DoesNotExist:
|
||||
thing = ThingModel.objects.create(
|
||||
user=user, thing_id=thing.id, data=thing.data, type=thing.type
|
||||
)
|
||||
action = "created"
|
||||
# TODO: deleted
|
||||
return {"action": action}
|
||||
|
||||
|
||||
@api.get("/thing/{id}")
|
||||
def get_thing(request, id: int, key: str):
|
||||
user = get_user_from_key(key)
|
||||
thing = ThingModel.objects.get(user=user, thing_id=id)
|
||||
return thing.data
|
6
apps/core/apps.py
Normal file
6
apps/core/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CoreConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "apps.core"
|
64
apps/core/migrations/0001_initial.py
Normal file
64
apps/core/migrations/0001_initial.py
Normal file
@ -0,0 +1,64 @@
|
||||
# Generated by Django 5.2 on 2025-05-04 00:55
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
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"
|
||||
),
|
||||
),
|
||||
("thing_id", models.PositiveIntegerField()),
|
||||
("data", models.JSONField()),
|
||||
("created_at", models.DateTimeField()),
|
||||
("synced_at", models.DateTimeField()),
|
||||
("deleted", models.BooleanField()),
|
||||
(
|
||||
"user",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="things",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"unique_together": {("thing_id", "user_id")},
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="ThingEdit",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
|
||||
),
|
||||
),
|
||||
("data", models.JSONField()),
|
||||
("timestamp", models.DateTimeField()),
|
||||
(
|
||||
"thing",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="edits",
|
||||
to="core.thing",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
0
apps/core/migrations/__init__.py
Normal file
0
apps/core/migrations/__init__.py
Normal file
20
apps/core/models.py
Normal file
20
apps/core/models.py
Normal file
@ -0,0 +1,20 @@
|
||||
from django.db import models
|
||||
from ..accounts.models import User
|
||||
|
||||
|
||||
class Thing(models.Model):
|
||||
thing_id = models.PositiveIntegerField()
|
||||
user = models.ForeignKey(User, related_name="things", on_delete=models.CASCADE)
|
||||
data = models.JSONField()
|
||||
created_at = models.DateTimeField()
|
||||
synced_at = models.DateTimeField()
|
||||
deleted = models.BooleanField()
|
||||
|
||||
class Meta:
|
||||
unique_together = ("thing_id", "user_id")
|
||||
|
||||
|
||||
class ThingEdit(models.Model):
|
||||
thing = models.ForeignKey(Thing, related_name="edits", on_delete=models.CASCADE)
|
||||
data = models.JSONField()
|
||||
timestamp = models.DateTimeField()
|
3
apps/core/tests.py
Normal file
3
apps/core/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
3
apps/core/views.py
Normal file
3
apps/core/views.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
@ -70,6 +70,7 @@ INSTALLED_APPS = [
|
||||
"django_structlog",
|
||||
"django_typer",
|
||||
"apps.accounts",
|
||||
"apps.core",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
Loading…
Reference in New Issue
Block a user