def init_db(): app.db.create_all() import helpers from Calculators.models import Calculator, Tag BMI_calc = helpers.get_or_create(app.db.session, Calculator, template='body_mass_index.formula', name='Body Mass Index') compound_interest_calc = helpers.get_or_create( app.db.session, Calculator, template='compound_interest.formula', name='Compound Interest') for t in ['health', 'weight', 'fat', 'body', 'mass', 'index', 'BMI']: tag = helpers.get_or_create(app.db.session, Tag, name=t) BMI_calc.tags.add(tag) for t in ['finance', 'money', 'compound', 'interest']: tag = helpers.get_or_create(app.db.session, Tag, name=t) compound_interest_calc.tags.add(tag) app.db.session.commit()
def _process_writers_and_artists(self): """Update the writers and artists many-to-many relationship.""" db(db.comicartist.comic == self.id).delete() for artist_name in self.form.vars.artists.split(';'): artist_id = get_or_create(db.artist, name=artist_name) db.comicartist.insert(comic=self.id, artist=artist_id) db(db.comicwriter.comic == self.id).delete() for writer_name in self.form.vars.writers.split(';'): writer_id = get_or_create(db.writer, name=writer_name) db.comicwriter.insert(comic=self.id, writer=writer_id) # find the IDs of all the artists and writers all_artist_ids = set(map(lambda x: x.id, db().select(db.artist.id))) all_writer_ids = set(map(lambda x: x.id, db().select(db.writer.id))) # find the IDs of the artists and writers that are referenced by comics used_artist_ids = set(map(lambda x: x.artist, db().select(db.comicartist.artist, distinct=True))) used_writer_ids = set(map(lambda x: x.writer, db().select(db.comicwriter.writer, distinct=True))) # clean up - delete any unused artists or writers db(db.artist.id.belongs(all_artist_ids - used_artist_ids)).delete() db(db.writer.id.belongs(all_writer_ids - used_writer_ids)).delete()
def update_title(json_title): title = get_or_create(db.session, Title, name=json_title['titleName'], country=json_title['countryName']) player = get_or_create_igokisen_player(json_title['winnerName'], json_title['countryName']) title.igo_url = urljoin(json_title['countryNameAbbreviation'], json_title['htmlFileName']) title.holding = json_title['holding'] if player: title.current_winner = player.id title.time_edited = datetime.now() db.session.commit() if not Tournament.query.filter_by(title=title.id, holding=title.holding): get_or_create_tournament(title.id, player.id, json_title)
def _on_success(self, form): """Called when a form is validated correctly.""" # Create the publisher if it doesn't exist already publisher_id = get_or_create(db.publisher, name=form.vars.publisher) comic_fields = db.comic._filter_fields(form.vars) comic_fields['publisher'] = publisher_id # Update the record if it's already in the database if self.id: db(db.comic.id == self.id).update(**comic_fields) else: self.id = db.comic.insert(**comic_fields) # Add the comic to the box db.comicbox.insert(comic=self.id, box=form.vars.box) # Update the writers and artists many-to-many relationship self._process_writers_and_artists() self._cleanup_unused_publishers()