Package: django-jutil
django-jutilCollection of small utilities for Django and Django REST framework projects.
Django 3.0 compatible.[![codecov](https://codecov.io/gh/kajala/django-jutil/branch/master/graph/badge.svg)](https://codecov.io/gh/kajala/django-jutil)
[![Build Status](https://travis-ci.org/kajala/django-jutil.svg?branch=master)](https://travis-ci.org/kajala/django-jutil)FeaturesSimplified admin changes history logging (admin_log)ModelAdmin with length limited history view (ModelAdminBase)Simplified object URL/link generation within admin (admin_obj_link and admin_obj_url)Extended admin log with changed field values and user IP (ModelAdminBase)Admin file download mixin with file permission checks (AdminFileDownloadMixin)User authentication helpers (require_auth, AuthUserMixin)Mixin for cached model fields management (CachedFieldsMixin)BaseCommand extension which catches, logs and emails errors (SafeCommand)Command options for simplified handling of date ranges (add_date_range_arguments, parse_date_range_arguments)Various utilities for date ranges generation and iteration (dates.py)Utilities for dict sorting and formatting, choices list label fetching (dict.py)Simplified email sending via SendGrid API (send_email)Various formatting utilities (e.g. XML, timedelta, Decimal)Decimal encoder for encoding JSON objects/dictionaries containing Decimal instances (SimpleDecimalEncoder)Django middleware for exception logging/emailing (LogExceptionMiddleware)Django middleware for language cookie handling (EnsureLanguageCookieMiddleware)Django middleware for user.profile.timezone based timezone activationUtilities or Django Model handling (e.g. clone_model, get_object_or_none)Utilities for parsing booleans and datetime values (using pytz)Simple user field based permission checking for REST APIs (permissions.py)Geo IP / IP info functions using IPStack API (request.py)Download responses (FileSystemFileResponse and CsvFileResponse)Simple SMS sending (send_sms)Mixin for basic test user setup (DefaultTestSetupMixin)Pretty good unit test coverage (tests.py)URL modifying/comparison functions (urls.py)Validators and filters for various types (validators.py)XML Element to/from dict conversions (dict_to_element, xml_to_dict)XML file/content pretty formatting (format_xml, format_xml_bytes, format_xml_file, FormattedXmlResponse)Installpip install django-jutilNotes About FeaturesExtended Admin Loggingjutil.ModelAdminBase supports (by default) extended logging using Django’s native LogEntry’ change_message
JSON field. Normally Django logs only field verbose names but jutil implementation
logs changed field names and values and user IP as well to the same change_message JSON field.
To actually show output from this extended logging data you need to format change_message using
format_change_message_ex filter. The default ModelAdminBase object history template (jutil/admin/object_history.html) uses it.Static Code AnalysisThe library passes both prospector and mypy checking. To install:pip install prospector
pip install mypyTo analyze:prospector
mypy .Test Code Coveragecoverage run manage.py; coverage reportNotes About Email Configurations# Microsoft 365 (2022)
EMAIL_HOST = “smtp.office365.com”
EMAIL_PORT = 587
EMAIL_HOST_USER = (microsoft 365 email account)
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = (email password)
EMAIL_USE_TLS = True
EMAIL_TIMEOUT = 60# SendGrid (2022)
EMAIL_SENDGRID_API_KEY = (api_key)
EMAIL_HOST = “smtp.sendgrid.net”
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = “apikey”
EMAIL_HOST_PASSWORD = (sendgrid api key)Changes4.1.1:
* Refactoring: get_date_range_by_name moved from jutil.command to jutil.dates (since usage not tied to cli as is)4.0.1:
* ZoneInfo support with backports fallback
* Dropped pytz from dependencies (following Django 5.0 example)3.11.1:
* REMOVED: AdminLogEntryMixin, no longer needed as ModelAdminBase logs everything automatically
* REMOVED: admin_log_changed_fields, no longer needed as ModelAdminBase logs everything automatically
* RENAMED: get_model_keys -> get_model_field_names, for consistent naming with Django3.7.4:
* Stricter get_media_full_path and is_media_full_path3.7.3:
* Unit tests
* Get media full path cleanup3.7.2:
* Cleanup3.7.1:
* Requirements.txt: deleted optional dependency sendgrid, add sendgrid on project level sendgrid>=6.3.1,<7.0.0
* Deleted deprecated auth.require_auth, use auth.get_auth_user and auth.get_auth_user_or_none
* Deleted deprecated dict.choices_label and dict.dict_to_html, use format.choices_label and format.format_dict_as_html
* Deleted deprecated logs.*, use logging directly
* Deleted deprecated object.*, use type-safer alternatives
* Deleted deprecated url_equals and url_host, use urlparse directly
* Removed deprecated testing.*
* Cleaned up parse_datetime, parse_bool, parse_datetime_or_none and parse_bool_or_none
* Simpler cleaned up testing mixin TestSetupMixin3.6.11:
* Deprecated object.get/set obj attr functions
* Deprecated url equals and url host
* Deprecated testing.DefaultTestSetupMixin
* Separated parse functions /w xxx or none versions for better typing support
* Dropped python-dateutil dependency, added unit tests for get time steps
* Deprecated log event
* Updated send_email_sendgrid docs
* Refactored choices label and dict to html in dict and format, added deprecation warnings
* Added –this-month, –this-week, –this-year to add date range arguments, get date range by name, parse date range arguments
* Require auth refactoring
* Removed AdminFileDownloadMixin - Django 3.1 has new logic for uploaded file URL format making the mixin obsolete
* (origin/master, origin/HEAD, master) README update3.6.10:
* Unit test fix
* Log entry ordering fix3.6.9:
* Minor refactoring3.6.8:
* API testing client to support non-json content
* Parse bool unit test3.6.7:
* Added format as html json and unit tests3.6.6:
* Millisecond handling to format timedelta
* Deploy cleanup
* Python-dotenv3.6.5:
* API testing tweaks3.6.4:
* Mypy fix
* Unit test improvements, added support for FI ssns before 1900 and after 2000
* Test-coverage tweak3.6.3:
* Code QA / Prospector fixes
* Added wait object or none and unit test3.6.2:
* Added .env to ignore3.6.1:
* Cleanup
* Dropped parse sftp connection (as redundant)
* Docs3.5.3:
* Tweaks to usage of ipware3.5.2:
* Minor refactoring3.5.1:
* Upgrade of ipware to newer version
* Dev requirements change of psycopg23.4.9:
* Mypy fix
* Added get_ip() wrapper for ipware3.4.8:
* Usage cleanup of ipware3.4.7:
* Minor refactoring, added unit tests3.4.6:
* More relaxed typing to admin log
* DB check to build process3.4.5:
* Format table max col setting fix
* Fixed format table bug, added docs3.4.4:
* Added User as explicit opt parameter to admin log3.4.3:
* Minor refactoring3.4.2:
* Added strip tag fields and unit tests3.4.1:
* Type checking fixes
* Added middleware unit tests
* Changed ActivateUserProfileTimezone -> ActivateUserProfileTimezoneMiddleware to conform Django naming conventions
* Cleanup, docs
* Test-coverage script tweaks
* Added dependency-check to requirements-dev.txt
* .gitignore tweaks3.3.6:
* Unit test fix
* Build process cleanup
* Cleanup
* Test-coverage script update
* Added htmlcov to ignore3.3.5:
* Test fixes
* Admin obj url fix3.3.4:
* Admin log type fix3.3.3:
* Code QA
* Unit test improvements
* Updated travis test to python 3.83.3.2:
* Added camel case conversion funcs3.3.1:
* Format timedelta cleanup
* Test-coverage script update
* Unit test fixes
* Re-enabled pytype3.2.21:
* Email fix3.2.20:
* Unit test coverage update
* Sendgrid email format fix3.2.19:
* Added end_of_month() (as in Excel) and unit tests
* L10n3.2.18:
* Type checking fixes3.2.17:
* Type checking fixes3.2.16:
* Type checking fixes3.2.15:
* CachedFieldsMixin save() TYPE CHECKING fix3.2.14:
* Type checking fix
* Readme updates
* MANIFEST tweaks
* Pre-release script tweaks3.2.13:
* Added py.typed marker file3.2.12:
* Mypy / cleanup3.2.11:
* Test coverage update
* Added twine to dev reqs
* Deploy script update3.2.10:
* Test coverage update
* Mypy support3.2.9:
* Test coverage update
* Pre-release script tweaks
* Test coverage script tweaks3.2.8:
* Travis sys import
* Added vanity icons to readme3.2.7:
* Added sudo apt-get install -y libxml2-utils to travis3.2.6:
* Travis log changes3.2.5:
* Added travis config3.2.4:
* Codecov tests3.2.3:
* Added codecov.yml3.2.2:
* Test coverage update3.2.1:
* Dependency cleanup3.1.4:
* Test coverage update
* Language cookie to use secure and httponly settings
* Updated LICENSE.txt3.1.3:
* Test coverage update
* Cleanup
* IBAN generation3.1.2:
* Test coverage update
* Config tweaks / pytype3.1.1:
* Test coverage update3.0.16:
* Test coverage update
* Code QA pytype tool integration to build process3.0.15:
* Formatting options to format timedelta as 3h40min14s3.0.14:
* Test coverage update
* Added helpers for media path handling3.0.13:
* BIC validator
* Added body and subject to test email3.0.12:
* Test coverage update
* CsvFileResponse -> CsvResponse to be consistent with naming of other responses
* Separate XmlResponse and XmlFileResponse3.0.11:
* Test coverage update
* Cleanup3.0.10:
* Test coverage update
* Cleanup
* Xml formatting error reporting3.0.9:
* Test coverage update
* Xml content decoding fix on error
* L10n
* Email unit tests3.0.8:
* Test coverage update
* Test email sending tweaks
* More unit tests3.0.7:
* Test coverage update
* Pre-release step update3.0.6:
* Deploy tweaks
* Added coverage report
* Cleanup, deleted format xml (unnecessary since multiple tools for that)3.0.5:
* Changed SafeCommand to be compatible with BaseCommand on Exception return3.0.4:
* Unit tests, refactoring3.0.3:
* Added ucfirst and ucfirst lazy3.0.2:
* Added format table
* Added test-coverage script
* Docs3.0.1:
* Prospector 1.2.0 fixes
* Django 3.0 compatibility2.4.14:
* Unit tests, model changed check2.4.13:
* Assert tweaks
* File download admin tweaks
* Admin download link fix2.4.12:
* Support for multiple file fields in AdminFileDownloadMixin2.4.11:
* Prospector / code QA fixes
* Added SMTP based email support, logging2.4.10:
* Deploy fix2.4.9:
* Deploy cleanup and tweaks2.4.8:
* Improved testing coverage
* Coverage tweaks
* Added .coverage to .gitignore
* Removed coverage2.4.7:
* Noqa fixes (prospector)
* Added .coveragerc
* Doc tweaks
* Sftp connection string parsing cleanup2.4.6:
* Sftp connection string parsing2.4.5:
* Sendgrid api usage fix2.4.4:
* Added base url support to admin links2.4.3:
* Disabled sendgrid click tracking by default2.4.2:
* Updated to use sendgrid v3 api, added cc and bcc support2.4.1:
* Reg id -> org id name refactoring2.3.5:
* Format keys option to dict to html2.3.4:
* Added fi company reg id generator2.3.3:
* Admin helper stweaks
* Admin file download tweak
* Pre-release process
* Docs2.3.2:
* Deploy tweaks
* Prospector check to build
* Prospector clean2.3.1:
* Code cleanup2.2.43:
* Etree related refactoring2.2.42:
* ElementTree usage fix2.2.41:
* Updated requirements
* Cleaned up ugettext usage2.2.40:
* ElementTree cleanup, issues related to isinstance() usage2.2.39:
* Separate FormattedXmlResponse and FormattedXmlFileResponse2.2.38:
* Added FormattedXmlResponse2.2.37:
* Format xml bytes2.2.36:
* Format xml file improvements
* Advanced xml formatting using xmllint2.2.35:
* Sweden clearing code fixes2.2.34:
* Localized actions list sorting2.2.33:
* Dict to html unit test
* Dict to html fix2.2.32:
* Improved dict formatting2.2.31:
* Added some sanitizers to validators.py2.2.30:
* Admin obj link None handling2.2.29:
* Nordea Sweden clearing code additions2.2.28:
* Minor refactoring2.2.27:
* Added obj tools2.2.26:
* Debug code cleanup2.2.25:
* Fi ssn generator fix2.2.24:
* Added fi ssn generator2.2.23:
* Nordea/SE clearing number addition
* Deploy cleanup2.2.22:
* Added changelist view download support to AdminFileDownloadMixin2.2.21:
* Unit tests
* L10n check
* Cleanup2.2.20:
* Generic country filtering
* Docs2.2.19:
* Added one NordeaAB clearing code range2.2.18:
* Fixed non-xmllint xml data formatting
* Added format xml command2.2.17:
* Added format xml file helper2.2.16:
* Media path stripping2.2.15:
* Int conversion fix / validators
* Unit test tweaks
* Better unit tests2.2.13:
* Added missing Nordea AB / Sweden clearing number
* Clarification / swedish clearing code2.2.12:
* Added admin obj link and admin obj url2.2.11:
* Sweden clearing code filtering2.2.10:
* Reduced Swedbank account number length requirement2.2.9:
* Unit tests2.2.8:
* Dk bank info2.2.7:
* Unit test
* Sparbanken Syd account number length2.2.6:
* Belgium iban data to 4 spaces instead of 8
* Validation of all iban countries2.2.5:
* Added be iban validator, refactoring validator code a bit
* Doc cleanup2.2.4:
* Docs2.2.3:
* Element tree cleanup2.2.2:
* Unified xml.etree.ElementTree.Element usage2.2.1:
* Adding BE (Belgium) bic bank to iban bic validators (thanks kutera/master)2.1.24:
* Sweden bank const fix2.1.23:
* Unit test fixes
* Swedish bank const cleanup2.1.22:
* Swedish bank clearing code fixes2.1.21:
* Validator return space fix2.1.20:
* Added ascii filter and unit tests2.1.19:
* Refactoring
* Swedish bank const php generation2.1.18:
* Cleanup2.1.17:
* Updated bank constants, added Sweden2.1.16:
* Added ActivateUserProfileTimezone middleware2.1.15:
* Upgraded external dependencies, added days to time delta2.1.14:
* Upgraded external dependencies2.1.13:
* Age calculation2.1.12:
* Upgraded django2.1.11:
* Iso payment ref validator2.1.10:
* Email filtering tweaks2.1.9:
* Email filter tweak2.1.8:
* Dependency upgrade2.1.7:
* Error message improvements2.1.6:
* Selective cached fields update2.1.5:
* Added simple email validator2.1.4:
* Company reg id validator tweak2.1.3:
* Dropped separate read/write views since Django supports this natively2.1.2:
* Dependency upgrade2.1.1:
* Upgraded version to match django version1.2.11:
* Bank info unit test fix, upgraded libs1.2.10:
* Admin log convention change: user system username instead of the first user in the system1.2.9:
* Iban bank info convention change
* Tests
* Version bump
* Iban bank info to return None, None to be more consistent with normal output1.2.7:
* Replaced freegeoip.net API with ipstack.com1.2.6:
* Unit tests and cleanup1.2.5:
* Prev 30d, 60d and 90d time ranges1.2.4:
* 404 check1.2.3:
* Docs and cleanup1.2.2:
* Minor version bump
* Admin file download cleanup1.1.12:
* File download response cleanup1.1.11:
* SafeCommand return value1.1.10:
* Parse requirements compatibility1.1.9:
* Gz .gitignore
* Cleanup
* Parse requirements fix1.1.8:
* Cleanup
* Replaced parse requirements (fix)1.1.7:
* Replaced parse requirements1.1.6:
* FI company reg id check filtering1.1.5:
* Extended FI company reg id acceptance1.1.4:
* Permissions fix1.1.3:
* Renamed passwd -> setpass to avoid name collision with django extensions project1.1.2:
* Tests1.1.1:
* Cleanup1.0.5:
* Removed obsolete SimpleJsonDecoder since Django has better one now
* Language cookie fix1.0.4:
* Unit test fix1.0.3:
* Fi bank const update1.0.2:
* Unit tests
* Dependency fix
* Twine up1.0.1:
* Cleanup
* Docs1.0.0:
* Docs
* Github repository init