Essex
Essex

Reputation: 6138

Django CBV : create functions in the same class

I'm working on a new project based on Django Class Based View and I would like to get advices in order to factorize an important post function.

I'm pretty new with this concept.

My post function looks like this :

def post(self, request, *args, **kwargs):
        form = self.form_class()
        query_document = None
        query_document_updated = None
        query_app = None
        query_document_count = None


    if "UpdateDocument" in request.POST:
        checkbox_id = request.POST['DocumentChoice']
        checkbox_id_minus_1 = int(checkbox_id) - 1

        query_document_updated = Document.objects.get(id=checkbox_id)

        APP_CODE = query_document_updated.app.code
        SRC_FILENAME = query_document_updated.src_filename
        FILENAME, file_extension = os.path.splitext(SRC_FILENAME)
        CATEGORY = query_document_updated.category

        if CATEGORY == "ANNUAL":
            CATEGORY = "ANNUAL_REPORT"

        # Get the new year selected by user
        year = self.request.POST.get('q1year')

        # Create the new document title updated by the new year
        new_document_title = f"{year}_{CATEGORY}_{APP_CODE}" + " - " + f"{SRC_FILENAME}"

        # Create the new document file updated by the new year
        new_document_file = "app_docs/" + f"{APP_CODE}" + "/" + \
                            f"{year}_{CATEGORY}_{APP_CODE}_{checkbox_id_minus_1}{file_extension}"

    context = {
        'form': form,
        'query_app' : query_app,
        'query_document': query_document,
        'query_document_updated': query_document_updated,
        'query_document_count' : query_document_count
    }
    return render(request, self.template_name, context)

I would like to create two new functions : new_document_title and new_document_file and call both functions in post

How I can do that? Create both functions after post and pass variables in arguments ?

Upvotes: 1

Views: 534

Answers (3)

Umair Mohammad
Umair Mohammad

Reputation: 4635

You can write your function in the view class itself(if those are specific to that view) and also in some other mixin(when common to other views too).

#!/usr/bin/python
# -*- coding: utf-8 -*-


class Mixin(object):
    def process(self, data):
        return data


class CBV(APIView, Mixin):

    def utility_function_1(self, data):
        return self.process(data)

    def utility_function_2(self, data):
        return self.process(data)

    def post(self, request, *args, **kwargs):
        processed_data = self.utility_function_1(request.data)
        processed_data = self.utility_function_2(processed_data)
        return render(request, self.template_name, {'data': processed_data})

Upvotes: 1

JPG
JPG

Reputation: 88589

I think you can do it in a number of ways! I'm writing a few of them here,

Method-1 : Using class methods

from rest_framework.views import APIView


class MyView(APIView):
    def _new_document_title(self, *args, **kwargs):
        # do something
        return something

    def _new_document_file(self, *args, **kwargs):
        # do something
        return something

    def post(self, request, *args, **kwargs):
        self._new_document_title()  # calling "_new_document_title"
        self._new_document_file()  # _new_document_file
        # do something
        return some_response

Method-2 : Using Mixin class

class MyMixin(object):
    def new_document_title(self, *args, **kwargs):
        # do something
        return something

    def new_document_file(self, *args, **kwargs):
        # do something
        return something


class MyView(APIView, MyMixin):
    def post(self, request, *args, **kwargs):
        self.new_document_title()  # calling "new_document_title"
        self.new_document_file()  # new_document_file
        # do something
        return some_response

Method-3 : calling external functions

from rest_framework.views import APIView


def new_document_title(self, *args, **kwargs):
    # do something
    return something


def new_document_file(self, *args, **kwargs):
    # do something
    return something


class MyView(APIView):

    def post(self, request, *args, **kwargs):
        new_document_title()  # calling "new_document_title"
        new_document_file()  # new_document_file
        # do something
        return some_response

Upvotes: 2

Andrey Bulezyuk
Andrey Bulezyuk

Reputation: 197

The code sample from @umair is the correct direction.

Still, if you want your code to be testable and maintainable at scale, you should outsource your helper functions into another python file:

helpers.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

class Helper():

    @staticmethod
    def utility_function_1(self, data):
        return process(data)

    @staticmethod
    def utility_function_2(self, data):
        return process(data)

views.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

# -- adjust -- >import helpers as helper

class CBV(APIView, Mixin):

    def post(self, request, *args, **kwargs):
        processed_data = helper.utility_function_1(request.data)
        processed_data = helper.utility_function_2(processed_data)
        return render(request, self.template_name, {'data': processed_data})

Upvotes: 1

Related Questions