Exemplo n.º 1
0
def upload():
    result = False
    errmsg = ''
    if request.method == 'POST':
        f = request.files['file']
        m = request.form['model']
        if f and allowed_file(f.filename):
            if not os.path.exists(app.config['UPLOAD_FOLDER']):
                os.mkdir(app.config['UPLOAD_FOLDER'])
            path = os.path.join(app.config['UPLOAD_FOLDER'],
                                secure_filename(f.filename))
            f.save(path)
            model = resource.sources.get(m)
            if model is None:
                errmsg = 'model %s not found' % m
            else:
                with open(path, 'rb') as fs:
                    data_book = tablib.import_book(fs.read())
                    model.import_data(data_book)
                    result = True
        else:
            errmsg = 'only xls, xlsx file allowed'
    return render_template('index.html',
                           mappings=mappings,
                           status='success' if result else 'failure',
                           request=request,
                           errmsg=errmsg)
Exemplo n.º 2
0
def import_years(input_xls, accept=False):
    with open(input_xls, 'rb') as f:
        databook = tablib.import_book(f.read())

    for sheet in databook.sheets():
        try:
            year = int(sheet.title)
        except:
            puts(
                red('ignoring sheet because it is not an int: {}'.format(
                    sheet.title)))
            continue

        process_year(sheet, year, accept=accept)
Exemplo n.º 3
0
    def readDocumentsXLSX(self, inFile):
        '''
        Read a tabbed spreadsheet of docshares, recording OWNERS selected by the submittor.
        :param inFile: .xlsx spreadsheet of same format as exported by writeSelectedDocumentsXLSX()
        '''
        # open database connection:
        self.driverOracle.connect()

        # sheet column indexes:
        OWNER = 0
        DOCID = 1

        # load the spreadheet:
        with open(inFile, 'rb') as file:
            data = tablib.import_book(file, format='xlsx', headers=True)
        # loop on tabs:
        for sheet in data.sheets():
            # tab title is the FORUMNAME.  Find it in forums tree:
            forum = self.forums.find(sheet.title)
            if forum:
                # cache the last OWNER we see while loading to assign to the forum
                owner = None
                # load the forum's documents tree:
                if not forum['attrs'].get('docshare', False):
                    print('loadDocshare forum: ' + forum['name'])
                    self.__loadDocshare(forum)
                else:
                    print('Already loaded forum: ' + forum['name'])
                # loop over rows in the sheet:
                for row in sheet:
                    if row[OWNER]:
                        # and assign OWNER:
                        owner = row[OWNER]
                        document = forum['attrs']['docshare'].find(row[DOCID])
                        if document:
                            document['attrs']['OWNER'] = owner
                        else:
                            print('Document not found: ' + row[DOCID])
                # set ownership on the forum as well:
                if owner:
                    forum['attrs']['OWNER'] = owner
            else:
                print('Forum not found: ' + sheet.title)

        # close database connection:
        self.driverOracle.disconnect()
Exemplo n.º 4
0
                else:
                    # Use the internal command itself, if it's not a range
                    name = re.sub(r'\s*Key$', '', range)   # sometimes ends in key, remove
                    name = make_command(name)

            this = data[prefix]['values'][range] = OrderedDict()
            if name:
                this['name'] = name
            this['description'] = desc
            this['models'] = setname

    return data


with open(sys.argv[1], 'r') as f:
    book = tablib.import_book(f.read())


# Model sets collect unique combinations of supported models.
model_sets = OrderedDict()
data = OrderedDict((
    ('main', import_sheet('main', book.sheets()[4], model_sets)),
    ('zone2', import_sheet('zone2', book.sheets()[5], model_sets)),
    ('zone3', import_sheet('zone3', book.sheets()[6], model_sets)),
    ('zone4', import_sheet('zone4', book.sheets()[7], model_sets)),
    ('dock', import_sheet('dock', book.sheets()[8], model_sets)),
))
data['modelsets'] = OrderedDict(zip(model_sets.values(), model_sets.keys()))


Exemplo n.º 5
0
                    # Use the internal command itself, if it's not a range
                    name = re.sub(r'\s*Key$', '',
                                  range)  # sometimes ends in key, remove
                    name = make_command(name)

            this = data[prefix]['values'][range] = OrderedDict()
            if name:
                this['name'] = name
            this['description'] = desc
            this['models'] = setname

    return data


with open(sys.argv[1], 'r') as f:
    book = tablib.import_book(f.read())

# Model sets collect unique combinations of supported models.
model_sets = OrderedDict()
data = OrderedDict((
    ('main', import_sheet('main',
                          book.sheets()[4], model_sets)),
    ('zone2', import_sheet('zone2',
                           book.sheets()[5], model_sets)),
    ('zone3', import_sheet('zone3',
                           book.sheets()[6], model_sets)),
    ('zone4', import_sheet('zone4',
                           book.sheets()[7], model_sets)),
    ('dock', import_sheet('dock',
                          book.sheets()[8], model_sets)),
))
Exemplo n.º 6
0
    def is_file(self, collection_name, key):
        return collection_name in self.file_fields and key in self.file_fields[collection_name] and self.file_fields[collection_name][key]

    def downloaded_file(self, url):
        return self.url_to_file[url] if url in self.url_to_file else None
    
    def add_downloaded_file(self, url, filename):
        self.url_to_file['url'] = filename
        self.downloads.append({'url': url, 'file': filename})

    def upload_collection_multiple_times(self, collection_name):
        return self.multiple_uploads[collection_name] if collection_name in  self.multiple_uploads else 1

#Get the main data
databook = tablib.import_book(inputfile)
data = yaml.load(databook.yaml)
#Get mapping data
mapfile = inputfile.name + ".mapping.xlsx"
if os.path.exists(mapfile):
    previous_output = tablib.import_book(open(mapfile,"rb"))
    previous = yaml.load(previous_output.yaml)
else:
    previous = []    

mapping = XlsxMapping(omeka_client, previous)


id_mapping = []
for d in data:
    collection_name =  d['title']