Example #1
0
def update_author(request, slug):
    author = get_object_or_404(Author, slug=slug)
    if request.user != author.user and not (request.user.is_superuser
                                            or request.user.is_staff):
        return Response("Unauthorized", status=status.HTTP_401_UNAUTHORIZED)
    if request.method == "PATCH":
        is_partial = True
    else:
        is_partial = False
    serializer = AuthorSerializer(author,
                                  data=request.data,
                                  partial=is_partial)
    if serializer.is_valid():
        serializer.save()
        result_status = status.HTTP_200_OK
    else:
        result_status = status.HTTP_400_BAD_REQUEST
    return Response(serializer.errors, status=result_status)
Example #2
0
def router_index(request, *args, **kwargs):
    # Get logged in user
    user_session = {'authenticated': False}
    if request.user:
        user_session.update({
            'username': request.user.username,
            'admin': request.user.is_staff,
            'authenticated': request.user.is_authenticated
        })
        if hasattr(request.user, 'author'):
            # TODO: Check here if author page is_activated?
            serialized = AuthorSerializer(request.user.author).data
            user_session['author'] = serialized
    session_data = {'user_session_json': json.dumps(user_session)}
    return render(request, 'router_index.html', session_data)
Example #3
0
def create_author(request):
    if request.method == 'POST':
        serializer = AuthorSerializer(data=request.data)
        if serializer.is_valid():
            author = serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
    else:
        serializer = AuthorSerializer()
        return Response(serializer.data, status=status.HTTP_200_OK)
Example #4
0
def view_author(request, slug):
    queryset = get_object_or_404(Author, slug=slug)
    serializer = AuthorSerializer(instance=queryset)
    return Response(serializer.data)
Example #5
0
def list_authors(request):
    queryset = Author.objects.all().select_related('user').prefetch_related(
        'articles')
    serializer = AuthorSerializer(instance=queryset, many=True)
    return Response(serializer.data)