Skip to content

CjkCMS Tags

cjkcms.templatetags.cjkcms_tags

can_show_item

can_show_item(context, item_visibility: str) -> bool

Menu item visibility conditional on selection in cms_settings.AUTH_VISIBILITY_CHOICES

Source code in cjkcms/templatetags/cjkcms_tags.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@register.simple_tag(takes_context=True)
def can_show_item(context, item_visibility: str) -> bool:
    """
    Menu item visibility conditional on selection in cms_settings.AUTH_VISIBILITY_CHOICES
    """
    if item_visibility == "hidden":
        return False
    if item_visibility == "all":
        return True

    try:
        context["request"].user.is_authenticated
    except KeyError:
        return False
    is_auth = context["request"].user.is_authenticated
    return bool(
        is_auth
        and item_visibility == "auth-only"
        or not is_auth
        and item_visibility == "non-auth-only"
    )

cjkcms_version

cjkcms_version() -> str

Returns version of the CjkCMS formatted as string YY:[M]M:[D]D. Note: the CjkCMS uses Calendar Versioning

Source code in cjkcms/templatetags/cjkcms_tags.py
57
58
59
60
61
@register.simple_tag
def cjkcms_version() -> str:
    """Returns version of the CjkCMS formatted as string YY:[M]M:[D]D.
    Note: the CjkCMS uses Calendar Versioning"""
    return __version__

define

define(val=None)

Allows defining a new variable in the template

Source code in cjkcms/templatetags/cjkcms_tags.py
270
271
272
273
@register.simple_tag
def define(val=None):
    """Allows defining a new variable in the template"""
    return val

is_advanced_setting

is_advanced_setting(obj: object) -> bool

Returns True if object passed as argument is of type CjkcmsAdvSettings

Source code in cjkcms/templatetags/cjkcms_tags.py
51
52
53
54
@register.filter
def is_advanced_setting(obj: object) -> bool:
    """Returns True if object passed as argument is of type CjkcmsAdvSettings"""
    return CjkcmsAdvSettings in (obj.__class__,) + obj.__class__.__bases__

is_in_future

is_in_future(the_date)

Checks if given date or datetime is in future

Source code in cjkcms/templatetags/cjkcms_tags.py
276
277
278
279
280
281
282
283
284
285
@register.filter(name="is_in_future")
def is_in_future(the_date):
    """Checks if given date or datetime is in future"""
    if isinstance(the_date, datetime):
        return the_date >= datetime.now()
    elif isinstance(the_date, date):
        # Convert date to datetime with time set to midnight
        datetime_date = datetime(the_date.year, the_date.month, the_date.day)
        return datetime_date >= datetime.now()
    return False
link_display(context, text: str) -> str

Check for double-curly braces + user.username/first_name/last_name/first_last_name. If found, display as requested from user record

Source code in cjkcms/templatetags/cjkcms_tags.py
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
@register.simple_tag(takes_context=True)
def link_display(context, text: str) -> str:
    """
    Check for double-curly braces + user.username/first_name/last_name/first_last_name.
    If found, display as requested from user record
    """
    if text not in {
        "{{ user.username }}",
        "{{ user.first_name }}",
        "{{ user.last_name }}",
        "{{ user.first_last_name }}",
    }:
        return text
    u = context["request"].user
    if text == "{{ user.username }}":
        return u.username
    if text == "{{ user.first_name }}":
        return u.first_name
    if text == "{{ user.last_name }}":
        return u.last_name
    return f"{u.first_name} {u.last_name}"

map_to_bootstrap_alert

map_to_bootstrap_alert(message_tag)

Converts a message level to a bootstrap 5 alert class

Source code in cjkcms/templatetags/cjkcms_tags.py
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
@register.filter
def map_to_bootstrap_alert(message_tag):
    """
    Converts a message level to a bootstrap 5 alert class
    """
    message_to_alert_dict = {
        "debug": "primary",
        "info": "info",
        "success": "success",
        "warning": "warning",
        "error": "danger",
    }

    try:
        return message_to_alert_dict[message_tag]
    except KeyError:
        return ""

query_update

query_update(querydict, key=None, value=None)

Alters querydict (request.GET) by updating/adding/removing key to value

Source code in cjkcms/templatetags/cjkcms_tags.py
179
180
181
182
183
184
185
186
187
188
189
190
191
@register.simple_tag
def query_update(querydict, key=None, value=None):
    """
    Alters querydict (request.GET) by updating/adding/removing key to value
    """
    get = querydict.copy()
    if key:
        if value:
            get[key] = value
        else:
            with contextlib.suppress(KeyError):
                del get[key]
    return get