def delete_user(username): prefix = "Delete existing user: "******"no such user:"******"deleted")
def create_user(username, email, password, roles): if password is None: password = input_password(username) prefix = "Modified existing user: "******"Created new user: " acc = Account(id=username, email=email) acc.set_email(email) acc.set_role(roles) acc.set_password(password) acc.save() print(prefix + username)
def send_editor_email(journal): editor = Account.pull(journal.editor) eg = EditorGroup.pull_by_key("name", journal.editor_group) url_root = app.config.get("BASE_URL") to = [editor.email] fro = app.config.get('SYSTEM_EMAIL_FROM', '*****@*****.**') subject = app.config.get("SERVICE_NAME","") + " - new journal assigned to you" app_email.send_mail(to=to, fro=fro, subject=subject, template_name="email/journal_assigned_editor.txt", editor=editor.id.encode('utf-8', 'replace'), journal_name=journal.bibjson().title.encode('utf-8', 'replace'), group_name=eg.name.encode("utf-8", "replace"), url_root=url_root )
def send_editor_email(journal): editor = Account.pull(journal.editor) eg = EditorGroup.pull_by_key("name", journal.editor_group) url_root = app.config.get("BASE_URL") to = [editor.email] fro = app.config.get('SYSTEM_EMAIL_FROM', '*****@*****.**') subject = app.config.get("SERVICE_NAME", "") + " - new journal assigned to you" app_email.send_mail(to=to, fro=fro, subject=subject, template_name="email/journal_assigned_editor.txt", editor=editor.id.encode('utf-8', 'replace'), journal_name=journal.bibjson().title.encode( 'utf-8', 'replace'), group_name=eg.name.encode("utf-8", "replace"), url_root=url_root)
def journals_applications_provenance(outfile_applications, outfile_accounts, outfile_reapps, conn): with codecs.open(outfile_applications, "wb", "utf-8") as f, codecs.open( outfile_accounts, "wb", "utf-8") as g, codecs.open(outfile_reapps, "wb", "utf-8") as h: out_applications = csv.writer(f) out_applications.writerow([ "Journal ID", "Journal Created", "Journal Reapplied", "Application ID", "Application Last Updated", "Application Status", "Published Diff", "Latest Edit Recorded", "Latest Accepted Recorded" ]) out_accounts = csv.writer(g) out_accounts.writerow([ "Journal ID", "Journal Created", "Journal Reapplied", "In DOAJ", "Missing Account ID" ]) out_reapps = csv.writer(h) out_reapps.writerow([ "Journal ID", "Journal Created", "Journal Reapplied", "Application ID", "Application Created", "Application Last Updated", "Application Last Manual Update", "Application Status", "Published Diff" ]) counter = 0 for result in esprit.tasks.scroll(conn, "journal", keepalive="45m"): counter += 1 journal = Journal(**result) print counter, journal.id # first figure out if there is a broken related application issns = journal.bibjson().issns() applications = Suggestion.find_by_issn(issns) latest = None for application in applications: if latest is None: latest = application if application.last_updated_timestamp > latest.last_updated_timestamp: latest = application if latest is None: continue jcreated = journal.created_timestamp reapp = journal.last_update_request print counter, journal.id, reapp if reapp is not None: jcreated = datetime.strptime(reapp, "%Y-%m-%dT%H:%M:%SZ") jcreated = adjust_timestamp(jcreated, JOURNAL_TIMEZONE_CUTOFF) app_lustamp = adjust_timestamp(latest.last_updated_timestamp, APP_TIMEZONE_CUTOFF) # app_man_lustamp = latest.last_manual_update_timestamp # no need to adjust this one app_man_lustamp = adjust_timestamp( latest.last_manual_update_timestamp, APP_TIMEZONE_CUTOFF) td = jcreated - app_lustamp mtd = jcreated - app_man_lustamp diff = td.total_seconds() mdiff = mtd.total_seconds() # was the journal created after the application by greater than the threshold? if diff > THRESHOLD: last_edit = "" last_accept = "" edit_query = deepcopy(PROV_QUERY) edit_query["query"]["bool"]["must"][0]["term"][ "resource_id.exact"] = latest.id edit_query["query"]["bool"]["must"][1]["term"][ "action.exact"] = "edit" provs = Provenance.q2obj(q=edit_query) if len(provs) > 0: last_edit = provs[0].last_updated accept_query = deepcopy(PROV_QUERY) accept_query["query"]["bool"]["must"][0]["term"][ "resource_id.exact"] = latest.id accept_query["query"]["bool"]["must"][1]["term"][ "action.exact"] = "status:accepted" provs = Provenance.q2obj(q=accept_query) if len(provs) > 0: last_accept = provs[0].last_updated out_applications.writerow([ journal.id, journal.created_date, journal.last_update_request, latest.id, latest.last_updated, latest.application_status, diff, last_edit, last_accept ]) # was the journal (in doaj) created before the application by greater than the threshold, and is it in a state other than rejected if mdiff < -1 * THRESHOLD and latest.application_status != constants.APPLICATION_STATUS_REJECTED and journal.is_in_doaj( ): out_reapps.writerow([ journal.id, journal.created_date, journal.last_update_request, latest.id, latest.created_date, latest.last_updated, latest.last_manual_update, latest.application_status, mdiff ]) # now figure out if the account is missing owner = journal.owner if owner is None: out_accounts.writerow([ journal.id, journal.created_date, journal.last_update_request, str(journal.is_in_doaj()), "NO OWNER" ]) else: acc = Account.pull(owner) if acc is None: out_accounts.writerow([ journal.id, journal.created_date, journal.last_update_request, str(journal.is_in_doaj()), owner ]) print "processed", counter, "journals"
def get_associate_accounts(self): accs = [] for a in self.associates: acc = Account.pull(a) accs.append(acc) return accs
def get_editor_account(self): return Account.pull(self.editor)
def get_associate_accounts(self): accs = [] for a in self.associates: acc = Account.pull(a) accs.append(acc) return accs
def get_editor_account(self): return Account.pull(self.editor)
from portality.models import Account if __name__ == "__main__": import argparse, getpass parser = argparse.ArgumentParser() parser.add_argument("-e", "--email", help="email address of user to make admin") args = parser.parse_args() if not args.email: print "Please specify an email with the -e option" exit() email = args.email acc = Account.pull(email) if not acc: print "Account with email " + email + " does not exist" current_roles = acc.role if "admin" not in current_roles: acc.set_role(current_roles + ["admin"]) acc.save()
"--password", help="password for the new or existing user. If omitted, you will be prompted for one on the next line", ) parser.add_argument("-r", "--role", help="comma separated list of roles to be held by this account") args = parser.parse_args() if not args.username: print "Please specify a username with the -u option" exit() if not args.role: print "WARNING: no role specified, so this user won't be able to do anything" username = args.username email = args.email password = None roles = [r.strip() for r in args.role.split(",")] if args.role is not None else [] if args.password: password = args.password else: password = input_password() acc = Account.pull(username) if not acc: acc = Account({"id": username, "email": email}) acc.set_role(roles) acc.set_password(password) acc.save()
def create_account_on_suggestion_approval(suggestion, journal): o = Account.pull(suggestion.owner) if o: flash('Account {username} already exists, so simply associating the new journal with it.'.format(username=o.id), 'success') o.add_journal(journal.id) if not o.has_role('publisher'): o.add_role('publisher') o.save() return o suggestion_contact = util.listpop(suggestion.contacts()) if not suggestion_contact.get('email'): msg = ERROR_MSG_TEMPLATE.format(username=o.id, missing_thing='journal contact email in the application') app.logger.error(msg) flash(msg) return o send_info_to = suggestion_contact.get('email') o = Account.make_account( suggestion.owner, name=suggestion_contact.get('name'), email=send_info_to, roles=['publisher'], associated_journal_ids=[journal.id] ) o.save() url_root = request.url_root if url_root.endswith("/"): url_root = url_root[:-1] if not o.reset_token: msg = ERROR_MSG_TEMPLATE.format(username=o.id, missing_thing='reset token') app.logger.error(msg) flash(msg) return o reset_url = url_root + url_for('account.reset', reset_token=o.reset_token) forgot_pw_url = url_root + url_for('account.forgot') password_create_timeout_seconds = int(app.config.get("PASSWORD_CREATE_TIMEOUT", app.config.get('PASSWORD_RESET_TIMEOUT', 86400) * 14)) password_create_timeout_days = password_create_timeout_seconds / (60*60*24) to = [send_info_to] fro = app.config.get('SYSTEM_EMAIL_FROM', '*****@*****.**') subject = app.config.get("SERVICE_NAME","") + " - account created" try: if app.config.get("ENABLE_PUBLISHER_EMAIL", False): app_email.send_mail(to=to, fro=fro, subject=subject, template_name="email/account_created.txt", reset_url=reset_url, username=o.id, timeout_days=password_create_timeout_days, forgot_pw_url=forgot_pw_url ) flash('Sent email to ' + send_info_to + ' to tell them about the new account.', 'success') else: flash('Did not email to ' + send_info_to + ' to tell them about the new account, as publisher emailing is disabled.', 'error') if app.config.get('DEBUG',False): util.flash_with_url('Debug mode - url for create is <a href="{url}">{url}</a>'.format(url=reset_url)) except Exception as e: magic = str(uuid.uuid1()) util.flash_with_url('Hm, sending the account creation email didn\'t work. Please quote this magic number when reporting the issue: ' + magic + ' . Thank you!', 'error') if app.config.get('DEBUG',False): util.flash_with_url('Debug mode - url for create is <a href="{url}">{url}</a>'.format(url=reset_url)) app.logger.error(magic + "\n" + repr(e)) raise e flash('Account {username} created'.format(username=o.id), 'success') return o
"-r", "--role", help="comma separated list of roles to be held by this account") args = parser.parse_args() if not args.username: print "Please specify a username with the -u option" exit() if not args.role: print "WARNING: no role specified, so this user won't be able to do anything" username = args.username email = args.email password = None roles = [r.strip() for r in args.role.split(",")] if args.role is not None else [] if args.password: password = args.password else: password = input_password() acc = Account.pull(username) if not acc: acc = Account(id=username, email=email) acc.set_role(roles) acc.set_password(password) acc.save()
def journals_applications_provenance(outfile_applications, outfile_accounts, outfile_reapps, conn): with codecs.open(outfile_applications, "wb", "utf-8") as f, codecs.open(outfile_accounts, "wb", "utf-8") as g, codecs.open(outfile_reapps, "wb", "utf-8") as h: out_applications = csv.writer(f) out_applications.writerow(["Journal ID", "Journal Created", "Journal Reapplied", "Application ID", "Application Last Updated", "Application Status", "Published Diff", "Latest Edit Recorded", "Latest Accepted Recorded"]) out_accounts = csv.writer(g) out_accounts.writerow(["Journal ID", "Journal Created", "Journal Reapplied", "In DOAJ", "Missing Account ID"]) out_reapps = csv.writer(h) out_reapps.writerow(["Journal ID", "Journal Created", "Journal Reapplied", "Application ID", "Application Created", "Application Last Updated", "Application Last Manual Update", "Application Status", "Published Diff"]) counter = 0 for result in esprit.tasks.scroll(conn, "journal", keepalive="45m"): counter += 1 journal = Journal(**result) print counter, journal.id # first figure out if there is a broken related application issns = journal.bibjson().issns() applications = Suggestion.find_by_issn(issns) latest = None for application in applications: if latest is None: latest = application if application.last_updated_timestamp > latest.last_updated_timestamp: latest = application if latest is None: continue jcreated = journal.created_timestamp reapp = journal.last_update_request print counter, journal.id, reapp if reapp is not None: jcreated = datetime.strptime(reapp, "%Y-%m-%dT%H:%M:%SZ") jcreated = adjust_timestamp(jcreated, JOURNAL_TIMEZONE_CUTOFF) app_lustamp = adjust_timestamp(latest.last_updated_timestamp, APP_TIMEZONE_CUTOFF) # app_man_lustamp = latest.last_manual_update_timestamp # no need to adjust this one app_man_lustamp = adjust_timestamp(latest.last_manual_update_timestamp, APP_TIMEZONE_CUTOFF) td = jcreated - app_lustamp mtd = jcreated - app_man_lustamp diff = td.total_seconds() mdiff = mtd.total_seconds() # was the journal created after the application by greater than the threshold? if diff > THRESHOLD: last_edit = "" last_accept = "" edit_query = deepcopy(PROV_QUERY) edit_query["query"]["bool"]["must"][0]["term"]["resource_id.exact"] = latest.id edit_query["query"]["bool"]["must"][1]["term"]["action.exact"] = "edit" provs = Provenance.q2obj(q=edit_query) if len(provs) > 0: last_edit = provs[0].last_updated accept_query = deepcopy(PROV_QUERY) accept_query["query"]["bool"]["must"][0]["term"]["resource_id.exact"] = latest.id accept_query["query"]["bool"]["must"][1]["term"]["action.exact"] = "status:accepted" provs = Provenance.q2obj(q=accept_query) if len(provs) > 0: last_accept = provs[0].last_updated out_applications.writerow([journal.id, journal.created_date, journal.last_update_request, latest.id, latest.last_updated, latest.application_status, diff, last_edit, last_accept]) # was the journal (in doaj) created before the application by greater than the threshold, and is it in a state other than rejected if mdiff < -1 * THRESHOLD and latest.application_status != constants.APPLICATION_STATUS_REJECTED and journal.is_in_doaj(): out_reapps.writerow([journal.id, journal.created_date, journal.last_update_request, latest.id, latest.created_date, latest.last_updated, latest.last_manual_update, latest.application_status, mdiff]) # now figure out if the account is missing owner = journal.owner if owner is None: out_accounts.writerow([journal.id, journal.created_date, journal.last_update_request, str(journal.is_in_doaj()), "NO OWNER"]) else: acc = Account.pull(owner) if acc is None: out_accounts.writerow([journal.id, journal.created_date, journal.last_update_request, str(journal.is_in_doaj()), owner]) print "processed", counter, "journals"