Skip to content

Commit

Permalink
Merge pull request #7 from wharton/feature/add-suffix-settings
Browse files Browse the repository at this point in the history
Adds a setting to customize the var suffix.
  • Loading branch information
FlipperPA authored Jan 24, 2025
2 parents 192aa7a + 465327c commit 6d0fe51
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ class Command(build_data_models.Command):
"""
```

### Python Reserved Words and Underscores

`AUTOMAGIC_REST_RESERVED_WORD_SUFFIX` is an available Django setting, defaulting to `var`. Make sure this value does not end in an underscore, or it will circumvent the fix. Python reserved words and fields ending in an underscore will have an underscore and this value appended to their Django model field name:

* Python reserved words example: `for` -> `for_var`
* Columns ending in an underscore example: `date_` -> `date__var`.

### class views.GenericViewSet

The view has several methods and attributes which can be overridden as well.
Expand Down
4 changes: 0 additions & 4 deletions automagic_rest/management/commands/build_data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.db import connections
from django.template.loader import render_to_string

from automagic_rest.settings import get_reserved_words_to_append_underscore
from automagic_rest.views import reserved_word_check

# Map PostgreSQL column types to Django ORM field type
Expand Down Expand Up @@ -39,9 +38,6 @@
"jsonb": "JSONField({}blank=True, null=True{})",
}

# Words that can't be used as column names
RESERVED_WORDS = get_reserved_words_to_append_underscore()


def fetch_result_with_blank_row(cursor):
"""
Expand Down
16 changes: 13 additions & 3 deletions automagic_rest/settings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import keyword

from django.conf import settings

def get_reserved_words_to_append_underscore():

def get_reserved_words_to_append_suffix():
"""
A list of reserved words list that can not be used for Django field names. This
includes the Python reserved words list, and additional fields not allowed by
Django REST Framework.
We will append `_var` to the model field names and map to the underlying database
column in the models in the code generator.
We will append the value returned by `get_reserved_word_suffix` to the model field
names and map to the underlying database column in the models in the code generator.
"""
reserved_words = keyword.kwlist
reserved_words.append("format")

return reserved_words


def get_reserved_word_suffix():
"""
Returns the Django setting for the reserved word suffix, or the default of `var`.
"""

return getattr(settings, "AUTOMAGIC_REST_RESERVED_WORD_SUFFIX", "var")
17 changes: 9 additions & 8 deletions automagic_rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
)

from .pagination import estimate_count, CountEstimatePagination
from .settings import get_reserved_words_to_append_underscore
from .settings import get_reserved_words_to_append_suffix, get_reserved_word_suffix


RESERVED_WORDS = get_reserved_words_to_append_underscore()
RESERVED_WORDS = get_reserved_words_to_append_suffix()
RESERVED_WORD_SUFFIX = get_reserved_word_suffix()


def split_basename(basename):
Expand All @@ -34,13 +35,13 @@ def reserved_word_check(column_name):
are appended with `var` to avoid conflicts with URL double underscores.
"""
changed = False
if column_name in RESERVED_WORDS:
column_name = f"{column_name}_var"
if (
column_name in RESERVED_WORDS
or (column_name.endswith("_") and column_name != "__BLANK__")
):
column_name = f"{column_name}_{RESERVED_WORD_SUFFIX}"
changed = True
elif column_name.endswith("_"):
column_name = f"{column_name}var"
changed = True


return column_name, changed


Expand Down

0 comments on commit 6d0fe51

Please sign in to comment.