Package: django-linkcheck
A fairly flexible app that will analyze and report on links in any model that
you register with it.Links can be bare (urls or image and file fields) or
embedded in HTML (linkcheck handles the parsing). It’s fairly easy to override
methods of the Linkcheck object should you need to do anything more
complicated (like generate URLs from slug fields etc).You should run its management command via cron or similar to check external
links regularly to see if their status changes. All links are checked
automatically when objects are saved. This is handled by signals.Minimal requirementsdjango-linkcheck requires Python 3.8 and Django 3.2.Basic usageInstall app to somewhere on your Python path (e.g.pip installdjango-linkcheck).Add'linkcheck'to yoursettings.INSTALLED_APPS.Add a file namedlinklists.pyto every app (see an example inexamples/linklists.py) that either:has models that contain content (e.g. url/image fields, chunks of markup
or anything that gets transformed into a IMG or HREF when displayedcan be the target of a link - i.e. is addressed by a url - in this case
make sure it has an instance method named ‘get_absolute_url’Hint:You can create a sample config for your model with:manage.py linkcheck_suggest_config --model sampleapp.SampleModel > sampleapp/linklists.pyRun./manage.py migrate.Add to your root url config:path('admin/linkcheck/', include('linkcheck.urls'))View/admin/linkcheck/from your browser.We are aware that this documentation is on the brief side of things so any
suggestions for elaboration or clarification would be gratefully accepted.Linklist classesThe following class attributes can be added to yourLinklistsubclasses to
customize the extracted links:object_filter: a dictionary which will be passed as a filter argument to
thefilterapplied to the default queryset of the target class. This
allows you to filter the objects from which the links will be extracted.
(example:{'active': True})object_exclude: a dictionary which will be passed as a filter argument to
theexcludeapplied to the default queryset of the target class. As withobject_filter, this allows you to exclude objects from which the links
will be extracted.html_fields: a list of field names which will be searched for links.url_fields: a list ofURLFieldfield names whose content will be
considered as links. If the field content is empty and the field name is
inignore_empty, the content is ignored.ignore_empty: a list of fields fromurl_fields. See the explanation
above. (new in django-linkcheck 1.1)image_fields: a list ofImageFieldfield names whose content will be
considered as links. EmptyImageFieldcontent is always ignored.filter_callable: a callable which allows to pass a function as filter
for your linklist class. It allows to apply more advanced filter operations.
This function must be a class method and it should be passed the objects query
set and return the filtered objects.
Example usage in your linklists.py - only check latest versions:@classmethod
def filter_callable(cls, objects):
    latest = Model.objects.filter(id=OuterRef('id')).order_by('-version')
    return objects.filter(version=Subquery(latest.values('version')[:1]))Management commandsfindlinksThis command goes through all registered fields and records the URLs it finds.
This command does not validate anything. Typically run just after installing
and configuring django-linkcheck.checklinksFor each recorded URL, check and report the validity of the URL. All internal
links are checked, but only external links that have not been checked during
the lastLINKCHECK_EXTERNAL_RECHECK_INTERVALminutes are checked. This
interval can be adapted per-invocation by using the--externalinterval(-e) command option (in minutes).You can also limit the maximum number of links to be checked by passing a number
to the--limit(--l) command option.linkcheck_suggest_configThis command goes through all models and checks whether they contain fields that
can potentially be checked by linkcheck.
If they are not yet registered, a sample config is suggested.You can also pass the option--modelto generate a sample config for the given model.SettingsLINKCHECK_DISABLE_LISTENERSA setting to totally disable linkcheck, typically when running tests. See also
the context managers below.LINKCHECK_EXTERNAL_RECHECK_INTERVALDefault: 10080 (1 week in minutes)Will not recheck any external link that has been checked more recently than this value.LINKCHECK_EXTERNAL_REGEX_STRINGDefault: r’^https?://’A string applied as a regex to a URL to determine whether it’s internal or external.LINKCHECK_MEDIA_PREFIXDefault: ‘/media/’Currently linkcheck tests whether links to internal static media are correct by wrangling the URL to be a local filesystem path.It strips MEDIA_PREFIX off the interal link and concatenates the result onto settings.MEDIA_ROOT and tests that using os.path.existsThis ‘works for me’ but it is probably going to break for other people’s setups. Patches welcome.LINKCHECK_RESULTS_PER_PAGEControls pagination.Pagination is slightly peculiar at the moment due to the way links are grouped by object.LINKCHECK_MAX_URL_LENGTHDefault: 255The length of the URL field. Defaults to 255 for compatibility with MySQL (seehttp://docs.djangoproject.com/en/dev/ref/databases/#notes-on-specific-fields)LINKCHECK_CONNECTION_ATTEMPT_TIMEOUTDefault: 10The timeout in seconds for each connection attempts. Sometimes it is useful to limit check time per connection in order to hold at bay the total check time.SITE_DOMAIN and LINKCHECK_SITE_DOMAINSLinkcheck tests external and internal using differently. Internal links use the Django test client whereas external links are tested using urllib2.Testing internal links this as if they were external can cause errors in some circumstances so Linkcheck needs to know which external urls are to be treated as internal.Linkcheck looks for either of the settings above. It only uses SITE_DOMAIN if LINKCHECK_SITE_DOMAINS isn’t presentSITE_DOMAIN = “mysite.com”would tell linkchecker to treat the following as internal links:mysite.com
www.mysite.com
test.mysite.comIf you instead set LINKCHECK_SITE_DOMAINS to be a list or tuple then you can explicitly list the domains that should be treated as internal.LINKCHECK_TOLERATE_BROKEN_ANCHORDefault:TrueWhether links with broken hash anchors should be marked as valid.
Disable this if you want that links to anchors which are not contained in the link target’s HTML source are marked as invalid.django-filebrowser integrationIf django-filebrowser is present on your path then linkcheck will listen to the post-upload, delete and rename signals and update itself accordingContributingYou can install all requirements of the development setup with the extradev:$python3-mvenv.venv$source.venv/bin/activate$pipinstall-e.[dev]$django-admincompilemessages--ignore=.venv# Optionally compile translation fileIf you want to make use of the flake8 and isort pre-commit hooks, enable them with:$pre-commitinstallRunning testsTests can be run standalone by using theruntests.pyscript in linkcheck root:$pythonruntests.pyIf you want to run linkcheck tests in the context of your project, you should include'linkcheck.tests.sampleapp'in yourINSTALLED_APPSsetting.Linkcheck gives you two context managers to enable or disable listeners in your
own tests. For example:deftest_something_without_listeners(self):withlisteners.disable_listeners():# Create/update here without linkcheck intervening.In the case you defined theLINKCHECK_DISABLE_LISTENERSsetting, you can
temporarily enable it by:deftest_something_with_listeners(self):withlisteners.enable_listeners():# Create/update here and see linkcheck activated.TranslationsAt the moment this app is available in English, German, and French.
If you want to contribute translations forLOCALE, run:django-adminmakemessages--localeLOCALEand edit the corresponding file inlinkcheck/locale/LOCALE/LC_MESSAGES/django.po.Create new releaseBump version inpyproject.tomlUpdateCHANGELOGCreate release commit:git commit--message"Release vX.Y.Z"Create git tag:git tag-a"X.Y.Z"-m"Release vX.Y.Z"Push the commit and tag to the repository:git push && git push--tagsBuild the source distribution:python-mbuildPublish the package to PyPI:twine uploaddist/django-linkcheck-X.Y.Z*