From b6fd0bc1113aa4bd1f39488c58931ae0414d299a Mon Sep 17 00:00:00 2001 From: Ryan Castner Date: Mon, 20 Feb 2017 13:49:54 -0500 Subject: [PATCH] Issue 93 (#3) * Add coverage Fixes #93 - Add 'changes_display_dict' property to 'LogEntry' model to display diff in a more human readable format 'changes_display_dict' currently handles fields with choices, long textfields and charfields, and datetime fields. Fields with choices display in the diff with their human readable text. Textfields and Charfields longer than 140 characters are truncated with an ellipsis appended. Datetime fields are rendered with the format 'Aug. 21, 2017, 02:25pm' A new kwarg was added to 'AuditlogModelRegistry' called 'mapping_fields'. The kwarg allows the user to map the fields in the model to a more human readable or intuitive name. If a field isn't mapped it will default to the name as defined in the model. Partial mapping is supported, all fields do not need to be mapped to use the feature. Tests added for 'changes_display_dict' Tests added for 'mapping_fields' property Added python 3.5 support Updated Documentation Update .travis.yml to use requirements_test.txt as well in build * Fix bug where integer fields and others would be able to be parsed by dateutil's parser and would get formatted as datetime output. Introspection is used to see if the internal field type is DateTime, Date, or Time and then try to parse it and format it appropriately. This adds functionality in that DateTime, Date and Time each now have their separate formats. --- src/auditlog/models.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/auditlog/models.py b/src/auditlog/models.py index bc64dc29..db317b80 100644 --- a/src/auditlog/models.py +++ b/src/auditlog/models.py @@ -255,12 +255,24 @@ def changes_display_dict(self): values_display.append(choices_dict.get(value, 'None')) else: for value in values: - try: - value = parser.parse(value) - value = value.strftime("%b %d, %Y %I:%M %p") - except ValueError: - pass - + if "DateTime" in field.get_internal_type(): + try: + value = parser.parse(value) + value = value.strftime("%b %d, %Y %I:%M %p") + except ValueError: + pass + elif "Date" in field.get_internal_type(): + try: + value = parser.parse(value) + value = value.strftime("%b %d, %Y") + except ValueError: + pass + elif "Time" in field.get_internal_type(): + try: + value = parser.parse(value) + value = value.strftime("%I:%M %p") + except ValueError: + pass if len(value) > 140: value = "{}...".format(value[:140])