def test_get_or_create_get(self, db): """Load Index if it exists.""" idx = Index(name='Finger') db.session.add(idx) db.session.commit() assert Index.get_or_create(name='Finger') is idx assert not idx.created
def save_row_to_db(self, row, stream=sys.stdout): """Save a row representing in Index to the database. Args: row: The number of the row to save. stream: Optional IO stream to print messages to. Returns: bool: `True` if changes have been made, `False` if not. """ name = dbify(self.cell(row, self.cols['Index']).value) description = self.cell(row, self.cols['Description']).value print('-- BEGIN editing/creating Index \'{0}\' from row #{1}. --' .format(name, row), file=stream) edited = False idx = Index.get_or_create(name=name, stream=stream) if idx.created: edited = True db.session.add(idx) if description != idx.description: edited = True if description: idx.description = description print('Description for the Index \'{0}\' set to: {1}' .format(idx.name, idx.description), file=stream) elif idx.description: idx.description = None print('Description for the Index \'{0}\' has been cleared.' .format(idx.name), file=stream) if edited: db.session.commit() print('Changes to Index \'{0}\' have been flushed to the database.' .format(idx.name), file=stream) else: print('No changes were made to the Index \'{0}\'.' .format(idx.name), file=stream) print('-- END editing/creating Index \'{0}\' from row #{1}. --' .format(idx.name, row), file=stream) return edited
def add_index_to_database(d): print('Adding index {} to the database...'.format(d['name'])) idx = Index.get_or_create(d['name']) db.session.add(idx) db.session.flush() idx.slug = d['slug'] idx.description = d['description'] if 'annual' in idx.slug: idx.thumbnail = download_image( 'https://www.swallowtailgardenseeds.com/images/index-image-links' '/annual-flower-seeds4.jpg' ) elif 'perennial' in idx.slug: idx.thumbnail = download_image( 'https://www.swallowtailgardenseeds.com/images/index-image-links' '/perennial-flower-seeds.jpg' ) elif 'vine' in idx.slug: idx.thumbnail = download_image( 'https://www.swallowtailgardenseeds.com/images/index-image-links' '/flowering-vine-seeds2.jpg' ) elif 'vegetable' in idx.slug: idx.thumbnail = download_image( 'https://www.swallowtailgardenseeds.com/images/index-image-links' '/vegetable-seeds2.jpg' ) elif 'herb' in idx.slug: idx.thumbnail = download_image( 'https://www.swallowtailgardenseeds.com/images/index-image-links' '/herb-seeds2.jpg' ) idx.common_names = list(generate_common_names(idx, d['common_names'])) db.session.flush() idx.common_names = sorted(idx.common_names, key=lambda x: x.list_as) idx.common_names.reorder() db.session.commit() print('Finished adding {} to the database.'.format(d['name']))
def test_get_or_create_create(self, db): """Create Index if it does not exist.""" idx = Index.get_or_create(name='Finger') assert not row_exists(Index.name, 'Finger') assert idx.created