Api de opinione
This commit is contained in:
@@ -13,6 +13,7 @@ class Entity(Model):
|
||||
|
||||
Pueden ser: artist, release-group, release o recording
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'Entidad'
|
||||
verbose_name_plural = 'Entidades'
|
||||
@@ -49,7 +50,7 @@ class ListItem(Model):
|
||||
stars_validators = [MinValueValidator(0), MaxValueValidator(5)]
|
||||
|
||||
user = ForeignKey(User, on_delete=CASCADE, related_name='list')
|
||||
entity = ForeignKey(Entity, on_delete=CASCADE)
|
||||
entity = ForeignKey(Entity, on_delete=CASCADE, related_name='entities')
|
||||
stars = IntegerField('estrellas', default=0, validators=stars_validators, blank=True)
|
||||
opinion = TextField('opinion', default='', blank=True)
|
||||
tags = ManyToManyField(Tag, related_name='list', blank=True)
|
||||
@@ -60,6 +61,7 @@ class ListItem(Model):
|
||||
|
||||
class OpinionHelpful(Model):
|
||||
"""Voto sobre una opinion"""
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'valoración de opinion'
|
||||
verbose_name_plural = 'valoraciones de opinion'
|
||||
|
||||
@@ -28,7 +28,7 @@ class TestList(TestCase):
|
||||
for tag in self.tags:
|
||||
self.user.tags.add(tag)
|
||||
|
||||
list_item = ListItem.objects.create(user_id=self.user.id, entity=self.entities[0])
|
||||
list_item = ListItem.objects.create(user_id=self.user.id, entity=self.entities[0], opinion='oh no', stars=4)
|
||||
list_item.tags.set(self.tags)
|
||||
self.user.list.add(list_item)
|
||||
|
||||
@@ -269,3 +269,16 @@ class TestList(TestCase):
|
||||
self.assertEqual(Tag.objects.all().count(), 2)
|
||||
|
||||
self.assertEqual(ListItem.objects.get(pk=1).tags.count(), 2)
|
||||
|
||||
def test_entity_opinions(self):
|
||||
response = self.client.get('/api/lists/entity/a/opinions', follow=True)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
opinions = response.json()['opinions']
|
||||
|
||||
self.assertEqual(len(opinions), 1)
|
||||
self.assertEqual(opinions[0]['user']['id'], 1)
|
||||
self.assertEqual(opinions[0]['user']['username'], 'user')
|
||||
self.assertEqual(opinions[0]['opinion'], 'oh no')
|
||||
self.assertEqual(opinions[0]['stars'], 4)
|
||||
|
||||
@@ -6,5 +6,6 @@ urlpatterns = [
|
||||
path('list/<int:user_id>/', views.list_view),
|
||||
path('list/<int:user_id>/<int:list_item_id>/', views.list_item_view),
|
||||
path('tag/<int:user_id>/', views.tag_view),
|
||||
path('tag/<int:user_id>/<int:tag_id>/', views.tag_view)
|
||||
path('tag/<int:user_id>/<int:tag_id>/', views.tag_view),
|
||||
path('entity/<str:entity_mbid>/opinions/', views.get_entity_opinions),
|
||||
]
|
||||
|
||||
@@ -5,7 +5,7 @@ from django.http import JsonResponse
|
||||
from oauth2_provider.decorators import protected_resource
|
||||
|
||||
from lists.forms import ListItemForm, TagForm, EntityForm
|
||||
from lists.models import ListItem, Tag, Entity
|
||||
from lists.models import ListItem, Tag, Entity, OpinionHelpful
|
||||
from users.models import User
|
||||
|
||||
|
||||
@@ -305,3 +305,37 @@ def _delete_tag(request, user, tag):
|
||||
tag.delete()
|
||||
|
||||
return JsonResponse({'status': 200}, status=200)
|
||||
|
||||
|
||||
def get_entity_opinions(request, entity_mbid):
|
||||
"""Opiniones sobre una entidad"""
|
||||
|
||||
if request.method != 'GET':
|
||||
return JsonResponse({'status': 404, 'error': 'La ruta no existe'}, status=404)
|
||||
|
||||
# Tiene que existir la entidad con el id entregado.
|
||||
items = ListItem.objects.filter(entity__mbid=entity_mbid)
|
||||
if items.count() == 0:
|
||||
return JsonResponse({'status': 404, 'error': f'La entidad {entity_mbid} no esta en ninguna lista'}, status=404)
|
||||
|
||||
encoded_opinions = []
|
||||
for item in items:
|
||||
helpful_y = OpinionHelpful.objects.filter(opinion_id=item, vote='Y').count()
|
||||
helpful_n = OpinionHelpful.objects.filter(opinion_id=item, vote='N').count()
|
||||
helpful_f = OpinionHelpful.objects.filter(opinion_id=item, vote='F').count()
|
||||
|
||||
encoded_opinions.append({
|
||||
'user': {
|
||||
'id': item.user.id,
|
||||
'username': item.user.username,
|
||||
},
|
||||
'opinion': item.opinion,
|
||||
'stars': item.stars,
|
||||
'helpful': {
|
||||
'Y': helpful_y,
|
||||
'N': helpful_n,
|
||||
'F': helpful_f,
|
||||
}
|
||||
})
|
||||
|
||||
return JsonResponse({'opinions': encoded_opinions})
|
||||
|
||||
Reference in New Issue
Block a user