CiviWiki/OpenCiviWiki

View on GitHub
project/threads/api.py

Summary

Maintainability
D
2 days
Test Coverage

File api.py has 415 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import json

from accounts.models import Profile
from common.utils import check_database, save_image_from_url
from core.custom_decorators import require_post_params
Severity: Minor
Found in project/threads/api.py - About 5 hrs to fix

    Function upload_civi_image has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
    Open

    def upload_civi_image(request):
        """This function is used to upload an image for a Civi"""
        if request.method == "POST":
            r = request.POST
            civi_id = r.get("civi_id")
    Severity: Minor
    Found in project/threads/api.py - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function create_civi has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
    Open

    def create_civi(request):
        """
        USAGE:
            Use this function to insert a new connected civi into the database.
    
    
    Severity: Minor
    Found in project/threads/api.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function upload_thread_image has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
    Open

    def upload_thread_image(request):
        """This function is used to upload an image to a thread"""
        if request.method == "POST":
            r = request.POST
            thread_id = r.get("thread_id")
    Severity: Minor
    Found in project/threads/api.py - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function edit_civi has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

    def edit_civi(request):
        """Use this function to edit an existing Civi"""
        civi_id = request.POST.get("civi_id", "")
        title = request.POST.get("title", "")
        body = request.POST.get("body", "")
    Severity: Minor
    Found in project/threads/api.py - About 55 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function delete_civi has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
    Open

    def delete_civi(request):
        """Use this function to delete an existing Civi"""
        civi_id = request.POST.get("civi_id", "")
    
        c = Civi.objects.get(id=civi_id)
    Severity: Minor
    Found in project/threads/api.py - About 45 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function edit_thread has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
    Open

    def edit_thread(request):
        """Use this function to edit an existing thread"""
        thread_id = request.POST.get("thread_id")
        non_required_params = [
            "title",
    Severity: Minor
    Found in project/threads/api.py - About 45 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Avoid too many return statements within this function.
    Open

            return HttpResponseServerError(reason=str(e))
    Severity: Major
    Found in project/threads/api.py - About 30 mins to fix

      Avoid too many return statements within this function.
      Open

              return HttpResponseForbidden("allowed only via POST")
      Severity: Major
      Found in project/threads/api.py - About 30 mins to fix

        Avoid too many return statements within this function.
        Open

            return JsonResponse({"data": return_data})
        Severity: Major
        Found in project/threads/api.py - About 30 mins to fix

          Avoid too many return statements within this function.
          Open

                  return HttpResponseForbidden("allowed only via POST")
          Severity: Major
          Found in project/threads/api.py - About 30 mins to fix

            Function get_responses has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
            Open

            def get_responses(request, thread_id, civi_id):
                """
                USAGE:
                   This is used to get responses for a Civi
                """
            Severity: Minor
            Found in project/threads/api.py - About 25 mins to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed.
            Open

            def upload_civi_image(request):
            Severity: Critical
            Found in project/threads/api.py by sonar-python

            Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain.

            See

            Line too long (88 > 79 characters)
            Open

                                {"id": img.id, "image_url": img.image_url} for img in c.images.all()
            Severity: Minor
            Found in project/threads/api.py by pep8

            Limit all lines to a maximum of 79 characters.

            There are still many devices around that are limited to 80 character
            lines; plus, limiting windows to 80 characters makes it possible to
            have several windows side-by-side.  The default wrapping on such
            devices looks ugly.  Therefore, please limit all lines to a maximum
            of 79 characters. For flowing long blocks of text (docstrings or
            comments), limiting the length to 72 characters is recommended.
            
            Reports error E501.

            Line too long (86 > 79 characters)
            Open

                    c = [Civi.objects.serialize(c) for c in Civi.objects.filter(thread=thread_id)]
            Severity: Minor
            Found in project/threads/api.py by pep8

            Limit all lines to a maximum of 79 characters.

            There are still many devices around that are limited to 80 character
            lines; plus, limiting windows to 80 characters makes it possible to
            have several windows side-by-side.  The default wrapping on such
            devices looks ugly.  Therefore, please limit all lines to a maximum
            of 79 characters. For flowing long blocks of text (docstrings or
            comments), limiting the length to 72 characters is recommended.
            
            Reports error E501.

            Line too long (83 > 79 characters)
            Open

                            c_dict["rebuttal"] = c_rebuttal[0].dict_with_score(request.user.id)
            Severity: Minor
            Found in project/threads/api.py by pep8

            Limit all lines to a maximum of 79 characters.

            There are still many devices around that are limited to 80 character
            lines; plus, limiting windows to 80 characters makes it possible to
            have several windows side-by-side.  The default wrapping on such
            devices looks ugly.  Therefore, please limit all lines to a maximum
            of 79 characters. For flowing long blocks of text (docstrings or
            comments), limiting the length to 72 characters is recommended.
            
            Reports error E501.

            Line too long (87 > 79 characters)
            Open

                        reason=str("Cannot vote on a civi that is in a thread still in draft mode")
            Severity: Minor
            Found in project/threads/api.py by pep8

            Limit all lines to a maximum of 79 characters.

            There are still many devices around that are limited to 80 character
            lines; plus, limiting windows to 80 characters makes it possible to
            have several windows side-by-side.  The default wrapping on such
            devices looks ugly.  Therefore, please limit all lines to a maximum
            of 79 characters. For flowing long blocks of text (docstrings or
            comments), limiting the length to 72 characters is recommended.
            
            Reports error E501.

            Line too long (84 > 79 characters)
            Open

                                pk__in=civis.distinct("author").values_list("author", flat=True)
            Severity: Minor
            Found in project/threads/api.py by pep8

            Limit all lines to a maximum of 79 characters.

            There are still many devices around that are limited to 80 character
            lines; plus, limiting windows to 80 characters makes it possible to
            have several windows side-by-side.  The default wrapping on such
            devices looks ugly.  Therefore, please limit all lines to a maximum
            of 79 characters. For flowing long blocks of text (docstrings or
            comments), limiting the length to 72 characters is recommended.
            
            Reports error E501.

            Line too long (84 > 79 characters)
            Open

                                popup_string="{user} responded to your civi in {thread}".format(
            Severity: Minor
            Found in project/threads/api.py by pep8

            Limit all lines to a maximum of 79 characters.

            There are still many devices around that are limited to 80 character
            lines; plus, limiting windows to 80 characters makes it possible to
            have several windows side-by-side.  The default wrapping on such
            devices looks ugly.  Therefore, please limit all lines to a maximum
            of 79 characters. For flowing long blocks of text (docstrings or
            comments), limiting the length to 72 characters is recommended.
            
            Reports error E501.

            Line too long (84 > 79 characters)
            Open

                                    popup_string="{user} created a new civi in {thread}".format(
            Severity: Minor
            Found in project/threads/api.py by pep8

            Limit all lines to a maximum of 79 characters.

            There are still many devices around that are limited to 80 character
            lines; plus, limiting windows to 80 characters makes it possible to
            have several windows side-by-side.  The default wrapping on such
            devices looks ugly.  Therefore, please limit all lines to a maximum
            of 79 characters. For flowing long blocks of text (docstrings or
            comments), limiting the length to 72 characters is recommended.
            
            Reports error E501.

            Line too long (86 > 79 characters)
            Open

                    return HttpResponseBadRequest(reason=f"Civi with id:{civi_id} does not exist")
            Severity: Minor
            Found in project/threads/api.py by pep8

            Limit all lines to a maximum of 79 characters.

            There are still many devices around that are limited to 80 character
            lines; plus, limiting windows to 80 characters makes it possible to
            have several windows side-by-side.  The default wrapping on such
            devices looks ugly.  Therefore, please limit all lines to a maximum
            of 79 characters. For flowing long blocks of text (docstrings or
            comments), limiting the length to 72 characters is recommended.
            
            Reports error E501.

            Expected an indentation at 8 instead of at 7.
            Open

                   This is used ot get a group of specified Civis
            Severity: Minor
            Found in project/threads/api.py by editorconfig

            Expected an indentation at 8 instead of at 7.
            Open

                   This is used to get a specified Civi
            Severity: Minor
            Found in project/threads/api.py by editorconfig

            Expected an indentation at 8 instead of at 7.
            Open

                   This is used to get responses for a Civi
            Severity: Minor
            Found in project/threads/api.py by editorconfig

            Expected an indentation at 8 instead of at 7.
            Open

                   This is used to get a requested thread
            Severity: Minor
            Found in project/threads/api.py by editorconfig

            There are no issues that match your filters.

            Category
            Status