-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
418 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright (c) 2018 Alexander Todorov <atodorov@MrSenko.com> | ||
|
||
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
|
||
from pylint import interfaces | ||
from pylint import checkers | ||
from pylint.checkers import utils | ||
|
||
|
||
class BulkCreateChecker(checkers.BaseChecker): | ||
__implements__ = (interfaces.IAstroidChecker,) | ||
|
||
name = 'bulk-create-checker' | ||
|
||
msgs = {'E4451': ("Use bulk_create_with_history() instead of bulk_create()", | ||
'bulk-create-used', | ||
"bulk_create() will not save model history. " | ||
"Use bulk_create_with_history() instead!")} | ||
|
||
@utils.check_messages('bulk-create-used') | ||
def visit_attribute(self, node): | ||
if node.attrname == 'bulk_create': | ||
self.add_message('bulk-create-used', node=node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright (c) 2018 Alexander Todorov <atodorov@MrSenko.com> | ||
|
||
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
|
||
from pylint import interfaces | ||
from pylint import checkers | ||
from pylint.checkers import utils | ||
|
||
|
||
class ObjectsUpdateChecker(checkers.BaseChecker): | ||
__implements__ = (interfaces.IAstroidChecker,) | ||
|
||
name = 'objects-update-checker' | ||
|
||
msgs = {'E4461': ("Model.objects.update() doesn't update history! Use .save() instead.", | ||
'objects-update-used', | ||
"")} | ||
|
||
@utils.check_messages('objects-update-used') | ||
def visit_attribute(self, node): | ||
""" | ||
Note: this checker will produce false-positives on | ||
dict.update() or anything else that is named .update(). | ||
These should be white-listed on a line by line basis | ||
b/c there can be many situations where .update() is used | ||
after filtering or directly on top of another variable which | ||
itself is a query set. | ||
""" | ||
if node.attrname == 'update': | ||
# white-list | ||
if node.as_string() == 'context.update': | ||
return | ||
if node.as_string() == 'data.update': | ||
return | ||
self.add_message('objects-update-used', node=node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from django.db.models import signals | ||
from simple_history.models import HistoricalRecords | ||
from simple_history.admin import SimpleHistoryAdmin | ||
|
||
|
||
class KiwiHistoricalRecords(HistoricalRecords): | ||
""" | ||
This class will keep track of what fields were changed | ||
inside of the ``history_change_reason`` field. This gives us | ||
a crude changelog until upstream introduces their new interface. | ||
""" | ||
|
||
def pre_save(self, instance, **kwargs): | ||
""" | ||
Signal handlers don't have access to the previous version of | ||
an object so we have to load it from the database! | ||
""" | ||
if instance.pk: | ||
try: | ||
instance.previous = instance.__class__.objects.get(pk=instance.pk) | ||
except: # noqa: bare-except | ||
# raises for tcms.settings.test tcms.xmlrpc.tests.test_category.TestCategory | ||
# b/c there are no statuses in the DB for some reason. | ||
# todo: not sure if this can raise in production | ||
pass | ||
|
||
def post_save(self, instance, created, **kwargs): | ||
""" | ||
Calculate the changelog and call the inherited method to | ||
write the data into the database. | ||
""" | ||
change_reason = [] | ||
|
||
if hasattr(instance, 'previous'): | ||
for field in self.fields_included(instance): | ||
old_value = getattr(instance.previous, field.attname) | ||
new_value = getattr(instance, field.attname) | ||
if old_value != new_value: | ||
change_reason.append("%s: %s -> %s" % (field.attname, old_value, new_value)) | ||
instance.changeReason = "; ".join(change_reason) | ||
super().post_save(instance, created, **kwargs) | ||
|
||
def finalize(self, sender, **kwargs): | ||
""" | ||
Connect the pre_save signal handler after calling the inherited method. | ||
""" | ||
super().finalize(sender, **kwargs) | ||
signals.pre_save.connect(self.pre_save, sender=sender, weak=False) | ||
|
||
|
||
class ReadOnlyHistoryAdmin(SimpleHistoryAdmin): | ||
""" | ||
Custom history admin which shows all fields | ||
as read-only. | ||
""" | ||
history_list_display = ['history_change_reason'] | ||
|
||
def get_readonly_fields(self, request, obj=None): | ||
# make all fields readonly | ||
readonly_fields = list(set( | ||
[field.name for field in self.opts.local_fields] + | ||
[field.name for field in self.opts.local_many_to_many] | ||
)) | ||
return readonly_fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.