Skip to content

Commit

Permalink
Added an endpoint for a simple list of authors with quotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jessamynsmith committed Apr 20, 2022
1 parent 7192da3 commit cc9ee67
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions quotations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ class Meta:
fields = ('name', 'total_quotations',)


class AuthorDetailSerializer(serializers.ModelSerializer):
underquoted = QuotationOnlySerializer(many=True, read_only=True)

class Meta:
model = quotation_models.Author
fields = ('id', 'name', 'underquoted',)


class AuthorQuotationsSerializer(KeyedListSerializerSerializer):
underquoted = QuotationOnlySerializer(many=True, read_only=True)

Expand Down
11 changes: 11 additions & 0 deletions quotations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def get_queryset(self):
return queryset


class AuthorViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = serializers.AuthorDetailSerializer

def get_queryset(self):
queryset = quotation_models.Author.objects.all()
name = self.request.query_params.get('name')
if name is not None:
queryset = queryset.filter(name__icontains=name)
return queryset


def redirect_to_random(request):
quotations = query_set.get_random(quotation_models.Quotation.objects.all())
return redirect(quotations[0])
Expand Down
1 change: 1 addition & 0 deletions underquoted/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
base_name='quotation_author_create')
router.register(r'quotations', quotation_views.QuotationViewSet, base_name='quotations')
router.register(r'author_summary', quotation_views.AuthorSummaryViewSet, base_name='author_summary')
router.register(r'author_details', quotation_views.AuthorViewSet, base_name='authors')
router.register(r'authors', quotation_views.QuotationsByAuthorViewSet, base_name='authors')

urlpatterns = [
Expand Down

0 comments on commit cc9ee67

Please sign in to comment.