Skip to content

Commit

Permalink
implement withdraw all
Browse files Browse the repository at this point in the history
  • Loading branch information
philipkcl committed Aug 18, 2023
1 parent eba5386 commit db7fab9
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 52 deletions.
61 changes: 36 additions & 25 deletions portality/bll/services/journal.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import csv
import logging
import random
import re
import string
from datetime import datetime

from portality.lib.argvalidate import argvalidate
from portality.lib import dates
from portality import lock
from portality import models, constants
from portality.bll import exceptions
from portality.core import app
from portality import lock
from portality.bll.doaj import DOAJ
from portality.core import app
from portality.crosswalks.journal_questions import Journal2QuestionXwalk
from portality.lib import dates
from portality.lib.argvalidate import argvalidate
from portality.lib.dates import FMT_DATETIME_SHORT
from portality.store import StoreFactory, prune_container
from portality.crosswalks.journal_questions import Journal2QuestionXwalk

from datetime import datetime
import re, csv, random, string


class JournalService(object):
"""
~~Journal:Service~~
"""

def journal_2_application(self, journal, account=None, keep_editors=False):
"""
Function to convert a given journal into an application object.
Expand All @@ -37,8 +40,8 @@ def journal_2_application(self, journal, account=None, keep_editors=False):

# first validate the incoming arguments to ensure that we've got the right thing
argvalidate("journal_2_application", [
{"arg": journal, "instance" : models.Journal, "allow_none" : False, "arg_name" : "journal"},
{"arg" : account, "instance" : models.Account, "arg_name" : "account"}
{"arg": journal, "instance": models.Journal, "allow_none": False, "arg_name": "journal"},
{"arg": account, "instance": models.Account, "arg_name": "account"}
], exceptions.ArgumentException)

if app.logger.isEnabledFor(logging.DEBUG): app.logger.debug("Entering journal_2_application")
Expand All @@ -49,9 +52,10 @@ def journal_2_application(self, journal, account=None, keep_editors=False):
# if an account is specified, check that it is allowed to perform this action
if account is not None:
try:
authService.can_create_update_request(account, journal) # throws exception if not allowed
authService.can_create_update_request(account, journal) # throws exception if not allowed
except exceptions.AuthoriseException as e:
msg = "Account {x} is not permitted to create an update request on journal {y}".format(x=account.id, y=journal.id)
msg = "Account {x} is not permitted to create an update request on journal {y}".format(x=account.id,
y=journal.id)
app.logger.info(msg)
e.args += (msg,)
raise
Expand All @@ -60,7 +64,7 @@ def journal_2_application(self, journal, account=None, keep_editors=False):
bj = journal.bibjson()
notes = journal.notes

application = models.Suggestion() # ~~-> Application:Model~~
application = models.Suggestion() # ~~-> Application:Model~~
application.set_application_status(constants.APPLICATION_STATUS_UPDATE_REQUEST)
application.set_current_journal(journal.id)
if keep_editors is True:
Expand All @@ -78,7 +82,8 @@ def journal_2_application(self, journal, account=None, keep_editors=False):
application.set_bibjson(bj)
application.date_applied = dates.now_str()

if app.logger.isEnabledFor(logging.DEBUG): app.logger.debug("Completed journal_2_application; return application object")
if app.logger.isEnabledFor(logging.DEBUG): app.logger.debug(
"Completed journal_2_application; return application object")
return application

def journal(self, journal_id, lock_journal=False, lock_account=None, lock_timeout=None):
Expand All @@ -95,10 +100,10 @@ def journal(self, journal_id, lock_journal=False, lock_account=None, lock_timeou
"""
# first validate the incoming arguments to ensure that we've got the right thing
argvalidate("journal", [
{"arg": journal_id, "allow_none" : False, "arg_name" : "journal_id"},
{"arg": lock_journal, "instance" : bool, "allow_none" : False, "arg_name" : "lock_journal"},
{"arg": lock_account, "instance" : models.Account, "allow_none" : True, "arg_name" : "lock_account"},
{"arg": lock_timeout, "instance" : int, "allow_none" : True, "arg_name" : "lock_timeout"}
{"arg": journal_id, "allow_none": False, "arg_name": "journal_id"},
{"arg": lock_journal, "instance": bool, "allow_none": False, "arg_name": "lock_journal"},
{"arg": lock_account, "instance": models.Account, "allow_none": True, "arg_name": "lock_account"},
{"arg": lock_timeout, "instance": int, "allow_none": True, "arg_name": "lock_timeout"}
], exceptions.ArgumentException)

# retrieve the journal
Expand All @@ -111,7 +116,8 @@ def journal(self, journal_id, lock_journal=False, lock_account=None, lock_timeou
# ~~->Lock:Feature~~
the_lock = lock.lock(constants.LOCK_JOURNAL, journal_id, lock_account.id, lock_timeout)
else:
raise exceptions.ArgumentException("If you specify lock_journal on journal retrieval, you must also provide lock_account")
raise exceptions.ArgumentException(
"If you specify lock_journal on journal retrieval, you must also provide lock_account")

return journal, the_lock

Expand All @@ -127,7 +133,7 @@ def csv(self, prune=True):
"""
# first validate the incoming arguments to ensure that we've got the right thing
argvalidate("csv", [
{"arg": prune, "allow_none" : False, "arg_name" : "prune"}
{"arg": prune, "allow_none": False, "arg_name": "prune"}
], exceptions.ArgumentException)

# ~~->FileStoreTemp:Feature~~
Expand All @@ -145,16 +151,20 @@ def csv(self, prune=True):
mainStore.store(container_id, filename, source_path=out)
url = mainStore.url(container_id, filename)
finally:
tmpStore.delete_file(container_id, filename) # don't delete the container, just in case someone else is writing to it
tmpStore.delete_file(container_id,
filename) # don't delete the container, just in case someone else is writing to it

action_register = []
if prune:
def sort(filelist):
rx = "journalcsv__doaj_(.+?)_utf8.csv"
return sorted(filelist, key=lambda x: datetime.strptime(re.match(rx, x).groups(1)[0], FMT_DATETIME_SHORT), reverse=True)
return sorted(filelist,
key=lambda x: datetime.strptime(re.match(rx, x).groups(1)[0], FMT_DATETIME_SHORT),
reverse=True)

def _filter(f_name):
return f_name.startswith("journalcsv__")

action_register = prune_container(mainStore, container_id, sort, filter=_filter, keep=2)

# update the ES record to point to the new file
Expand Down Expand Up @@ -183,7 +193,9 @@ def usernames(j):
if o in unmap:
sub = unmap[o]
else:
sub = "".join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for i in range(account_sub_length))
sub = "".join(
random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for i in
range(account_sub_length))
unmap[o] = sub
return [("Owner", sub)]
else:
Expand Down Expand Up @@ -242,7 +254,7 @@ def _get_article_kvs(journal):

# ~~!JournalCSV:Feature->Journal:Model~~
cols = {}
for j in models.Journal.all_in_doaj(page_size=1000): #Fixme: limited by ES, this may not be sufficient
for j in models.Journal.all_in_doaj(page_size=1000): # Fixme: limited by ES, this may not be sufficient
bj = j.bibjson()
issn = bj.get_one_identifier(idtype=bj.P_ISSN)
if issn is None:
Expand Down Expand Up @@ -275,4 +287,3 @@ def _get_article_kvs(journal):
csvwriter.writerow(qs)
vs = [v for _, v in cols[i]]
csvwriter.writerow(vs)

2 changes: 0 additions & 2 deletions portality/models/v2/journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ def get_future_continuations(self):
irb = self.bibjson().is_replaced_by
q = ContinuationQuery(irb)

future = []
journals = self.q2obj(q=q.query())
subjournals = []
for j in journals:
Expand All @@ -776,7 +775,6 @@ def get_past_continuations(self):
replaces = self.bibjson().replaces
q = ContinuationQuery(replaces)

past = []
journals = self.q2obj(q=q.query())
subjournals = []
for j in journals:
Expand Down
59 changes: 53 additions & 6 deletions portality/templates/application_form/maned_journal.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,29 @@
{% block content scoped %}
{% include "application_form/editorial_form_body.html" %}

<div class="modal" id="modal-withdraw_reinstate" tabindex="-1" role="dialog" aria-labelledby="modalLabel-withdraw_reinstate">


{% macro show_cont_list(cont_list, cont_name) %}
{% if cont_list %}
<span>{{ cont_name }}</span>
<ul>
{% for issn, jid in cont_list %}
<li>
{% if jid %}
<a href="{{ url_for("admin.journal_page", journal_id=jid ) }}">
{{ issn }} ( {{ jid }} )
</a>
{% else %}
{{ issn }} ( Related journal not found )
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}

<div class="modal" id="modal-withdraw_reinstate" tabindex="-1" role="dialog"
aria-labelledby="modalLabel-withdraw_reinstate">
<div class="modal__dialog" role="document">
{% set action_label = "Withdraw" if obj.is_in_doaj() else "Reinstate" %}
{% set message = "Are you sure you want to " + action_label + " " + obj.id %}
Expand All @@ -28,17 +50,42 @@
{% set message = "There is currently a background job " + action_label + " " + obj.id + ". You can view the progress of this job <a href='" + job_url + "' target='_blank'>here</a> (opens in new tab). You will get an email when your request has been processed; this could take anything from a few minutes to a few hours." %}
{% endif %}
<h3 class="modal__title" id="modalLabel-withdraw_reinstate">
{{action_label}}
{{ action_label }}
</h3>
<p>{% autoescape off %}{{ message }}{% endautoescape %}</p>

{% if not job %}
{% set action = url_for("admin.journal_activate", journal_id=obj.id) %}
{% if obj.is_in_doaj() %}
{% set action = url_for("admin.journal_deactivate", journal_id=obj.id) %}
{% endif %}

{# withdraw or reinstate only this journal #}
{% set url_name = "admin.journal_deactivate" if obj.is_in_doaj() else "admin.journal_activate" %}
{% set action = url_for(url_name, journal_id=obj.id) %}
<form method="post" action="{{ action }}" id="journal_witdraw_reinstate">
<button class="button button--primary" type="submit" id="submit_quick_reject">{{ action_label }}</button>
</form>


{# withdraw or reinstate all continuations #}
{% set j_replaces = obj.bibjson().replaces %}
{% set j_is_replaced_by = obj.bibjson().is_replaced_by %}

{% if obj.is_in_doaj() and (j_replaces or j_is_replaced_by) %}

<p>
{% autoescape off %}
Following are continuations journal, should we {{ action_label }} all ?
{% endautoescape %}
</p>

{{ show_cont_list(past_cont_list, "Older") }}
{{ show_cont_list(future_cont_list, "Newer") }}

<form method="post" action="{{ action }}" id="journal_witdraw_reinstate">
<button class="button button--primary" type="submit" id="submit_quick_reject">
<input type="hidden" name="cont_all" value="1">
{{ action_label }} All
</button>
</form>
{% endif %}
{% endif %}
<button type="button" data-dismiss="modal" class="modal__close">Close</button>
</div>
Expand Down
Loading

0 comments on commit db7fab9

Please sign in to comment.