Skip to content

Commit

Permalink
replace create.py to install ckan command create table (#6)
Browse files Browse the repository at this point in the history
* replace create.py to install ckan command create table

* delete some words which dont need

* rename create.py to feedback.py and rewrite cli.py create to feedback to use feedback command

* add feedback.py to take feedback

* set default of ckan db setting and change where commit() is

* add execute of download module

* integrate if and for to change for xxxxx in modules

* change bool(modules) for modules is None

* add s which forgets attaching module

* move and integrate CLEAN at the first of init action

* rename incorrect names

* exit from programme if error occurs

* format the programme

* change if statement to do action after cleaning

* add 1 as an argument of exit

* add README.md about ckan command 'feedback init'

* modify README.md about ckan command

* modify README.md about color of sentences

* add explanation about information of postgresql

* delete README.md

* change option name from --name to -dbname

* delete feedback commnad from cli.py

* change the location of feedback.py

* change from \' to "

* add environment variable in click.option

* delete short_help about click.group

* add explanation about feedback command

* change cur and conn to cursor and connection

* change the order of get_connection's argument

* delete else to reduce the nest

* put together error process

* move README.md about ckan command from misc to docs

* add comment to example code

* delete cli.py and modify setup.sh because of movement of feedback.py

* change double-quart to single-quart

* add explanation about config file

* modify explanation about config file

* create function of drop tables and delete sql sentences

* create function of creating utilization tables

* create function of creating resource tables

* create function of creating download tables

* change double-quart to single-quart of sql sentences

* use linter and formatter

* test in container and fix some bugs

* change single-quart to double-quart at docstring

* change the location of clean action

* fix the omitted word

* use f-string instead of format-function

* change type of POSTGRES_PORT from string to integer

* rename genre1/genre2 to utilization_comment_category/resource_comment_category

* use formatter/linter

* rename genre1/genre2 in the function of drop table

* move feedback.py to ckanext/feedback/commnad

* add IClick to install feedback command

* change double-quart to single-quart

* add tool.black to skip string normalization

* add flake8-quotes setting to use single quotes

* install flake8-quotes in pyproject.toml

---------

Co-authored-by: TokioMiyaoka <gin@AlterEgo.local>
  • Loading branch information
To-Ki-O and TokioMiyaoka committed Feb 15, 2023
1 parent bfc8297 commit 0a530b9
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 244 deletions.
265 changes: 265 additions & 0 deletions ckanext/feedback/command/feedback.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# ckan feedback init

## 概要

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

## 実行

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

### オプション

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

**任意項目**

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

##### 実行例

```
# 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
```

0 comments on commit 0a530b9

Please sign in to comment.