Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace create.py to install ckan command create table #6

Merged
merged 58 commits into from Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
2efdd61
replace create.py to install ckan command create table
Feb 1, 2023
6903abd
delete some words which dont need
Feb 1, 2023
ca16e7d
rename create.py to feedback.py and rewrite cli.py create to feedback…
Feb 7, 2023
b5ba5a9
add feedback.py to take feedback
Feb 7, 2023
4fba292
set default of ckan db setting and change where commit() is
Feb 7, 2023
f9e244e
add execute of download module
Feb 7, 2023
3546862
integrate if and for to change for xxxxx in modules
Feb 7, 2023
0989859
change bool(modules) for modules is None
Feb 7, 2023
4b8c66d
add s which forgets attaching module
Feb 7, 2023
3494fe6
move and integrate CLEAN at the first of init action
Feb 8, 2023
1604c9f
rename incorrect names
Feb 8, 2023
8a513e0
exit from programme if error occurs
Feb 8, 2023
f938e65
format the programme
Feb 8, 2023
eb1347b
change if statement to do action after cleaning
Feb 8, 2023
da742a0
add 1 as an argument of exit
Feb 8, 2023
ae8da13
add README.md about ckan command 'feedback init'
Feb 8, 2023
cd69b6d
modify README.md about ckan command
Feb 8, 2023
a58453a
modify README.md about color of sentences
Feb 8, 2023
35ae9f4
add explanation about information of postgresql
Feb 8, 2023
c6f571f
delete README.md
Feb 9, 2023
c424fae
change option name from --name to -dbname
Feb 12, 2023
882fa6f
delete feedback commnad from cli.py
Feb 12, 2023
516498b
change the location of feedback.py
Feb 12, 2023
836e135
change from \' to "
Feb 12, 2023
d3356e9
add environment variable in click.option
Feb 12, 2023
bea74df
delete short_help about click.group
Feb 12, 2023
467648c
add explanation about feedback command
Feb 12, 2023
a08d7d0
change cur and conn to cursor and connection
Feb 12, 2023
7d38c48
change the order of get_connection's argument
Feb 12, 2023
4d45a9e
delete else to reduce the nest
Feb 12, 2023
4a8a3a1
put together error process
Feb 12, 2023
9d02898
move README.md about ckan command from misc to docs
Feb 12, 2023
b50aaf3
add comment to example code
Feb 13, 2023
66e4538
delete cli.py and modify setup.sh because of movement of feedback.py
Feb 13, 2023
b3fda2d
change double-quart to single-quart
Feb 13, 2023
844b9f1
add explanation about config file
Feb 13, 2023
4696778
modify explanation about config file
Feb 13, 2023
69eccce
create function of drop tables and delete sql sentences
Feb 13, 2023
926250c
create function of creating utilization tables
Feb 13, 2023
fe4c444
create function of creating resource tables
Feb 13, 2023
049c926
create function of creating download tables
Feb 13, 2023
e5addfe
change double-quart to single-quart of sql sentences
Feb 13, 2023
c65ceb2
use linter and formatter
Feb 13, 2023
9cd96ea
test in container and fix some bugs
Feb 13, 2023
d52b1c1
change single-quart to double-quart at docstring
Feb 13, 2023
ba9a52f
change the location of clean action
Feb 13, 2023
d600197
fix the omitted word
Feb 14, 2023
682bf88
use f-string instead of format-function
Feb 14, 2023
c0b483a
change type of POSTGRES_PORT from string to integer
Feb 14, 2023
51e8553
rename genre1/genre2 to utilization_comment_category/resource_comment…
Feb 14, 2023
f8ed9c9
use formatter/linter
Feb 14, 2023
be7f6e5
rename genre1/genre2 in the function of drop table
Feb 14, 2023
ddc9465
move feedback.py to ckanext/feedback/commnad
Feb 14, 2023
c5c3bda
add IClick to install feedback command
Feb 14, 2023
2b86c3d
change double-quart to single-quart
Feb 14, 2023
40e715c
add tool.black to skip string normalization
Feb 14, 2023
44b5c13
add flake8-quotes setting to use single quotes
Feb 14, 2023
f0e054a
install flake8-quotes in pyproject.toml
Feb 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
265 changes: 265 additions & 0 deletions ckanext/feedback/command/feedback.py
@@ -0,0 +1,265 @@
import sys
import psycopg2
import click

import ckan.plugins.toolkit as tk


@click.group()
def feedback():
'''CLI tool for ckanext-feedback plugin.'''


def get_connection(host, port, dbname, user, password):
try:
connector = psycopg2.connect(
f'postgresql://{user}:{password}@{host}:{port}/{dbname}'
)
except Exception as e:
tk.error_shout(e)
sys.exit(1)
else:
return connector


@feedback.command(
name='init', short_help='create tables in ckan db to activate modules.'
)
@click.option(
'-m',
'--modules',
multiple=True,
type=click.Choice(['utilization', 'resource', 'download']),
help='specify the module you want to use from utilization, resource, download',
)
@click.option(
'-h',
'--host',
envvar='POSTGRES_HOST',
default='db',
help='specify the host name of postgresql',
)
@click.option(
'-p',
'--port',
envvar='POSTGRES_PORT',
default=5432,
help='specify the port number of postgresql',
)
@click.option(
'-d',
'--dbname',
envvar='POSTGRES_DB',
default='ckan',
help='specify the name of postgresql',
)
@click.option(
'-u',
'--user',
envvar='POSTGRES_USER',
default='ckan',
help='specify the user name of postgresql',
)
@click.option(
'-P',
'--password',
envvar='POSTGRES_PASSWORD',
default='ckan',
help='specify the password to connect postgresql',
)
def init(modules, host, port, dbname, user, password):
with get_connection(host, port, dbname, user, password) as connection:
with connection.cursor() as cursor:
try:
if not modules:
_drop_utilization_tables(cursor)
_drop_resource_tables(cursor)
_drop_download_tables(cursor)
_create_utilization_tables(cursor)
_create_resource_tabels(cursor)
_create_download_tables(cursor)
click.secho(
'Initialize all modules: SUCCESS', fg='green', bold=True
)
elif 'utilization' in modules:
_drop_utilization_tables(cursor)
_create_utilization_tables(cursor)
click.secho(
'Initialize utilization: SUCCESS', fg='green', bold=True
)
elif 'resource' in modules:
_drop_resource_tables(cursor)
_create_resource_tabels(cursor)
click.secho('Initialize resource: SUCCESS', fg='green', bold=True)
elif 'download' in modules:
_drop_download_tables(cursor)
_create_download_tables(cursor)
click.secho('Initialize download: SUCCESS', fg='green', bold=True)
except Exception as e:
tk.error_shout(e)
sys.exit(1)

connection.commit()


def _drop_utilization_tables(cursor):
cursor.execute(
'''
DROP TABLE IF EXISTS utilization CASCADE;
DROP TABLE IF EXISTS issue_resolution_summary CASCADE;
DROP TABLE IF EXISTS issue_resolution CASCADE;
DROP TABLE IF EXISTS utilization_comment CASCADE;
DROP TABLE IF EXISTS utilization_summary CASCADE;
DROP TYPE IF EXISTS utilization_comment_category;
'''
)


def _drop_resource_tables(cursor):
cursor.execute(
'''
DROP TABLE IF EXISTS resource_comment CASCADE;
DROP TABLE IF EXISTS resource_comment_reply CASCADE;
DROP TABLE IF EXISTS resource_comment_summary CASCADE;
DROP TYPE IF EXISTS resource_comment_category;
'''
)


def _drop_download_tables(cursor):
cursor.execute(
'''
DROP TABLE IF EXISTS download_summary CASCADE;
'''
)


def _create_utilization_tables(cursor):
cursor.execute(
'''
CREATE TABLE utilization (
id TEXT NOT NULL,
resource_id TEXT NOT NULL,
title TEXT,
description TEXT,
created TIMESTAMP,
approval BOOLEAN DEFAULT false,
approved TIMESTAMP,
approval_user_id TEXT,
PRIMARY KEY (id),
FOREIGN KEY (resource_id) REFERENCES resource (id),
FOREIGN KEY (approval_user_id) REFERENCES public.user (id)
);

CREATE TABLE issue_resolution_summary (
id TEXT NOT NULL,
utilization_id TEXT NOT NULL,
issue_resolution INTEGER,
created TIMESTAMP,
updated TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (utilization_id) REFERENCES utilization (id)
);

CREATE TABLE issue_resolution (
id TEXT NOT NULL,
utilization_id TEXT NOT NULL,
description TEXT,
created TIMESTAMP,
creator_user_id TEXT,
PRIMARY KEY (id),
FOREIGN KEY (utilization_id) REFERENCES utilization (id),
FOREIGN KEY (creator_user_id) REFERENCES public.user (id)
);

CREATE TYPE utilization_comment_category AS ENUM (
'Request', 'Question', 'Advertise', 'Thank'
);
CREATE TABLE utilization_comment (
id TEXT NOT NULL,
utilization_id TEXT NOT NULL,
category utilization_comment_category NOT NULL,
content TEXT,
created TIMESTAMP,
approval BOOLEAN DEFAULT false,
approved TIMESTAMP,
approval_user_id TEXT,
PRIMARY KEY (id),
FOREIGN KEY (utilization_id) REFERENCES utilization (id),
FOREIGN KEY (approval_user_id) REFERENCES public.user (id)
);

CREATE TABLE utilization_summary (
id TEXT NOT NULL,
resource_id TEXT NOT NULL,
utilization INTEGER,
comment INTEGER,
created TIMESTAMP,
updated TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (resource_id) REFERENCES resource (id)
);
'''
)


def _create_resource_tabels(cursor):
cursor.execute(
'''
CREATE TYPE resource_comment_category AS ENUM (
'Request', 'Question', 'Advertise', 'Thank'
);
CREATE TABLE resource_comment (
id TEXT NOT NULL,
resource_id TEXT NOT NULL,
category resource_comment_category NOT NULL,
content TEXT,
rating INTEGER,
created TIMESTAMP,
approval BOOLEAN DEFAULT false,
approved TIMESTAMP,
approval_user_id TEXT,
PRIMARY KEY (id),
FOREIGN KEY (resource_id) REFERENCES resource (id),
FOREIGN KEY (approval_user_id) REFERENCES public.user (id)
);

CREATE TABLE resource_comment_reply (
id TEXT NOT NULL,
resource_comment_id TEXT NOT NULL,
content TEXT,
created TIMESTAMP,
creator_user_id TEXT,
PRIMARY KEY (id),
FOREIGN KEY (resource_comment_id) REFERENCES resource_comment (id),
FOREIGN KEY (creator_user_id) REFERENCES public.user (id)
);

CREATE TABLE resource_comment_summary (
id TEXT NOT NULL,
resource_id TEXT NOT NULL,
comment INTEGER,
rating NUMERIC,
created TIMESTAMP,
updated TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (resource_id) REFERENCES resource (id)
);
'''
)


def _create_download_tables(cursor):
cursor.execute(
'''
CREATE TABLE download_summary (
id TEXT NOT NULL,
resource_id TEXT NOT NULL,
download INTEGER,
created TIMESTAMP,
updated TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (resource_id) REFERENCES resource (id)
);
'''
)
5 changes: 5 additions & 0 deletions ckanext/feedback/plugin.py
@@ -1,9 +1,11 @@
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit

from ckanext.feedback.command import feedback

class FeedbackPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IConfigurer)
plugins.implements(plugins.IClick)

# IConfigurer

Expand All @@ -12,3 +14,6 @@ def update_config(self, config_):
toolkit.add_public_directory(config_, 'public')
toolkit.add_resource('fanstatic',
'feedback')

def get_commands(self):
return [feedback.feedback]
111 changes: 111 additions & 0 deletions development/docs/README.md
@@ -0,0 +1,111 @@
# ckan feedback init

## 概要

指定した機能に関係するPostgreSQLのテーブルを初期化する。

## 実行

```
ckan feedback init [options]
```

### オプション

#### -m, --modules < utilization/ resource/ download >

**任意項目**

一部の機能を利用する場合に以下の3つから指定して実行する。(複数選択可)
このオプションの指定がない場合は全てのテーブルに対して初期化処理を行う。
* utilization
* resource
* download

##### 実行例
ryo-ma marked this conversation as resolved.
Show resolved Hide resolved

```
# ckanext-feedback plugins に関わる全てのテーブルに対して初期化を行う
ckan --config=/etc/ckan/production.ini feedback init

# utilization(利活用方法)機能に関わるテーブルに対して初期化を行う
ckan --config=/etc/ckan/production.ini feedback init -m utilization

# resource(データリソース)機能に関わるテーブルに対して初期化を行う
ckan --config=/etc/ckan/production.ini feedback init -m resource

# download(ダウンロード)機能に関わるテーブルに対して初期化を行う
ckan --config=/etc/ckan/production.ini feedback init -m download

# resource(データリソース)機能とdownload(ダウンロード)機能に関わるテーブルに対して初期化を行う
ckan --config=/etc/ckan/production.ini feedback init -m resource -m download
```

※ ckanコマンドを実行する際は```--config=/etc/ckan/production.ini```と記述して、configファイルを指定する必要がある

#### -h, --host <host_name>

**任意項目**

PostgreSQLコンテナのホスト名を指定する。
指定しない場合、以下の順で参照された値を使用する。
1. 環境変数 ```POSTGRES_HOST```
2. CKANのデフォルト値 ```db```

#### -p, --port <port>

**任意項目**

PostgreSQLコンテナのポート番号を指定する。
指定しない場合、以下の順で参照された値を使用する。
1. 環境変数 ```POSTGRES_PORT```
2. CKANのデフォルト値 ```5432```

#### -d, --dbname <db_name>

**任意項目**

PostgreSQLのデータベース名を指定する。
指定しない場合、以下の順で参照された値を使用する。
1. 環境変数 ```POSTGRES_DB```
2. CKANのデフォルト値 ```ckan```

#### -u, --user <user_name>

**任意項目**

PostgreSQLに接続するためのユーザ名を指定する。
指定しない場合、以下の順で参照された値を使用する。
1. 環境変数 ```POSTGRES_USER```
2. CKANのデフォルト値 ```ckan```

#### -P, --password <password>

**任意項目**

PostgreSQLに接続するためのパスワードを指定する。
指定しない場合、以下の順で参照された値を使用する。
1. 環境変数 ```POSTGRES_PASSWORD```
2. CKANのデフォルト値 ```ckan```

##### 実行例

```
# ホスト名として"postgresdb"を指定する
ckan --config=/etc/ckan/production.ini feedback init -h postgresdb

# ポート番号として"5000"を指定する
ckan --config=/etc/ckan/production.ini feedback init -p 5000

# データベース名として"ckandb"を指定する
ckan --config=/etc/ckan/production.ini feedback init -d ckandb

# ユーザ名として"root"を指定する
ckan --config=/etc/ckan/production.ini feedback init -u root

# パスワードとして"root"を指定する
ckan --config=/etc/ckan/production.ini feedback init -P root

# ホスト名として"postgresdb", ユーザ名として"root", パスワードとして"root"を指定する
ckan --config=/etc/ckan/production.ini feedback init -h postgresdb -u root -P root
```