You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/handler/import.js

45 lines
1.4 KiB
JavaScript

const problem = require('../model/problem');
const { Route, Handler } = require('../service/server');
const { PERM_CREATE_PROBLEM } = require('../permission');
const { ValidationError } = require('../error');
class ProblemImportHandler extends Handler {
async prepare() {
this.checkPerm(PERM_CREATE_PROBLEM);
}
async get() {
this.response.template = 'problem_import.html';
this.response.body = {
path: [
['Hydro', '/'],
['problem_main', '/p'],
['problem_import', null],
],
};
}
async post({
domainId, url, pid, hidden, remoteType,
}) {
if (typeof global.Hydro.lib[`import.${remoteType}`] !== 'function') {
5 years ago
throw new ValidationError('remoteType');
}
const [pdoc, testdata] = await global.Hydro.lib[`import.${remoteType}`](url, this);
if (pid) pdoc.pid = pid;
if (hidden) pdoc.hidden = true;
const docId = await problem.add(domainId, pdoc.title, pdoc.content, this.user._id, pdoc);
await problem.setTestdata(docId, testdata);
this.response.body = { pid: pid || docId };
this.response.redirect = `/p/${pid || docId}/settings`;
}
}
async function apply() {
Route('/problem/import', module.exports.ProblemImportHandler);
}
5 years ago
global.Hydro.handler.import = module.exports = {
ProblemImportHandler, apply,
};