forked from SIOS-Svalbard/darwinsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cgi
executable file
·282 lines (218 loc) · 7.9 KB
/
index.cgi
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/python3
# encoding: utf-8
'''
-- Web interface for generating Excel sheet.
Fork of https://github.com/umeldt/darwinsheet
@author: Pål Ellingsen
@author: Christian Svindseth
@deffield updated: Updated
'''
import os
import sys
import cgi
import cgitb
import codecs
import uuid
import yaml
import rdflib
import tempfile
import xlsxwriter
import shutil
import http.cookies as Cookie
import textwrap
import scripts.make_xlsx as mx
from scripts.process_xlsx import Term
import config.fields as fields
__updated__ = '2019-03-06'
#cgitb.enable()
SETUP_DEFAULT = 'darwin'
cookie = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE"))
method = os.environ.get("REQUEST_METHOD", "GET")
# method = "POST"
#sys.stdout.buffer.write(b"Content-Type: text/html\n\n")
form = cgi.FieldStorage()
# For determining which layout
if 'setup' in form:
setup = form['setup'].value
else:
setup = SETUP_DEFAULT
# print(setup)
cores = yaml.load(
open(os.path.join("config", "config.yaml"), encoding='utf-8'))['cores']
names = [] # For holding a list over the possible setups
for core in cores:
names.append(core['name'])
if core['name'] == setup:
config = core['sheets'][0]
# Put the language in an ordered place
config['languages'] = yaml.load(
open(os.path.join("config", "config.yaml"), encoding='utf-8'))['languages']
if 'language' in cookie:
language = cookie['language'].value
else:
language = 'en'
g = rdflib.Graph()
g.load("dwcterms.rdf")
# Populate the terms with the terms from dwc
for s, p, o in g:
term = Term.get(s)
if str(p) == "http://www.w3.org/2000/01/rdf-schema#comment":
term.examples["_"] = str(o)
elif str(p) == "http://purl.org/dc/terms/description":
term.definitions["_"] = str(o)
g.load("dcterms.rdf")
# Populate the terms with the terms from dublin core
for s, p, o in g:
term = Term.get(s)
if str(p) == "http://www.w3.org/2000/01/rdf-schema#comment":
term.examples["_"] = str(o)
elif str(p) == "http://purl.org/dc/terms/description":
term.definitions["_"] = str(o)
elif str(p) == "http://www.w3.org/2000/01/rdf-schema#label":
term.labels[o.language] = str(o)
# Translate terms
t = rdflib.Graph()
t.load("dwctranslations.rdf")
for s, p, o in t:
if type(o) == rdflib.term.Literal and o.language:
term = Term.get(s)
if str(p) == "http://www.w3.org/2004/02/skos/core#example":
term.examples[o.language] = str(o)
elif str(p) == "http://www.w3.org/2004/02/skos/core#definition":
term.definitions[o.language] = str(o)
elif str(p) == "http://www.w3.org/2004/02/skos/core#prefLabel":
term.labels[o.language] = str(o)
# Make variable to hold terms not specified in config
# Remove terms in config
dwcnames = []
for term in Term.terms:
if term.name != '':
dwcnames.append(term.name)
dwcnames = sorted(dwcnames, key=lambda s: s.lower())
for group in config['grouping']:
for term in config['terms'][group]:
if term in dwcnames:
dwcnames.remove(term)
# Append these to the config under new headline
fill_name = 'Other'
dwc_name1 = 'Other_DwC_Terms'
dwc_name2 = 'Other_DwC_Terms_cont'
if len(config['grouping']) % 2 != 0:
config['grouping'].append(fill_name)
config['terms'][fill_name] = []
config['grouping'].append(dwc_name1)
config['grouping'].append(dwc_name2)
config['terms'][dwc_name1] = []
config['terms'][dwc_name2] = []
ii = 0
for name in dwcnames:
if ii < len(dwcnames) / 2:
config['terms'][dwc_name1].append(name)
else:
config['terms'][dwc_name2].append(name)
ii = ii + 1
# print("New config\n", config)
# print("Other_DwC_Terms\n", config['terms'][dwc_name])
# Add terms not in dwc
for field in fields.fields:
term = Term.get(field['name'])
if not(term.labels): # Set the label
term.labels = {'en': field['disp_name']}
if not(term.validations): # Set the validation input
term.validations = {
'en': field['valid']['input_message'].replace('\n', '</br>')}
# ignore = [] # ["Organism", "Occurrence", "Taxon"] # ?
mofterms = [
"measurementID", "measurementType", "measurementValue",
"measurementAccuracy", "measurementUnit",
"measurementDeterminedBy", "measurementDeterminedDate",
"measurementMethod"
]
from mako.template import Template
from mako.lookup import TemplateLookup
templates = TemplateLookup(
directories=['templates'], output_encoding='utf-8')
# method = 'POST'
if method == "GET": # This is for getting the page
sys.stdout.buffer.write(b"Content-Type: text/html\n\n")
# Using sys, as print doesn't work for cgi in python3
fname = os.path.basename(setup) # Stop any reference to other places
template = templates.get_template(fname + ".html")
sys.stdout.flush()
sys.stdout.buffer.write(
template.render(config=config, Term=Term, lang=language, names=names))
elif method == "POST":
if 'language' in form:
cookie['language'] = form['language'].value
print(cookie.output())
print("Status: 303 See other")
print("Location: %s\n" % os.environ["HTTP_REFERER"])
sys.exit(0)
reserved = [setup] # ['measurementorfact', 'uuid']
# form = ['uuid', 'shipid', 'eventDate',
# 'decimalLatitude', 'decimalLongitude']
terms = []
# Build list of terms from config, this is for sorting
terms_config = []
# print(config['terms'])
for group in config['grouping']:
# print(group)
for term in config['terms'][group]:
terms_config.append(term)
# print(terms)
# print(terms_config)
# print(terms)
# Use the config terms to sort the terms from the form
#sys.stdout.buffer.write(b"Content-Type: text/html\n\n")
#print(form)
for term in terms_config:
if term in form and term not in reserved:
terms.append(term)
# Add terms from darwin core not in the config
print("Content-Type: application/vnd.ms-excel")
if setup == "aen":
print(
"Content-Disposition: attachment; filename=AeN_cruisenumber_instr_name.xlsx\n")
else:
print(
"Content-Disposition: attachment; filename=Excel_template.xlsx\n")
path = "/tmp/" + next(tempfile._get_candidate_names()) + '.xlsx'
# Need to make the field_dict and append useful info from dwc
field_dict = mx.make_dict_of_fields()
for t in Term.terms:
# print(field)
if t.name in field_dict.keys():
if t.definition(language):
# print(t.name)
valid = field_dict[t.name].validation
valid['input_message'] = valid['input_message'] + \
'\n\nDarwin core supl. info:\n' + \
textwrap.fill(
' '.join(t.definition(language).split()), width=40)
field_dict[t.name].set_validation(valid)
else:
field_dict[t.name] = mx.Field(
name=t.name,
disp_name=t.label(language),
width=len(t.label(language)),
validation={'validate': 'any',
'input_title': t.label(language),
'input_message': 'Darwin core supl. info:\n' +
textwrap.fill(
' '.join(t.definition(language).split()),
width=40)
}
)
# print(depth.name, depth.disp_name, depth.validation)
# print(terms)
metadata = False
conversions = False
if 'extrasheet' in config:
if 'metadata' in config['extrasheet']:
metadata = True
if 'conversions' in config['extrasheet']:
conversions = True
mx.write_file(path, terms, field_dict, metadata, conversions)
with open(path, "rb") as f:
sys.stdout.flush()
shutil.copyfileobj(f, sys.stdout.buffer)