def add_journal(journal_name, xml_config): """ Add a new journal to the DB. Also create the configuration file Parameters: journal_name - the name (used in URLs) of the new journal xml_config - the xml configuration of the journal (string) Returns: the id of the journal if successfully added -1 if could not be added because journal name already exists -2 if config could not be saved -3 if could not be added for other reasons -4 if database cache could not be added """ try: get_journal_id(journal_name) except InvenioWebJournalJournalIdNotFoundDBError: # Perfect, journal does not exist res = run_sql("INSERT INTO jrnJOURNAL (name) VALUES(%s)", (journal_name, )) # Also save xml_config config_dir = '%s/webjournal/%s/' % (CFG_ETCDIR, journal_name) try: if not os.path.exists(config_dir): os.makedirs(config_dir) xml_config_file = file(config_dir + journal_name + '-config.xml', 'w') xml_config_file.write(xml_config) xml_config_file.close() except Exception: res = -2 # And save some info in file in case database is down journal_info_path = get_journal_info_path(journal_name) journal_info_dir = os.path.dirname(journal_info_path) if not os.path.exists(journal_info_dir): try: os.makedirs(journal_info_dir) except Exception: if res <= 0: res = -4 journal_info_file = open(journal_info_path, 'w') cPickle.dump( { 'journal_id': res, 'journal_name': journal_name, 'current_issue': '01/2000' }, journal_info_file) return res return -1
def delete_journal_issue(issue, journal_name, ln=CFG_SITE_LANG): """ Deletes an issue from the DB. (Not currently used) """ journal_id = get_journal_id(journal_name, ln) run_sql("DELETE FROM jrnISSUE WHERE issue_number=%s \ AND id_jrnJOURNAL=%s",(issue, journal_id)) # update information in file (in case DB is down) journal_info_path = get_journal_info_path(journal_name) journal_info_file = open(journal_info_path, 'w') cPickle.dump({'journal_id': journal_id, 'journal_name': journal_name, 'current_issue': get_current_issue(ln, journal_name)}, journal_info_file)
def add_journal(journal_name, xml_config): """ Add a new journal to the DB. Also create the configuration file Parameters: journal_name - the name (used in URLs) of the new journal xml_config - the xml configuration of the journal (string) Returns: the id of the journal if successfully added -1 if could not be added because journal name already exists -2 if config could not be saved -3 if could not be added for other reasons -4 if database cache could not be added """ try: get_journal_id(journal_name) except InvenioWebJournalJournalIdNotFoundDBError: # Perfect, journal does not exist res = run_sql("INSERT INTO jrnJOURNAL (name) VALUES(%s)", (journal_name,)) # Also save xml_config config_dir = '%s/webjournal/%s/' % (CFG_ETCDIR, journal_name) try: if not os.path.exists(config_dir): os.makedirs(config_dir) xml_config_file = file(config_dir + journal_name + '-config.xml', 'w') xml_config_file.write(xml_config) xml_config_file.close() except Exception: res = -2 # And save some info in file in case database is down journal_info_path = get_journal_info_path(journal_name) journal_info_dir = os.path.dirname(journal_info_path) if not os.path.exists(journal_info_dir): try: os.makedirs(journal_info_dir) except Exception: if res <= 0: res = -4 journal_info_file = open(journal_info_path, 'w') cPickle.dump({'journal_id': res, 'journal_name': journal_name, 'current_issue':'01/2000'}, journal_info_file) return res return -1
def release_journal_issue(publish_issues, journal_name, ln=CFG_SITE_LANG): """ Releases a new issue. This sets the current issue in the database to 'publish_issues' for given 'journal_name' Parameters: journal_name - the journal for which we release a new issue publish_issues - the list of issues that will be considered as current (there can be several) ln - language """ journal_id = get_journal_id(journal_name, ln) if len(publish_issues) > 1: publish_issues.sort(compare_issues) low_bound = publish_issues[0] high_bound = publish_issues[-1] issue_display = '%s-%s/%s' % (low_bound.split("/")[0], high_bound.split("/")[0], high_bound.split("/")[1]) # remember convention: if we are going over a new year, take the higher else: issue_display = publish_issues[0] # produce the DB lines for publish_issue in publish_issues: move_drafts_articles_to_ready(journal_name, publish_issue) run_sql( "INSERT INTO jrnISSUE (id_jrnJOURNAL, issue_number, issue_display) \ VALUES(%s, %s, %s)", (journal_id, publish_issue, issue_display)) # set first issue to published release_journal_update(publish_issues[0], journal_name, ln) # update information in file (in case DB is down) journal_info_path = get_journal_info_path(journal_name) journal_info_file = open(journal_info_path, 'w') cPickle.dump( { 'journal_id': journal_id, 'journal_name': journal_name, 'current_issue': get_current_issue(ln, journal_name) }, journal_info_file)
def release_journal_issue(publish_issues, journal_name, ln=CFG_SITE_LANG): """ Releases a new issue. This sets the current issue in the database to 'publish_issues' for given 'journal_name' Parameters: journal_name - the journal for which we release a new issue publish_issues - the list of issues that will be considered as current (there can be several) ln - language """ journal_id = get_journal_id(journal_name, ln) if len(publish_issues) > 1: publish_issues.sort(compare_issues) low_bound = publish_issues[0] high_bound = publish_issues[-1] issue_display = '%s-%s/%s' % (low_bound.split("/")[0], high_bound.split("/")[0], high_bound.split("/")[1]) # remember convention: if we are going over a new year, take the higher else: issue_display = publish_issues[0] # produce the DB lines for publish_issue in publish_issues: move_drafts_articles_to_ready(journal_name, publish_issue) run_sql("INSERT INTO jrnISSUE (id_jrnJOURNAL, issue_number, issue_display) \ VALUES(%s, %s, %s)", (journal_id, publish_issue, issue_display)) # set first issue to published release_journal_update(publish_issues[0], journal_name, ln) # update information in file (in case DB is down) journal_info_path = get_journal_info_path(journal_name) journal_info_file = open(journal_info_path, 'w') cPickle.dump({'journal_id': journal_id, 'journal_name': journal_name, 'current_issue': get_current_issue(ln, journal_name)}, journal_info_file)
def _get_whatsNew_from_cache(journal_name, issue, ln): """ Try to get the "whats new" box from cache. """ issue = issue.replace("/", "_") issue_number, year = issue.split("_", 1) cache_path = os.path.abspath('%s/webjournal/%s/%s/%s/whatsNew_%s.html' % \ (CFG_CACHEDIR, journal_name, year, issue_number, ln)) if not cache_path.startswith(CFG_CACHEDIR + '/webjournal'): # Make sure we are reading from correct directory (you # know, in case there are '../../' inside journal name..) return False try: last_update = os.path.getctime(cache_path) except: return False try: # Get last journal update, based on journal info file last # modification time journal_info_path = get_journal_info_path(journal_name) last_journal_update = os.path.getctime(journal_info_path) except: return False now = time.time() if ((last_update + 30*60) < now) or \ (last_journal_update > last_update): # invalidate after 30 minutes or if last journal release is # newer than cache return False try: cached_file = open(cache_path).read() except: return False return cached_file
def test_get_journal_info_path(self): """webjournal - returns the path to the info file of the given journal""" info = wju.get_journal_info_path('AtlantisTimes') path = CFG_PREFIX + '/var/cache/webjournal/AtlantisTimes/info.dat' self.assertEqual(info, path)