-
Notifications
You must be signed in to change notification settings - Fork 4
/
feedback.py
265 lines (239 loc) · 7.88 KB
/
feedback.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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)
);
"""
)