Add failsafes, if no receive_category values are set in the API: enable them all

parent a1b2460d
from __future__ import absolute_import
from rest_framework.relations import ManyRelatedField, PrimaryKeyRelatedField
from rest_framework.serializers import ModelSerializer
from pushnotifications.models import Device, Category
class DeviceSerializer(ModelSerializer):
receive_category = ManyRelatedField(
allow_empty=True,
child_relation=PrimaryKeyRelatedField(allow_empty=True,
queryset=Category.objects.all(),
required=False)
)
class Meta:
model = Device
......
......@@ -29,7 +29,12 @@ class DeviceViewSet(ModelViewSet):
)
except Device.DoesNotExist:
pass
serializer.save(user=self.request.user, language=language)
if len(serializer.validated_data['receive_category']) > 0:
serializer.save(user=self.request.user, language=language)
else:
categories = [c.pk for c in Category.objects.all()]
serializer.save(user=self.request.user, language=language,
receive_category=categories)
def perform_update(self, serializer):
serializer.save(user=self.request.user)
......
......@@ -17,6 +17,11 @@ def forwards_func(apps, schema_editor):
Category(key="board", name_en="board", name_nl="bestuur"),
])
Device = apps.get_model("pushnotifications", "Device")
for device in Device.objects.using(db_alias).all():
for category in Category.objects.using(db_alias).all():
device.receive_category.add(category)
def reverse_func(apps, schema_editor):
Category = apps.get_model("pushnotifications", "Category")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment