feat: add user management viewset and sync module to API routes

This commit is contained in:
2026-04-04 17:44:05 -03:00
parent 645a5b4093
commit 6c4e95e579
13 changed files with 328 additions and 2 deletions

14
sync/api_views.py Normal file
View File

@@ -0,0 +1,14 @@
from rest_framework import viewsets
from .models import ChangeLog
from .serializers import ChangeLogSerializer
class ChangeLogViewSet(viewsets.ReadOnlyModelViewSet):
queryset = ChangeLog.objects.all()
serializer_class = ChangeLogSerializer
def get_queryset(self):
queryset = super().get_queryset()
since_id = self.request.query_params.get('since_id')
if since_id:
queryset = queryset.filter(id__gt=since_id)
return queryset

9
sync/apps.py Normal file
View File

@@ -0,0 +1,9 @@
from django.apps import AppConfig
class SyncConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'sync'
def ready(self):
from .signals import register_signals
register_signals()

View File

@@ -0,0 +1,28 @@
# Generated by Django 5.1.4 on 2026-03-28 15:30
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='ChangeLog',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('model_name', models.CharField(choices=[('Product', 'Product'), ('Comanda', 'Comanda'), ('Order', 'Order'), ('Client', 'Client'), ('Categories', 'Categories'), ('Mesa', 'Mesa'), ('Payments', 'Payments')], max_length=50)),
('object_id', models.IntegerField()),
('action', models.CharField(choices=[('SAVE', 'Save'), ('DELETE', 'Delete')], max_length=10)),
('timestamp', models.DateTimeField(auto_now_add=True)),
],
options={
'ordering': ['timestamp'],
'indexes': [models.Index(fields=['timestamp'], name='sync_change_timesta_c62f24_idx'), models.Index(fields=['model_name', 'object_id'], name='sync_change_model_n_3f4f8a_idx')],
},
),
]

View File

31
sync/models.py Normal file
View File

@@ -0,0 +1,31 @@
from django.db import models
class ChangeLog(models.Model):
MODEL_CHOICES = [
('Product', 'Product'),
('Comanda', 'Comanda'),
('Order', 'Order'),
('Client', 'Client'),
('Categories', 'Categories'),
('Mesa', 'Mesa'),
('Payments', 'Payments'),
]
ACTION_CHOICES = [
('SAVE', 'Save'),
('DELETE', 'Delete'),
]
model_name = models.CharField(max_length=50, choices=MODEL_CHOICES)
object_id = models.IntegerField()
action = models.CharField(max_length=10, choices=ACTION_CHOICES)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['timestamp']
indexes = [
models.Index(fields=['timestamp']),
models.Index(fields=['model_name', 'object_id']),
]
def __str__(self):
return f"{self.model_name} {self.object_id} {self.action} at {self.timestamp}"

7
sync/serializers.py Normal file
View File

@@ -0,0 +1,7 @@
from rest_framework import serializers
from .models import ChangeLog
class ChangeLogSerializer(serializers.ModelSerializer):
class Meta:
model = ChangeLog
fields = '__all__'

35
sync/signals.py Normal file
View File

@@ -0,0 +1,35 @@
from django.db.models.signals import post_save, post_delete
from .models import ChangeLog
from products.models import Product
from comandas.models import Comanda, ProductComanda
from orders.models import Order
from clients.models import Client
from categories.models import Categories
from mesas.models import Mesa
from payments.models import Payments
MODELS_TO_TRACK = [
Product, Comanda, ProductComanda, Order, Client, Categories, Mesa, Payments
]
def handle_save(sender, instance, created, **kwargs):
model_name = sender.__name__
ChangeLog.objects.create(
model_name=model_name,
object_id=instance.id,
action='SAVE'
)
def handle_delete(sender, instance, **kwargs):
model_name = sender.__name__
ChangeLog.objects.create(
model_name=model_name,
object_id=instance.id,
action='DELETE'
)
def register_signals():
for model in MODELS_TO_TRACK:
post_save.connect(handle_save, sender=model, dispatch_uid=f"sync_save_{model.__name__}")
post_delete.connect(handle_delete, sender=model, dispatch_uid=f"sync_delete_{model.__name__}")