Skip to content

Commit 5c30d7a

Browse files
authored
Merge pull request #134 from open-data/changes/linting
CKAN Coding Standards
2 parents 1d4ea1f + 9a11449 commit 5c30d7a

25 files changed

+1230
-570
lines changed

.github/workflows/flake8.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Lint
2+
on: [pull_request]
3+
4+
permissions:
5+
contents: read
6+
7+
jobs:
8+
lint:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-python@v2
13+
with:
14+
python-version: '3.9'
15+
- name: Install requirements
16+
run: pip install flake8 pycodestyle
17+
- name: Check syntax
18+
# Stop the build if there are Python syntax errors or undefined names
19+
run: flake8 --show-source
20+
21+
- name: Warnings
22+
run: flake8

.github/workflows/pyright.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Check types
2+
on: [pull_request]
3+
env:
4+
NODE_VERSION: '18'
5+
PYTHON_VERSION: '3.9'
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
typecheck:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
id: gitcheckout
16+
- uses: actions/setup-python@v2
17+
with:
18+
python-version: ${{ env.PYTHON_VERSION }}
19+
- uses: actions/setup-node@v3
20+
with:
21+
node-version: ${{ env.NODE_VERSION }}
22+
- name: Install python deps
23+
id: pydepends
24+
if: steps.gitcheckout.outcome == 'success'
25+
run: |
26+
python3 -m venv /home/runner/work/app
27+
mkdir -p /home/runner/work/app/src/ckanext-recombinant
28+
cp -R . /home/runner/work/app/src/ckanext-recombinant
29+
source /home/runner/work/app/bin/activate
30+
pip install --upgrade setuptools==70.0.0
31+
pip install --upgrade pip==23.2.1
32+
pip install -e 'git+https://github.com/open-data/ckan@canada-v2.10#egg=ckan' -r 'https://raw.githubusercontent.com/open-data/ckan/canada-v2.10/requirement-setuptools.txt' -r 'https://raw.githubusercontent.com/open-data/ckan/canada-v2.10/requirements.txt' -r 'https://raw.githubusercontent.com/open-data/ckan/canada-v2.10/dev-requirements.txt'
33+
pip install -e 'git+https://github.com/ckan/ckanapi.git#egg=ckanapi' -r 'https://raw.githubusercontent.com/ckan/ckanapi/master/requirements.txt'
34+
pip install -e /home/runner/work/app/src/ckanext-recombinant/. -r /home/runner/work/app/src/ckanext-recombinant/requirements.txt -r /home/runner/work/app/src/ckanext-recombinant/test-requirements.txt
35+
pip install -e 'git+https://github.com/ckan/ckantoolkit.git#egg=ckantoolkit' -r 'https://raw.githubusercontent.com/ckan/ckantoolkit/master/requirements.txt'
36+
find /home/runner/work/app -name '*.pyc' -delete
37+
- name: Install node deps
38+
if: steps.pydepends.outcome == 'success'
39+
run: |
40+
cd /home/runner/work/app/src/ckanext-recombinant
41+
npm ci
42+
- name: Check types
43+
if: steps.pydepends.outcome == 'success'
44+
run: |
45+
cd /home/runner/work/app/src/ckanext-recombinant
46+
npx pyright

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ ckan.log
22
*.pyc
33
*.egg-info/
44
*.mo
5+
node_modules/**

changes/134.changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor code to meet CKAN and Python coding standards.

ckanext/recombinant/auth.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,63 @@
1+
from typing import Optional
2+
from ckan.types import (
3+
AuthFunction,
4+
Context,
5+
DataDict,
6+
AuthResult
7+
)
8+
19
from ckan.plugins.toolkit import chained_auth_function, h, _
2-
from ckan.model.group import Group
3-
from ckanapi import LocalCKAN, ValidationError
410

511

612
@chained_auth_function
7-
def package_update(up_func, context, data_dict):
13+
def package_update(up_func: AuthFunction,
14+
context: Context,
15+
data_dict: Optional[DataDict]) -> AuthResult:
816
"""
917
Recombinant packages and resources should not be
1018
editable by users after they have been made.
1119
1220
Affects update, patch, and delete.
1321
"""
14-
if data_dict and data_dict.get(u'type') in h.recombinant_get_types():
22+
if data_dict and data_dict.get('type') in h.recombinant_get_types():
1523
return {'success': False,
1624
'msg': _('User %s not authorized to modify Recombinant type: %s') %
17-
(str(context[u'user']), data_dict.get(u'type'))}
18-
return up_func(context, data_dict)
25+
(str(context['user']), data_dict.get('type'))}
26+
# type_ignore_reason: incomplete typing
27+
return up_func(context, data_dict) # type: ignore
1928

2029

2130
@chained_auth_function
22-
def package_create(up_func, context, data_dict):
31+
def package_create(up_func: AuthFunction,
32+
context: Context,
33+
data_dict: Optional[DataDict]) -> AuthResult:
2334
"""
2435
If the user has permissions to create packages, they should still
2536
be allowed to create the Recombinant packages and resources.
2637
2738
However, users should not be allowed to create multiple packages
2839
of the same type for a single organization.
2940
"""
30-
if data_dict and data_dict.get(u'type') in h.recombinant_get_types():
41+
if data_dict and data_dict.get('type') in h.recombinant_get_types():
3142
return {'success': False,
32-
'msg': _('User %s not authorized to create Recombinant packages: %s') %
33-
(str(context[u'user']), data_dict.get(u'type'))}
34-
return up_func(context, data_dict)
43+
'msg': _('User %s not authorized to create '
44+
'Recombinant packages: %s') %
45+
(str(context['user']), data_dict.get('type'))}
46+
# type_ignore_reason: incomplete typing
47+
return up_func(context, data_dict) # type: ignore
3548

3649

3750
@chained_auth_function
38-
def datastore_delete(up_func, context, data_dict):
51+
def datastore_delete(up_func: AuthFunction,
52+
context: Context,
53+
data_dict: Optional[DataDict]) -> AuthResult:
3954
"""
4055
Users should not be able to delete a Datestore table for
4156
Recombinant types. We only want them to be able to delete rows.
4257
"""
43-
res = context['model'].Resource.get(data_dict.get('resource_id'))
58+
if not data_dict:
59+
data_dict = {}
60+
res = context['model'].Resource.get(data_dict.get('resource_id', ''))
4461
pkg = context['model'].Package.get(getattr(res, 'package_id', None))
4562
if not res or not pkg or pkg.type not in h.recombinant_get_types():
4663
return up_func(context, data_dict)
@@ -49,6 +66,5 @@ def datastore_delete(up_func, context, data_dict):
4966
# we do not want that to happen for Recombinant types.
5067
return {'success': False,
5168
'msg': _("Cannot delete Datastore for type: %s. "
52-
"Use datastore_records_delete instead.")
53-
% pkg.type}
69+
"Use datastore_records_delete instead.") % pkg.type}
5470
return up_func(context, data_dict)

0 commit comments

Comments
 (0)