intranet.utils package¶
Submodules¶
intranet.utils.admin_helpers module¶
- intranet.utils.admin_helpers.export_csv_action(description='Export selected objects as CSV file', fields=None, exclude=None, header=True)[source]¶
This function returns an export csv action.
‘fields’ and ‘exclude’ work like in django
ModelForm ‘header’ is whether or not to output the column names as the first row.
intranet.utils.cache module¶
intranet.utils.date module¶
intranet.utils.deletion module¶
intranet.utils.helpers module¶
- class intranet.utils.helpers.GlobList(iterable=(), /)[source]¶
Bases:
list
A list of glob-style strings.
- class intranet.utils.helpers.InvalidString[source]¶
Bases:
str
An error for undefined context variables in templates.
- intranet.utils.helpers.debug_toolbar_callback(request)[source]¶
- In development:
Show the debug toolbar to all users.
- In production:
Show the debug toolbar to those with the Django staff permission, excluding the Eighth Period office.
- intranet.utils.helpers.get_ap_week_warning(request)[source]¶
ap_day = timezone.localtime() if ap_day.hour > 16:
ap_day += datetime.timedelta(days=1)
- while ap_day.weekday() >= 5: # Saturday or Sunday
ap_day += datetime.timedelta(days=1)
data = {“day”: ap_day.day, “date”: request.GET.get(“date”, None)} if ap_day.month == 5 and 4 <= ap_day.day <= 17:
return get_template(“auth/ap_week_schedule.html”).render(data)
- intranet.utils.helpers.get_theme() Dict[str, Dict[str, str]] [source]¶
Return JS and CSS for the currently active special event theme.
- intranet.utils.helpers.get_theme_name() str [source]¶
Get the name of the currently active special event theme.
- intranet.utils.helpers.get_warning_html(warnings, dashboard=False, login=False)[source]¶
Returns HTML for announcements.models.WarningAnnouncement objects. Dashboard and login logic is processed here because they cannot be filtered using .filter(). Global warnings are pre-filtered and passed directly to this function from the context processor.
- intranet.utils.helpers.join_nicely(items: Collection) str [source]¶
Joins together a list of items in a human-readable format. Examples: >>> join_nicely([]) ‘’ >>> join_nicely([‘a’]) ‘a’ >>> join_nicely([‘a’, ‘b’]) ‘a and b’ >>> join_nicely([‘a’, ‘b’, ‘c’]) ‘a, b, and c’
- Parameters:
items – The items to join together.
- Returns:
The resulting joined-together string.
intranet.utils.html module¶
- intranet.utils.html.link_removal_callback(attrs: Mapping[Union[str, Tuple[Optional[str]]], str], new: bool = False) Optional[Mapping[Union[str, Tuple[Optional[str]]], str]] [source]¶
Internal callback for
nullify_links()
.
- intranet.utils.html.nullify_links(text: str) str [source]¶
Given a string containing HTML, changes the
href
attribute of any links to “javascript:void(0)” to render the link useless.- Parameters:
text – The HTML string in which links should be nullified.
- Returns:
The HTML string with all links nullified.
intranet.utils.locking module¶
- intranet.utils.locking.lock_on(items: Iterable[Union[Model, Manager, QuerySet]]) None [source]¶
Given an iterable of
Model
instances, ``Manager``s, and/or ``QuerySet``s, locks the corresponding database rows.More specifically, this uses the Django ORM’s
select_for_update()
method, which translates to aSELECT FOR UPDATE
SQL query. For more information on what this actually does in PostgreSQL (used as the database backend in all environments) see PostgreSQL’s documentation on locking at https://www.postgresql.org/docs/current/explicit-locking.html#LOCKING-ROWS.As described in Django’s documentation at https://docs.djangoproject.com/en/stable/ref/models/queryset/#django.db.models.query.QuerySet.select_for_update, the
select_for_update
locks prevent other transactions from acquiring locks until this transaction is complete.This MUST by run in a transaction. A straightforward way to do this is to use the
django.db.transaction.atomic
wrapper.