Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
 def test_dbify(self):
     """Convert a string into a proper titlecase version."""
     assert dbify('stuff') == 'Stuff'
     assert dbify('This is a Title') == 'This Is a Title'
     assert dbify('lowercase stuff') == 'Lowercase Stuff'
     assert dbify('You will forget-me-not') == 'You Will Forget-me-not'
     assert dbify('tears for fears') == 'Tears for Fears'
     assert dbify('ashes to ashes') == 'Ashes to Ashes'
     assert dbify('CRUISE CONTROL FOR COOL') == 'Cruise Control for Cool'
Ejemplo n.º 3
0
 def test_dbify_cb(self):
     """Test special cases handled by the callback function cb in dbify."""
     assert dbify('I II III IV V XP BLBP') == 'I II III IV V XP BLBP'
     assert dbify('THIRTY-THREE') == 'Thirty-three'
     assert dbify('FORM 1040EZ') == 'Form 1040EZ'
     assert dbify('ROYALE W/ CHEESE') == 'Royale w/ Cheese'
     assert dbify('D\'AVIGNON RADISH') == 'd\'Avignon Radish'
     assert dbify('BIRD\'S EYE') == 'Bird\'s Eye'
     assert dbify('O\'HARA') == 'O\'Hara'
Ejemplo n.º 4
0
 def name(self):
     try:
         rawname = next(
             t for t in self.intro.h2.contents if isinstance(t, str) and
             not isinstance (t, Comment) and
             t.strip()
         )
     except AttributeError:
         rawname = ' '.join(
             self.class_.replace('section', '').split('-')
         ).strip()
     return dbify(rawname)
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    def save_row_to_db(self, row, stream=sys.stdout):
        """Save a row from the Packets 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.
        """
        cultivar_json = self.cell(row, self.cols['Cultivar (JSON)']).value
        cv_dict = json.loads(cultivar_json)
        sku = self.cell(row, self.cols['SKU']).value
        price = self.cell(row, self.cols['Price']).value
        quantity = self.cell(row, self.cols['Quantity']).value
        units = self.cell(row, self.cols['Units']).value

        print('-- BEGIN editing/creating Packet with the SKU \'{0}\' from row '
              '#{1}. --'.format(sku, row), file=stream)
        edited = False
        pkt = Packet.query.filter(Packet.sku == sku).one_or_none()
        if pkt:
            print('The Packet with SKU \'{0}\' has been loaded from the '
                  'database.'.format(pkt.sku), file=stream)
        else:
            edited = True
            qty = Quantity.from_queryable_values(value=quantity, units=units)
            if not qty:
                qty = Quantity(value=quantity, units=units)
            pkt = Packet(sku=sku, price=price, quantity=qty)
            db.session.add(pkt)
            pkt.cultivar = Cultivar.get_or_create(
                name=dbify(cv_dict['Cultivar Name']),
                common_name=dbify(cv_dict['Common Name']),
                index=dbify(cv_dict['Index']),
                stream=stream
            )
            print('The Packet with SKU \'{0}\' does not yet exist, so it has '
                  'been created.'.format(pkt.sku), file=stream)
        if price != str(pkt.price):
            edited = True
            pkt.price = price
            print('The price for Packet SKU \'{0}\' has been set to: ${1}.'
                  .format(pkt.sku, pkt.price), file=stream)
        qty = Quantity.from_queryable_values(value=quantity, units=units)
        if not qty:
            qty = Quantity(value=quantity, units=units)
        if qty is not pkt.quantity:
            edited = True
            pkt.quantity = qty
            print('The quantity for the Packet SKU \'{0}\' has been set to: '
                  '{1} {2}'.format(pkt.sku, qty.value, qty.units), file=stream)
        if edited:
            db.session.flush()
            print('Changes to the Packet \'{0}\' have been flushed to '
                  'the database.'.format(pkt.info), file=stream)
        else:
            print('No changes were made to the Packet \'{0}\'.'
                  .format(pkt.info), file=stream)
        print('-- END editing/creating Packet with SKU \'{0}\' from row #{1}. '
              '--'.format(pkt.sku, row), file=stream)
        return edited
Ejemplo n.º 7
0
    def save_row_to_db(self, row, stream=sys.stdout):
        """Save a row from the Cultivars 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)
        common_name = dbify(self.cell(row, self.cols['Common Name']).value)
        cultivar = dbify(self.cell(row, self.cols['Cultivar Name']).value)
        section = dbify(self.cell(row, self.cols['Section']).value)
        if not section:
            section = None
        botanical_name = self.cell(row, self.cols['Botanical Name']).value
        thumbnail = self.cell(row, self.cols['Thumbnail Filename']).value
        description = self.cell(row, self.cols['Description']).value
        synonyms = self.cell(row, self.cols['Synonyms']).value
        if not synonyms:
            synonyms = ''
        nus = self.cell(row, self.cols['New Until']).value
        if nus:
            new_until = datetime.datetime.strptime(nus, '%m/%d/%Y').date()
        else:
            new_until = None
        n_stk = self.cell(row, self.cols['In Stock']).value
        if n_stk and 'true' in n_stk.lower():
            in_stock = True
        else:
            in_stock = False
        act = self.cell(row, self.cols['Active']).value
        if act and 'true' in act.lower():
            active = True
        else:
            active = False
        vis = self.cell(row, self.cols['Visible']).value
        if vis and 'true' in vis.lower():
            visible = True
        else:
            visible = False

        print('-- BEGIN editing/creating Cultivar \'{0}\' from row #{1}. '
              '--'.format(cultivar + ' ' + common_name, row), file=stream)
        edited = False
        cv = Cultivar.get_or_create(name=cultivar,
                                    index=index,
                                    common_name=common_name,
                                    stream=stream)
        if cv.created:
            edited = True
            db.session.add(cv)
            if section:  # Section already exists if cv was not created.
                sec = Section.query\
                    .join(CommonName, CommonName.id == Section.common_name_id)\
                    .join(Index, Index.id == CommonName.index_id)\
                    .filter(Section.name == section,
                            CommonName.name == common_name,
                            Index.name == index)\
                    .one_or_none()
                if sec:
                    print('The Section \'{0}\' has been loaded from the '
                          'database.'.format(sec.name), file=stream)
                else:
                    sec = Section(name=section)
                    sec.common_name = cv.common_name
                    print('The Section \'{0}\' does not yet exist, so it has '
                          'been created.'.format(sec.name), file=stream)
                cv.section = sec
                print('Section for the Cultivar \'{0}\' set to: {1}'
                      .format(cv.fullname, sec.name), file=stream)
        if botanical_name:
            if not BotanicalName.validate(botanical_name):
                obn = botanical_name
                words = botanical_name.strip().split(' ')
                words[0] = words[0].capitalize()
                botanical_name = ' '.join(words)
                print('The BotanicalName \'{0}\' does not appear to be a '
                      'validly formatted botanical name. In an attempt to fix '
                      'it, it has been changed to: \'{1}\''
                      .format(obn, botanical_name), file=stream)
            bn = BotanicalName.query\
                .filter(BotanicalName.name == botanical_name)\
                .one_or_none()
            if bn and bn is not cv.botanical_name:
                print('The BotanicalName \'{0}\' has been loaded from the '
                      'database.'.format(bn.name), file=stream)
            elif not bn:
                bn = BotanicalName(name=botanical_name)
                bn.common_names.append(cv.common_name)
                print('The BotanicalName \'{0}\' does not yet exist, so it '
                      'has been created.'.format(bn.name), file=stream)
            if bn is not cv.botanical_name:
                edited = True
                cv.botanical_name = bn
                print('BotanicalName for the Cultivar \'{0}\' set to: {1}'
                      .format(cv.fullname, bn.name), file=stream)
        if thumbnail:
            if not cv.thumbnail or cv.thumbnail.filename != thumbnail:
                edited = True
                tn = Image.query\
                    .filter(Image.filename == thumbnail)\
                    .one_or_none()
                if tn:
                    print('The Image with the filename \'{0}\' has been '
                          'loaded from the database.'.format(tn.filename),
                          file=stream)
                else:
                    tn = Image(filename=thumbnail)
                    print('The Image with the filename \'{0}\' does not yet '
                          'exist in the database, so it has been created.'
                          .format(tn.filename), file=stream)
                cv.thumbnail = tn
                print('The Image with the filename \'{0}\' has been set as '
                      'the thumbnail for the Cultivar \'{1}\'.'
                      .format(tn.filename, cv.fullname), file=stream)
                if not tn.exists():
                    print('WARNING: The image file \'{0}\' set as the '
                          'thumbnail for the Cultivar \'{1}\' does not exist! '
                          'Please make sure you add the image file to the '
                          'images directory.'.format(tn.filename, cv.fullname),
                          file=stream)
        if description != cv.description:
            edited = True
            if description:
                cv.description = description
                print('Description for the Cultivar \'{0}\' set to: {1}'
                      .format(cv.fullname, cv.description), file=stream)
            else:
                cv.description = None
                print('Description for the Cultivar \'{0}\' has been cleared.'
                      .format(cv.fullname), file=stream)
        if synonyms != cv.synonyms_string:
            edited = True
            cv.synonyms_string = synonyms
            if synonyms:
                print('Synonyms for the Cultivar \'{0}\' set to: {1}'
                      .format(cv.fullname, cv.synonyms_string),
                      file=stream)
            else:
                print('Synonyms for the Cultivar \'{0}\' have been cleared.'
                      .format(cv.fullname), file=stream)
        if new_until != cv.new_until:
            edited = True
            if new_until:
                cv.new_until = new_until
                print('The Cultivar \'{0}\' has been set as new until {1}.'
                      .format(cv.fullname, cv.new_until.strftime('%m/%d/%Y')),
                      file=stream)
            else:
                cv.new_until = None
                print('The Cultivar \'{0}\' is no longer set as new.'
                      .format(cv.fullname), file=stream)
        if in_stock != cv.in_stock:
            edited = True
            cv.in_stock = in_stock
            if cv.in_stock:
                print('The Cultivar \'{0}\' is in stock.'.format(cv.fullname),
                      file=stream)
            else:
                print('The Cultivar \'{0}\' is out of stock.'
                      .format(cv.fullname), file=stream)
        if active != cv.active:
            edited = True
            cv.active = active
            if cv.active:
                print('The Cultivar \'{0}\' is active.'.format(cv.fullname),
                      file=stream)
            else:
                print('The Cultivar \'{0}\' is inactive.'.format(cv.fullname),
                      file=stream)
        if visible != cv.visible:
            edited = True
            cv.visible = visible
            if cv.visible:
                print('The Cultivar \'{0}\' will be shown on auto-generated '
                      'pages.'.format(cv.fullname), file=stream)
            else:
                print('The Cultivar \'{0}\' will not be shown on '
                      'auto-generated pages.'.format(cv.fullname), file=stream)
        if edited:
            db.session.flush()
            print('Changes to the Cultivar \'{0}\' have been flushed to '
                  'the database.'.format(cv.fullname), file=stream)
        else:
            print('No changes were made to the Cultivar \'{0}\'.'
                  .format(cv.fullname), file=stream)
        print('-- END editing/creating Cultivar \'{0}\' from row #{1}. '
              '--'.format(cv.fullname, row), file=stream)
        return edited
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
    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
Ejemplo n.º 10
0
def clean_title(title):
    return dbify(title.lower().replace('seeds', '').strip())
Ejemplo n.º 11
0
 def test_dbify_mixed_case(self):
     """Don't let titlecase leave mixed-case strings alone."""
     assert dbify(
         'BENARY\'S GIANT FORMULA MIX (Blue Point)'
     ) == 'Benary\'s Giant Formula Mix (Blue Point)'
     assert dbify('ONE TWO THREE (four)') == 'One Two Three (Four)'
Ejemplo n.º 12
0
 def test_dbify_null(self):
     """Return None if given None or an empty string."""
     assert dbify(None) is None
     assert dbify('') is None