org.utils

Attributes

_T

EMPTY_PARAGRAPHS

HASHTAG

IMG_URLS

libres_error_messages

ORDERED_ACCESS

Classes

ReservationInfo

AllocationEventInfo

FindYourSpotEventInfo

Functions

djb2_hash(→ int)

Implementation of the djb2 hash, a simple hash function with a

get_random_color(→ str)

Gets a random color using the given seed (a text value).

get_user_color(→ str)

Gets a user color for each username which is used for the

get_extension_color(→ str)

Gets an extension color for each file extension. This is similar to

add_class_to_node(→ None)

Adds the given classname to the given lxml node's class list.

annotate_html(…)

Takes the given html and annotates the following elements for some

remove_empty_paragraphs(…)

set_image_sizes(→ None)

parse_fullcalendar_request(→ tuple[datetime.datetime, ...)

Parses start and end from the given fullcalendar request. It is

render_time_range(→ str)

get_libres_error(→ str)

show_libres_error(→ None)

Shows a human readable error message for the given libres exception,

predict_next_daterange(→ DateRange | None)

Takes a list of dateranges (start, end) and tries to predict the next

predict_next_value(…)

Takes a list of values and tries to predict the next value in the

group_by_column(…)

Groups the given query by the given group.

keywords_first(→ Callable[[str], tuple[int, str]])

Returns a sort key which prefers values matching the given keywords

hashtag_elements(→ str)

Takes a text and adds html around the hashtags found inside.

ticket_directory_groups(→ Iterator[str])

Yields all ticket groups.

user_group_emails_for_new_ticket(→ set[str])

The user can be part of a UserGroup that defines directories. This

widest_access(→ str)

Module Contents

org.utils._T[source]
org.utils.EMPTY_PARAGRAPHS[source]
org.utils.HASHTAG[source]
org.utils.IMG_URLS[source]
org.utils.djb2_hash(text: str, size: int) int[source]

Implementation of the djb2 hash, a simple hash function with a configurable table size.

** Do NOT use for cryptography! **

org.utils.get_random_color(seed: str, lightness: float, saturation: float) str[source]

Gets a random color using the given seed (a text value).

Since the colorspace is very limited there are lots of collisions.

org.utils.get_user_color(username: str) str[source]

Gets a user color for each username which is used for the user-initials-* elements. Each username is mapped to a color.

Returns:

The user color in an css rgb string.

org.utils.get_extension_color(extension: str) str[source]

Gets an extension color for each file extension. This is similar to get_user_color(), but returns a darker color (text is white).

org.utils.add_class_to_node(node: lxml.etree._Element, classname: str) None[source]

Adds the given classname to the given lxml node’s class list.

org.utils.annotate_html(html: None, request: CoreRequest | None = None) None[source]
org.utils.annotate_html(html: str, request: CoreRequest | None = None) str

Takes the given html and annotates the following elements for some advanced styling:

  • Every paragraph containing an img element will be marked with the has-img class.

  • If a link is found which points to a youtube or a vimeo video, the link itself as well as the surrounding paragraph is marked with the has-video class

  • If a hashtag is found, the paragraph gets the ‘has-hashtag’ class.

org.utils.remove_empty_paragraphs(html: None) None[source]
org.utils.remove_empty_paragraphs(html: str) str
org.utils.set_image_sizes(images: list[lxml.etree._Element], request: onegov.core.request.CoreRequest) None[source]
org.utils.parse_fullcalendar_request(request: onegov.core.request.CoreRequest, timezone: str) tuple[datetime.datetime, datetime.datetime] | tuple[None, None][source]

Parses start and end from the given fullcalendar request. It is expected that no timezone is passed (the default).

See https://fullcalendar.io/docs/timezone/timezone/

Returns:

A tuple of timezone-aware datetime objects or (None, None).

org.utils.render_time_range(start: datetime.datetime | datetime.time, end: datetime.datetime | datetime.time) str[source]
class org.utils.ReservationInfo(resource: onegov.reservation.Resource, reservation: onegov.reservation.Reservation, request: onegov.org.request.OrgRequest)[source]
__slots__ = ('resource', 'reservation', 'request', 'translate')[source]
property date: str[source]
property warning: str | None[source]
property time: str[source]
property price: PriceDict | None[source]
as_dict() dict[str, Any][source]
class org.utils.AllocationEventInfo(resource: onegov.reservation.Resource, allocation: onegov.reservation.Allocation, availability: float, request: onegov.org.request.OrgRequest)[source]
__slots__ = ('resource', 'allocation', 'availability', 'request', 'translate')[source]
classmethod from_allocations(request: onegov.org.request.OrgRequest, resource: onegov.reservation.Resource, allocations: Iterable[Allocation]) list[typing_extensions.Self][source]
property event_start: str[source]
property event_end: str[source]
property event_identification: str[source]
property event_time: str[source]
property quota: int[source]
property quota_left: int[source]
property event_title: str[source]
property event_classes: Iterator[str][source]
property event_actions: Iterator[Link][source]
as_dict() dict[str, Any][source]
class org.utils.FindYourSpotEventInfo(allocation: onegov.reservation.Allocation, slot_time: DateRange | None, availability: float, quota_left: int, request: onegov.org.request.OrgRequest)[source]
__slots__ = ('allocation', 'slot_time', 'availability', 'quota_left', 'request', 'translate')[source]
property event_start: str[source]
property event_end: str[source]
property event_time: str[source]
property quota: int[source]
property whole_day: str[source]
property partly_available: str[source]
property available: str[source]
property event_classes: Iterator[str][source]
property css_class: str[source]
property reserveurl: str[source]
org.utils.libres_error_messages[source]
org.utils.get_libres_error(e: Exception, request: onegov.org.request.OrgRequest) str[source]
org.utils.show_libres_error(e: Exception, request: onegov.org.request.OrgRequest) None[source]

Shows a human readable error message for the given libres exception, using request.alert.

org.utils.predict_next_daterange(dateranges: Sequence[DateRange], min_probability: float = 0.8, tzinfo: TzInfo | None = None) DateRange | None[source]

Takes a list of dateranges (start, end) and tries to predict the next daterange in the list.

See predict_next_value() for more information.

org.utils.predict_next_value(values: Sequence[_T], min_probability: float = 0.8) _T | None[source]
org.utils.predict_next_value(values: Sequence[_T], min_probability: float, compute_delta: Callable[[_T, _T], _DeltaT], add_delta: Callable[[_T, _DeltaT], _T | None]) _T | None
org.utils.predict_next_value(values: Sequence[_T], min_probability: float = 0.8, *, compute_delta: Callable[[_T, _T], _DeltaT], add_delta: Callable[[_T, _DeltaT], _T | None]) _T | None

Takes a list of values and tries to predict the next value in the series.

Meant to work on a small set of ranges (with first predictions appearing with only three values), this algorithm will look at all possible deltas between the values and keep track of the probability of delta y following delta x.

If the delta between the second last and last value has a high probability of being followed by some delta p, then delta p is used to predict the next range.

If the probability is too low (signified by min_probability), then None is returned.

For large ranges better statistical models should be used. Here we are concerned with small series of data to answer the question “if a user selected three values, what will his fourth be?”

If we for example know that the user selected 1, 2 and 3, then 4 is the next probable value in the series.

org.utils.group_by_column(request: onegov.org.request.OrgRequest, query: Query[_T], group_column: Column[str] | Column[str | None], sort_column: Column[_SortT], default_group: str | None = None, transform: Callable[[_T], _T] | None = None) dict[str, list[_T]][source]
org.utils.group_by_column(request: onegov.org.request.OrgRequest, query: Query[_T], group_column: Column[str] | Column[str | None], sort_column: Column[_SortT], default_group: str | None, transform: Callable[[_T], _TransformedT]) dict[str, list[_TransformedT]]
org.utils.group_by_column(request: onegov.org.request.OrgRequest, query: Query[_T], group_column: Column[str] | Column[str | None], sort_column: Column[_SortT], default_group: str | None = None, *, transform: Callable[[_T], _TransformedT]) dict[str, list[_TransformedT]]

Groups the given query by the given group.

Parameters:
  • request – The current request used for translation and to exclude invisible records.

  • query – The query that should be grouped

  • group_column – The column by which the grouping should happen.

  • sort_column – The column by which the records should be sorted.

  • default_group – The group in use if the found group is empty (optional).

  • transform – Called with each record to transform the result (optional).

org.utils.keywords_first(keywords: Sequence[str]) Callable[[str], tuple[int, str]][source]

Returns a sort key which prefers values matching the given keywords before other values which are sorted alphabetically.

org.utils.hashtag_elements(request: onegov.org.request.OrgRequest, text: str) str[source]

Takes a text and adds html around the hashtags found inside.

The safety of this hinges on the HASHTAG regex not allowing any dangerous characters like ‘<’

org.utils.ticket_directory_groups(session: sqlalchemy.orm.Session) Iterator[str][source]

Yields all ticket groups.

For example: (‘Sportanbieter’, ‘Verein’)

If no groups exist, returns an empty generator.

org.utils.user_group_emails_for_new_ticket(request: onegov.core.request.CoreRequest, ticket: onegov.ticket.Ticket) set[str][source]

The user can be part of a UserGroup that defines directories. This means the users in this group are interested in a subset of tickets. The group is determined by the Ticket group.

This allows for more granular control over who gets notified.

org.utils.ORDERED_ACCESS = ('private', 'member', 'secret_mtan', 'mtan', 'secret', 'public')[source]
org.utils.widest_access(*accesses: str) str[source]