Coverage for portality/view/publisher.py: 41%

308 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 13:43 +0000

1from flask import Blueprint, request, make_response 

2from flask import render_template, abort, redirect, url_for, flash 

3from flask_login import current_user, login_required 

4 

5from portality.app_email import EmailException 

6from portality import models, constants 

7from portality.bll.exceptions import AuthoriseException, ArticleMergeConflict, DuplicateArticleException, \ 

8 ArticleNotAcceptable, NoSuchObjectException 

9from portality.decorators import ssl_required, restrict_to_role, write_required 

10from portality.dao import ESMappingMissingError 

11from portality.forms.application_forms import ApplicationFormFactory 

12from portality.tasks.ingestarticles import IngestArticlesBackgroundTask, BackgroundException 

13from portality.tasks.preservation import * 

14from portality.ui.messages import Messages 

15from portality.ui import templates 

16from portality import lock 

17from portality.models import DraftApplication 

18from portality.lcc import lcc_jstree 

19from portality.forms.article_forms import ArticleFormFactory 

20from portality.store import StoreFactory 

21 

22from huey.exceptions import TaskException 

23 

24import uuid 

25 

26from portality.view.view_helper import exparam_editing_user 

27 

28blueprint = Blueprint('publisher', __name__) 

29 

30# restrict everything in admin to logged in users with the "publisher" role 

31@blueprint.before_request 

32def restrict(): 

33 return restrict_to_role('publisher') 

34 

35@blueprint.route("/") 

36@login_required 

37@ssl_required 

38def index(): 

39 return render_template(templates.PUBLISHER_DRAFTS) 

40 

41 

42@blueprint.route("/journal") 

43@login_required 

44@ssl_required 

45def journals(): 

46 return render_template(templates.PUBLISHER_JOURNAL_SEARCH, lcc_tree=lcc_jstree) 

47 

48 

49@blueprint.route("/application/<application_id>/delete", methods=["GET"]) 

50@write_required() 

51def delete_application(application_id): 

52 # FIXME: this should be a POST or DELETE request 

53 # if this is a draft application, we can just remove it 

54 draft_application = DraftApplication.pull(application_id) 

55 if draft_application is not None: 

56 draft_application.delete() 

57 return redirect(url_for("publisher.deleted_thanks")) 

58 

59 # otherwise delegate to the application service to sort this out 

60 appService = DOAJ.applicationService() 

61 try: 

62 appService.delete_application(application_id, current_user._get_current_object()) 

63 except NoSuchObjectException: 

64 abort(404) 

65 

66 return redirect(url_for("publisher.deleted_thanks")) 

67 

68 

69@blueprint.route("/application/deleted") 

70def deleted_thanks(): 

71 return render_template(templates.PUBLISHER_APPLICATION_DELETED) 

72 

73 

74@blueprint.route("/update_request/<journal_id>", methods=["GET", "POST", "DELETE"]) 

75@login_required 

76@ssl_required 

77@write_required() 

78def update_request(journal_id): 

79 # DOAJ BLL for this request 

80 journalService = DOAJ.journalService() 

81 applicationService = DOAJ.applicationService() 

82 

83 # if this is a delete request, deal with it first and separately from the below logic 

84 if request.method == "DELETE": 

85 journal, _ = journalService.journal(journal_id) 

86 application_id = journal.current_application 

87 if application_id is not None: 

88 applicationService.delete_application(application_id, current_user._get_current_object()) 

89 else: 

90 abort(404) 

91 return "" 

92 

93 # load the application either directly or by crosswalking the journal object 

94 application = None 

95 jlock = None 

96 alock = None 

97 try: 

98 application, jlock, alock = applicationService.update_request_for_journal(journal_id, account=current_user._get_current_object()) 

99 except AuthoriseException as e: 

100 if e.reason == AuthoriseException.WRONG_STATUS: 

101 journal, _ = journalService.journal(journal_id) 

102 return render_template(templates.PUBLISHER_APPLICATION_ALREADY_SUBMITTED, journal=journal) 

103 elif e.reason == AuthoriseException.NOT_OWNER: 

104 flash(Messages.JOURNAL__YOU_ARE_NOT_OWNER, "info") 

105 return redirect(url_for("publisher.journals")) 

106 else: 

107 abort(404) 

108 except lock.Locked as e: 

109 journal, _ = journalService.journal(journal_id) 

110 return render_template(templates.PUBLISHER_LOCKED, journal=journal, lock=e.lock) 

111 

112 # if we didn't find an application or journal, 404 the user 

113 if application is None: 

114 if jlock is not None: jlock.delete() 

115 if alock is not None: alock.delete() 

116 abort(404) 

117 

118 # if we have a live application and cancel was hit, then cancel the operation and redirect 

119 # first determine if this is a cancel request on the form 

120 cancelled = request.values.get("cancel") 

121 if cancelled is not None: 

122 if jlock is not None: jlock.delete() 

123 if alock is not None: alock.delete() 

124 return redirect(url_for("publisher.updates_in_progress")) 

125 

126 fc = ApplicationFormFactory.context("update_request", extra_param=exparam_editing_user()) 

127 

128 # if we are requesting the page with a GET, we just want to show the form 

129 if request.method == "GET": 

130 fc.processor(source=application) 

131 return fc.render_template(obj=application) 

132 

133 # if we are requesting the page with a POST, we need to accept the data and handle it 

134 elif request.method == "POST": 

135 processor = fc.processor(formdata=request.form, source=application) 

136 if processor.validate(): 

137 try: 

138 processor.finalise() 

139 Messages.flash(Messages.PUBLISHER_APPLICATION_UPDATE_SUBMITTED_FLASH) 

140 for a in processor.alert: 

141 Messages.flash_with_url(a, "success") 

142 return redirect(url_for("publisher.updates_in_progress")) 

143 except Exception as e: 

144 Messages.flash(str(e)) 

145 return redirect(url_for("publisher.update_request", journal_id=journal_id, _anchor='cannot_edit')) 

146 finally: 

147 if jlock is not None: jlock.delete() 

148 if alock is not None: alock.delete() 

149 else: 

150 return fc.render_template(obj=application) 

151 

152 

153@blueprint.route("/view_application/<application_id>", methods=["GET"]) 

154@login_required 

155@ssl_required 

156def application_readonly(application_id): 

157 # DOAJ BLL for this request 

158 applicationService = DOAJ.applicationService() 

159 authService = DOAJ.authorisationService() 

160 

161 application, _ = applicationService.application(application_id) 

162 try: 

163 authService.can_view_application(current_user._get_current_object(), application) 

164 except AuthoriseException as e: 

165 abort(404) 

166 

167 fc = ApplicationFormFactory.context("application_read_only") 

168 fc.processor(source=application) 

169 # fc = formcontext.ApplicationFormFactory.get_form_context(role="update_request_readonly", source=application) 

170 

171 return fc.render_template(obj=application) 

172 

173 

174@blueprint.route("/view_update_request/<application_id>", methods=["GET", "POST"]) 

175@login_required 

176@ssl_required 

177def update_request_readonly(application_id): 

178 return redirect(url_for("publisher.application_readonly", application_id=application_id)) 

179 

180 

181@blueprint.route('/progress') 

182@login_required 

183@ssl_required 

184def updates_in_progress(): 

185 return render_template(templates.PUBLISHER_UPDATE_REQUESTS) 

186 

187 

188@blueprint.route("/uploadfile", methods=["GET", "POST"]) 

189@login_required 

190@ssl_required 

191@write_required() 

192def upload_file(): 

193 """ 

194 ~~UploadMetadata:Feature->UploadMetadata:Page~~ 

195 ~~->Crossref442:Feature~~ 

196 ~~->Crossref531:Feature~~ 

197 """ 

198 

199 # all responses involve getting the previous uploads 

200 previous = models.FileUpload.by_owner(current_user.id) 

201 

202 if request.method == "GET": 

203 schema = request.cookies.get("schema") 

204 if schema is None: 

205 schema = "" 

206 return render_template(templates.PUBLISHER_XML_UPLOAD, previous=previous, schema=schema, error=False) 

207 

208 # otherwise we are dealing with a POST - file upload or supply of url 

209 f = request.files.get("file") 

210 schema = request.values.get("schema") 

211 if not schema: 

212 abort(400) 

213 url = request.values.get("upload-xml-link") 

214 resp = make_response(redirect(url_for("publisher.upload_file"))) 

215 resp.set_cookie("schema", schema) 

216 

217 # file upload takes precedence over URL, in case the user has given us both 

218 if f is not None and f.filename != "" and url is not None and url != "": 

219 flash("You provided a file and a URL - the URL has been ignored") 

220 

221 try: 

222 job = IngestArticlesBackgroundTask.prepare(current_user.id, upload_file=f, schema=schema, url=url, previous=previous) 

223 IngestArticlesBackgroundTask.submit(job) 

224 except TaskException as e: 

225 flash(Messages.PUBLISHER_UPLOAD_ERROR.format(error_str=str(e))) 

226 app.logger.exception('File upload error. ' + str(e)) 

227 return resp 

228 except BackgroundException as e: 

229 if str(e) == Messages.NO_FILE_UPLOAD_ID: 

230 schema = request.cookies.get("schema") 

231 if schema is None: 

232 schema = "" 

233 return render_template(templates.PUBLISHER_XML_UPLOAD, previous=previous, schema=schema, error=True) 

234 

235 flash(Messages.PUBLISHER_UPLOAD_ERROR.format(error_str=str(e))) 

236 app.logger.exception('File upload error. ' + str(e)) 

237 return resp 

238 

239 if f is not None and f.filename != "": 

240 flash("File uploaded and waiting to be processed. Check back here for updates.", "success") 

241 return resp 

242 

243 if url is not None and url != "": 

244 flash("File successfully received - it will be processed shortly", "success") 

245 return resp 

246 

247 flash("No file or URL provided", "error") 

248 return resp 

249 

250 

251@blueprint.route("/preservation", methods=["GET", "POST"]) 

252@login_required 

253@ssl_required 

254@write_required() 

255def preservation(): 

256 """Upload articles on Internet Servers for archiving. 

257 This feature is available for the users who has 'preservation' role. 

258 """ 

259 

260 if app.config.get('PRESERVATION_PAGE_UNDER_MAINTENANCE', False): 

261 return render_template(templates.PUBLISHER_PRESERVATION_READONLY) 

262 

263 previous = [] 

264 try: 

265 previous = models.PreservationState.by_owner(current_user.id) 

266 # handle exception if there are no records available 

267 except ESMappingMissingError: 

268 pass 

269 

270 if request.method == "GET": 

271 return render_template(templates.PUBLISHER_PRESERVATION, previous=previous) 

272 

273 if request.method == "POST": 

274 

275 f = request.files.get("file") 

276 

277 resp = make_response(redirect(url_for("publisher.preservation"))) 

278 

279 # create model object to store status details 

280 preservation_model = models.PreservationState() 

281 preservation_model.set_id() 

282 

283 

284 previous.insert(0, preservation_model) 

285 

286 app.logger.debug(f"Preservation model created with id {preservation_model.id}") 

287 

288 if f is None or f.filename == "": 

289 error_str = Messages.PRESERVATION_NO_FILE 

290 flash(error_str, "error") 

291 preservation_model.initiated(current_user.id, "none") 

292 preservation_model.failed(error_str) 

293 preservation_model.save() 

294 return resp 

295 

296 app.logger.info(f"Preservation file {f.filename}") 

297 

298 preservation_model.initiated(current_user.id, f.filename) 

299 preservation_model.validated() 

300 preservation_model.save() 

301 

302 # check if collection has been assigned for the user 

303 # collection must be in the format {"user_id1",["collection_name1","collection_id1"], 

304 # "user_id2",["collection_name2","collection_id2"]} 

305 collection_dict = app.config.get("PRESERVATION_COLLECTION", {}) 

306 collection_available = True if collection_dict else False 

307 if collection_dict and not current_user.id in collection_dict: 

308 collection_available = False 

309 elif collection_dict: 

310 params = collection_dict[current_user.id] 

311 if not len(params) == 2: 

312 collection_available = False 

313 

314 if not collection_available: 

315 flash( 

316 "Cannot process upload - you do not have Collection details associated with your user ID. Please contact the DOAJ team.", 

317 "error") 

318 preservation_model.failed(FailedReasons.collection_not_available) 

319 preservation_model.save() 

320 

321 else: 

322 try: 

323 job = PreservationBackgroundTask.prepare(current_user.id, upload_file=f) 

324 PreservationBackgroundTask.set_param(job.params, "model_id", preservation_model.id) 

325 PreservationBackgroundTask.submit(job) 

326 

327 flash("File uploaded and waiting to be processed.", "success") 

328 

329 except EmailException: 

330 app.logger.exception('Error sending email' ) 

331 except (PreservationStorageException, PreservationException, Exception) as exp: 

332 try: 

333 uid = str(uuid.uuid1()) 

334 flash("An error has occurred and your preservation upload may not have succeeded. Please report the issue with the ID " + uid) 

335 preservation_model.failed(str(exp) + " Issue id : " + uid) 

336 preservation_model.save() 

337 app.logger.exception('Preservation upload error. ' + uid) 

338 if job: 

339 background_task = PreservationBackgroundTask(job) 

340 background_task.cleanup() 

341 except Exception as e: 

342 app.logger.exception('Unknown error.' + str(e)) 

343 return resp 

344 

345 

346@blueprint.route("/metadata", methods=["GET", "POST"]) 

347@login_required 

348@ssl_required 

349@write_required() 

350def metadata(): 

351 user = current_user._get_current_object() 

352 # if this is a get request, give the blank form - there is no edit feature 

353 if request.method == "GET": 

354 fc = ArticleFormFactory.get_from_context(user=user, role="publisher") 

355 return fc.render_template() 

356 

357 # if this is a post request, a form button has been hit and we need to do 

358 # a bunch of work 

359 elif request.method == "POST": 

360 

361 fc = ArticleFormFactory.get_from_context(role="publisher", user=user, 

362 form_data=request.form) 

363 # first we need to do any server-side form modifications which 

364 # the user might request by pressing the add/remove authors buttons 

365 

366 fc.modify_authors_if_required(request.values) 

367 

368 validated = False 

369 if fc.validate(): 

370 try: 

371 fc.finalise() 

372 validated = True 

373 except ArticleMergeConflict: 

374 Messages.flash(Messages.ARTICLE_METADATA_MERGE_CONFLICT) 

375 except DuplicateArticleException: 

376 Messages.flash(Messages.ARTICLE_METADATA_UPDATE_CONFLICT) 

377 except ArticleNotAcceptable as e: 

378 Messages.flash_with_param(e.message, "error") 

379 return fc.render_template(validated=validated) 

380 

381@blueprint.route("/journal-csv", methods=["GET"]) 

382@login_required 

383@ssl_required 

384@write_required() 

385def journal_csv(): 

386 if current_user.has_role(constants.ROLE_PUBLISHER_JOURNAL_CSV): 

387 return render_template(templates.PUBLISHER_CSV_UPLOAD) 

388 abort(403) 

389 

390@blueprint.route("/journal-csv/validate", methods=["POST"]) 

391@login_required 

392@ssl_required 

393@write_required() 

394def journal_csv_validate(): 

395 if not current_user.has_role(constants.ROLE_PUBLISHER_JOURNAL_CSV): 

396 abort(403) 

397 if "journal_csv" not in request.files: 

398 abort(400) 

399 file = request.files["journal_csv"] 

400 if not file.filename.endswith(".csv"): 

401 abort(400) 

402 

403 tmpStore = StoreFactory.tmp() 

404 container_id = app.config.get("JOURNAL_CSV_UPLOAD__TMP_CONTAINER", "publisher_csvs_validation") 

405 filename = current_user.id + "_" + dates.now_str() + ".csv" 

406 out = tmpStore.path(container_id, filename, create_container=True, must_exist=False) 

407 file.save(out) 

408 

409 try: 

410 report = DOAJ.applicationService().validate_update_csv(out, current_user) 

411 finally: 

412 tmpStore.delete_file(container_id, filename) 

413 

414 resp = make_response(report.json()) 

415 resp.mimetype = "application/json" 

416 return resp 

417 

418 

419@blueprint.route("/help") 

420@login_required 

421@ssl_required 

422def help(): 

423 return render_template(templates.PUBLISHER_XML_HELP) 

424 

425 

426def _validate_authors(form, require=1): 

427 counted = 0 

428 for entry in form.authors.entries: 

429 name = entry.data.get("name") 

430 if name is not None and name != "": 

431 counted += 1 

432 return counted >= require