def generate_common_names(idx, l): for d in l: cn = CommonName.get_or_create(d['name'], idx) db.session.add(cn) cn.list_as = d['list_as'] cn.slug = d['slug'] cn.subtitle = d['subtitle'] cn.sunlight = d['sunlight'] cn.thumbnail = download_image(d['thumb_url']) cn.botanical_names = d['botanical_names'] cn.description = d['description'] cn.instructions = d['instructions'] if not cn.cultivars: cn.cultivars = [] for cv in generate_cultivars(cn, d['cultivars']): if cv not in cn.cultivars: cn.cultivars.append(cv) if not cn.sections: cn.sections = [] for s in generate_sections(cn, d['sections']): if s not in cn.sections: cn.sections.append(s) db.session.flush() cn.child_sections.reorder() cn.child_cultivars.reorder() yield cn
def test_get_or_create_get(self, db): cn = CommonName(name='Foxglove', index=Index(name='Perennial')) db.session.add(cn) db.session.commit() assert CommonName.get_or_create(name='Foxglove', index='Perennial') is cn assert not cn.created
def test_get_or_create_create_with_existing_index(self, db): idx = Index(name='Perennial') db.session.add(idx) db.session.commit() cn = CommonName.get_or_create(name='Foxglove', index='Perennial') assert cn.created assert cn.index is idx assert not idx.created
def save_row_to_db(self, row, stream=sys.stdout): """Save a row from the Common Names sheet 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. """ cn_json = self.cell(row, self.cols['Common Name (JSON)']).value cn_dict = json.loads(cn_json) section = dbify(self.cell(row, self.cols['Section']).value) description = self.cell(row, self.cols['Description']).value print('-- BEGIN editing/creating Section \'{0}\' from row #{1}. ' '--'.format(section, row), file=stream) edited = False cn = CommonName.get_or_create(name=dbify(cn_dict['Common Name']), index=dbify(cn_dict['Index']), stream=stream) sec = None if not cn.created: sec = Section.query\ .filter(Section.name == section, Section.common_name_id == cn.id)\ .one_or_none() if sec: print('The Section \'{0}\' has been loaded from the database.' .format(sec.name), file=stream) else: edited = True sec = Section(name=section) sec.common_name = cn print('CommonName for the Section \'{0}\' set to: {1}' .format(sec.name, cn.name), file=stream) db.session.add(sec) print('The Section \'{0}\' does not yet exist in the database, ' 'so it has been created.'.format(sec.name), file=stream) if description != sec.description: edited = True if description: sec.description = description print('Description for the Section \'{0}\' set to: {1}' .format(sec.name, sec.description), file=stream) else: sec.description = None print('Description for the Section \'{0}\' has been cleared.' .format(sec.name), file=stream) if edited: db.session.flush() print('Changes to the Section \'{0}\' have been flushed to ' 'the database.'.format(sec.name), file=stream) else: print('No changes were made to the Section \'{0}\'.' .format(sec.name), file=stream) print('-- END editing/creating Section \'{0}\' from row #{1}. ' '--'.format(sec.name, row), file=stream) return edited
def save_row_to_db(self, row, stream=sys.stdout): """Save a row from the Botanical Names sheet 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. """ botanical_name = self.cell(row, self.cols['Botanical Name']).value cn_json = self.cell(row, self.cols['Common Names (JSON)']).value cn_dicts = json.loads(cn_json) synonyms = self.cell(row, self.cols['Synonyms']).value if not synonyms: synonyms = '' if not BotanicalName.validate(botanical_name): print('Could not add the BotanicalName \'{0}\' because it does ' 'not appear to be a validly formatted botanical name.' .format(botanical_name), file=stream) return False print('-- BEGIN editing/creating BotanicalName \'{0}\' from row #{1}. ' '--'.format(botanical_name, row), file=stream) edited = False bn = BotanicalName.query\ .filter(BotanicalName.name == botanical_name)\ .one_or_none() if bn: print('The BotanicalName \'{0}\' has been loaded from the ' 'database.'.format(bn.name), file=stream) else: edited = True bn = BotanicalName(name=botanical_name) db.session.add(bn) print('The BotanicalName \'{0}\' does not yet exist in the ' 'database, so it has been created.'.format(bn.name), file=stream) cns = tuple(CommonName.get_or_create( name=dbify(d['Common Name']), index=dbify(d['Index']), stream=stream ) for d in cn_dicts) for cn in cns: if cn not in bn.common_names: edited = True bn.common_names.append(cn) print('The CommonName \'{0}\' has been added to CommonNames ' 'for the BotanicalName \'{1}\'.' .format(cn.name, bn.name), file=stream) for cn in list(bn.common_names): if cn not in cns: edited = True bn.common_names.remove(cn) print('The CommonName \'{0}\' has been removed from ' 'CommonNames for the BotanicalName \'{1}\'.' .format(cn.name, bn.name), file=stream) if synonyms != bn.synonyms_string: edited = True bn.synonyms_string = synonyms if synonyms: print('Synonyms for the BotanicalName \'{0}\' set to: {1}' .format(bn.name, bn.synonyms_string), file=stream) else: print('Synonyms for the BotanicalName \'{0}\' have been ' 'cleared.'.format(bn.name), file=stream) if edited: db.session.flush() print('Changes to the BotanicalName \'{0}\' have been flushed to ' 'the database.'.format(bn.name), file=stream) else: print('No changes were made to the BotanicalName \'{0}\'.' .format(bn.name), file=stream) print('-- END editing/creating BotanicalName \'{0}\' from row #{1}. ' '--'.format(bn.name, row), file=stream) return edited
def save_row_to_db(self, row, stream=sys.stdout): """Save a row from the Common Names sheet 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. """ index = dbify(self.cell(row, self.cols['Index']).value) name = dbify(self.cell(row, self.cols['Common Name']).value) description = self.cell(row, self.cols['Description']).value instructions = self.cell(row, self.cols['Planting Instructions']).value synonyms = self.cell(row, self.cols['Synonyms']).value if not synonyms: synonyms = '' # Match result of CommonName.synonyms_string vis = self.cell(row, self.cols['Visible']).value if vis and 'true' in vis.lower(): visible = True else: visible = False print('-- BEGIN editing/creating CommonName \'{0}\' from row #{1}. --' .format(name, row), file=stream) edited = False cn = CommonName.get_or_create(name=name, index=index, stream=stream) if cn.created: edited = True db.session.add(cn) if description != cn.description: edited = True if description: cn.description = description print('Description for the CommonName \'{0}\' set to: {1}' .format(cn.name, cn.description), file=stream) elif cn.description: cn.description = None print('Description for the CommonName \'{0}\' has been ' 'cleared.'.format(cn.name), file=stream) if instructions != cn.instructions: edited = True if instructions: cn.instructions = instructions print('Planting instructions for the CommonName \'{0}\' set ' 'to: {1}'.format(cn.name, cn.instructions), file=stream) elif cn.instructions: cn.instructions = None print('Planting instructions for the CommonName \'{0}\' have ' 'been cleared.'.format(cn.name), file=stream) if synonyms != cn.synonyms_string: edited = True cn.synonyms_string = synonyms if synonyms: print('Synonyms for the CommonName \'{0}\' set to: {1}' .format(cn.name, cn.synonyms_string), file=stream) else: print('Synonyms for the CommonName \'{0}\' have been cleared.' .format(cn.name), file=stream) if visible != cn.visible: edited = True cn.visible = visible if cn.visible: print('The CommonName \'{0}\' is visible on generated ' 'pages.'.format(cn.name), file=stream) else: print('The CommonName \'{0}\' is not visible on generated ' 'pages.'.format(cn.name), file=stream) if edited: db.session.flush() print('Changes to the CommonName \'{0}\' have been flushed to the ' 'database.'.format(cn.name), file=stream) else: print('No changes were made to the CommonName \'{0}\'.' .format(cn.name), file=stream) print('-- END editing/creating CommonName \'{0}\' from row #{1}. --' .format(cn.name, row), file=stream) return edited
def test_get_or_create_create_all(self, db): cn = CommonName.get_or_create(name='Foxglove', index='Perennial') assert cn.created assert cn.index.created