Module shortner.views

Views module stores all the views of the application

Expand source code
"""Views module stores all the views of the application"""

from shortner.new_view import NewView
from shortner.stub_view import StubView
from shortner.delete_view import DeleteView

from shortner.update_view import UpdateView


__all__ = ["NewView", "StubView", "UpdateView", "DeleteView"]

Classes

class DeleteView (**kwargs)

DeleteView is responsible for deletion of the shortened links

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

Expand source code
class DeleteView(View):
    """DeleteView is responsible for deletion of the shortened links"""

    def delete(self, request: HttpRequest):
        """delete handles requests to be deleted /delete"""
        httpBody = json.loads(request.body)
        special_codes = httpBody["special_code"]
        try:
            member = Link.objects.get(special_code=special_codes)
            member.delete()
            return HttpResponse(status=204)
        except Exception as e:
            error_msg = {"exception": str(e)}
            return HttpResponse(json.dumps(error_msg), status=404)

Ancestors

  • django.views.generic.base.View

Methods

def delete(self, request: django.http.request.HttpRequest)

delete handles requests to be deleted /delete

Expand source code
def delete(self, request: HttpRequest):
    """delete handles requests to be deleted /delete"""
    httpBody = json.loads(request.body)
    special_codes = httpBody["special_code"]
    try:
        member = Link.objects.get(special_code=special_codes)
        member.delete()
        return HttpResponse(status=204)
    except Exception as e:
        error_msg = {"exception": str(e)}
        return HttpResponse(json.dumps(error_msg), status=404)
class NewView (**kwargs)

NewView is responsible for creation of new shortened links

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

Expand source code
class NewView(View):
    """NewView is responsible for creation of new shortened links"""

    def post(self, request: HttpRequest):
        """post handles post requests to /new"""
        httpBody = json.loads(request.body)
        long_url = httpBody["long_url"]
        link = Link(long_url)
        link.save()
        response = link.to_json()
        return HttpResponse(response, status=201)

Ancestors

  • django.views.generic.base.View

Methods

def post(self, request: django.http.request.HttpRequest)

post handles post requests to /new

Expand source code
def post(self, request: HttpRequest):
    """post handles post requests to /new"""
    httpBody = json.loads(request.body)
    long_url = httpBody["long_url"]
    link = Link(long_url)
    link.save()
    response = link.to_json()
    return HttpResponse(response, status=201)
class StubView (**kwargs)

StubView redirects user from stub to URL

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

Expand source code
class StubView(View):
    """StubView redirects user from stub to URL"""

    def get(self, _, stub: str):
        """get redirects user to the long url"""
        try:
            link = Link.objects.get(stub=stub)  # pylint: disable=no-member
            return HttpResponseRedirect(link.long_url)
        except Link.DoesNotExist:  # pylint: disable=no-member
            return HttpResponseRedirect(REDIRECT_404_URL)

Ancestors

  • django.views.generic.base.View

Methods

def get(self, _, stub: str)

get redirects user to the long url

Expand source code
def get(self, _, stub: str):
    """get redirects user to the long url"""
    try:
        link = Link.objects.get(stub=stub)  # pylint: disable=no-member
        return HttpResponseRedirect(link.long_url)
    except Link.DoesNotExist:  # pylint: disable=no-member
        return HttpResponseRedirect(REDIRECT_404_URL)
class UpdateView (**kwargs)

UpdateView is responsible for updating the long url of existing links

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

Expand source code
class UpdateView(View):
    """UpdateView is responsible for updating the long url of existing links"""

    def put(self, request: HttpRequest):
        """put handles updating requests"""
        data = json.loads(request.body)
        new_long_url = data["new_long_url"]
        # special_code = data["special_code"]
        stub = data["stub"]
        try:
            link = Link.objects.get(stub=stub)  # pylint: disable=no-member
            link.long_url = new_long_url
            link.save()
            return HttpResponse(link.long_url, status=201)
        except Link.DoesNotExist:  # pylint: disable=no-member
            return HttpResponse("Incorrect stub", status=404)

Ancestors

  • django.views.generic.base.View

Methods

def put(self, request: django.http.request.HttpRequest)

put handles updating requests

Expand source code
def put(self, request: HttpRequest):
    """put handles updating requests"""
    data = json.loads(request.body)
    new_long_url = data["new_long_url"]
    # special_code = data["special_code"]
    stub = data["stub"]
    try:
        link = Link.objects.get(stub=stub)  # pylint: disable=no-member
        link.long_url = new_long_url
        link.save()
        return HttpResponse(link.long_url, status=201)
    except Link.DoesNotExist:  # pylint: disable=no-member
        return HttpResponse("Incorrect stub", status=404)