Add warning if RunPython model variable name not the same as model class
This commit is contained in:
1 parent
601e02e024
commit
780630131f
4 files changed
+180
-12
No files matched your search
@@ -456,6 +456,25 @@ class MigrationLinter(object):
|
||||
ignored.append(issue)
|
||||
else:
|
||||
error.append(issue)
|
||||
|
||||
# Detect warning if model variable name is not the same as model class
|
||||
issues = self.get_runpython_model_variable_naming_issues(runpython.code)
|
||||
for issue in issues:
|
||||
if issue and issue["code"] in self.exclude_migration_tests:
|
||||
ignored.append(issue)
|
||||
else:
|
||||
warning.append(issue)
|
||||
|
||||
if runpython.reversible:
|
||||
issues = self.get_runpython_model_variable_naming_issues(
|
||||
runpython.reverse_code
|
||||
)
|
||||
for issue in issues:
|
||||
if issue and issue["code"] in self.exclude_migration_tests:
|
||||
ignored.append(issue)
|
||||
else:
|
||||
warning.append(issue)
|
||||
|
||||
return error, ignored, warning
|
||||
|
||||
@staticmethod
|
||||
@@ -487,3 +506,43 @@ class MigrationLinter(object):
|
||||
}
|
||||
)
|
||||
return issues
|
||||
|
||||
@staticmethod
|
||||
def get_runpython_model_variable_naming_issues(code):
|
||||
model_object_regex = re.compile(r"[^a-zA-Z]?([a-zA-Z0-9]+?)\.objects")
|
||||
|
||||
function_name = code.__name__
|
||||
source_code = inspect.getsource(code)
|
||||
|
||||
called_models = model_object_regex.findall(source_code)
|
||||
issues = []
|
||||
for model in called_models:
|
||||
has_same_model_name = (
|
||||
re.search(
|
||||
"{model}.*= +\w+?\.get_model\([^\)]+?\.{model}.*?\)".format(
|
||||
model=model
|
||||
),
|
||||
source_code,
|
||||
re.MULTILINE | re.DOTALL,
|
||||
)
|
||||
is not None
|
||||
or re.search(
|
||||
"{model}.*= +\w+?\.get_model\([^\)]+?,[^\)]*?{model}.*?\)".format(
|
||||
model=model
|
||||
),
|
||||
source_code,
|
||||
re.MULTILINE | re.DOTALL,
|
||||
)
|
||||
is not None
|
||||
)
|
||||
if not has_same_model_name:
|
||||
issues.append(
|
||||
{
|
||||
"code": "DATA_MIGRATION_MODEL_VARIABLE_NAME",
|
||||
"msg": (
|
||||
"'{}': Model variable name {} is different from the "
|
||||
"model class name that was found in the apps.get_model(...) call."
|
||||
).format(function_name, model),
|
||||
}
|
||||
)
|
||||
return issues
|
||||
Reference in new issue
Block a user