Commit 216c01f4 authored by Raphael Jacquot's avatar Raphael Jacquot

allow users to edit lines with errors, and re-submits the line to the validation system

parent f70dabf1
Pipeline #32286 passed with stage
in 3 minutes and 33 seconds
......@@ -4,6 +4,7 @@ def includeme(config):
config.add_route('submit_csv', '/submit/csv/')
config.add_route('submit_thanks', '/submit/thanks/')
config.add_route('verify', '/verify/{token}')
config.add_route('get_line_form', '/verify/{token}/{line}')
config.add_route('moderate', '/moderate/')
config.add_route('moderate-submission', '/moderate/submission/{token}')
config.add_route('jmdc_catalog', '/')
......
......@@ -5,4 +5,29 @@
border-bottom: 1px solid grey;
background-color: silver;
cursor: pointer;
}
.input-form {
font-size: 8pt;
border-bottom: 1px solid grey;
margin-bottom: 4px;
}
.input-group {
margin-top: 4px;
display: flex;
align-items: center;
}
.input-form label {
display: inline-block;
flex-basis: 10em;
}
.input-form input {
flex: auto;
border: 1px solid grey;
padding: 1px 4px;
border-radius: 3px;
}
.input-form button {
font-size: 8pt;
margin: 4px 10em;
}
\ No newline at end of file
function get_line_form(entry_log) {
let input_forms = entry_log.getElementsByClassName('input-form');
if (input_forms.length!=0) return;
let ajax = new XMLHttpRequest()
ajax.addEventListener("load", (event) => {
set_line_form(event, entry_log);
});
var line_no = entry_log.getElementsByClassName("lineno");
if (line_no.length != 1) return false;
line_no = line_no[0];
line_no = line_no.innerHTML;
console.log("'"+line_no+"'");
line_no = parseInt(line_no);
console.log(line_no);
ajax.open("GET", document.location+"/"+line_no);
ajax.send();
}
function set_line_form(event, entry_log) {
if (event.target.status!=200) {
console.log("error");
console.log(event.target);
return;
}
let template = document.createElement("template");
template.innerHTML = event.target.response.trim();
let form = template.content.firstChild;
let buttons = form.getElementsByTagName('button');
for (var button of buttons)
button.addEventListener("click", submit_line_form);
entry_log.append(form);
}
function submit_line_form(event) {
event.preventDefault();
let button = event.target;
let form = button.form;
let formdata = new FormData(form);
formdata.append(button.name, button.value);
let ajax = new XMLHttpRequest();
ajax.addEventListener("load", (event) => {
update_line(event, form);
});
let line = form.closest(".entry-log");
let line_no = parseInt(line.querySelector('.lineno').innerHTML);
ajax.open('POST', document.location+"/"+line_no);
ajax.send(formdata);
}
function update_line(event, form) {
if (event.target.status!=200) {
console.log("error");
console.log(event.target);
}
let entry = form.closest('.entry-log');
console.log(entry);
let template = document.createElement("template");
template.innerHTML = event.target.response.trim();
var new_entry = template.content.firstChild;
new_entry = new_entry.querySelector('.entry-log');
let error_lines = new_entry.getElementsByClassName("error");
if (error_lines.length>0) {
let lineno = new_entry.querySelector('.lineno');
lineno.classList.add('button');
lineno.addEventListener("click", edit_line);
console.log(lineno);
}
entry.replaceWith(new_entry);
}
function edit_line(event) {
let entry_log = event.target.parentElement.parentElement;
console.log(entry_log);
get_line_form(entry_log);
};
function initialize_form() {
......
<form class="input-form">
{% for field_name in headers %}{% if field_name %}
<div class="input-group"><label for="{{ field_name }}">{{ field_name }}</label><input type="text" name="{{ field_name }}" value="{{ values[loop.index0] }}"/></div>
{% endif %}{% endfor %}
<button type="button" name="action" value="update">update line</button>
</form>
\ No newline at end of file
......@@ -7,6 +7,7 @@ from multiprocessing import Process
import pyramid.httpexceptions as exc
from pyramid.request import Request
from pyramid.response import Response
from pyramid.renderers import render_to_response
from pyramid.view import view_config
from pyramid.threadlocal import get_current_registry
from pyramid_mailer.mailer import Mailer
......@@ -189,7 +190,8 @@ def moderate_submission(request):
if submission.processed:
entries = []
for e in submission.star_info_entries:
sie = session.query(StarInfoEntry).filter_by(submission_id=submission.id).order_by(StarInfoEntry.line_number).all()
for e in sie:
entry = {
'srcdata': e.line_data
}
......@@ -205,7 +207,6 @@ def moderate_submission(request):
entry['unique'] = True
problem_fields = []
for v in entry.values():
# print(v)
if type(v) is dict:
for k in v.keys():
if k not in problem_fields:
......@@ -216,3 +217,86 @@ def moderate_submission(request):
context['entries'] = entries
return context
@view_config(route_name='get_line_form')
def get_line_form(request):
"""
This view generates the form, and updates the data for one line
"""
token = request.matchdict["token"]
line = request.matchdict["line"]
session = request.dbsession
submission = session.query(Submission).filter_by(upload_ticket=token).first()
if not submission:
raise exc.HTTPForbidden()
line = session.query(StarInfoEntry).filter_by(submission_id=submission.id, line_number=line).first()
if not line:
raise exc.HTTPNotFound()
print(token, line)
print(submission.headers)
context = {
}
headers = json.loads(submission.headers)
if request.method == 'GET':
renderer = '../templates/line_form.jinja2'
context['headers'] = headers
context['values'] = json.loads(line.line_data)
print(context)
if request.method == 'POST':
renderer = '../templates/submit_errors.jinja2'
print(request.matchdict)
line_data = []
for k in headers:
value = ''
if k:
values = request.POST.getall(k)
if len(values)==1:
value = values[0]
line_data.append(value)
line.line_data = json.dumps(line_data)
print(line_data)
si = line.star_info
si.session = session
si.warnings = None
si.errors = None
si.problem_fields = None
si.parse(headers, line_data)
print(si)
si_msg = si.all_messages()
if si_msg.get('errors', None) is not None:
errors_present = True
line.error_data = json.dumps(si_msg)
if si_msg:
print('--------------------------------------------------')
print(si_msg)
print('--------------------------------------------------')
entries = []
entry = {
'srcdata': line.line_data,
'unique': True
}
entry.update(si_msg)
problem_fields = []
for v in entry.values():
if type(v) is dict:
for k in v.keys():
if k not in problem_fields:
problem_fields.append(k)
problem_fields.sort()
entry['problem_fields'] = problem_fields
entries.append({'line': line.line_number-1, 'entry': entry})
context['entries'] = entries
print(context)
# should test if there are any lines with errors. if not, do that
# send_email_to_moderator(self.settings, self.moderate_url, self.sub)
return render_to_response(renderer, context)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment