org.utils
Attributes
Classes
Functions
|
Implementation of the djb2 hash, a simple hash function with a |
|
Gets a random color using the given seed (a text value). |
|
Gets a user color for each username which is used for the |
|
Gets an extension color for each file extension. This is similar to |
|
Adds the given classname to the given lxml node's class list. |
Takes the given html and annotates the following elements for some |
|
|
|
|
Parses start and end from the given fullcalendar request. It is |
|
|
|
|
|
Shows a human readable error message for the given libres exception, |
|
Takes a list of dateranges (start, end) and tries to predict the next |
Takes a list of values and tries to predict the next value in the |
|
Groups the given query by the given group. |
|
|
Returns a sort key which prefers values matching the given keywords |
|
Takes a text and adds html around the hashtags found inside. |
|
Yields all ticket groups. |
|
The user can be part of a UserGroup that defines directories. This |
|
|
Extracts categories and subcategories from the newsletter categories |
Module Contents
- 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: markupsafe.Markup, request: CoreRequest | None = None) markupsafe.Markup [source]
- org.utils.annotate_html(html: None, request: CoreRequest | None = None) None
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: markupsafe.Markup) markupsafe.Markup
- 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]
- class org.utils.AllocationEventInfo(resource: onegov.reservation.Resource, allocation: onegov.reservation.Allocation, availability: float, request: onegov.org.request.OrgRequest)[source]
- class org.utils.FindYourSpotEventInfo(allocation: onegov.reservation.Allocation, slot_time: DateRange | None, availability: float, quota_left: int, request: onegov.org.request.OrgRequest)[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) markupsafe.Markup [source]
Takes a text and adds html around the hashtags found inside.
- 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.extract_categories_and_subcategories(categories: dict[str, list[dict[str, list[str]] | str]], flattened: Literal[False] = False) tuple[list[str], list[list[str]]] [source]
- org.utils.extract_categories_and_subcategories(categories: dict[str, list[dict[str, list[str]] | str]], flattened: Literal[True]) list[str]
Extracts categories and subcategories from the newsletter categories dictionary in newsletter settings.
Example for categories dict: {
- ‘org_name’: [
{‘main_category_1’}, {‘main_category_2’: [‘sub_category_21’, ‘sub_category_22’]}
]
} returning a tuple of lists:
[‘main_category_1’, ‘main_category_2’], [[], [‘sub_category_21’, ‘sub_category_22’]]
- if flattened is True, it returns a flat list of the above tuple:
[‘main_category_1’, ‘main_category_2’, ‘sub_category_21’, ‘sub_category_22’]