def test_BibRecDocs(self):
     """bibdocfile - BibRecDocs functions"""
     my_bibrecdoc = BibRecDocs(2)
     #add bibdoc
     my_bibrecdoc.add_new_file(CFG_PREFIX + '/lib/webtest/invenio/test.jpg', 'Main', 'img_test', False, 'test add new file', 'test', '.jpg')
     my_bibrecdoc.add_bibdoc(doctype='Main', docname='file', never_fail=False)
     self.assertEqual(len(my_bibrecdoc.list_bibdocs()), 3)
     my_added_bibdoc = my_bibrecdoc.get_bibdoc('file')
     #add bibdocfile in empty bibdoc
     my_added_bibdoc.add_file_new_version(CFG_PREFIX + '/lib/webtest/invenio/test.gif', \
     description= 'added in empty bibdoc', comment=None, format=None, flags=['PERFORM_HIDE_PREVIOUS'])
     #propose unique docname
     self.assertEqual(my_bibrecdoc.propose_unique_docname('file'), 'file_2')
     #has docname
     self.assertEqual(my_bibrecdoc.has_docname_p('file'), True)
     #merge 2 bibdocs
     my_bibrecdoc.merge_bibdocs('img_test', 'file')
     self.assertEqual(len(my_bibrecdoc.get_bibdoc("img_test").list_all_files()), 2)
     #check file exists
     self.assertEqual(my_bibrecdoc.check_file_exists(CFG_PREFIX + '/lib/webtest/invenio/test.jpg'), True)
     #get bibdoc names
     self.assertEqual(my_bibrecdoc.get_bibdoc_names('Main')[0], '0104007_02')
     self.assertEqual(my_bibrecdoc.get_bibdoc_names('Main')[1],'img_test')
     #get total size
     self.assertEqual(my_bibrecdoc.get_total_size(), 1647591)
     #get total size latest version
     self.assertEqual(my_bibrecdoc.get_total_size_latest_version(), 1647591)
     #display
     value = my_bibrecdoc.display(docname='img_test', version='', doctype='', ln='en', verbose=0, display_hidden=True)
     self.assert_("<small><b>Main</b>" in value)
     #get xml 8564
     value = my_bibrecdoc.get_xml_8564()
     self.assert_('/record/2/files/img_test.jpg</subfield>' in value)
     #check duplicate docnames
     self.assertEqual(my_bibrecdoc.check_duplicate_docnames(), True)
def Move_Files_Archive(parameters, curdir, form, user_info=None):
    """DEPRECATED: Use FFT instead."""
    MainDir = "%s/files/MainFiles" % curdir
    IncludeDir = "%s/files/AdditionalFiles" % curdir
    watcheddirs = {'Main' : MainDir, 'Additional' : IncludeDir}
    for type, dir in watcheddirs.iteritems():
        if os.path.exists(dir):
            formats = {}
            files = os.listdir(dir)
            files.sort()
            for file in files:
                dummy, filename, extension = decompose_file(file)
                if not formats.has_key(filename):
                    formats[filename] = []
                formats[filename].append(normalize_format(extension))
            # first delete all missing files
            bibarchive = BibRecDocs(sysno)
            existingBibdocs = bibarchive.list_bibdocs(type)
            for existingBibdoc in existingBibdocs:
                if not formats.has_key(existingBibdoc.get_docname()):
                    existingBibdoc.delete()
            # then create/update the new ones
            for key in formats.keys():
                # instanciate bibdoc object
                bibarchive.add_new_file('%s/%s%s' % (dir, key, formats[key]), doctype=type, never_fail=True)
    return ""
def download_one(recid, version):
    """Download given version of the PDF from arxiv"""
    write_message('fetching %s' % recid)
    for count, arxiv_id in enumerate(extract_arxiv_ids_from_recid(recid)):
        if count != 0:
            write_message("Warning: %s has multiple arxiv #" % recid)
            continue

        url_for_pdf = build_arxiv_url(arxiv_id, version)
        filename_arxiv_id = arxiv_id.replace('/', '_')
        temp_file = NamedTemporaryFile(prefix="arxiv-pdf-checker",
                                       dir=CFG_TMPSHAREDDIR,
                                       suffix="%s.pdf" % filename_arxiv_id)
        write_message('downloading pdf from %s' % url_for_pdf)
        path = download_external_url(url_for_pdf,
                                     temp_file.name,
                                     content_type='pdf')

        # Check if it is not an html not found page
        filesize = os.path.getsize(path)
        if filesize < 25000:
            f = open(path)
            try:
                for line in f:
                    if 'PDF unavailable' in line:
                        raise PdfNotAvailable()
            finally:
                f.close()

        docs = BibRecDocs(recid)
        bibdocfiles = docs.list_latest_files(doctype="arXiv")

        needs_update = False
        try:
            bibdocfile = bibdocfiles[0]
        except IndexError:
            bibdocfile = None
            needs_update = True
        else:
            existing_md5 = calculate_md5(bibdocfile.fullpath)
            new_md5 = calculate_md5(path.encode('utf-8'))
            if new_md5 != existing_md5:
                write_message('md5 differs updating')
                needs_update = True
            else:
                write_message('md5 matches existing pdf, skipping')

        if needs_update:
            if bibdocfiles:
                write_message('adding as new version')
                docs.add_new_version(path, docname=bibdocfile.name)
            else:
                write_message('adding as new file')
                docs.add_new_file(path,
                                  doctype="arXiv",
                                  docname="arXiv:%s" % filename_arxiv_id)
        else:
            raise FoundExistingPdf()
Exemple #4
0
def Add_Files(parameters, curdir, form, user_info=None):
    """DEPRECATED: Use FFT instead."""
    if os.path.exists("%s/files" % curdir):
        bibrecdocs = BibRecDocs(sysno)
        for file in os.listdir("%s/files" % curdir):
            fullpath = "%s/files/%s" % (curdir,file)
            if not bibrecdocs.check_file_exists(fullpath):
                bibrecdocs.add_new_file(fullpath, "Main", never_fail=True)
    return ""
    def test_BibDocFiles(self):
        """bibdocfile - BibDocFile functions """
        #add bibdoc
        my_bibrecdoc = BibRecDocs(2)
        timestamp = datetime(*(time.strptime("2010-09-08 07:06:05", "%Y-%m-%d %H:%M:%S")[:6]))
        my_bibrecdoc.add_new_file(CFG_PREFIX + '/lib/webtest/invenio/test.jpg', 'Main', 'img_test', False, 'test add new file', 'test', '.jpg', modification_date=timestamp)

        my_new_bibdoc = my_bibrecdoc.get_bibdoc("img_test")
        my_new_bibdocfile = my_new_bibdoc.list_all_files()[0]
        #get url
        self.assertEqual(my_new_bibdocfile.get_url(), CFG_SITE_URL + '/%s/2/files/img_test.jpg' % CFG_SITE_RECORD)
        #get type
        self.assertEqual(my_new_bibdocfile.get_type(), 'Main')
        #get path
        # we should not test for particular path ! this is in the gestion of the underlying implementation,
        # not the interface which should ne tested
        #        self.assert_(my_new_bibdocfile.get_path().startswith(CFG_BIBDOCFILE_FILEDIR))
        #        self.assert_(my_new_bibdocfile.get_path().endswith('/img_test.jpg;1'))
        #get bibdocid
        self.assertEqual(my_new_bibdocfile.get_bibdocid(), my_new_bibdoc.get_id())
        #get name
        self.assertEqual(my_new_bibdocfile.get_name() , 'img_test')
        #get full name
        self.assertEqual(my_new_bibdocfile.get_full_name() , 'img_test.jpg')
        #get full path
        #self.assert_(my_new_bibdocfile.get_full_path().startswith(CFG_BIBDOCFILE_FILEDIR))
        #self.assert_(my_new_bibdocfile.get_full_path().endswith('/img_test.jpg;1'))
        #get format
        self.assertEqual(my_new_bibdocfile.get_format(), '.jpg')
        #get version
        self.assertEqual(my_new_bibdocfile.get_version(), 1)
        #get description
        self.assertEqual(my_new_bibdocfile.get_description(), my_new_bibdoc.get_description('.jpg', version=1))
        #get comment
        self.assertEqual(my_new_bibdocfile.get_comment(), my_new_bibdoc.get_comment('.jpg', version=1))
        #get recid
        self.assertEqual(my_new_bibdocfile.get_recid(), 2)
        #get status
        self.assertEqual(my_new_bibdocfile.get_status(), '')
        #get size
        self.assertEqual(my_new_bibdocfile.get_size(), 91750)
        #get checksum
        self.assertEqual(my_new_bibdocfile.get_checksum(), '28ec893f9da735ad65de544f71d4ad76')
        #check
        self.assertEqual(my_new_bibdocfile.check(), True)
        #display
        tmpl = invenio.template.load("bibdocfile")
        value = tmpl.tmpl_display_bibdocfile(my_new_bibdocfile, ln='en')
        assert 'files/img_test.jpg?version=1">' in value
        #hidden?
        self.assertEqual(my_new_bibdocfile.hidden_p(), False)
        #check modification date
        self.assertEqual(my_new_bibdocfile.md, timestamp)
        #delete
        my_new_bibdoc.delete()
        self.assertEqual(my_new_bibdoc.deleted_p(), True)
Exemple #6
0
def Add_Files(parameters, curdir, form, user_info=None):
    """DEPRECATED: Use FFT instead."""
    if os.path.exists("%s/files" % curdir):
        bibrecdocs = BibRecDocs(sysno)
        for current_file in os.listdir("%s/files" % curdir):
            fullpath = "%s/files/%s" % (curdir,current_file)
            dummy, filename, extension = decompose_file(current_file)
            if extension and extension[0] != ".":
                extension = '.' + extension
            if not bibrecdocs.check_file_exists(fullpath, extension):
                bibrecdocs.add_new_file(fullpath, "Main", never_fail=True)
    return ""
 def test_BibDocFiles(self):
     """bibdocfile - BibDocFile functions """
     #add bibdoc
     my_bibrecdoc = BibRecDocs(2)
     my_bibrecdoc.add_new_file(CFG_PREFIX + '/lib/webtest/invenio/test.jpg', 'Main', 'img_test', False, 'test add new file', 'test', '.jpg')
     my_new_bibdoc = my_bibrecdoc.get_bibdoc("img_test")
     my_new_bibdocfile = my_new_bibdoc.list_all_files()[0]
     #get url
     self.assertEqual(my_new_bibdocfile.get_url(), CFG_SITE_URL + '/record/2/files/img_test.jpg')
     #get type
     self.assertEqual(my_new_bibdocfile.get_type(), 'Main')
     #get path
     self.assert_(my_new_bibdocfile.get_path().startswith(CFG_WEBSUBMIT_FILEDIR))
     self.assert_(my_new_bibdocfile.get_path().endswith('/img_test.jpg;1'))
     #get bibdocid
     self.assertEqual(my_new_bibdocfile.get_bibdocid(), my_new_bibdoc.get_id())
     #get name
     self.assertEqual(my_new_bibdocfile.get_name() , 'img_test')
     #get full name
     self.assertEqual(my_new_bibdocfile.get_full_name() , 'img_test.jpg')
     #get full path
     self.assert_(my_new_bibdocfile.get_full_path().startswith(CFG_WEBSUBMIT_FILEDIR))
     self.assert_(my_new_bibdocfile.get_full_path().endswith('/img_test.jpg;1'))
     #get format
     self.assertEqual(my_new_bibdocfile.get_format(), '.jpg')
     #get version
     self.assertEqual(my_new_bibdocfile.get_version(), 1)
     #get description
     self.assertEqual(my_new_bibdocfile.get_description(), my_new_bibdoc.get_description('.jpg', version=1))
     #get comment
     self.assertEqual(my_new_bibdocfile.get_comment(), my_new_bibdoc.get_comment('.jpg', version=1))
     #get recid
     self.assertEqual(my_new_bibdocfile.get_recid(), 2)
     #get status
     self.assertEqual(my_new_bibdocfile.get_status(), '')
     #get size
     self.assertEqual(my_new_bibdocfile.get_size(), 91750)
     #get checksum
     self.assertEqual(my_new_bibdocfile.get_checksum(), '28ec893f9da735ad65de544f71d4ad76')
     #check
     self.assertEqual(my_new_bibdocfile.check(), True)
     #display
     value = my_new_bibdocfile.display(ln='en')
     assert 'files/img_test.jpg?version=1">' in value
     #hidden?
     self.assertEqual(my_new_bibdocfile.hidden_p(), False)
     #delete
     my_new_bibdoc.delete()
     self.assertEqual(my_new_bibdoc.deleted_p(), True)
def upload_fulltext(recid, path):
    '''
        This method save the uploaded file to associated record
        @param (recid) : id of the record
        @param (fulltext) : uploaded document to store
    '''

    # upload the file to the record

    bibarchiv = BibRecDocs(recid)
    docname = path.split('/')[-1].split('.')[0]
    doctype = path.split('.')[-1].split(';')[0]
    bibarchiv.add_new_file(path, CFG_DOCTYPE_UPLOAD_COLLECTION, docname,
                           format=doctype)

    return ''
    def test_BibRecDocs(self):
        """bibdocfile - BibRecDocs functions"""
        my_bibrecdoc = BibRecDocs(2)
        # add bibdoc
        my_bibrecdoc.add_new_file(
            CFG_PREFIX + "/lib/webtest/invenio/test.jpg", "Main", "img_test", False, "test add new file", "test", ".jpg"
        )
        my_bibrecdoc.add_bibdoc(doctype="Main", docname="file", never_fail=False)
        self.assertEqual(len(my_bibrecdoc.list_bibdocs()), 3)
        my_added_bibdoc = my_bibrecdoc.get_bibdoc("file")
        # add bibdocfile in empty bibdoc
        my_added_bibdoc.add_file_new_version(
            CFG_PREFIX + "/lib/webtest/invenio/test.gif",
            description="added in empty bibdoc",
            comment=None,
            docformat=None,
            flags=["PERFORM_HIDE_PREVIOUS"],
        )
        # propose unique docname
        self.assertEqual(my_bibrecdoc.propose_unique_docname("file"), "file_2")
        # has docname
        self.assertEqual(my_bibrecdoc.has_docname_p("file"), True)
        # merge 2 bibdocs
        my_bibrecdoc.merge_bibdocs("img_test", "file")
        self.assertEqual(len(my_bibrecdoc.get_bibdoc("img_test").list_all_files()), 2)
        # check file exists
        self.assertEqual(my_bibrecdoc.check_file_exists(CFG_PREFIX + "/lib/webtest/invenio/test.jpg", ".jpg"), True)
        # get bibdoc names
        # we can not rely on the order !
        names = set([my_bibrecdoc.get_bibdoc_names("Main")[0], my_bibrecdoc.get_bibdoc_names("Main")[1]])
        self.assertTrue("0104007_02" in names)
        self.assertTrue("img_test" in names)

        # get total size
        self.assertEqual(my_bibrecdoc.get_total_size(), 1647591)
        # get total size latest version
        self.assertEqual(my_bibrecdoc.get_total_size_latest_version(), 1647591)
        # display
        # value = my_bibrecdoc.display(docname='img_test', version='', doctype='', ln='en', verbose=0, display_hidden=True)
        # self.assert_("<small><b>Main</b>" in value)
        # get xml 8564
        value = my_bibrecdoc.get_xml_8564()
        self.assert_("/" + CFG_SITE_RECORD + "/2/files/img_test.jpg</subfield>" in value)
        # check duplicate docnames
        self.assertEqual(my_bibrecdoc.check_duplicate_docnames(), True)
class BibDocFsInfoTest(unittest.TestCase):
    """Regression tests about the table bibdocfsinfo"""

    def setUp(self):
        self.my_bibrecdoc = BibRecDocs(2)
        self.unique_name = self.my_bibrecdoc.propose_unique_docname("file")
        self.my_bibdoc = self.my_bibrecdoc.add_new_file(
            CFG_PREFIX + "/lib/webtest/invenio/test.jpg", docname=self.unique_name
        )
        self.my_bibdoc_id = self.my_bibdoc.id

    def tearDown(self):
        self.my_bibdoc.expunge()

    def test_hard_delete(self):
        """bibdocfile - test correct update of bibdocfsinfo when hard-deleting"""
        self.assertEqual(
            run_sql("SELECT MAX(version) FROM bibdocfsinfo WHERE id_bibdoc=%s", (self.my_bibdoc_id,))[0][0], 1
        )
        self.assertEqual(
            run_sql(
                "SELECT last_version FROM bibdocfsinfo WHERE id_bibdoc=%s AND version=1 AND format='.jpg'",
                (self.my_bibdoc_id,),
            )[0][0],
            True,
        )
        self.my_bibdoc.add_file_new_version(CFG_PREFIX + "/lib/webtest/invenio/test.gif")
        self.assertEqual(
            run_sql("SELECT MAX(version) FROM bibdocfsinfo WHERE id_bibdoc=%s", (self.my_bibdoc_id,))[0][0], 2
        )
        self.assertEqual(
            run_sql(
                "SELECT last_version FROM bibdocfsinfo WHERE id_bibdoc=%s AND version=2 AND format='.gif'",
                (self.my_bibdoc_id,),
            )[0][0],
            True,
        )
        self.assertEqual(
            run_sql(
                "SELECT last_version FROM bibdocfsinfo WHERE id_bibdoc=%s AND version=1 AND format='.jpg'",
                (self.my_bibdoc_id,),
            )[0][0],
            False,
        )
        self.my_bibdoc.delete_file(".gif", 2)
        self.assertEqual(
            run_sql("SELECT MAX(version) FROM bibdocfsinfo WHERE id_bibdoc=%s", (self.my_bibdoc_id,))[0][0], 1
        )
        self.assertEqual(
            run_sql(
                "SELECT last_version FROM bibdocfsinfo WHERE id_bibdoc=%s AND version=1 AND format='.jpg'",
                (self.my_bibdoc_id,),
            )[0][0],
            True,
        )
def Move_Files_to_Storage(parameters, curdir, form, user_info=None):
    """
    The function moves files received from the standard submission's
    form through file input element(s). The document are assigned a
    'doctype' (or category) corresponding to the file input element
    (eg. a file uploaded throught 'DEMOPIC_FILE' will go to
    'DEMOPIC_FILE' doctype/category).

    Websubmit engine builds the following file organization in the
    directory curdir/files:

                  curdir/files
                        |
      _____________________________________________________________________
            |                                   |                          |
      ./file input 1 element's name      ./file input 2 element's name    ....
         (for eg. 'DEMOART_MAILFILE')       (for eg. 'DEMOART_APPENDIX')
         |                                     |
      test1.pdf                             test2.pdf


    There is only one instance of all possible extension(pdf, gz...) in each part
    otherwise we may encounter problems when renaming files.

    + parameters['rename']: if given, all the files in curdir/files
      are renamed.  parameters['rename'] is of the form:
      <PA>elemfilename[re]</PA>* where re is an regexp to select(using
      re.sub) what part of the elem file has to be selected.
      e.g: <PA>file:TEST_FILE_RN</PA>

    + parameters['documenttype']: if given, other formats are created.
      It has 2 possible values: - if "picture" icon in gif format is created
                                - if "fulltext" ps, gz .... formats are created

    + parameters['paths_and_suffixes']: directories to look into and
      corresponding suffix to add to every file inside. It must have
      the same structure as a Python dictionnary of the following form
      {'FrenchAbstract':'french', 'EnglishAbstract':''}

      The keys are the file input element name from the form <=>
      directories in curdir/files The values associated are the
      suffixes which will be added to all the files in
      e.g. curdir/files/FrenchAbstract

    + parameters['iconsize'] need only if 'icon' is selected in
      parameters['documenttype']

    + parameters['paths_and_restrictions']: the restrictions to apply
      to each uploaded file. The parameter must have the same
      structure as a Python dictionnary of the following form:
      {'DEMOART_APPENDIX':'restricted'}
      Files not specified in this parameter are not restricted.
      The specified restrictions can include a variable that can be
      replaced at runtime, for eg:
      {'DEMOART_APPENDIX':'restricted to <PA>file:SuE</PA>'}

    + parameters['paths_and_doctypes']: if a doctype is specified,
      the file will be saved under the 'doctype/collection' instead
      of under the default doctype/collection given by the name
      of the upload element that was used on the websubmit interface.
      to configure the doctype in websubmit, enter the value as in a
      dictionnary, for eg:
      {'PATHS_SWORD_UPL' : 'PUSHED_TO_ARXIV'} -> from
      Demo_Export_Via_Sword [DEMOSWR] Document Types
    """

    global sysno
    paths_and_suffixes = parameters['paths_and_suffixes']
    paths_and_restrictions = parameters['paths_and_restrictions']
    rename = parameters['rename']
    documenttype = parameters['documenttype']
    iconsizes = parameters['iconsize'].split(',')
    paths_and_doctypes = parameters['paths_and_doctypes']

    ## Create an instance of BibRecDocs for the current recid(sysno)
    bibrecdocs = BibRecDocs(sysno)

    paths_and_suffixes = get_dictionary_from_string(paths_and_suffixes)

    paths_and_restrictions = get_dictionary_from_string(paths_and_restrictions)

    paths_and_doctypes = get_dictionary_from_string(paths_and_doctypes)

    ## Go through all the directories specified in the keys
    ## of parameters['paths_and_suffixes']
    for path in paths_and_suffixes.keys():
        ## Check if there is a directory for the current path
        if os.path.exists("%s/files/%s" % (curdir, path)):
            ## Retrieve the restriction to apply to files in this
            ## directory
            restriction = paths_and_restrictions.get(path, '')
            restriction = re.sub('<PA>(?P<content>[^<]*)</PA>',
                                 get_pa_tag_content,
                                 restriction)

            ## Go through all the files in curdir/files/path
            for current_file in os.listdir("%s/files/%s" % (curdir, path)):
                ## retrieve filename and extension
                dummy, filename, extension = decompose_file(current_file)
                if extension and extension[0] != ".":
                    extension = '.' + extension
                if len(paths_and_suffixes[path]) != 0:
                    extension = "_%s%s" % (paths_and_suffixes[path], extension)
                ## Build the new file name if rename parameter has been given
                if rename:
                    filename = re.sub('<PA>(?P<content>[^<]*)</PA>', \
                                      get_pa_tag_content, \
                                      parameters['rename'])

                if rename or len(paths_and_suffixes[path]) != 0 :
                    ## Rename the file
                    try:
                        # Write the log rename_cmd
                        fd = open("%s/rename_cmd" % curdir, "a+")
                        fd.write("%s/files/%s/%s" % (curdir, path, current_file) + " to " +\
                                  "%s/files/%s/%s%s" % (curdir, path, filename, extension) + "\n\n")
                        ## Rename
                        os.rename("%s/files/%s/%s" % (curdir, path, current_file), \
                                  "%s/files/%s/%s%s" % (curdir, path, filename, extension))

                        fd.close()
                        ## Save the new name in a text file in curdir so that
                        ## the new filename can be used by templates to created the recmysl
                        fd = open("%s/%s_RENAMED" % (curdir, path), "w")
                        fd.write("%s%s" % (filename, extension))
                        fd.close()
                    except OSError, err:
                        msg = "Cannot rename the file.[%s]"
                        msg %= str(err)
                        raise InvenioWebSubmitFunctionWarning(msg)
                fullpath = "%s/files/%s/%s%s" % (curdir, path, filename, extension)
                ## Check if there is any existing similar file
                if not bibrecdocs.check_file_exists(fullpath):
                    bibdoc = bibrecdocs.add_new_file(fullpath, doctype=paths_and_doctypes.get(path, path), never_fail=True)
                    bibdoc.set_status(restriction)
                    ## Fulltext
                    if documenttype == "fulltext":
                        additionalformats = createRelatedFormats(fullpath)
                        if len(additionalformats) > 0:
                            for additionalformat in additionalformats:
                                try:
                                    bibrecdocs.add_new_format(additionalformat)
                                except InvenioWebSubmitFileError:
                                    pass
                    ## Icon
                    elif documenttype == "picture":
                        has_added_default_icon_subformat_p = False
                        for iconsize in iconsizes:
                            try:
                                iconpath, iconname = create_icon({
                                    'input-file' : fullpath,
                                    'icon-scale' : iconsize,
                                    'icon-name' : None,
                                    'icon-file-format' : None,
                                    'multipage-icon' : False,
                                    'multipage-icon-delay' : 100,
                                    'verbosity' : 0,
                                })
                            except Exception, e:
                                register_exception(prefix='Impossible to create icon for %s (record %s)' % (fullpath, sysno), alert_admin=True)
                                continue
                            iconpath = os.path.join(iconpath, iconname)
                            docname = decompose_file(fullpath)[1]
                            try:
                                mybibdoc = bibrecdocs.get_bibdoc(docname)
                            except InvenioWebSubmitFileError:
                                mybibdoc = None
                            if iconpath is not None and mybibdoc is not None:
                                try:
                                    icon_suffix = iconsize.replace('>', '').replace('<', '').replace('^', '').replace('!', '')
                                    if not has_added_default_icon_subformat_p:
                                        mybibdoc.add_icon(iconpath)
                                        has_added_default_icon_subformat_p = True
                                    else:
                                        mybibdoc.add_icon(iconpath, subformat=CFG_WEBSUBMIT_DEFAULT_ICON_SUBFORMAT + "-" + icon_suffix)
                                    ## Save the new icon filename in a text file in curdir so that
                                    ## it can be used by templates to created the recmysl
                                    try:
                                        if not has_added_default_icon_subformat_p:
                                            fd = open("%s/%s_ICON" % (curdir, path), "w")
                                        else:
                                            fd = open("%s/%s_ICON_%s" % (curdir, path, iconsize + '_' + icon_suffix), "w")
                                        fd.write(os.path.basename(iconpath))
                                        fd.close()
                                    except OSError, err:
                                        msg = "Cannot store icon filename.[%s]"
                                        msg %= str(err)
                                        raise InvenioWebSubmitFunctionWarning(msg)
                                except InvenioWebSubmitFileError, e:
                                    # Most probably icon already existed.
                                    pass
                            elif mybibdoc is not None:
                                mybibdoc.delete_icon()
 def test_BibDocs(self):
     """bibdocfile - BibDocs functions"""
     #add file
     my_bibrecdoc = BibRecDocs(2)
     my_bibrecdoc.add_new_file(CFG_PREFIX + '/lib/webtest/invenio/test.jpg', 'Main', 'img_test', False, 'test add new file', 'test', '.jpg')
     my_new_bibdoc = my_bibrecdoc.get_bibdoc("img_test")
     value = my_bibrecdoc.list_bibdocs()
     self.assertEqual(len(value), 2)
     #get total file (bibdoc)
     self.assertEqual(my_new_bibdoc.get_total_size(), 91750)
     #get recid
     self.assertEqual(my_new_bibdoc.get_recid(), 2)
     #change name
     my_new_bibdoc.change_name('new_name')
     #get docname
     self.assertEqual(my_new_bibdoc.get_docname(), 'new_name')
     #get type
     self.assertEqual(my_new_bibdoc.get_type(), 'Main')
     #get id
     self.assert_(my_new_bibdoc.get_id() > 80)
     #set status
     my_new_bibdoc.set_status('new status')
     #get status
     self.assertEqual(my_new_bibdoc.get_status(), 'new status')
     #get base directory
     self.assert_(my_new_bibdoc.get_base_dir().startswith(CFG_WEBSUBMIT_FILEDIR))
     #get file number
     self.assertEqual(my_new_bibdoc.get_file_number(), 1)
     #add file new version
     my_new_bibdoc.add_file_new_version(CFG_PREFIX + '/lib/webtest/invenio/test.jpg', description= 'the new version', comment=None, format=None, flags=["PERFORM_HIDE_PREVIOUS"])
     self.assertEqual(my_new_bibdoc.list_versions(), [1, 2])
     #revert
     my_new_bibdoc.revert(1)
     self.assertEqual(my_new_bibdoc.list_versions(), [1, 2, 3])
     self.assertEqual(my_new_bibdoc.get_description('.jpg', version=3), 'test add new file')
     #get total size latest version
     self.assertEqual(my_new_bibdoc.get_total_size_latest_version(), 91750)
     #get latest version
     self.assertEqual(my_new_bibdoc.get_latest_version(), 3)
     #list latest files
     self.assertEqual(len(my_new_bibdoc.list_latest_files()), 1)
     self.assertEqual(my_new_bibdoc.list_latest_files()[0].get_version(), 3)
     #list version files
     self.assertEqual(len(my_new_bibdoc.list_version_files(1, list_hidden=True)), 1)
     #display
     value = my_new_bibdoc.display(version='', ln='en', display_hidden=True)
     self.assert_('>test add new file<' in value)
     #format already exist
     self.assertEqual(my_new_bibdoc.format_already_exists_p('.jpg'), True)
     #get file
     self.assertEqual(my_new_bibdoc.get_file('.jpg', version='1').get_version(), 1)
     #set description
     my_new_bibdoc.set_description('new description', '.jpg', version=1)
     #get description
     self.assertEqual(my_new_bibdoc.get_description('.jpg', version=1), 'new description')
     #set comment
     my_new_bibdoc.set_description('new comment', '.jpg', version=1)
     #get comment
     self.assertEqual(my_new_bibdoc.get_description('.jpg', version=1), 'new comment')
     #get history
     assert len(my_new_bibdoc.get_history()) > 0
     #delete file
     my_new_bibdoc.delete_file('.jpg', 2)
     #list all files
     self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
     #delete file
     my_new_bibdoc.delete_file('.jpg', 3)
     #add new format
     my_new_bibdoc.add_file_new_format(CFG_PREFIX + '/lib/webtest/invenio/test.gif', version=None, description=None, comment=None, format=None)
     self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
     #delete file
     my_new_bibdoc.delete_file('.jpg', 1)
     #delete file
     my_new_bibdoc.delete_file('.gif', 1)
     #empty bibdoc
     self.assertEqual(my_new_bibdoc.empty_p(), True)
     #hidden?
     self.assertEqual(my_new_bibdoc.hidden_p('.jpg', version=1), False)
     #hide
     my_new_bibdoc.set_flag('HIDDEN', '.jpg', version=1)
     #hidden?
     self.assertEqual(my_new_bibdoc.hidden_p('.jpg', version=1), True)
     #add and get icon
     my_new_bibdoc.add_icon( CFG_PREFIX + '/lib/webtest/invenio/icon-test.gif')
     value =  my_bibrecdoc.list_bibdocs()[1]
     self.assertEqual(value.get_icon(), my_new_bibdoc.get_icon())
     #delete icon
     my_new_bibdoc.delete_icon()
     #get icon
     self.assertEqual(my_new_bibdoc.get_icon(), None)
     #delete
     my_new_bibdoc.delete()
     self.assertEqual(my_new_bibdoc.deleted_p(), True)
     #undelete
     my_new_bibdoc.undelete(previous_status='')
Exemple #13
0
def download_one(recid, version):
    """Download given version of the PDF from arxiv"""
    write_message('fetching %s' % recid)
    for count, arxiv_id in enumerate(extract_arxiv_ids_from_recid(recid)):
        if count != 0:
            write_message("Warning: %s has multiple arxiv #" % recid)
            continue

        url_for_pdf = build_arxiv_url(arxiv_id, version)
        filename_arxiv_id = arxiv_id.replace('/', '_')
        temp_file = NamedTemporaryFile(prefix="arxiv-pdf-checker",
                                       dir=CFG_TMPSHAREDDIR,
                                       suffix="%s.pdf" % filename_arxiv_id)
        write_message('downloading pdf from %s' % url_for_pdf)
        path = download_external_url(url_for_pdf,
                                     temp_file.name,
                                     content_type='pdf')

        # Check if it is not an html not found page
        filesize = os.path.getsize(path)
        if filesize < 25000:
            f = open(path)
            try:
                for line in f:
                    if 'PDF unavailable' in line:
                        raise PdfNotAvailable()
            finally:
                f.close()

        docs = BibRecDocs(recid)
        bibdocfiles = docs.list_latest_files(doctype="arXiv")
        if not bibdocfiles:
            # Maybe that this is one of those INSPIRE-PUBLIC with
            # still an arXiv file in it
            for name, bibdoc in docs.list_bibdocs_by_names().items():
                if name.startswith('arXiv:'):
                    bibdocfiles = bibdoc.list_latest_files()

        bibdocfiles = [
            bibdocfile for bibdocfile in bibdocfiles
            if bibdocfile.get_superformat() == '.pdf'
        ]

        needs_update = False
        try:
            bibdocfile = bibdocfiles[0]
        except IndexError:
            bibdocfile = None
            needs_update = True
        else:
            existing_md5 = calculate_md5(bibdocfile.fullpath)
            new_md5 = calculate_md5(path.encode('utf-8'))
            if new_md5 != existing_md5:
                write_message('md5 differs updating')
                needs_update = True
            else:
                write_message('md5 matches existing pdf, skipping')

        if needs_update:
            if bibdocfiles:
                write_message('adding as new version')
                docs.add_new_version(path, docname=bibdocfile.name)
            else:
                write_message('adding as new file')
                docs.add_new_file(path,
                                  doctype="arXiv",
                                  docname="arXiv:%s" % filename_arxiv_id)
        else:
            raise FoundExistingPdf()
def Move_Files_to_Storage(parameters, curdir, form, user_info=None):
    """
    The function moves files received from the standard submission's
    form through file input element(s). The document are assigned a
    'doctype' (or category) corresponding to the file input element
    (eg. a file uploaded throught 'DEMOPIC_FILE' will go to
    'DEMOPIC_FILE' doctype/category).

    Websubmit engine builds the following file organization in the
    directory curdir/files:

                  curdir/files
                        |
      _____________________________________________________________________
            |                                   |                          |
      ./file input 1 element's name      ./file input 2 element's name    ....
         (for eg. 'DEMOART_MAILFILE')       (for eg. 'DEMOART_APPENDIX')
         |                                     |
      test1.pdf                             test2.pdf


    There is only one instance of all possible extension(pdf, gz...) in each part
    otherwise we may encounter problems when renaming files.

    + parameters['rename']: if given, all the files in curdir/files
      are renamed.  parameters['rename'] is of the form:
      <PA>elemfilename[re]</PA>* where re is an regexp to select(using
      re.sub) what part of the elem file has to be selected.
      e.g: <PA>file:TEST_FILE_RN</PA>

    + parameters['documenttype']: if given, other formats are created.
      It has 2 possible values: - if "picture" icon in gif format is created
                                - if "fulltext" ps, gz .... formats are created

    + parameters['paths_and_suffixes']: directories to look into and
      corresponding suffix to add to every file inside. It must have
      the same structure as a Python dictionnary of the following form
      {'FrenchAbstract':'french', 'EnglishAbstract':''}

      The keys are the file input element name from the form <=>
      directories in curdir/files The values associated are the
      suffixes which will be added to all the files in
      e.g. curdir/files/FrenchAbstract

    + parameters['iconsize'] need only if 'icon' is selected in
      parameters['documenttype']

    + parameters['paths_and_restrictions']: the restrictions to apply
      to each uploaded file. The parameter must have the same
      structure as a Python dictionnary of the following form:
      {'DEMOART_APPENDIX':'restricted'}
      Files not specified in this parameter are not restricted.
      The specified restrictions can include a variable that can be
      replaced at runtime, for eg:
      {'DEMOART_APPENDIX':'restricted to <PA>file:SuE</PA>'}

    + parameters['paths_and_doctypes']: if a doctype is specified,
      the file will be saved under the 'doctype/collection' instead
      of under the default doctype/collection given by the name
      of the upload element that was used on the websubmit interface.
      to configure the doctype in websubmit, enter the value as in a
      dictionnary, for eg:
      {'PATHS_SWORD_UPL' : 'PUSHED_TO_ARXIV'} -> from
      Demo_Export_Via_Sword [DEMOSWR] Document Types
    """

    global sysno
    paths_and_suffixes = parameters['paths_and_suffixes']
    paths_and_restrictions = parameters['paths_and_restrictions']
    rename = parameters['rename']
    documenttype = parameters['documenttype']
    iconsizes = parameters['iconsize'].split(',')
    paths_and_doctypes = parameters['paths_and_doctypes']

    ## Create an instance of BibRecDocs for the current recid(sysno)
    bibrecdocs = BibRecDocs(sysno)

    paths_and_suffixes = get_dictionary_from_string(paths_and_suffixes)

    paths_and_restrictions = get_dictionary_from_string(paths_and_restrictions)

    paths_and_doctypes = get_dictionary_from_string(paths_and_doctypes)

    ## Go through all the directories specified in the keys
    ## of parameters['paths_and_suffixes']
    for path in paths_and_suffixes.keys():
        ## Check if there is a directory for the current path
        if os.path.exists("%s/files/%s" % (curdir, path)):
            ## Retrieve the restriction to apply to files in this
            ## directory
            restriction = paths_and_restrictions.get(path, '')
            restriction = re.sub('<PA>(?P<content>[^<]*)</PA>',
                                 get_pa_tag_content, restriction)

            ## Go through all the files in curdir/files/path
            for current_file in os.listdir("%s/files/%s" % (curdir, path)):
                ## retrieve filename and extension
                dummy, filename, extension = decompose_file(current_file)
                if extension and extension[0] != ".":
                    extension = '.' + extension
                if len(paths_and_suffixes[path]) != 0:
                    extension = "_%s%s" % (paths_and_suffixes[path], extension)
                ## Build the new file name if rename parameter has been given
                if rename:
                    filename = re.sub('<PA>(?P<content>[^<]*)</PA>', \
                                      get_pa_tag_content, \
                                      parameters['rename'])

                if rename or len(paths_and_suffixes[path]) != 0:
                    ## Rename the file
                    try:
                        # Write the log rename_cmd
                        fd = open("%s/rename_cmd" % curdir, "a+")
                        fd.write("%s/files/%s/%s" % (curdir, path, current_file) + " to " +\
                                  "%s/files/%s/%s%s" % (curdir, path, filename, extension) + "\n\n")
                        ## Rename
                        os.rename("%s/files/%s/%s" % (curdir, path, current_file), \
                                  "%s/files/%s/%s%s" % (curdir, path, filename, extension))

                        fd.close()
                        ## Save the new name in a text file in curdir so that
                        ## the new filename can be used by templates to created the recmysl
                        fd = open("%s/%s_RENAMED" % (curdir, path), "w")
                        fd.write("%s%s" % (filename, extension))
                        fd.close()
                    except OSError, err:
                        msg = "Cannot rename the file.[%s]"
                        msg %= str(err)
                        raise InvenioWebSubmitFunctionWarning(msg)
                fullpath = "%s/files/%s/%s%s" % (curdir, path, filename,
                                                 extension)
                ## Check if there is any existing similar file
                if not bibrecdocs.check_file_exists(fullpath):
                    bibdoc = bibrecdocs.add_new_file(
                        fullpath,
                        doctype=paths_and_doctypes.get(path, path),
                        never_fail=True)
                    bibdoc.set_status(restriction)
                    ## Fulltext
                    if documenttype == "fulltext":
                        additionalformats = createRelatedFormats(fullpath)
                        if len(additionalformats) > 0:
                            for additionalformat in additionalformats:
                                try:
                                    bibrecdocs.add_new_format(additionalformat)
                                except InvenioWebSubmitFileError:
                                    pass
                    ## Icon
                    elif documenttype == "picture":
                        has_added_default_icon_subformat_p = False
                        for iconsize in iconsizes:
                            try:
                                iconpath, iconname = create_icon({
                                    'input-file':
                                    fullpath,
                                    'icon-scale':
                                    iconsize,
                                    'icon-name':
                                    None,
                                    'icon-file-format':
                                    None,
                                    'multipage-icon':
                                    False,
                                    'multipage-icon-delay':
                                    100,
                                    'verbosity':
                                    0,
                                })
                            except Exception, e:
                                register_exception(
                                    prefix=
                                    'Impossible to create icon for %s (record %s)'
                                    % (fullpath, sysno),
                                    alert_admin=True)
                                continue
                            iconpath = os.path.join(iconpath, iconname)
                            docname = decompose_file(fullpath)[1]
                            try:
                                mybibdoc = bibrecdocs.get_bibdoc(docname)
                            except InvenioWebSubmitFileError:
                                mybibdoc = None
                            if iconpath is not None and mybibdoc is not None:
                                try:
                                    icon_suffix = iconsize.replace(
                                        '>', '').replace('<', '').replace(
                                            '^', '').replace('!', '')
                                    if not has_added_default_icon_subformat_p:
                                        mybibdoc.add_icon(iconpath)
                                        has_added_default_icon_subformat_p = True
                                    else:
                                        mybibdoc.add_icon(
                                            iconpath,
                                            subformat=
                                            CFG_WEBSUBMIT_DEFAULT_ICON_SUBFORMAT
                                            + "-" + icon_suffix)
                                    ## Save the new icon filename in a text file in curdir so that
                                    ## it can be used by templates to created the recmysl
                                    try:
                                        if not has_added_default_icon_subformat_p:
                                            fd = open(
                                                "%s/%s_ICON" % (curdir, path),
                                                "w")
                                        else:
                                            fd = open(
                                                "%s/%s_ICON_%s" %
                                                (curdir, path,
                                                 iconsize + '_' + icon_suffix),
                                                "w")
                                        fd.write(os.path.basename(iconpath))
                                        fd.close()
                                    except OSError, err:
                                        msg = "Cannot store icon filename.[%s]"
                                        msg %= str(err)
                                        raise InvenioWebSubmitFunctionWarning(
                                            msg)
                                except InvenioWebSubmitFileError, e:
                                    # Most probably icon already existed.
                                    pass
                            elif mybibdoc is not None:
                                mybibdoc.delete_icon()
def Move_Photos_to_Storage(parameters, curdir, form, user_info=None):
    """
    The function moves files received from the submission's form
    through the PHOTO_MANAGER element and its asynchronous uploads at
    CFG_SITE_URL/submit/uploadfile.

    Parameters:
        @iconsize - Seperate multiple sizes with commas. The ImageMagick geometry inputs are supported.
              Use type 'geometry' as defined in ImageMagick.
              (eg. 320 or 320x240 or 100> or 5%)
              Example: "180>,700>" will create two icons, one with maximum dimension 180px, one 700px
        @iconformat - Allowed extensions (as defined in websubmit_icon_creator.py) are:
                "pdf", "gif", "jpg",
                "jpeg", "ps", "png", "bmp"
                "eps", "epsi", "epsf"

    The PHOTO_MANAGER elements builds the following file organization
    in the directory curdir::

                                     curdir/
                                        |
         ______________________________________________________________________
        |                                   |                                  |
      files/                         PHOTO_MANAGER_ICONS                     icons/
        |                            PHOTO_MANAGER_ORDER                       |
     (user id)/                      PHOTO_MANAGER_DELETE                  (user id)/
        |                            PHOTO_MANAGER_NEW                         |
     NewFile/                        PHOTO_MANAGER_DESCRIPTION_X           NewFile/
        |                                                                      |
        _______________________                                      _____________________
       |            |          |                                    |          |          |
     photo1.jpg  myPhoto.gif   ...                             photo1.jpg  myPhoto.gif   ...


    where the files are:
      - PHOTO_MANAGER_ORDER: ordered list of file IDs. One per line.

      - PHOTO_MANAGER_ICONS: mappings from file IDs to URL of the icons.
                             One per line. Separator: /

      - PHOTO_MANAGER_NEW: mapping from file ID to filename on disk. Only
                           applicable to files that have just been
                           uploaded (i.e. not bibdocfiles). One per
                           line. Separator: /

      - PHOTO_MANAGER_DELETE: list of files IDs that must be deleted. One
                               per line

      - PHOTO_MANAGER_DESCRIPTION_X, where X is file ID: contains photos
                                     descriptions (one per file)

    """
    global sysno

    icon_sizes = parameters.get('iconsize').split(',')
    icon_format = parameters.get('iconformat')
    if not icon_format:
        icon_format = 'gif'

    PHOTO_MANAGER_ICONS = read_param_file(curdir,
                                          'PHOTO_MANAGER_ICONS',
                                          split_lines=True)
    photo_manager_icons_dict = dict([value.split('/', 1) \
                                     for value in PHOTO_MANAGER_ICONS \
                                     if '/' in value])
    PHOTO_MANAGER_ORDER = read_param_file(curdir,
                                          'PHOTO_MANAGER_ORDER',
                                          split_lines=True)
    photo_manager_order_list = [
        value for value in PHOTO_MANAGER_ORDER if value.strip()
    ]
    PHOTO_MANAGER_DELETE = read_param_file(curdir,
                                           'PHOTO_MANAGER_DELETE',
                                           split_lines=True)
    photo_manager_delete_list = [
        value for value in PHOTO_MANAGER_DELETE if value.strip()
    ]
    PHOTO_MANAGER_NEW = read_param_file(curdir,
                                        'PHOTO_MANAGER_NEW',
                                        split_lines=True)
    photo_manager_new_dict = dict([value.split('/', 1) \
                               for value in PHOTO_MANAGER_NEW \
                               if '/' in value])

    ## Create an instance of BibRecDocs for the current recid(sysno)
    bibrecdocs = BibRecDocs(sysno)
    for photo_id in photo_manager_order_list:
        photo_description = read_param_file(
            curdir, 'PHOTO_MANAGER_DESCRIPTION_' + photo_id)
        # We must take different actions depending if we deal with a
        # file that already exists, or if it is a new file
        if photo_id in photo_manager_new_dict.keys():
            # New file
            if photo_id not in photo_manager_delete_list:
                filename = photo_manager_new_dict[photo_id]
                filepath = os.path.join(curdir, 'files', str(user_info['uid']),
                                        'NewFile', filename)
                icon_filename = os.path.splitext(filename)[0] + ".gif"
                fileiconpath = os.path.join(curdir, 'icons',
                                            str(user_info['uid']), 'NewFile',
                                            icon_filename)

                # Add the file
                if os.path.exists(filepath):
                    _do_log(curdir, "Adding file %s" % filepath)
                    bibdoc = bibrecdocs.add_new_file(filepath,
                                                     doctype="picture",
                                                     never_fail=True)
                    has_added_default_icon_subformat_p = False
                    for icon_size in icon_sizes:
                        # Create icon if needed
                        try:
                            (icon_path, icon_name) = create_icon({
                                'input-file':
                                filepath,
                                'icon-name':
                                icon_filename,
                                'icon-file-format':
                                icon_format,
                                'multipage-icon':
                                False,
                                'multipage-icon-delay':
                                100,
                                'icon-scale':
                                icon_size,  # Resize only if width > 300
                                'verbosity':
                                0,
                            })
                            fileiconpath = os.path.join(icon_path, icon_name)
                        except InvenioWebSubmitIconCreatorError, e:
                            _do_log(
                                curdir, "Icon could not be created to %s: %s" %
                                (filepath, e))
                            pass
                        if os.path.exists(fileiconpath):
                            try:
                                if not has_added_default_icon_subformat_p:
                                    bibdoc.add_icon(fileiconpath)
                                    has_added_default_icon_subformat_p = True
                                    _do_log(curdir,
                                            "Added icon %s" % fileiconpath)
                                else:
                                    icon_suffix = icon_size.replace(
                                        '>', '').replace('<', '').replace(
                                            '^', '').replace('!', '')
                                    bibdoc.add_icon(
                                        fileiconpath,
                                        subformat=
                                        CFG_BIBDOCFILE_DEFAULT_ICON_SUBFORMAT +
                                        "-" + icon_suffix)
                                    _do_log(curdir,
                                            "Added icon %s" % fileiconpath)
                            except InvenioBibDocFileError, e:
                                # Most probably icon already existed.
                                pass

                    if photo_description and bibdoc:
                        for file_format in [bibdocfile.get_format() \
                                       for bibdocfile in bibdoc.list_latest_files()]:
                            bibdoc.set_comment(photo_description, file_format)
                            _do_log(curdir,
                                    "Added comment %s" % photo_description)
 def test_BibDocFiles(self):
     """bibdocfile - BibDocFile functions """
     # add bibdoc
     my_bibrecdoc = BibRecDocs(2)
     timestamp = datetime(*(time.strptime("2010-09-08 07:06:05", "%Y-%m-%d %H:%M:%S")[:6]))
     my_bibrecdoc.add_new_file(
         CFG_PREFIX + "/lib/webtest/invenio/test.jpg",
         "Main",
         "img_test",
         False,
         "test add new file",
         "test",
         ".jpg",
         modification_date=timestamp,
     )
     my_new_bibdoc = my_bibrecdoc.get_bibdoc("img_test")
     my_new_bibdocfile = my_new_bibdoc.list_all_files()[0]
     # get url
     self.assertEqual(my_new_bibdocfile.get_url(), CFG_SITE_URL + "/%s/2/files/img_test.jpg" % CFG_SITE_RECORD)
     # get type
     self.assertEqual(my_new_bibdocfile.get_type(), "Main")
     # get path
     self.assert_(my_new_bibdocfile.get_path().startswith(CFG_BIBDOCFILE_FILEDIR))
     self.assert_(my_new_bibdocfile.get_path().endswith("/img_test.jpg;1"))
     # get bibdocid
     self.assertEqual(my_new_bibdocfile.get_bibdocid(), my_new_bibdoc.get_id())
     # get name
     self.assertEqual(my_new_bibdocfile.get_name(), "img_test")
     # get full name
     self.assertEqual(my_new_bibdocfile.get_full_name(), "img_test.jpg")
     # get full path
     self.assert_(my_new_bibdocfile.get_full_path().startswith(CFG_BIBDOCFILE_FILEDIR))
     self.assert_(my_new_bibdocfile.get_full_path().endswith("/img_test.jpg;1"))
     # get format
     self.assertEqual(my_new_bibdocfile.get_format(), ".jpg")
     # get version
     self.assertEqual(my_new_bibdocfile.get_version(), 1)
     # get description
     self.assertEqual(my_new_bibdocfile.get_description(), my_new_bibdoc.get_description(".jpg", version=1))
     # get comment
     self.assertEqual(my_new_bibdocfile.get_comment(), my_new_bibdoc.get_comment(".jpg", version=1))
     # get recid
     self.assertEqual(my_new_bibdocfile.get_recid(), 2)
     # get status
     self.assertEqual(my_new_bibdocfile.get_status(), "")
     # get size
     self.assertEqual(my_new_bibdocfile.get_size(), 91750)
     # get checksum
     self.assertEqual(my_new_bibdocfile.get_checksum(), "28ec893f9da735ad65de544f71d4ad76")
     # check
     self.assertEqual(my_new_bibdocfile.check(), True)
     # display
     value = my_new_bibdocfile.display(ln="en")
     assert 'files/img_test.jpg?version=1">' in value
     # hidden?
     self.assertEqual(my_new_bibdocfile.hidden_p(), False)
     # check modification date
     self.assertEqual(my_new_bibdocfile.md, timestamp)
     # delete
     my_new_bibdoc.delete()
     self.assertEqual(my_new_bibdoc.deleted_p(), True)
 def test_BibDocs(self):
     """bibdocfile - BibDocs functions"""
     # add file
     my_bibrecdoc = BibRecDocs(2)
     timestamp1 = datetime(*(time.strptime("2011-10-09 08:07:06", "%Y-%m-%d %H:%M:%S")[:6]))
     my_bibrecdoc.add_new_file(
         CFG_PREFIX + "/lib/webtest/invenio/test.jpg",
         "Main",
         "img_test",
         False,
         "test add new file",
         "test",
         ".jpg",
         modification_date=timestamp1,
     )
     my_new_bibdoc = my_bibrecdoc.get_bibdoc("img_test")
     value = my_bibrecdoc.list_bibdocs()
     self.assertEqual(len(value), 2)
     # get total file (bibdoc)
     self.assertEqual(my_new_bibdoc.get_total_size(), 91750)
     # get recid
     self.assertEqual(my_new_bibdoc.get_recid(), 2)
     # change name
     my_new_bibdoc.change_name("new_name")
     # get docname
     self.assertEqual(my_new_bibdoc.get_docname(), "new_name")
     # get type
     self.assertEqual(my_new_bibdoc.get_type(), "Main")
     # get id
     self.assert_(my_new_bibdoc.get_id() > 80)
     # set status
     my_new_bibdoc.set_status("new status")
     # get status
     self.assertEqual(my_new_bibdoc.get_status(), "new status")
     # get base directory
     self.assert_(my_new_bibdoc.get_base_dir().startswith(CFG_BIBDOCFILE_FILEDIR))
     # get file number
     self.assertEqual(my_new_bibdoc.get_file_number(), 1)
     # add file new version
     timestamp2 = datetime(*(time.strptime("2010-09-08 07:06:05", "%Y-%m-%d %H:%M:%S")[:6]))
     my_new_bibdoc.add_file_new_version(
         CFG_PREFIX + "/lib/webtest/invenio/test.jpg",
         description="the new version",
         comment=None,
         format=None,
         flags=["PERFORM_HIDE_PREVIOUS"],
         modification_date=timestamp2,
     )
     self.assertEqual(my_new_bibdoc.list_versions(), [1, 2])
     # revert
     timestamp3 = datetime.now()
     time.sleep(2)  # so we can see a difference between now() and the time of the revert
     my_new_bibdoc.revert(1)
     self.assertEqual(my_new_bibdoc.list_versions(), [1, 2, 3])
     self.assertEqual(my_new_bibdoc.get_description(".jpg", version=3), "test add new file")
     # get total size latest version
     self.assertEqual(my_new_bibdoc.get_total_size_latest_version(), 91750)
     # get latest version
     self.assertEqual(my_new_bibdoc.get_latest_version(), 3)
     # list latest files
     self.assertEqual(len(my_new_bibdoc.list_latest_files()), 1)
     self.assertEqual(my_new_bibdoc.list_latest_files()[0].get_version(), 3)
     # list version files
     self.assertEqual(len(my_new_bibdoc.list_version_files(1, list_hidden=True)), 1)
     # display
     value = my_new_bibdoc.display(version="", ln="en", display_hidden=True)
     self.assert_(">test add new file<" in value)
     # format already exist
     self.assertEqual(my_new_bibdoc.format_already_exists_p(".jpg"), True)
     # get file
     self.assertEqual(my_new_bibdoc.get_file(".jpg", version="1").get_version(), 1)
     # set description
     my_new_bibdoc.set_description("new description", ".jpg", version=1)
     # get description
     self.assertEqual(my_new_bibdoc.get_description(".jpg", version=1), "new description")
     # set comment
     my_new_bibdoc.set_description("new comment", ".jpg", version=1)
     # get comment
     self.assertEqual(my_new_bibdoc.get_description(".jpg", version=1), "new comment")
     # get history
     assert len(my_new_bibdoc.get_history()) > 0
     # check modification date
     self.assertEqual(my_new_bibdoc.get_file(".jpg", version=1).md, timestamp1)
     self.assertEqual(my_new_bibdoc.get_file(".jpg", version=2).md, timestamp2)
     assert my_new_bibdoc.get_file(".jpg", version=3).md > timestamp3
     # delete file
     my_new_bibdoc.delete_file(".jpg", 2)
     # list all files
     self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
     # delete file
     my_new_bibdoc.delete_file(".jpg", 3)
     # add new format
     timestamp4 = datetime(*(time.strptime("2012-11-10 09:08:07", "%Y-%m-%d %H:%M:%S")[:6]))
     my_new_bibdoc.add_file_new_format(
         CFG_PREFIX + "/lib/webtest/invenio/test.gif",
         version=None,
         description=None,
         comment=None,
         format=None,
         modification_date=timestamp4,
     )
     self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
     # check modification time
     self.assertEqual(my_new_bibdoc.get_file(".jpg", version=1).md, timestamp1)
     self.assertEqual(my_new_bibdoc.get_file(".gif", version=1).md, timestamp4)
     # delete file
     my_new_bibdoc.delete_file(".jpg", 1)
     # delete file
     my_new_bibdoc.delete_file(".gif", 1)
     # empty bibdoc
     self.assertEqual(my_new_bibdoc.empty_p(), True)
     # hidden?
     self.assertEqual(my_new_bibdoc.hidden_p(".jpg", version=1), False)
     # hide
     my_new_bibdoc.set_flag("HIDDEN", ".jpg", version=1)
     # hidden?
     self.assertEqual(my_new_bibdoc.hidden_p(".jpg", version=1), True)
     # add and get icon
     my_new_bibdoc.add_icon(CFG_PREFIX + "/lib/webtest/invenio/icon-test.gif", modification_date=timestamp4)
     value = my_bibrecdoc.list_bibdocs()[1]
     self.assertEqual(value.get_icon(), my_new_bibdoc.get_icon())
     # check modification time
     self.assertEqual(my_new_bibdoc.get_icon().md, timestamp4)
     # delete icon
     my_new_bibdoc.delete_icon()
     # get icon
     self.assertEqual(my_new_bibdoc.get_icon(), None)
     # delete
     my_new_bibdoc.delete()
     self.assertEqual(my_new_bibdoc.deleted_p(), True)
     # undelete
     my_new_bibdoc.undelete(previous_status="")
     # expunging
     my_new_bibdoc.expunge()
     my_bibrecdoc.build_bibdoc_list()
     self.failIf("new_name" in my_bibrecdoc.get_bibdoc_names())
     self.failUnless(my_bibrecdoc.get_bibdoc_names())
    def test_BibDocs(self):
        """bibdocfile - BibDocs functions"""
        #add file
        my_bibrecdoc = BibRecDocs(2)
        timestamp1 = datetime(
            *(time.strptime("2011-10-09 08:07:06", "%Y-%m-%d %H:%M:%S")[:6]))
        my_bibrecdoc.add_new_file(CFG_PREFIX + '/lib/webtest/invenio/test.jpg',
                                  'Main',
                                  'img_test',
                                  False,
                                  'test add new file',
                                  'test',
                                  '.jpg',
                                  modification_date=timestamp1)
        my_new_bibdoc = my_bibrecdoc.get_bibdoc("img_test")
        value = my_bibrecdoc.list_bibdocs()
        self.assertEqual(len(value), 2)
        #get total file (bibdoc)
        self.assertEqual(my_new_bibdoc.get_total_size(), 91750)
        #get recid
        self.assertEqual(my_new_bibdoc.bibrec_links[0]["recid"], 2)
        #change name
        my_new_bibdoc.change_name(2, 'new_name')
        #get docname
        my_bibrecdoc = BibRecDocs(2)
        self.assertEqual(my_bibrecdoc.get_docname(my_new_bibdoc.id),
                         'new_name')
        #get type
        self.assertEqual(my_new_bibdoc.get_type(), 'Main')
        #get id
        self.assert_(my_new_bibdoc.get_id() > 80)
        #set status
        my_new_bibdoc.set_status('new status')
        #get status
        self.assertEqual(my_new_bibdoc.get_status(), 'new status')
        #get base directory
        self.assert_(
            my_new_bibdoc.get_base_dir().startswith(CFG_BIBDOCFILE_FILEDIR))
        #get file number
        self.assertEqual(my_new_bibdoc.get_file_number(), 1)
        #add file new version
        timestamp2 = datetime(
            *(time.strptime("2010-09-08 07:06:05", "%Y-%m-%d %H:%M:%S")[:6]))
        my_new_bibdoc.add_file_new_version(CFG_PREFIX +
                                           '/lib/webtest/invenio/test.jpg',
                                           description='the new version',
                                           comment=None,
                                           docformat=None,
                                           flags=["PERFORM_HIDE_PREVIOUS"],
                                           modification_date=timestamp2)
        self.assertEqual(my_new_bibdoc.list_versions(), [1, 2])
        #revert
        timestamp3 = datetime.now()
        time.sleep(
            2
        )  # so we can see a difference between now() and the time of the revert
        my_new_bibdoc.revert(1)
        self.assertEqual(my_new_bibdoc.list_versions(), [1, 2, 3])
        self.assertEqual(my_new_bibdoc.get_description('.jpg', version=3),
                         'test add new file')
        #get total size latest version
        self.assertEqual(my_new_bibdoc.get_total_size_latest_version(), 91750)
        #get latest version
        self.assertEqual(my_new_bibdoc.get_latest_version(), 3)
        #list latest files
        self.assertEqual(len(my_new_bibdoc.list_latest_files()), 1)
        self.assertEqual(my_new_bibdoc.list_latest_files()[0].get_version(), 3)
        #list version files
        self.assertEqual(
            len(my_new_bibdoc.list_version_files(1, list_hidden=True)), 1)
        #display # No Display facility inside of an object !
        #        value = my_new_bibdoc.display(version='', ln='en', display_hidden=True)
        #        self.assert_('>test add new file<' in value)
        #format already exist
        self.assertEqual(my_new_bibdoc.format_already_exists_p('.jpg'), True)
        #get file
        self.assertEqual(
            my_new_bibdoc.get_file('.jpg', version='1').get_version(), 1)
        #set description
        my_new_bibdoc.set_description('new description', '.jpg', version=1)
        #get description
        self.assertEqual(my_new_bibdoc.get_description('.jpg', version=1),
                         'new description')
        #set comment
        my_new_bibdoc.set_description('new comment', '.jpg', version=1)
        #get comment
        self.assertEqual(my_new_bibdoc.get_description('.jpg', version=1),
                         'new comment')
        #get history
        assert len(my_new_bibdoc.get_history()) > 0
        #check modification date
        self.assertEqual(
            my_new_bibdoc.get_file('.jpg', version=1).md, timestamp1)
        self.assertEqual(
            my_new_bibdoc.get_file('.jpg', version=2).md, timestamp2)
        assert my_new_bibdoc.get_file('.jpg', version=3).md > timestamp3
        #delete file
        my_new_bibdoc.delete_file('.jpg', 2)
        #list all files
        self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
        #delete file
        my_new_bibdoc.delete_file('.jpg', 3)
        #add new format
        timestamp4 = datetime(
            *(time.strptime("2012-11-10 09:08:07", "%Y-%m-%d %H:%M:%S")[:6]))
        my_new_bibdoc.add_file_new_format(CFG_PREFIX +
                                          '/lib/webtest/invenio/test.gif',
                                          version=None,
                                          description=None,
                                          comment=None,
                                          docformat=None,
                                          modification_date=timestamp4)
        self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
        #check modification time
        self.assertEqual(
            my_new_bibdoc.get_file('.jpg', version=1).md, timestamp1)
        self.assertEqual(
            my_new_bibdoc.get_file('.gif', version=1).md, timestamp4)
        #change the format name
        my_new_bibdoc.change_docformat('.gif', '.gif;icon-640')
        self.assertEqual(my_new_bibdoc.format_already_exists_p('.gif'), False)
        self.assertEqual(
            my_new_bibdoc.format_already_exists_p('.gif;icon-640'), True)
        #delete file
        my_new_bibdoc.delete_file('.jpg', 1)
        #delete file
        my_new_bibdoc.delete_file('.gif;icon-640', 1)
        #empty bibdoc
        self.assertEqual(my_new_bibdoc.empty_p(), True)
        #hidden?
        self.assertEqual(my_new_bibdoc.hidden_p('.jpg', version=1), False)
        #hide
        my_new_bibdoc.set_flag('HIDDEN', '.jpg', version=1)
        #hidden?
        self.assertEqual(my_new_bibdoc.hidden_p('.jpg', version=1), True)
        #add and get icon

        my_new_bibdoc.add_icon(CFG_PREFIX +
                               '/lib/webtest/invenio/icon-test.gif',
                               modification_date=timestamp4)

        my_bibrecdoc = BibRecDocs(2)
        value = my_bibrecdoc.get_bibdoc("new_name")
        self.assertEqual(value.get_icon().docid,
                         my_new_bibdoc.get_icon().docid)
        self.assertEqual(value.get_icon().version,
                         my_new_bibdoc.get_icon().version)
        self.assertEqual(value.get_icon().format,
                         my_new_bibdoc.get_icon().format)

        #check modification time
        self.assertEqual(my_new_bibdoc.get_icon().md, timestamp4)
        #delete icon
        my_new_bibdoc.delete_icon()
        #get icon
        self.assertEqual(my_new_bibdoc.get_icon(), None)
        #delete
        my_new_bibdoc.delete()
        self.assertEqual(my_new_bibdoc.deleted_p(), True)
        #undelete
        my_new_bibdoc.undelete(previous_status='', recid=2)
        #expunging
        my_new_bibdoc.expunge()
        my_bibrecdoc.build_bibdoc_list()
        self.failIf('new_name' in my_bibrecdoc.get_bibdoc_names())
        self.failUnless(my_bibrecdoc.get_bibdoc_names())
def Move_Photos_to_Storage(parameters, curdir, form, user_info=None):
    """
    The function moves files received from the submission's form
    through the PHOTO_MANAGER element and its asynchronous uploads at
    CFG_SITE_URL/submit/uploadfile.

    Parameters:
        @iconsize - Seperate multiple sizes with commas. The ImageMagick geometry inputs are supported.
              Use type 'geometry' as defined in ImageMagick.
              (eg. 320 or 320x240 or 100> or 5%)
              Example: "180>,700>" will create two icons, one with maximum dimension 180px, one 700px
        @iconformat - Allowed extensions (as defined in websubmit_icon_creator.py) are:
                "pdf", "gif", "jpg",
                "jpeg", "ps", "png", "bmp"
                "eps", "epsi", "epsf"

    The PHOTO_MANAGER elements builds the following file organization
    in the directory curdir::

                                     curdir/
                                        |
         ______________________________________________________________________
        |                                   |                                  |
      files/                         PHOTO_MANAGER_ICONS                     icons/
        |                            PHOTO_MANAGER_ORDER                       |
     (user id)/                      PHOTO_MANAGER_DELETE                  (user id)/
        |                            PHOTO_MANAGER_NEW                         |
     NewFile/                        PHOTO_MANAGER_DESCRIPTION_X           NewFile/
        |                                                                      |
        _______________________                                      _____________________
       |            |          |                                    |          |          |
     photo1.jpg  myPhoto.gif   ...                             photo1.jpg  myPhoto.gif   ...


    where the files are:
      - PHOTO_MANAGER_ORDER: ordered list of file IDs. One per line.

      - PHOTO_MANAGER_ICONS: mappings from file IDs to URL of the icons.
                             One per line. Separator: /

      - PHOTO_MANAGER_NEW: mapping from file ID to filename on disk. Only
                           applicable to files that have just been
                           uploaded (i.e. not bibdocfiles). One per
                           line. Separator: /

      - PHOTO_MANAGER_DELETE: list of files IDs that must be deleted. One
                               per line

      - PHOTO_MANAGER_DESCRIPTION_X, where X is file ID: contains photos
                                     descriptions (one per file)

    """
    global sysno

    icon_sizes = parameters.get('iconsize').split(',')
    icon_format = parameters.get('iconformat')
    if not icon_format:
        icon_format = 'gif'

    PHOTO_MANAGER_ICONS = read_param_file(curdir, 'PHOTO_MANAGER_ICONS', split_lines=True)
    photo_manager_icons_dict = dict([value.split('/', 1) \
                                     for value in PHOTO_MANAGER_ICONS \
                                     if '/' in value])
    PHOTO_MANAGER_ORDER = read_param_file(curdir, 'PHOTO_MANAGER_ORDER', split_lines=True)
    photo_manager_order_list = [value for value in PHOTO_MANAGER_ORDER if value.strip()]
    PHOTO_MANAGER_DELETE = read_param_file(curdir, 'PHOTO_MANAGER_DELETE', split_lines=True)
    photo_manager_delete_list = [value for value in PHOTO_MANAGER_DELETE if value.strip()]
    PHOTO_MANAGER_NEW = read_param_file(curdir, 'PHOTO_MANAGER_NEW', split_lines=True)
    photo_manager_new_dict = dict([value.split('/', 1) \
                               for value in PHOTO_MANAGER_NEW \
                               if '/' in value])

    ## Create an instance of BibRecDocs for the current recid(sysno)
    bibrecdocs = BibRecDocs(sysno)
    for photo_id in photo_manager_order_list:
        photo_description = read_param_file(curdir, 'PHOTO_MANAGER_DESCRIPTION_' + photo_id)
        # We must take different actions depending if we deal with a
        # file that already exists, or if it is a new file
        if photo_id in photo_manager_new_dict.keys():
            # New file
            if photo_id not in photo_manager_delete_list:
                filename = photo_manager_new_dict[photo_id]
                filepath = os.path.join(curdir, 'files', str(user_info['uid']),
                                        'NewFile', filename)
                icon_filename = os.path.splitext(filename)[0] + ".gif"
                fileiconpath = os.path.join(curdir, 'icons', str(user_info['uid']),
                                            'NewFile', icon_filename)

                # Add the file
                if os.path.exists(filepath):
                    _do_log(curdir, "Adding file %s" % filepath)
                    bibdoc = bibrecdocs.add_new_file(filepath, doctype="picture", never_fail=True)
                    has_added_default_icon_subformat_p = False
                    for icon_size in icon_sizes:
                        # Create icon if needed
                        try:
                            (icon_path, icon_name) = create_icon(
                                { 'input-file'           : filepath,
                                  'icon-name'            : icon_filename,
                                  'icon-file-format'     : icon_format,
                                  'multipage-icon'       : False,
                                  'multipage-icon-delay' : 100,
                                  'icon-scale'           : icon_size, # Resize only if width > 300
                                  'verbosity'            : 0,
                                  })
                            fileiconpath = os.path.join(icon_path, icon_name)
                        except InvenioWebSubmitIconCreatorError, e:
                            _do_log(curdir, "Icon could not be created to %s: %s" % (filepath, e))
                            pass
                        if os.path.exists(fileiconpath):
                            try:
                                if not has_added_default_icon_subformat_p:
                                    bibdoc.add_icon(fileiconpath)
                                    has_added_default_icon_subformat_p = True
                                    _do_log(curdir, "Added icon %s" % fileiconpath)
                                else:
                                    icon_suffix = icon_size.replace('>', '').replace('<', '').replace('^', '').replace('!', '')
                                    bibdoc.add_icon(fileiconpath, subformat=CFG_BIBDOCFILE_DEFAULT_ICON_SUBFORMAT + "-" + icon_suffix)
                                    _do_log(curdir, "Added icon %s" % fileiconpath)
                            except InvenioBibDocFileError, e:
                                # Most probably icon already existed.
                                pass

                    if photo_description and bibdoc:
                        for file_format in [bibdocfile.get_format() \
                                       for bibdocfile in bibdoc.list_latest_files()]:
                            bibdoc.set_comment(photo_description, file_format)
                            _do_log(curdir, "Added comment %s" % photo_description)
 def test_BibDocs(self):
     """bibdocfile - BibDocs functions"""
     #add file
     my_bibrecdoc = BibRecDocs(2)
     my_bibrecdoc.add_new_file(CFG_PREFIX + '/lib/webtest/invenio/test.jpg',
                               'Main', 'img_test', False,
                               'test add new file', 'test', '.jpg')
     my_new_bibdoc = my_bibrecdoc.get_bibdoc("img_test")
     value = my_bibrecdoc.list_bibdocs()
     self.assertEqual(len(value), 2)
     #get total file (bibdoc)
     self.assertEqual(my_new_bibdoc.get_total_size(), 91750)
     #get recid
     self.assertEqual(my_new_bibdoc.get_recid(), 2)
     #change name
     my_new_bibdoc.change_name('new_name')
     #get docname
     self.assertEqual(my_new_bibdoc.get_docname(), 'new_name')
     #get type
     self.assertEqual(my_new_bibdoc.get_type(), 'Main')
     #get id
     self.assert_(my_new_bibdoc.get_id() > 80)
     #set status
     my_new_bibdoc.set_status('new status')
     #get status
     self.assertEqual(my_new_bibdoc.get_status(), 'new status')
     #get base directory
     self.assert_(
         my_new_bibdoc.get_base_dir().startswith(CFG_WEBSUBMIT_FILEDIR))
     #get file number
     self.assertEqual(my_new_bibdoc.get_file_number(), 1)
     #add file new version
     my_new_bibdoc.add_file_new_version(CFG_PREFIX +
                                        '/lib/webtest/invenio/test.jpg',
                                        description='the new version',
                                        comment=None,
                                        format=None,
                                        flags=["PERFORM_HIDE_PREVIOUS"])
     self.assertEqual(my_new_bibdoc.list_versions(), [1, 2])
     #revert
     my_new_bibdoc.revert(1)
     self.assertEqual(my_new_bibdoc.list_versions(), [1, 2, 3])
     self.assertEqual(my_new_bibdoc.get_description('.jpg', version=3),
                      'test add new file')
     #get total size latest version
     self.assertEqual(my_new_bibdoc.get_total_size_latest_version(), 91750)
     #get latest version
     self.assertEqual(my_new_bibdoc.get_latest_version(), 3)
     #list latest files
     self.assertEqual(len(my_new_bibdoc.list_latest_files()), 1)
     self.assertEqual(my_new_bibdoc.list_latest_files()[0].get_version(), 3)
     #list version files
     self.assertEqual(
         len(my_new_bibdoc.list_version_files(1, list_hidden=True)), 1)
     #display
     value = my_new_bibdoc.display(version='', ln='en', display_hidden=True)
     self.assert_('>test add new file<' in value)
     #format already exist
     self.assertEqual(my_new_bibdoc.format_already_exists_p('.jpg'), True)
     #get file
     self.assertEqual(
         my_new_bibdoc.get_file('.jpg', version='1').get_version(), 1)
     #set description
     my_new_bibdoc.set_description('new description', '.jpg', version=1)
     #get description
     self.assertEqual(my_new_bibdoc.get_description('.jpg', version=1),
                      'new description')
     #set comment
     my_new_bibdoc.set_description('new comment', '.jpg', version=1)
     #get comment
     self.assertEqual(my_new_bibdoc.get_description('.jpg', version=1),
                      'new comment')
     #get history
     assert len(my_new_bibdoc.get_history()) > 0
     #delete file
     my_new_bibdoc.delete_file('.jpg', 2)
     #list all files
     self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
     #delete file
     my_new_bibdoc.delete_file('.jpg', 3)
     #add new format
     my_new_bibdoc.add_file_new_format(CFG_PREFIX +
                                       '/lib/webtest/invenio/test.gif',
                                       version=None,
                                       description=None,
                                       comment=None,
                                       format=None)
     self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
     #delete file
     my_new_bibdoc.delete_file('.jpg', 1)
     #delete file
     my_new_bibdoc.delete_file('.gif', 1)
     #empty bibdoc
     self.assertEqual(my_new_bibdoc.empty_p(), True)
     #hidden?
     self.assertEqual(my_new_bibdoc.hidden_p('.jpg', version=1), False)
     #hide
     my_new_bibdoc.set_flag('HIDDEN', '.jpg', version=1)
     #hidden?
     self.assertEqual(my_new_bibdoc.hidden_p('.jpg', version=1), True)
     #add and get icon
     my_new_bibdoc.add_icon(CFG_PREFIX +
                            '/lib/webtest/invenio/icon-test.gif')
     value = my_bibrecdoc.list_bibdocs()[1]
     self.assertEqual(value.get_icon(), my_new_bibdoc.get_icon())
     #delete icon
     my_new_bibdoc.delete_icon()
     #get icon
     self.assertEqual(my_new_bibdoc.get_icon(), None)
     #delete
     my_new_bibdoc.delete()
     self.assertEqual(my_new_bibdoc.deleted_p(), True)
     #undelete
     my_new_bibdoc.undelete(previous_status='')
    def test_BibDocFiles(self):
        """bibdocfile - BibDocFile functions """
        #add bibdoc
        my_bibrecdoc = BibRecDocs(2)
        timestamp = datetime(
            *(time.strptime("2010-09-08 07:06:05", "%Y-%m-%d %H:%M:%S")[:6]))
        my_bibrecdoc.add_new_file(CFG_PREFIX + '/lib/webtest/invenio/test.jpg',
                                  'Main',
                                  'img_test',
                                  False,
                                  'test add new file',
                                  'test',
                                  '.jpg',
                                  modification_date=timestamp)

        my_new_bibdoc = my_bibrecdoc.get_bibdoc("img_test")
        my_new_bibdocfile = my_new_bibdoc.list_all_files()[0]
        #get url
        self.assertEqual(
            my_new_bibdocfile.get_url(),
            CFG_SITE_URL + '/%s/2/files/img_test.jpg' % CFG_SITE_RECORD)
        #get type
        self.assertEqual(my_new_bibdocfile.get_type(), 'Main')
        #get path
        # we should not test for particular path ! this is in the gestion of the underlying implementation,
        # not the interface which should ne tested
        #        self.assert_(my_new_bibdocfile.get_path().startswith(CFG_BIBDOCFILE_FILEDIR))
        #        self.assert_(my_new_bibdocfile.get_path().endswith('/img_test.jpg;1'))
        #get bibdocid
        self.assertEqual(my_new_bibdocfile.get_bibdocid(),
                         my_new_bibdoc.get_id())
        #get name
        self.assertEqual(my_new_bibdocfile.get_name(), 'img_test')
        #get full name
        self.assertEqual(my_new_bibdocfile.get_full_name(), 'img_test.jpg')
        #get full path
        #self.assert_(my_new_bibdocfile.get_full_path().startswith(CFG_BIBDOCFILE_FILEDIR))
        #self.assert_(my_new_bibdocfile.get_full_path().endswith('/img_test.jpg;1'))
        #get format
        self.assertEqual(my_new_bibdocfile.get_format(), '.jpg')
        #get version
        self.assertEqual(my_new_bibdocfile.get_version(), 1)
        #get description
        self.assertEqual(my_new_bibdocfile.get_description(),
                         my_new_bibdoc.get_description('.jpg', version=1))
        #get comment
        self.assertEqual(my_new_bibdocfile.get_comment(),
                         my_new_bibdoc.get_comment('.jpg', version=1))
        #get recid
        self.assertEqual(my_new_bibdocfile.get_recid(), 2)
        #get status
        self.assertEqual(my_new_bibdocfile.get_status(), '')
        #get size
        self.assertEqual(my_new_bibdocfile.get_size(), 91750)
        #get checksum
        self.assertEqual(my_new_bibdocfile.get_checksum(),
                         '28ec893f9da735ad65de544f71d4ad76')
        #check
        self.assertEqual(my_new_bibdocfile.check(), True)
        #display
        tmpl = invenio.template.load("bibdocfile")
        value = tmpl.tmpl_display_bibdocfile(my_new_bibdocfile, ln='en')
        assert 'files/img_test.jpg?version=1">' in value
        #hidden?
        self.assertEqual(my_new_bibdocfile.hidden_p(), False)
        #check modification date
        self.assertEqual(my_new_bibdocfile.md, timestamp)
        #delete
        my_new_bibdoc.delete()
        self.assertEqual(my_new_bibdoc.deleted_p(), True)
    def test_BibDocs(self):
        """bibdocfile - BibDocs functions"""
        #add file
        my_bibrecdoc = BibRecDocs(2)
        timestamp1 = datetime(*(time.strptime("2011-10-09 08:07:06", "%Y-%m-%d %H:%M:%S")[:6]))
        my_bibrecdoc.add_new_file(CFG_PREFIX + '/lib/webtest/invenio/test.jpg', 'Main', 'img_test', False, 'test add new file', 'test', '.jpg', modification_date=timestamp1)
        my_new_bibdoc = my_bibrecdoc.get_bibdoc("img_test")
        value = my_bibrecdoc.list_bibdocs()
        self.assertEqual(len(value), 2)
        #get total file (bibdoc)
        self.assertEqual(my_new_bibdoc.get_total_size(), 91750)
        #get recid
        self.assertEqual(my_new_bibdoc.bibrec_links[0]["recid"], 2)
        #change name
        my_new_bibdoc.change_name(2, 'new_name')
        #get docname
        my_bibrecdoc = BibRecDocs(2)
        self.assertEqual(my_bibrecdoc.get_docname(my_new_bibdoc.id), 'new_name')
        #get type
        self.assertEqual(my_new_bibdoc.get_type(), 'Main')
        #get id
        self.assert_(my_new_bibdoc.get_id() > 80)
        #set status
        my_new_bibdoc.set_status('new status')
        #get status
        self.assertEqual(my_new_bibdoc.get_status(), 'new status')
        #get base directory
        self.assert_(my_new_bibdoc.get_base_dir().startswith(CFG_BIBDOCFILE_FILEDIR))
        #get file number
        self.assertEqual(my_new_bibdoc.get_file_number(), 1)
        #add file new version
        timestamp2 = datetime(*(time.strptime("2010-09-08 07:06:05", "%Y-%m-%d %H:%M:%S")[:6]))
        my_new_bibdoc.add_file_new_version(CFG_PREFIX + '/lib/webtest/invenio/test.jpg', description= 'the new version', comment=None, docformat=None, flags=["PERFORM_HIDE_PREVIOUS"], modification_date=timestamp2)
        self.assertEqual(my_new_bibdoc.list_versions(), [1, 2])
        #revert
        timestamp3 = datetime.now()
        time.sleep(2) # so we can see a difference between now() and the time of the revert
        my_new_bibdoc.revert(1)
        self.assertEqual(my_new_bibdoc.list_versions(), [1, 2, 3])
        self.assertEqual(my_new_bibdoc.get_description('.jpg', version=3), 'test add new file')
        #get total size latest version
        self.assertEqual(my_new_bibdoc.get_total_size_latest_version(), 91750)
        #get latest version
        self.assertEqual(my_new_bibdoc.get_latest_version(), 3)
        #list latest files
        self.assertEqual(len(my_new_bibdoc.list_latest_files()), 1)
        self.assertEqual(my_new_bibdoc.list_latest_files()[0].get_version(), 3)
        #list version files
        self.assertEqual(len(my_new_bibdoc.list_version_files(1, list_hidden=True)), 1)
        #display # No Display facility inside of an object !
#        value = my_new_bibdoc.display(version='', ln='en', display_hidden=True)
#        self.assert_('>test add new file<' in value)
        #format already exist
        self.assertEqual(my_new_bibdoc.format_already_exists_p('.jpg'), True)
        #get file
        self.assertEqual(my_new_bibdoc.get_file('.jpg', version='1').get_version(), 1)
        #set description
        my_new_bibdoc.set_description('new description', '.jpg', version=1)
        #get description
        self.assertEqual(my_new_bibdoc.get_description('.jpg', version=1), 'new description')
        #set comment
        my_new_bibdoc.set_description('new comment', '.jpg', version=1)
        #get comment
        self.assertEqual(my_new_bibdoc.get_description('.jpg', version=1), 'new comment')
        #get history
        assert len(my_new_bibdoc.get_history()) > 0
        #check modification date
        self.assertEqual(my_new_bibdoc.get_file('.jpg', version=1).md, timestamp1)
        self.assertEqual(my_new_bibdoc.get_file('.jpg', version=2).md, timestamp2)
        assert my_new_bibdoc.get_file('.jpg', version=3).md > timestamp3
        #delete file
        my_new_bibdoc.delete_file('.jpg', 2)
        #list all files
        self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
        #delete file
        my_new_bibdoc.delete_file('.jpg', 3)
        #add new format
        timestamp4 = datetime(*(time.strptime("2012-11-10 09:08:07", "%Y-%m-%d %H:%M:%S")[:6]))
        my_new_bibdoc.add_file_new_format(CFG_PREFIX + '/lib/webtest/invenio/test.gif', version=None, description=None, comment=None, docformat=None, modification_date=timestamp4)
        self.assertEqual(len(my_new_bibdoc.list_all_files()), 2)
        #check modification time
        self.assertEqual(my_new_bibdoc.get_file('.jpg', version=1).md, timestamp1)
        self.assertEqual(my_new_bibdoc.get_file('.gif', version=1).md, timestamp4)
        #change the format name
        my_new_bibdoc.change_docformat('.gif', '.gif;icon-640')
        self.assertEqual(my_new_bibdoc.format_already_exists_p('.gif'), False)
        self.assertEqual(my_new_bibdoc.format_already_exists_p('.gif;icon-640'), True)
        #delete file
        my_new_bibdoc.delete_file('.jpg', 1)
        #delete file
        my_new_bibdoc.delete_file('.gif;icon-640', 1)
        #empty bibdoc
        self.assertEqual(my_new_bibdoc.empty_p(), True)
        #hidden?
        self.assertEqual(my_new_bibdoc.hidden_p('.jpg', version=1), False)
        #hide
        my_new_bibdoc.set_flag('HIDDEN', '.jpg', version=1)
        #hidden?
        self.assertEqual(my_new_bibdoc.hidden_p('.jpg', version=1), True)
        #add and get icon

        my_new_bibdoc.add_icon( CFG_PREFIX + '/lib/webtest/invenio/icon-test.gif', modification_date=timestamp4)

        my_bibrecdoc = BibRecDocs(2)
        value =  my_bibrecdoc.get_bibdoc("new_name")
        self.assertEqual(value.get_icon().docid, my_new_bibdoc.get_icon().docid)
        self.assertEqual(value.get_icon().version, my_new_bibdoc.get_icon().version)
        self.assertEqual(value.get_icon().format, my_new_bibdoc.get_icon().format)

        #check modification time
        self.assertEqual(my_new_bibdoc.get_icon().md, timestamp4)
        #delete icon
        my_new_bibdoc.delete_icon()
        #get icon
        self.assertEqual(my_new_bibdoc.get_icon(), None)
        #delete
        my_new_bibdoc.delete()
        self.assertEqual(my_new_bibdoc.deleted_p(), True)
        #undelete
        my_new_bibdoc.undelete(previous_status='', recid=2)
        #expunging
        my_new_bibdoc.expunge()
        my_bibrecdoc.build_bibdoc_list()
        self.failIf('new_name' in my_bibrecdoc.get_bibdoc_names())
        self.failUnless(my_bibrecdoc.get_bibdoc_names())