Skip to content

Code cleanup 160#565

Open
reenu153 wants to merge 12 commits intoTreyWW:mainfrom
LaraMerdol:code-cleanup-160
Open

Code cleanup 160#565
reenu153 wants to merge 12 commits intoTreyWW:mainfrom
LaraMerdol:code-cleanup-160

Conversation

@reenu153
Copy link

@reenu153 reenu153 commented Apr 10, 2025

Description

This pull request refactors the code primarily to address the code cleanup tasks outlined in issue #160. The refactoring addresses several code smells and aims to improve the readability, maintainability, and structure of the codebase.

Refactoring Details:

  1. Magic Numbers/Constants:

    • Issue: Magic numbers and hardcoded values were scattered across the codebase, making the code harder to maintain.
    • Refactoring:
      • Created a backend/core/constants.py file to define named constants for recurring numerical values (e.g., MAX_LENGTH_STANDARD, MAX_LENGTH_NAME, MAX_LENGTH_DESCRIPTION, DECIMAL_MAX_DIGITS, DECIMAL_PLACES).
      • Replaced hardcoded max_length and decimal constraints in models with the defined constants.
  2. Inappropriate/Unclear Naming:

    • Issue: Some variable and function names were ambiguous and did not clearly convey their purpose.
    • Refactoring:
      • Renamed functions to be more descriptive and Pythonic:
        • _public_storageget_public_storage
        • _private_storageget_private_storage
        • RandomCodegenerate_verification_code
        • RandomAPICodegenerate_api_key
        • upload_to_user_separate_folderget_file_upload_path
      • These changes enhance clarity by explicitly describing the functionality of each function.
  3. Set blank=False for name fields: Ensures name and related fields (e.g., description, event_name) are required across multiple models to prevent missing data.

Checklist

  • Ran the Black Formatter and
    djLint-er on any new code
    (checks
    will
    fail without)
  • Made any changes or additions to the documentation where required
  • Changes generate no new warnings/errors
  • New and existing unit tests pass locally with my
    changes

What type of PR is this?

  • ♻️ Code Refactor

Added/updated tests?

  • 🙅 no, because they aren't needed

Related PRs, Issues etc

Summary by CodeRabbit

  • New Features

    • Added support for a new "Tester" user role
  • Chores

    • Standardized field validation constraints and length limits across models for improved consistency
    • Refactored internal storage and code generation utilities to enhance code organization
    • Standardized financial calculation precision settings

@TreyWW
Copy link
Owner

TreyWW commented Apr 10, 2025

Hey @reenu153 / @LaraMerdol,

Thank you for these changes! There's currently a fairly big queue in PRs so I wont be able to merge this right away, but I do appreciate the effort! I'll let you know when I get round to testing and merging this :)

@TreyWW TreyWW self-assigned this Apr 10, 2025


def RandomCode(length=6):
def generate_verification_code(length=6):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def generate_verification_code(length=6):
def generate_verification_code(length: int = 6):

@coderabbitai
Copy link

coderabbitai bot commented Feb 5, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • ✅ Full review completed - (🔄 Check again to review again)
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
backend/core/views/auth/passwords/generate.py (1)

101-101: ⚠️ Potential issue | 🟠 Major

Security concern: Password reset code logged to stdout.

The print(f"code is {CODE}") statement logs the raw password reset code. While this appears to be a temporary debug mechanism (given the commented-out email code above), logging sensitive authentication tokens to stdout poses a security risk in production environments where logs may be captured and stored.

Consider removing this debug statement or gating it behind a development-only flag.

Proposed fix
-    print(f"code is {CODE}")
+    # TODO: Remove debug logging or implement proper email sending
+    if settings.DEBUG:
+        print(f"code is {CODE}")
🧹 Nitpick comments (4)
backend/migrations/0022_loginlog_service_alter_verificationcodes_expiry_and_more.py (1)

5-5: Unused import in migration file.

The import backend.models statement appears unused - the migration references backend.core.models.add_3hrs_from_now and backend.core.models.generate_verification_code directly via their dotted paths.

Note: This is auto-generated migration code, so modifying it is optional. Django resolves the dotted paths at runtime regardless of this import.

backend/migrations/0023_apikey_invoiceonetimeschedule.py (1)

6-6: Unused import in migration file.

The import backend.models statement appears unused - the migration references backend.core.models.generate_api_key directly via dotted path.

Note: This is auto-generated migration code, so modifying it is optional.

backend/storage/views/upload.py (1)

102-105: Passing the model class instead of an instance is unconventional.

get_file_upload_path expects an instance as the first parameter, but FileStorageFile (the class) is passed here. This works because optional_actor is always provided, which bypasses the instance attribute checks. However, this relies on the implementation detail of get_file_upload_path and could break if that function's logic changes.

Consider creating a minimal mock object or documenting this usage pattern to make the intent clearer.

backend/finance/models.py (1)

22-26: Imported constants DECIMAL_MAX_DIGITS and DECIMAL_PLACES appear unused.

These constants are imported but the file still uses hardcoded values like max_digits=15, decimal_places=2 for decimal fields (e.g., lines 67, 78-81, 130-131). Consider applying these constants to decimal fields for consistency, or remove the unused imports if they were intentionally left unchanged.

@@ -0,0 +1,13 @@
# Backend core cosntant.py
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Typo in comment: "cosntant.py" should be "constants.py".

Proposed fix
-# Backend core cosntant.py
+# Backend core constants.py
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Backend core cosntant.py
# Backend core constants.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants