Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 13, 2023
1 parent 8b49586 commit 83f3b69
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 107 deletions.
7 changes: 3 additions & 4 deletions django_readonly_field/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@


class Readonly(AppConfig):
name = 'django_readonly_field'
name = "django_readonly_field"

def ready(self):
from django.db import connections
from django.db import utils
from django.db import connections, utils

readonly_compiler_module = "django_readonly_field.compiler"

Expand All @@ -19,7 +18,7 @@ def ready(self):
def custom_load_backend(*args, **kwargs):
backend = original_load_backend(*args, **kwargs)

class ReadOnlyBackend(object):
class ReadOnlyBackend:
@staticmethod
def DatabaseWrapper(*args2, **kwargs2):
connection = backend.DatabaseWrapper(*args2, **kwargs2)
Expand Down
37 changes: 21 additions & 16 deletions django_readonly_field/compiler.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from django.db.models.sql.compiler import SQLCompiler
from django.db.models.sql.compiler import SQLInsertCompiler as BaseSQLInsertCompiler # noqa
from django.db.models.sql.compiler import SQLDeleteCompiler
from django.db.models.sql.compiler import SQLUpdateCompiler as BaseSQLUpdateCompiler # noqa
from django.db.models.sql.compiler import SQLAggregateCompiler
from django.db.models.sql.compiler import (
SQLAggregateCompiler,
SQLCompiler,
SQLDeleteCompiler,
)
from django.db.models.sql.compiler import ( # noqa
SQLInsertCompiler as BaseSQLInsertCompiler,
)
from django.db.models.sql.compiler import ( # noqa
SQLUpdateCompiler as BaseSQLUpdateCompiler,
)

SQLCompiler = SQLCompiler
SQLDeleteCompiler = SQLDeleteCompiler
SQLAggregateCompiler = SQLAggregateCompiler


class ReadonlySQLCompilerMixin(object):

class ReadonlySQLCompilerMixin:
@property
def readonly_field_names(self):
try:
Expand All @@ -21,18 +26,18 @@ def readonly_field_names(self):
fields = getattr(readonly_meta, "_cached_readonly", None)
if not fields:
readonly_meta._cached_readonly = fields = frozenset(
getattr(readonly_meta, "readonly", ()))
getattr(readonly_meta, "readonly", ())
)
return fields

def as_sql(self):
readonly_field_names = self.readonly_field_names
if readonly_field_names:
self.remove_readonly_fields(readonly_field_names)
return super(ReadonlySQLCompilerMixin, self).as_sql()
return super().as_sql()


class SQLUpdateCompiler(ReadonlySQLCompilerMixin, BaseSQLUpdateCompiler):

def remove_readonly_fields(self, readonly_field_names):
"""
Remove the values from the query which correspond to a
Expand All @@ -42,13 +47,13 @@ def remove_readonly_fields(self, readonly_field_names):

# The tuple is (field, model, value) where model if used for FKs.
values[:] = (
(field, _, __) for (field, _, __) in values
(field, _, __)
for (field, _, __) in values
if field.name not in readonly_field_names
)


class SQLInsertCompiler(ReadonlySQLCompilerMixin, BaseSQLInsertCompiler):

def _exclude_readonly_fields(self, fields, readonly_field_names):
for field in fields:
if field.name not in readonly_field_names:
Expand All @@ -62,8 +67,7 @@ def remove_readonly_fields(self, readonly_field_names):
fields = self.query.fields

try:
fields[:] = self._exclude_readonly_fields(
fields, readonly_field_names)
fields[:] = self._exclude_readonly_fields(fields, readonly_field_names)
except AttributeError:
# When deserializing, we might get an attribute error because this
# list shoud be copied first :
Expand All @@ -72,5 +76,6 @@ def remove_readonly_fields(self, readonly_field_names):
# should never be mutated. If you want to manipulate this list for
# your own use, make a copy first."

self.query.fields = list(self._exclude_readonly_fields(
fields, readonly_field_names))
self.query.fields = list(
self._exclude_readonly_fields(fields, readonly_field_names)
)
84 changes: 42 additions & 42 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# complexity documentation build configuration file, created by
# sphinx-quickstart on Tue Jul 9 22:26:36 2013.
Expand All @@ -11,12 +10,13 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys, os
import os
import sys

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# sys.path.insert(0, os.path.abspath('.'))

cwd = os.getcwd()
parent = os.path.dirname(cwd)
Expand All @@ -27,27 +27,27 @@
# -- General configuration -----------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode']
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# The suffix of source filenames.
source_suffix = '.rst'
source_suffix = ".rst"

# The encoding of source files.
#source_encoding = 'utf-8-sig'
# source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'
master_doc = "index"

# General information about the project.
project = u'Django Readonly Field'
copyright = u'2016, Joachim Jablon, PeopleDoc'
project = "Django Readonly Field"
copyright = "2016, Joachim Jablon, PeopleDoc"

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand All @@ -60,71 +60,71 @@

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None
# language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
# today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']
exclude_patterns = ["_build"]

# The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None
# default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True
# add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True
# add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False
# show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
pygments_style = "sphinx"

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []
# modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False
# keep_warnings = False


# -- Options for HTML output ---------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'alabaster'
html_theme = "alabaster"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}
# html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []
# html_theme_path = []

# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
#html_title = None
# html_title = None

# A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None
# html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None
# html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None
# html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand All @@ -133,44 +133,44 @@

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'
# html_last_updated_fmt = '%b %d, %Y'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True
# html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}
# html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}
# html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True
# html_domain_indices = True

# If false, no index is generated.
#html_use_index = True
# html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False
# html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True
# html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True
# html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True
# html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''
# html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None
# html_file_suffix = None

# Output file base name for HTML help builder.
htmlhelp_basename = 'django-readonly-fielddoc'
htmlhelp_basename = "django-readonly-fielddoc"
54 changes: 38 additions & 16 deletions tests/readonly_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,56 @@


class Migration(migrations.Migration):

dependencies = [
]
dependencies = []

operations = [
migrations.CreateModel(
name='Car',
name="Car",
fields=[
('id', models.BigAutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('wheel_number', models.IntegerField()),
('manufacturer', models.CharField(max_length=100)),
(
"id",
models.BigAutoField(
verbose_name="ID",
serialize=False,
auto_created=True,
primary_key=True,
),
),
("wheel_number", models.IntegerField()),
("manufacturer", models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Book',
name="Book",
fields=[
('id', models.BigAutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('ref', models.IntegerField()),
('iban', models.CharField(max_length=100)),
('name', models.CharField(max_length=250)),
(
"id",
models.BigAutoField(
verbose_name="ID",
serialize=False,
auto_created=True,
primary_key=True,
),
),
("ref", models.IntegerField()),
("iban", models.CharField(max_length=100)),
("name", models.CharField(max_length=250)),
],
),
migrations.CreateModel(
name='Bus',
name="Bus",
fields=[
('id', models.BigAutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('wheel_number', models.IntegerField()),
('manufacturer', models.CharField(max_length=100)),
(
"id",
models.BigAutoField(
verbose_name="ID",
serialize=False,
auto_created=True,
primary_key=True,
),
),
("wheel_number", models.IntegerField()),
("manufacturer", models.CharField(max_length=100)),
],
),
]
Loading

0 comments on commit 83f3b69

Please sign in to comment.