Welcome | Get started | Dive | Contribute | Topics | Reference | Changes | More

lino.core.scp

This module defines the SiteConfigPointer class.

This page contains code snippets (lines starting with >>>), which are being tested during our development workflow. The following snippet initializes the demo project used throughout this page.

>>> from lino_book.projects.min1.startup import *
class lino.core.scp.SiteConfigPointer

The SiteConfigPointer class defines methods that depend on the lino.modlib.system.SiteConfig. It is inherited by two very different other classes: lino.core.requests.base.BaseRequest and lino.core.site.Site. It has no __init__() method, so it doesn’t do anything on every incoming request.

today(self, *args, **kwargs)

Almost the same as datetime.date.today().

One difference is that the system’s today is replaced by the_demo_date if that attribute is set.

Another difference is that arguments can be passed to add some offset. See atelier.utils.date_offset().

This feature is being used in many test cases where e.g. the age of people would otherwise change.

now(self)

Return the current datetime, considering the current time zone, system.SiteConfig.simulate_today and the_demo_date.

demo_date(self, *args, **kwargs)

Deprecated. Should be replaced by today(). Compute a date using lino.utils.date_offset() based on the process startup time (or the_demo_date if this is set).

Used in Python fixtures and unit tests.

hide_natural_times

Whether naturaltime() should render naturaltime values as [ntime] instead of the actual value. This is used in doctests where output that contains naturaltime values would otherwise be difficult to test.

naturaltime(self, dt)

Return a given datetime as a text in natural language (like “5 seconds from now” or “2 months ago”).

This basically calls Django’s django.contrib.humanize.naturaltime() function (link), but after doing a bit of extra stuff.

First of all it honors the the_demo_date.

And then it hides natural times in doctests because showing natural times causes the output to be difficult to test.

>>> from datetime import timedelta
>>> ar = rt.login("robin")
>>> dt = ar.now()
>>> print(ar.naturaltime(dt+timedelta(days=2)))
[ntime]
>>> ar.hide_natural_times
True

If you really want to show a naturaltime in your doctest, you can use override_attrs to override hide_natural_times

>>> with override_attrs(ar, hide_natural_times=False):
...     print(ar.naturaltime(dt+timedelta(days=2)))
1 day, 23 hours from now
>>> with override_attrs(ar, hide_natural_times=False):
...     print(ar.naturaltime(dt+timedelta(days=2, milliseconds=1)))
2 days from now
>>> with override_attrs(ar, hide_natural_times=False):
...     print(ar.naturaltime(dt+timedelta(seconds=5, milliseconds=1)))
...     print(ar.naturaltime(dt+timedelta(seconds=-5)))
5 seconds from now
5 seconds ago