Example #1
0
def logout(request):
    """
    Logout by invalidating user access token
    """
    if request.method == 'POST':
        request.token.delete()
        return json_response({
            'status': 'success'
        })
    elif request.method == 'OPTIONS':
        return json_response({})
    else:
        return json_response({
            'error': 'Invalid Method'
        }, status=405)
Example #2
0
def images(request):
    """
    Get a list of all gallery images as json
    """
    if request.method == "GET":
        return json_response(
            [imageObject.image.url for imageObject in Image.objects.all()])

    elif request.method == "POST":
        return create_image(request)
Example #3
0
def posts(request):
    """
    Get a list of all blog posts as JSON
    """
    if request.method == "GET":
        return json_response(
            [post.as_dict(with_content=False) for post in Post.objects.all()])
    elif request.method == "POST":
        return create_post(request)
    else:
        return HttpResponse(status=500)
Example #4
0
def login(request):
    """
    Login user and return valid access token
    """
    if request.method == 'POST':
        parsed_body = json.loads(request.body)
        username = parsed_body["username"]
        password = parsed_body["password"]

        if username is not None and password is not None:
            try:
                user = auth.authenticate(username=username, password=password)
            except Exception as exception:
                print(exception)
            if user is not None:
                if user.is_active:
                    try:
                        token, _ = Token.objects.get_or_create(user=user)
                        return json_response({
                            'token': token.token,
                            'username': user.username
                        })
                    except Exception as exception:
                        print(exception)
                else:
                    return json_response({
                        'error': 'Invalid User'
                    }, status=400)
            else:
                return json_response({
                    'error': 'Invalid Username/Password'
                }, status=400)
        else:
            return json_response({
                'error': 'Invalid Data'
            }, status=400)
    elif request.method == 'OPTIONS':
        return json_response({})
    else:
        return json_response({
            'error': 'Invalid Method'
        }, status=405)
Example #5
0
def newest(request):
    """
    Get newest blog post as JSON
    """
    post = Post.objects.all().order_by('-date')[0]
    return json_response({"slug": post.slug}, status=200)
Example #6
0
def specific_post_by_id(request, id):
    """
    Get one blog post as JSON based on id
    """
    post = get_object_or_404(Post, id=id)
    return json_response(post.as_dict())
Example #7
0
def specific_post(request, slug):
    """
    Get one blog post as JSON based on slug
    """
    post = get_object_or_404(Post, slug=slug)
    return json_response(post.as_dict())