Commit d183c669 authored by Christophe Henry's avatar Christophe Henry

feat: support liking any content represented by an LDP model

parent a7d58b81
Pipeline #7141 passed with stage
in 27 seconds
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2019-09-02 12:58
# Generated by Django 1.11.26 on 2020-01-24 12:39
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import djangoldp.fields
class Migration(migrations.Migration):
......@@ -20,9 +21,15 @@ class Migration(migrations.Migration):
name='Like',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('receiver_object_id', models.PositiveIntegerField()),
('urlid', djangoldp.fields.LDPUrlField(blank=True, null=True, unique=True)),
('sender', djangoldp.fields.LDPUrlField(max_length=300)),
('object_id', models.PositiveIntegerField()),
('timestamp', models.DateTimeField(default=django.utils.timezone.now)),
('receiver_content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
],
),
migrations.AlterUniqueTogether(
name='like',
unique_together=set([('sender', 'content_type', 'object_id')]),
),
]
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(
ct_field="receiver_content_type",
fk_field="receiver_object_id"
)
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(default=timezone.now)
# 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 = ['sender', 'content_type', 'object_id']
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 = (
"sender",
"receiver_content_type",
"receiver_object_id",
"receiver",
"target",
"timestamp",
)
read_only_fields = (
"target",
)
def get_target(self, obj):
return {"@id": obj.receiver.urlid}
def to_representation(self, obj):
data = super().to_representation(obj)
data.pop("receiver_content_type")
data.pop("receiver_object_id")
data.pop("receiver")
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")
else:
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"] = resolve_id.pk
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