fix channels serializer
This commit is contained in:
parent
40f8c9f899
commit
fcde18b381
@ -171,18 +171,15 @@ class UniqueContentRuleSerializer(DynamicModelSerializer):
|
|||||||
fields = ("id", "name", "value")
|
fields = ("id", "name", "value")
|
||||||
|
|
||||||
|
|
||||||
class DiscordChannelSerializer(DynamicModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = DiscordChannel
|
|
||||||
fields = ("id", "channel_id")
|
|
||||||
|
|
||||||
|
|
||||||
class SubscriptionSerializer(DynamicModelSerializer):
|
class SubscriptionSerializer(DynamicModelSerializer):
|
||||||
filters = serializers.PrimaryKeyRelatedField(
|
filters = serializers.PrimaryKeyRelatedField(
|
||||||
queryset=ContentFilter.objects.all(),
|
queryset=ContentFilter.objects.all(),
|
||||||
many=True
|
many=True
|
||||||
)
|
)
|
||||||
channels = DiscordChannelSerializer(many=True)
|
channels = serializers.PrimaryKeyRelatedField(
|
||||||
|
queryset=DiscordChannel.objects.all(),
|
||||||
|
many=True
|
||||||
|
)
|
||||||
# unique_rules = UniqueContentRuleSerializer(many=True) # TODO: solve? causes issues with submission.
|
# unique_rules = UniqueContentRuleSerializer(many=True) # TODO: solve? causes issues with submission.
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -203,22 +200,6 @@ class SubscriptionSerializer(DynamicModelSerializer):
|
|||||||
"unique_rules"
|
"unique_rules"
|
||||||
)
|
)
|
||||||
|
|
||||||
def _delete_unused_channels(self):
|
|
||||||
referenced_channel_ids = set(Subscription.objects.values_list("channels__channel_id", flat=True))
|
|
||||||
del_count, _ = DiscordChannel.objects.exclude(channel_id__in=referenced_channel_ids).delete()
|
|
||||||
log.info("Deleted %s unreferenced DiscordChannel object(s).", del_count)
|
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
|
||||||
channels = validated_data.pop("channels")
|
|
||||||
|
|
||||||
instance.channels.clear()
|
|
||||||
for channel_id in channels:
|
|
||||||
channel, created = DiscordChannel.objects.get_or_create(channel_id=channel_id)
|
|
||||||
instance.channels.add(channel)
|
|
||||||
|
|
||||||
self._delete_unused_channels()
|
|
||||||
return super().update(instance, validated_data)
|
|
||||||
|
|
||||||
def validate(self, data):
|
def validate(self, data):
|
||||||
server = data.get("server") or self.context.get("server")
|
server = data.get("server") or self.context.get("server")
|
||||||
if not server:
|
if not server:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user