Commit 7934bd16 authored by Christophe Henry's avatar Christophe Henry

feat: support liking any content represented by an LDP model

parent a7d58b81
Pipeline #7139 failed with stage
in 13 seconds
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2019-09-02 12:58
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
operations = [
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('receiver_object_id', models.PositiveIntegerField()),
('timestamp', models.DateTimeField(,
('receiver_content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
from django.db import models
from djangoldp.fields import IdURLField
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from django.db import models
from django.utils import timezone
from djangoldp.fields import LDPUrlField
from djangoldp.models import Model
from djangoldp_like.serializers import LikeSerializer
class Like(models.Model):
sender = IdURLField(max_length=300)
receiver_content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
receiver_object_id = models.PositiveIntegerField()
receiver = GenericForeignKey(
class Like(Model):
sender = LDPUrlField(max_length=300)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
target = GenericForeignKey()
timestamp = models.DateTimeField(
# Please refer to Django documentation
class Meta:
serializer_fields = ['sender']
container_path = 'likes/'
rdf_type = 'coopstarter:like'
anonymous_perms = ['view']
authenticated_perms = ['inherit', 'add']
owner_perms = ['inherit', 'change', 'control', 'delete']
# unique_together = ['sender', 'receiver_content_type', 'receiver_object_id']
unique_together = ['target', 'content_type', 'object_id']
# ldp_read_serializer = LikeSerializer
# ldp_write_serializer = LikeSerializer
def __str__(self):
return self.sender
\ No newline at end of file
return self.sender
LikeSerializer.Meta.model = Like
from django.contrib.contenttypes.models import ContentType
from rest_framework import serializers
from djangoldp.models import Model
from djangoldp.serializers import LDPSerializer
from rest_framework.exceptions import ValidationError
class LikeSerializer(LDPSerializer):
receiver_content_type = serializers.PrimaryKeyRelatedField(queryset=ContentType.objects.all())
target = serializers.SerializerMethodField()
class Meta:
model = None # will be defnied later
fields = (
read_only_fields = (
def get_target(self, obj):
return {"@id": obj.receiver.urlid}
def to_representation(self, obj):
data = super().to_representation(obj)
return data
def to_internal_value(self, data):
data.pop("@context", None)
data["sender"] = data.pop("sender", {}).pop("@id")
if data["sender"] is None:
raise ValidationError("'sender' field must be defined")
target = data.pop("target", {}).pop("@id", None)
if target is None:
raise ValidationError("'target' field must be defined")
container, resolve_id = Model.resolve(target)
if resolve_id is None:
raise ValidationError("'target' with id {} couldn't be found".format(target))
data["receiver_object_id"] =
data["receiver_content_type"] = ContentType.objects.get_for_model(container).pk
result = super().to_internal_value(data)
return result
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