create GuestCommentRating Model
parent
9da2611fbb
commit
e2c5274c53
|
|
@ -0,0 +1,35 @@
|
|||
# Generated by Django 3.0.8 on 2020-07-24 10:12
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('portfolio', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameModel(
|
||||
old_name='CommentRating',
|
||||
new_name='GuestCommentRating',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='guestcommentrating',
|
||||
name='comment',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='portfolio.GuestComment'),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='UserCommentRating',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('value', models.PositiveSmallIntegerField(choices=[(1, 'POSITIVE'), (0, 'NEGATIVE')], verbose_name='Type of rating (1 - POSITIVE, 0 - NEGATIVE)')),
|
||||
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='portfolio.UserComment')),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='portfolio.Account')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -22,7 +22,7 @@ class AbstractRating(OneToManyModel):
|
|||
abstract = True
|
||||
|
||||
|
||||
class CommentRating(AbstractRating):
|
||||
class UserCommentRating(AbstractRating):
|
||||
comment = models.ForeignKey(UserComment, on_delete=models.CASCADE)
|
||||
|
||||
def toDict(self):
|
||||
|
|
@ -33,6 +33,17 @@ class CommentRating(AbstractRating):
|
|||
"comment_id": self.comment_id
|
||||
}
|
||||
|
||||
class GuestCommentRating(AbstractRating):
|
||||
comment = models.ForeignKey(GuestComment, on_delete=models.CASCADE)
|
||||
|
||||
def toDict(self):
|
||||
return {
|
||||
"id": self.id,
|
||||
"user_id": self.user_id,
|
||||
"value": self.value,
|
||||
"comment_id": self.comment_id
|
||||
}
|
||||
|
||||
|
||||
class AlbumRating(AbstractRating):
|
||||
album = models.ForeignKey(Album, on_delete=models.CASCADE)
|
||||
|
|
|
|||
|
|
@ -26,26 +26,26 @@ class TrackRatingSerializer(serializers.ModelSerializer):
|
|||
fields = ['id', 'user_id', 'value']
|
||||
|
||||
|
||||
class CommentRatingSerializer(serializers.ModelSerializer):
|
||||
class UserCommentRatingSerializer(serializers.ModelSerializer):
|
||||
id = serializers.IntegerField(read_only = True)
|
||||
user_id = serializers.IntegerField()
|
||||
|
||||
@staticmethod
|
||||
def get_default(comment_id):
|
||||
queryset = CommentRating.objects.filter(comment_id=comment_id)
|
||||
queryset = UserCommentRating.objects.filter(comment_id=comment_id)
|
||||
return [ x.toDict() for x in queryset ]
|
||||
|
||||
@staticmethod
|
||||
def create(validated_data, comment_id):
|
||||
validated_data["comment_id"] = comment_id
|
||||
return CommentRating.create(CommentRating, validated_data)
|
||||
return UserCommentRating.create(UserCommentRating, validated_data)
|
||||
|
||||
@staticmethod
|
||||
def delete(comment_id, user_id):
|
||||
return CommentRating.objects.get(comment_id=comment_id, user_id=user_id).delete()
|
||||
return UserCommentRating.objects.get(comment_id=comment_id, user_id=user_id).delete()
|
||||
|
||||
class Meta:
|
||||
model = CommentRating
|
||||
model = UserCommentRating
|
||||
fields = ['id', 'user_id', 'value']
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ from drf_yasg.utils import swagger_auto_schema
|
|||
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from .models import TrackRating, AlbumRating, CommentRating
|
||||
from .serializers import TrackRatingSerializer, AlbumRatingSerializer, CommentRatingSerializer
|
||||
from .models import TrackRating, AlbumRating, UserCommentRating
|
||||
from .serializers import TrackRatingSerializer, AlbumRatingSerializer, UserCommentRatingSerializer
|
||||
|
||||
class TrackRatingViewSet(
|
||||
mixins.ListModelMixin,
|
||||
|
|
@ -72,8 +72,8 @@ class CommentRatingViewSet(
|
|||
mixins.DestroyModelMixin,
|
||||
viewsets.GenericViewSet
|
||||
):
|
||||
queryset = CommentRating.objects.all()
|
||||
serializer_class = CommentRatingSerializer
|
||||
queryset = UserCommentRating.objects.all()
|
||||
serializer_class = UserCommentRatingSerializer
|
||||
lookup_url_kwarg = 'user_id'
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
|
|
|
|||
Loading…
Reference in New Issue