Skip to content

Commit

Permalink
Merge pull request ckan#8160 from aleeexgreeen/add-htmx-activity-stream
Browse files Browse the repository at this point in the history
Add HTMX to activity stream
  • Loading branch information
pdelboca authored Jul 1, 2024
2 parents 2804d83 + b7dd13e commit 442b1bf
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 104 deletions.
2 changes: 2 additions & 0 deletions changes/8129.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use htmx on the activity type select element filters.
Use htmx to navigate to newer and older versions of activity stream.
1 change: 1 addition & 0 deletions changes/8129.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove `activity-stream.js` module
4 changes: 2 additions & 2 deletions ckan/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def params(self):
request.args
'''
return cast(flask.Request, self).args
return self.args


def _get_c():
Expand Down Expand Up @@ -327,7 +327,7 @@ def repr_untrusted(danger: Any):
config_declaration = local.config_declaration = Declaration()

# Proxies to already thread-local safe objects
request = cast(flask.Request, CKANRequest(_get_request))
request = CKANRequest(_get_request)
# Provide a `c` alias for `g` for backwards compatibility
g: Any = LocalProxy(_get_c)
c = g
Expand Down
5 changes: 2 additions & 3 deletions ckan/lib/captcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import requests

from ckan.common import config
from ckan.types import Request
from ckan.common import config, CKANRequest


def check_recaptcha(request: Request) -> None:
def check_recaptcha(request: CKANRequest) -> None:
'''Check a user's recaptcha submission is valid, and raise CaptchaError
on failure.'''
recaptcha_private_key = config.get('ckan.recaptcha.privatekey')
Expand Down
2 changes: 1 addition & 1 deletion ckan/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get(self) -> str:

def post(self) -> Union[str, Response]:
try:
req: dict[str, Any] = request.form.copy()
req = request.form.copy()
req.update(request.files.to_dict())
data_dict = logic.clean_dict(
dict_fns.unflatten(
Expand Down
34 changes: 0 additions & 34 deletions ckanext/activity/assets/activity-stream.js

This file was deleted.

1 change: 0 additions & 1 deletion ckanext/activity/assets/webassets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ activity:
- base/main
contents:
- dashboard.js
- activity-stream.js

activity-css:
output: ckanext-activity/%(version)s_activity.css
Expand Down
20 changes: 10 additions & 10 deletions ckanext/activity/templates/group/activity_stream.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
{% block subtitle %}{{ _('Activity Stream') }} {{ g.template_title_delimiter }} {{ super() }}{% endblock %}

{% block primary_content_inner %}

{% if activity_types is defined %}
{% snippet 'snippets/activity_type_selector.html', id=id, activity_type=activity_type, activity_types=activity_types, blueprint='activity.group_activity' %}
{% endif %}

{% snippet 'snippets/stream.html', activity_stream=activity_stream, id=id, object_type='group' %}

{% snippet 'snippets/pagination.html', newer_activities_url=newer_activities_url, older_activities_url=older_activities_url %}

{% endblock %}
{% snippet 'snippets/activity_stream.html',
activity_stream=activity_stream,
id=id,
object_type='group',
group_type=group_type,
activity_types=activity_types,
blueprint='activity.group_activity',
newer_activities_url=newer_activities_url,
older_activities_url=older_activities_url %}
{% endblock primary_content_inner %}
19 changes: 10 additions & 9 deletions ckanext/activity/templates/organization/activity_stream.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
{% block subtitle %}{{ _('Activity Stream') }} {{ g.template_title_delimiter }} {{ super() }}{% endblock %}

{% block primary_content_inner %}

{% if activity_types is defined %}
{% snippet 'snippets/activity_type_selector.html', id=id, activity_type=activity_type, activity_types=activity_types, blueprint='activity.organization_activity' %}
{% endif %}

{% snippet 'snippets/stream.html', activity_stream=activity_stream, id=id, object_type='organization', group_type=group_type %}

{% snippet 'snippets/pagination.html', newer_activities_url=newer_activities_url, older_activities_url=older_activities_url %}
{% endblock %}
{% snippet 'snippets/activity_stream.html',
activity_stream=activity_stream,
id=id,
object_type='organization',
group_type=group_type,
activity_types=activity_types,
blueprint='activity.organization_activity',
newer_activities_url=newer_activities_url,
older_activities_url=older_activities_url %}
{% endblock primary_content_inner %}
2 changes: 1 addition & 1 deletion ckanext/activity/templates/organization/read_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
{% block content_primary_nav %}
{{ super() }}
{{ h.build_nav_icon('activity.organization_activity', _('Activity Stream'), id=group_dict.name, offset=0, icon='clock') }}
{% endblock content_primary_nav %}
{% endblock content_primary_nav %}
29 changes: 10 additions & 19 deletions ckanext/activity/templates/package/activity_stream.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
{% block subtitle %}{{ _('Activity Stream') }} {{ g.template_title_delimiter }} {{ super() }}{% endblock %}

{% block primary_content_inner %}
{% if activity_types is defined %}
{% snippet 'snippets/activity_type_selector.html', id=id, activity_type=activity_type, activity_types=activity_types, blueprint='dataset.activity' %}
{% endif %}

{% if activity_stream|length > 0 %}
{% snippet 'snippets/stream.html', activity_stream=activity_stream, id=id, object_type='package' %}
{% else %}
<p>
{% if activity_type %}
{{ _('No activity found for this type') }}
{% else %}
{{ _('No activity found') }}.
{% endif %}
</p>
{% endif %}

{% snippet 'snippets/pagination.html', newer_activities_url=newer_activities_url, older_activities_url=older_activities_url %}

{% endblock %}
{% snippet 'snippets/activity_stream.html',
activity_stream=activity_stream,
id=id,
object_type='package',
group_type=group_type,
activity_types=activity_types,
blueprint='activity.package_activity',
newer_activities_url=newer_activities_url,
older_activities_url=older_activities_url %}
{% endblock primary_content_inner %}
2 changes: 1 addition & 1 deletion ckanext/activity/templates/package/read_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
{% block content_primary_nav %}
{{ super() }}
{{ h.build_nav_icon('activity.package_activity', _('Activity Stream'), id=pkg.id if is_activity_archive else pkg.name, icon='clock') }}
{% endblock content_primary_nav %}
{% endblock content_primary_nav %}
17 changes: 17 additions & 0 deletions ckanext/activity/templates/snippets/activity_stream.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% if activity_types is defined %}
{% snippet 'snippets/activity_type_selector.html', id=id, activity_type=activity_type, activity_types=activity_types, blueprint=blueprint %}
{% endif %}

{% if activity_stream|length > 0 %}
{% snippet 'snippets/stream.html', activity_stream=activity_stream, id=id, object_type=object_type %}
{% else %}
<p>
{% if activity_type %}
{{ _('No activity found for this type') }}
{% else %}
{{ _('No activity found') }}.
{% endif %}
</p>
{% endif %}

{% snippet 'snippets/pagination.html', newer_activities_url=newer_activities_url, older_activities_url=older_activities_url %}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
blueprint - blueprint to call when selecting an activity type to filter by (eg: dataset.activity)
#}

<div id="activity_types_filter" style="margin-bottom: 15px;" data-module="activity-stream">
<div id="activity_types_filter" style="margin-bottom: 15px;">
<label for="activity_types_filter_select" class="form-label">
{{ _('Activity type') }}
</label>
<select id="activity_types_filter_select" class="form-select">
<option {% if not activity_types %}selected{% endif %} data-url="{{ h.url_for(blueprint, id=id) }}">
<option {% if not activity_types %}selected{% endif %} hx-get="{{ h.url_for(blueprint, id=id) }}" hx-target="closest .module-content" hx-push-url="true">
{{ _('All activity types') }}
</option>
{% for type_ in activity_types %}
<option {% if activity_type == type_ %}selected{% endif %} data-url="{{ h.url_for(blueprint, id=id, activity_type=type_) }}">
<option {% if activity_type == type_ %}selected{% endif %} hx-get="{{ h.url_for(blueprint, id=id, activity_type=type_) }}" hx-target="closest .module-content" hx-push-url="true">
{# TODO: Calling humanize gets complex when displaying orgs/groups since the activities also contains dataset activities #}
{{ type_ | title }}
</option>
Expand Down
2 changes: 1 addition & 1 deletion ckanext/activity/templates/snippets/pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% set class_next = "btn btn-default" if older_activities_url else "btn disabled" %}

{% if newer_activities_url or older_activities_url %}
<div id="activity_page_buttons" class="activity_buttons" style="margin-top: 25px;">
<div hx-boost="true" hx-target="closest .module-content" id="activity_page_buttons" class="activity_buttons" style="margin-top: 25px;">
<a href="{{ newer_activities_url }}" class="{{ class_prev }}">
{{ _('Newer activities') }}
</a>
Expand Down
60 changes: 41 additions & 19 deletions ckanext/activity/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
_replace_group_org,
)

from ckan.common import request as ckan_request

# TODO: don't use hidden funcitons
from ckan.views.user import _extra_template_variables

Expand Down Expand Up @@ -348,21 +350,32 @@ def package_activity(id: str) -> Union[Response, str]: # noqa
activity_type=activity_type
)

return tk.render(
"package/activity_stream.html",
{
"dataset_type": dataset_type,
"pkg_dict": pkg_dict,
"activity_stream": activity_stream,
"id": id, # i.e. package's current name
"limit": limit,
"has_more": has_more,
"activity_type": activity_type,
"activity_types": VALIDATORS_PACKAGE_ACTIVITY_TYPES.keys(),
"newer_activities_url": newer_activities_url,
"older_activities_url": older_activities_url,
},
)
object_type = "package"
blueprint = "activity.{}_activity".format(object_type)

extra_vars = {
"dataset_type": dataset_type,
"pkg_dict": pkg_dict,
"activity_stream": activity_stream,
"id": id, # i.e. package's current name
"limit": limit,
"has_more": has_more,
"activity_type": activity_type,
"activity_types": VALIDATORS_PACKAGE_ACTIVITY_TYPES.keys(),
"newer_activities_url": newer_activities_url,
"older_activities_url": older_activities_url,
"blueprint": blueprint,
"object_type": object_type,
}

if ckan_request.htmx:
return tk.render(
"snippets/activity_stream.html", extra_vars
)
else:
return tk.render(
"package/activity_stream.html", extra_vars
)


@bp.route("/dataset/changes/<id>")
Expand Down Expand Up @@ -561,6 +574,8 @@ def group_activity(id: str, group_type: str) -> str:
activity_type=activity_type
)

blueprint = "activity.{}_activity".format(group_type)

extra_vars = {
"id": id,
"activity_stream": activity_stream,
Expand All @@ -569,12 +584,19 @@ def group_activity(id: str, group_type: str) -> str:
"activity_type": activity_type,
"activity_types": filter_types.keys(),
"newer_activities_url": newer_activities_url,
"older_activities_url": older_activities_url
"older_activities_url": older_activities_url,
"blueprint": blueprint,
"object_type": group_type,
}

return tk.render(
_get_group_template("activity_template", group_type), extra_vars
)
if ckan_request.htmx:
return tk.render(
"snippets/activity_stream.html", extra_vars
)
else:
return tk.render(
_get_group_template("activity_template", group_type), extra_vars
)


@bp.route(
Expand Down

0 comments on commit 442b1bf

Please sign in to comment.