noggin.utility package

Submodules

noggin.utility.controllers module

noggin.utility.controllers.group_or_404(ipa, groupname)[source]
noggin.utility.controllers.require_self(f)[source]

Require the logged-in user to be the user that is currently being edited

noggin.utility.controllers.user_or_404(ipa, username)[source]
noggin.utility.controllers.with_ipa()[source]

noggin.utility.forms module

exception noggin.utility.forms.FormError(field, message)[source]

Bases: Exception

populate_form(form)[source]
noggin.utility.forms.handle_form_errors(form)[source]

Handle form errors by raising exceptions.

The point of this context manager is to let controller developers create form errors by raising exceptions instead of setting variables. This is particularly useful when you are making multiple API calls in a row and handling exceptions separately: instead of doing nested try..except..else statements they would have non-nested code raising exceptions.

For example, without this function you would have something similar to:

if form.validate_on_submit():
    try:
        api_call_1()
    except UserError as e:
        form.user.errors.append(e.msg)
    else:
        try:
            api_call_2()
        except PasswordError as e:
            form.password.errors.append(e.msg)
        else:
            try:
                api_call_3()
            except GenericError as e:
                form.non_field_errors.errors.append(e.msg)
            else:
                flash("Success!")
                return redirect("/")
return render_template(..., form=form)

Every API call causes an additional level of nesting because the code must fall through the initial if statement to reach the render_template call. With this function this could be rewritten as:

if form.validate_on_submit():
    with handle_form_errors(form):
        try:
            api_call_1()
        except UserError as e:
            raise FormError("user", e.msg)
        try:
            api_call_2()
        except PasswordError as e:
            raise FormError("password", e.msg)
        try:
            api_call_3()
        except GenericError as e:
            raise FormError("non_field_errors", e.msg)
        flash("Success!")
        return redirect("/")
return render_template(..., form=form)

This code does not nest more on each API call, which is (arguably) clearer as the number of necessary API call increases.

Args: form (wtforms.Form): The form that errors should be stored to

noggin.utility.messaging module

noggin.utility.messaging.backoff_hdlr(details)[source]
noggin.utility.messaging.publish(message)[source]

noggin.utility.pagination module

class noggin.utility.pagination.PagedResult(items=None, total=None, page_size=None, page_number=None)[source]

Bases: object

property has_next_page
property has_previous_page
page_url(page_number)[source]
property total_pages
truncated_pages_list(margin=4)[source]
noggin.utility.pagination.paginated_find(ipa, representation, *args, **kwargs)[source]

noggin.utility.password_reset module

class noggin.utility.password_reset.PasswordResetLock(username)[source]

Bases: object

delete()[source]
store()[source]
valid_until()[source]

noggin.utility.templates module

noggin.utility.templates.format_channel(value)[source]
noggin.utility.templates.format_chat(value, isnick)[source]
noggin.utility.templates.format_nickname(value)[source]
noggin.utility.templates.gravatar(email, size)[source]
noggin.utility.templates.undo_button(form_action, submit_name, submit_value, hidden_tag)[source]

return an undo button html snippet as a string, to be used in flash messages

noggin.utility.timezones module

noggin.utility.token module

class noggin.utility.token.Audience(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

In JWT the audience is a constant that must remain the same between the token creator and the token reader, as a way to prevent token re-use.

The longer the string, the longer the token, so we try to keep it short because some tokens end up in URLs and that’s limited.

email_validation = 'ev'
password_reset = 'pr'
spam_check = 'sc'
noggin.utility.token.make_password_change_token(user)[source]
noggin.utility.token.make_token(data, audience, ttl=None)[source]
noggin.utility.token.read_token(token, audience=None)[source]

Module contents

noggin.utility.import_all(import_name)[source]