Skip to content

Commit

Permalink
Merge branch '862-reject_duplicate_submissions' of github.com:kobotoo…
Browse files Browse the repository at this point in the history
…lbox/kpi into 862-reject_duplicate_submissions
  • Loading branch information
rajpatel24 committed Sep 18, 2024
2 parents 81d0b9d + 33e2c46 commit 728d0d1
Show file tree
Hide file tree
Showing 13 changed files with 753 additions and 25 deletions.
27 changes: 19 additions & 8 deletions .github/workflows/darker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@ jobs:
darker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-python@v5
- name: Install pip dependencies
run: python -m pip install flake8 flake8-quotes isort
- uses: akaihola/darker@master

- name: Set up Python
uses: actions/setup-python@v5
with:
options: '--check --isort -L "flake8 --max-line-length=88"'
revision: "origin/${{ github.event.pull_request.base.ref }}"
version: "~=2.1.1"
python-version: '3.10'

- name: Install pip dependencies
run: python -m pip install darker[isort] flake8 flake8-quotes isort --quiet

# use `--ignore=F821` to avoid raising false positive error in typing
# annotations with string, e.g. def my_method(my_model: 'ModelName')

# darker still exit with code 1 even with no errors on changes
- name: Run Darker with base commit
run: |
output=$(darker --check --isort -L "flake8 --max-line-length=88 --ignore=F821" kpi kobo hub -r ${{ github.event.pull_request.base.sha }})
[[ -n "$output" ]] && echo "$output" && exit 1 || exit 0
shell: /usr/bin/bash {0}
29 changes: 16 additions & 13 deletions format-python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

BASE_REVISION=$1

if [ -n "${UWSGI_USER}" ] && [ "${DEBIAN_FRONTEND}" == "noninteractive" ] && [ "${TMP_DIR}" == "/srv/tmp" ]; then
INSIDE_CONTAINER=1
else
INSIDE_CONTAINER=0
WHOAMI=$(whoami)
OWNER=$(ls -ld . | awk '{print $3}')
GOSU_USER=""

if [ "$WHOAMI" != "$OWNER" ]; then
GOSU_USER=$OWNER
fi

if [ -z "$BASE_REVISION" ]; then
BASE_REVISION="origin/beta"
echo "You must provide the base branch, e.g.: format-python.sh origin/beta"
exit
elif [ "$BASE_REVISION" == "-l" ] || [ "$BASE_REVISION" == "--last" ]; then
if [ "$INSIDE_CONTAINER" == "1" ]; then
BASE_REVISION=$(gosu "$UWSGI_USER" git log --oneline| head -n 1 | awk '{ print $1}')
if [ -n "$GOSU_USER" ]; then
BASE_REVISION=$(gosu "$GOSU_USER" git log --oneline| head -n 1 | awk '{ print $1}')
else
BASE_REVISION=$(git log --oneline| head -n 1 | awk '{ print $1}')
fi
Expand All @@ -27,16 +30,16 @@ fi
# - Unnecessary parentheses after class definition (--select UP039)
# - Indentation warning (--select W1)
# - No newline at end of file (--select W292)
if [ "$INSIDE_CONTAINER" == "1" ]; then
PYTHON_CHANGES=$(gosu "$UWSGI_USER" git diff --name-only "$BASE_REVISION" | grep '\.py')
if [ -n "$GOSU_USER" ]; then
PYTHON_CHANGES=$(gosu "$GOSU_USER" git diff --name-only "$BASE_REVISION" | grep '\.py')
else
PYTHON_CHANGES=$(git diff --name-only "$BASE_REVISION" | grep '\.py')
fi

if [ -n "$PYTHON_CHANGES" ]; then
echo "Using ruff..."
if [ "$INSIDE_CONTAINER" == "1" ]; then
gosu "$UWSGI_USER" git diff --name-only "$BASE_REVISION" | grep '\.py' | xargs ruff check --select Q --select I --select F401 --select UP026 --select UP034 --select UP039 --select W292 --fix
if [ -n "$GOSU_USER" ]; then
gosu "$GOSU_USER" git diff --name-only "$BASE_REVISION" | grep '\.py' | xargs ruff check --select Q --select I --select F401 --select UP026 --select UP034 --select UP039 --select W292 --fix
else
git diff --name-only "$BASE_REVISION" | grep '\.py' | xargs ruff check --select Q --select I --select F401 --select UP026 --select UP034 --select UP039 --select W292 --fix
fi
Expand All @@ -45,8 +48,8 @@ if [ -n "$PYTHON_CHANGES" ]; then
# --isort: Using isort
# --revision: Compare changes with revision $BASE_REVISION
echo "Using darker..."
if [ "$INSIDE_CONTAINER" == "1" ]; then
gosu "$UWSGI_USER" darker --isort --revision "$BASE_REVISION"
if [ -n "$GOSU_USER" ]; then
gosu "$GOSU_USER" darker --isort --revision "$BASE_REVISION"
else
darker --isort --revision "$BASE_REVISION"
fi
Expand Down
78 changes: 78 additions & 0 deletions jsapp/js/universalTable/paginatedQueryUniversalTable.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Libraries
import React, {useState, useMemo} from 'react';

// Partial components
import UniversalTable from './universalTable.component';

// Types
import type {UseQueryResult} from '@tanstack/react-query';
import type {PaginatedResponse} from 'js/dataInterface';
import type {UniversalTableColumn} from './universalTable.component';

interface PaginatedQueryHook<DataItem> extends Function {
(limit: number, offset: number): UseQueryResult<PaginatedResponse<DataItem>>;
}

interface PaginatedQueryUniversalTableProps<DataItem> {
queryHook: PaginatedQueryHook<DataItem>;
// Below are props from `UniversalTable` that should come from the parent
// component (these are kind of "configuration" props). The other
// `UniversalTable` props are being handled here internally.
columns: UniversalTableColumn[];
}

const PAGE_SIZES = [10, 30, 50, 100];
const DEFAULT_PAGE_SIZE = PAGE_SIZES[0];

/**
* This is a wrapper component for `UniversalTable`. It should be used in
* situations when you use `react-query` to fetch data, and the data is
* paginated. This component handles pagination in a neat, DRY way.
*
* All the rest of the functionalities are the same as `UniversalTable`.
*/
export default function PaginatedQueryUniversalTable<DataItem>(
props: PaginatedQueryUniversalTableProps<DataItem>
) {
const [pagination, setPagination] = useState({
limit: DEFAULT_PAGE_SIZE,
offset: 0,
});

const paginatedQuery = props.queryHook(pagination.limit, pagination.offset);

const availablePages = useMemo(
() => Math.ceil((paginatedQuery.data?.count ?? 0) / pagination.limit),
[paginatedQuery.data, pagination]
);

const currentPageIndex = useMemo(
() => Math.ceil(pagination.offset / pagination.limit),
[pagination]
);

const data = paginatedQuery.data?.results || [];

return (
<UniversalTable<DataItem>
columns={props.columns}
data={data}
pageIndex={currentPageIndex}
pageCount={availablePages}
pageSize={pagination.limit}
pageSizeOptions={PAGE_SIZES}
onRequestPaginationChange={(newPageInfo, oldPageInfo) => {
// Calculate new offset and limit from what we've got
let newOffset = newPageInfo.pageIndex * newPageInfo.pageSize;
const newLimit = newPageInfo.pageSize;

// If we change page size, we switch back to first page
if (newPageInfo.pageSize !== oldPageInfo.pageSize) {
newOffset = 0;
}

setPagination({limit: newLimit, offset: newOffset});
}}
/>
);
}
Loading

0 comments on commit 728d0d1

Please sign in to comment.