create GuestCommentRating Model

develop
TBS093A 2020-07-24 12:14:26 +02:00
parent 9da2611fbb
commit e2c5274c53
8 changed files with 56 additions and 10 deletions

View File

@ -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,
},
),
]

View File

@ -22,7 +22,7 @@ class AbstractRating(OneToManyModel):
abstract = True abstract = True
class CommentRating(AbstractRating): class UserCommentRating(AbstractRating):
comment = models.ForeignKey(UserComment, on_delete=models.CASCADE) comment = models.ForeignKey(UserComment, on_delete=models.CASCADE)
def toDict(self): def toDict(self):
@ -33,6 +33,17 @@ class CommentRating(AbstractRating):
"comment_id": self.comment_id "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): class AlbumRating(AbstractRating):
album = models.ForeignKey(Album, on_delete=models.CASCADE) album = models.ForeignKey(Album, on_delete=models.CASCADE)

View File

@ -26,26 +26,26 @@ class TrackRatingSerializer(serializers.ModelSerializer):
fields = ['id', 'user_id', 'value'] fields = ['id', 'user_id', 'value']
class CommentRatingSerializer(serializers.ModelSerializer): class UserCommentRatingSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(read_only = True) id = serializers.IntegerField(read_only = True)
user_id = serializers.IntegerField() user_id = serializers.IntegerField()
@staticmethod @staticmethod
def get_default(comment_id): 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 ] return [ x.toDict() for x in queryset ]
@staticmethod @staticmethod
def create(validated_data, comment_id): def create(validated_data, comment_id):
validated_data["comment_id"] = comment_id validated_data["comment_id"] = comment_id
return CommentRating.create(CommentRating, validated_data) return UserCommentRating.create(UserCommentRating, validated_data)
@staticmethod @staticmethod
def delete(comment_id, user_id): 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: class Meta:
model = CommentRating model = UserCommentRating
fields = ['id', 'user_id', 'value'] fields = ['id', 'user_id', 'value']

View File

@ -7,8 +7,8 @@ from drf_yasg.utils import swagger_auto_schema
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from .models import TrackRating, AlbumRating, CommentRating from .models import TrackRating, AlbumRating, UserCommentRating
from .serializers import TrackRatingSerializer, AlbumRatingSerializer, CommentRatingSerializer from .serializers import TrackRatingSerializer, AlbumRatingSerializer, UserCommentRatingSerializer
class TrackRatingViewSet( class TrackRatingViewSet(
mixins.ListModelMixin, mixins.ListModelMixin,
@ -72,8 +72,8 @@ class CommentRatingViewSet(
mixins.DestroyModelMixin, mixins.DestroyModelMixin,
viewsets.GenericViewSet viewsets.GenericViewSet
): ):
queryset = CommentRating.objects.all() queryset = UserCommentRating.objects.all()
serializer_class = CommentRatingSerializer serializer_class = UserCommentRatingSerializer
lookup_url_kwarg = 'user_id' lookup_url_kwarg = 'user_id'
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):