Skip to content

Commit

Permalink
HDX-9406 - upgrade python 3.10 and docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
danmihaila committed Dec 19, 2023
1 parent bf6a279 commit f5e0af3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 46 deletions.
9 changes: 4 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#for local env
FROM ubuntu:jammy
#FROM ubuntu:jammy

#for local MACOS env
#FROM --platform=linux/arm64/v8 ubuntu:jammy
FROM --platform=linux/arm64/v8 ubuntu:jammy
# FROM public.ecr.aws/unocha/debian-base-s6:11-slim

ARG S6_VERSION=v2.2.0.3
Expand Down Expand Up @@ -72,8 +72,8 @@ RUN apt-get -qq -y update && \
pip install -r requirement-setuptools.txt && \
#pip install --upgrade -r requirements.txt && \
pip install pip-tools==7.3.0 && \
pip-sync requirements.txt && \
pip install --upgrade -r requirements-hdxckantool.txt && \
pip-sync requirements.txt && \
pip install \
elastic-apm[flask] \
newrelic && \
Expand All @@ -100,8 +100,7 @@ RUN apt-get -qq -y update && \
libxml2-dev \
libxslt1-dev \
libyaml-dev \
zlib1g-dev \
python-dev && \
zlib1g-dev && \
apt-get -y autoremove && \
apt-get clean && \
rm -rf \
Expand Down
54 changes: 37 additions & 17 deletions ckanext-hdx_package/ckanext/hdx_package/actions/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging

import six

from flask import request

import ckan.lib.plugins as lib_plugins
Expand All @@ -18,6 +19,8 @@
import ckanext.hdx_package.helpers.resource_triggers.common
import ckanext.hdx_package.helpers.resource_triggers.geopreview as geopreview
import ckanext.hdx_package.helpers.helpers as helpers
from ckan.types import Context, DataDict, Schema
from ckan.types.logic import ActionResult
from ckanext.hdx_org_group.helpers.org_batch import get_batch_or_generate
from ckanext.hdx_package.actions.update import process_batch_mode, flag_if_file_uploaded, run_action_without_geo_preview
from ckanext.hdx_package.helpers.constants import BATCH_MODE, BATCH_MODE_DONT_GROUP
Expand All @@ -37,6 +40,7 @@
@geopreview.geopreview_4_resources
def resource_create(context, data_dict):
'''
This runs the 'resource_create' action from core ckan's create.py
It allows us to do some minor changes and wrap it.
'''
Expand Down Expand Up @@ -83,7 +87,8 @@ def resource_create(context, data_dict):
@ckanext.hdx_package.helpers.resource_triggers.common.trigger_4_resource_changes(
BEFORE_PACKAGE_UPDATE_LISTENERS, AFTER_PACKAGE_UPDATE_LISTENERS, VERSION_CHANGE_ACTIONS
)
def package_create(context, data_dict):
def package_create(
context: Context, data_dict: DataDict) -> ActionResult.PackageCreate:
'''Create a new dataset (package).
You must be authorized to create new datasets. If you specify any groups
Expand Down Expand Up @@ -144,6 +149,19 @@ def package_create(context, data_dict):
dictionary should have keys ``'key'`` (a string), ``'value'`` (a
string)
:type extras: list of dataset extra dictionaries
:param plugin_data: private package data belonging to plugins.
Only sysadmin users may set this value. It should be a dict that can
be dumped into JSON, and plugins should namespace their data with the
plugin name to avoid collisions with other plugins, eg::
{
"name": "test-dataset",
"plugin_data": {
"plugin1": {"key1": "value1"},
"plugin2": {"key2": "value2"}
}
}
:type plugin_data: dict
:param relationships_as_object: see :py:func:`package_relationship_create`
for the format of relationship dictionaries (optional)
:type relationships_as_object: list of relationship dictionaries
Expand Down Expand Up @@ -190,10 +208,8 @@ def package_create(context, data_dict):
else:
package_plugin = lib_plugins.lookup_package_plugin(data_dict['type'])

if 'schema' in context:
schema = context['schema']
else:
schema = package_plugin.create_package_schema()
schema: Schema = context.get(
'schema') or package_plugin.create_package_schema()

_check_access('package_create', context, data_dict)

Expand Down Expand Up @@ -233,16 +249,19 @@ def package_create(context, data_dict):
model.Session.rollback()
raise logic.ValidationError(errors)

plugin_data = data.get('plugin_data', False)
include_plugin_data = False
if user:

user_obj = model.User.by_name(six.ensure_text(user))
if user_obj:
data['creator_user_id'] = user_obj.id
include_plugin_data = user_obj.sysadmin and plugin_data

# Replace model_save.package_dict_save() call with our wrapped version to be able to save groups
# pkg = model_save.package_dict_save(data, context)
from ckanext.hdx_package.actions.update import modified_save
pkg = modified_save(context, data)
pkg = modified_save(context, data,include_plugin_data)

# pkg = model_save.package_dict_save(data, context)

Expand All @@ -263,7 +282,7 @@ def package_create(context, data_dict):
for item in plugins.PluginImplementations(plugins.IPackageController):
item.create(pkg)

item.after_create(context, data)
item.after_dataset_create(context, data)

# Make sure that a user provided schema is not used in create_views
# and on package_show
Expand All @@ -277,15 +296,15 @@ def package_create(context, data_dict):
{'package': data})

# Create activity
if pkg.type == 'dataset':
user_obj = model.User.by_name(user)
if user_obj:
user_id = user_obj.id
else:
user_id = 'not logged in'

activity = pkg.activity_stream_item('new', user_id)
session.add(activity)
# if pkg.type == 'dataset':
# user_obj = model.User.by_name(user)
# if user_obj:
# user_id = user_obj.id
# else:
# user_id = 'not logged in'
#
# activity = pkg.activity_stream_item('new', user_id)
# session.add(activity)

if not context.get('defer_commit'):
model.repo.commit()
Expand All @@ -296,7 +315,8 @@ def package_create(context, data_dict):
return pkg.id

return _get_action('package_show')(
context.copy(), {'id': pkg.id}
context.copy(),
{'id': pkg.id, 'include_plugin_data': include_plugin_data}
)


Expand Down
60 changes: 37 additions & 23 deletions ckanext-hdx_package/ckanext/hdx_package/actions/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from six import text_type
from flask import request
from sqlalchemy import or_
from typing import Any

import ckan.lib.dictization.model_save as model_save
import ckan.lib.munge as munge
Expand All @@ -19,9 +20,12 @@
import ckan.logic.action.update as core_update
import ckan.plugins as plugins
import ckan.plugins.toolkit as tk
import ckan.model as model
import ckanext.hdx_package.helpers.resource_triggers.common
import ckanext.hdx_package.helpers.resource_triggers.geopreview as geopreview
import ckanext.hdx_package.helpers.helpers as helpers
from ckan.types.logic import ActionResult
from ckan.types import Context, DataDict
from ckan.common import _
from ckanext.hdx_org_group.helpers.org_batch import get_batch_or_generate
from ckanext.hdx_package.helpers.analytics import QACompletedAnalyticsSender
Expand Down Expand Up @@ -176,12 +180,17 @@ def _fetch_prev_resources_info(model, resource_ids):
@ckanext.hdx_package.helpers.resource_triggers.common.trigger_4_resource_changes(
BEFORE_PACKAGE_UPDATE_LISTENERS, AFTER_PACKAGE_UPDATE_LISTENERS, VERSION_CHANGE_ACTIONS
)
def package_update(context, data_dict):
def package_update(
context: Context, data_dict: DataDict) -> ActionResult.PackageUpdate:
'''Update a dataset (package).
You must be authorized to edit the dataset and the groups that it belongs
to.
.. note:: Update methods may delete parameters not explicitly provided in the
data_dict. If you want to edit only a specific attribute use `package_patch`
instead.
It is recommended to call
:py:func:`ckan.logic.action.get.package_show`, make the desired changes to
the result, and then call ``package_update()`` with it.
Expand Down Expand Up @@ -231,12 +240,9 @@ def package_update(context, data_dict):

user = context['user']
# get the schema
package_plugin = lib_plugins.lookup_package_plugin(pkg.type)
if 'schema' in context:
schema = context['schema']
else:
schema = package_plugin.update_package_schema()

package_plugin = lib_plugins.lookup_package_plugin(pkg.type)
schema = context.get('schema') or package_plugin.update_package_schema()
if 'api_version' not in context:
# check_data_dict() is deprecated. If the package_plugin has a
# check_data_dict() we'll call it, if it doesn't have the method we'll
Expand Down Expand Up @@ -303,15 +309,21 @@ def package_update(context, data_dict):
model.Session.rollback()
raise ValidationError(errors)

# avoid revisioning by updating directly
#avoid revisioning by updating directly
model.Session.query(model.Package).filter_by(id=pkg.id).update(
{"metadata_modified": datetime.datetime.utcnow()})
model.Session.refresh(pkg)

include_plugin_data = False
user_obj = context.get('auth_user_obj')
if user_obj:
plugin_data = data.get('plugin_data', False)
include_plugin_data = user_obj.sysadmin and plugin_data

if 'tags' in data:
data['tags'] = helpers.get_tag_vocabulary(data['tags'])

pkg = modified_save(context, data)
pkg = modified_save(context, data, include_plugin_data)
# pkg = model_save.package_dict_save(data, context)

context_org_update = context.copy()
Expand All @@ -324,7 +336,7 @@ def package_update(context, data_dict):
# Needed to let extensions know the new resources ids
model.Session.flush()
for index, (resource, upload) in enumerate(
zip(data.get('resources', []), resource_uploads)):
zip(data.get('resources', []), resource_uploads)):
resource['id'] = pkg.resources[index].id

if upload:
Expand All @@ -335,18 +347,18 @@ def package_update(context, data_dict):
for item in plugins.PluginImplementations(plugins.IPackageController):
item.edit(pkg)

item.after_update(context, data)
item.after_dataset_update(context, data)

# Create activity
if pkg.type == 'dataset':
user_obj = model.User.by_name(user)
if user_obj:
user_id = user_obj.id
else:
user_id = 'not logged in'

activity = pkg.activity_stream_item('changed', user_id)
session.add(activity)
# if pkg.type == 'dataset':
# user_obj = model.User.by_name(user)
# if user_obj:
# user_id = user_obj.id
# else:
# user_id = 'not logged in'
#
# activity = pkg.activity_stream_item('changed', user_id)
# session.add(activity)

if not context.get('defer_commit'):
model.repo.commit()
Expand All @@ -360,7 +372,7 @@ def package_update(context, data_dict):

# we could update the dataset so we should still be able to read it.
context['ignore_auth'] = True
new_data_dict = _get_action('package_show')(context, {'id': data_dict['id']})
new_data_dict = _get_action('package_show')(context, {'id': data_dict['id'], "include_plugin_data": include_plugin_data})

# HDX - delete previous files if needed
for resource_dict in new_data_dict.get('resources'):
Expand Down Expand Up @@ -410,18 +422,20 @@ def process_skip_validation(context, data_dict):
del data_dict[SKIP_VALIDATION]


def modified_save(context, data):
def modified_save(
context: Context, data: dict[str, Any],
include_plugin_data: bool = False) -> 'model.Package':
"""
Wrapper around lib.dictization.model_save.package_dict_save
"""
groups_key = 'groups'
if groups_key in data:
temp_groups = data[groups_key]
data[groups_key] = None
pkg = model_save.package_dict_save(data, context)
pkg = model_save.package_dict_save(data, context, include_plugin_data)
data[groups_key] = temp_groups
else:
pkg = model_save.package_dict_save(data, context)
pkg = model_save.package_dict_save(data, context, include_plugin_data)
package_membership_list_save(data.get("groups"), pkg, context)
return pkg

Expand Down
2 changes: 1 addition & 1 deletion ckanext-hdx_theme/ckanext/hdx_theme/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def hdx_popular(type_, number, min=1, title=None):


def hdx_license_list():
license_touple_list = base.model.Package.get_license_options()
license_touple_list = model.Package.get_license_options()
license_dict_list = [{'value': _id, 'text': _title} for _title, _id in license_touple_list]
return license_dict_list

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<div class="col-8">
{% if h.check_access('api_token_create', {'user': user['name']}) %}
<form method="POST" class="form">
{{ h.csrf_input() }}
<legend>{{ _('API Tokens') }}</legend>
{% block errors %}{{ form.errors(error_summary) }}{% endblock %}

Expand Down

0 comments on commit f5e0af3

Please sign in to comment.