From: Radek Czajka Date: Wed, 3 Sep 2025 12:58:30 +0000 (+0200) Subject: idempotent device token X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/1d57e715ccbf1aee886af56ef486fff603cd2719?ds=sidebyside;hp=a6c437b4a4ef1c8d17154cb9d2c48101ef53cc47 idempotent device token --- diff --git a/src/push/api/views.py b/src/push/api/views.py index 9de21c9cc..3eae8e369 100644 --- a/src/push/api/views.py +++ b/src/push/api/views.py @@ -21,10 +21,11 @@ class DeviceTokenSerializer(serializers.ModelSerializer): return self.create(self.validated_data) def create(self, validated_data): - return models.DeviceToken.objects.create( + obj, created = models.DeviceToken.objects.get_or_create( user=self.context['request'].user, token=validated_data['token'], ) + return obj def destroy(self, validated_data): models.DeviceToken.objects.filter( diff --git a/src/push/migrations/0006_alter_devicetoken_options_alter_devicetoken_token.py b/src/push/migrations/0006_alter_devicetoken_options_alter_devicetoken_token.py new file mode 100644 index 000000000..520404a96 --- /dev/null +++ b/src/push/migrations/0006_alter_devicetoken_options_alter_devicetoken_token.py @@ -0,0 +1,22 @@ +# Generated by Django 4.0.8 on 2025-09-03 12:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('push', '0005_devicetoken'), + ] + + operations = [ + migrations.AlterModelOptions( + name='devicetoken', + options={'ordering': ('-updated_at',)}, + ), + migrations.AlterField( + model_name='devicetoken', + name='token', + field=models.CharField(max_length=1024, unique=True), + ), + ] diff --git a/src/push/models.py b/src/push/models.py index 71e169ee9..3d86b9690 100644 --- a/src/push/models.py +++ b/src/push/models.py @@ -20,7 +20,7 @@ class Notification(models.Model): class DeviceToken(models.Model): user = models.ForeignKey('auth.User', models.CASCADE) - token = models.CharField(max_length=1024) + token = models.CharField(max_length=1024, unique=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True)