def upgrade(): # main vars total_views = dict() file_handler = Path("migrations", "csv", "20140101-20141218_google_analytics.csv") # load csv data for path, views in reader(open(file_handler, "r")): # get whisky id from database if "/w/" in path: whisky_id = int(path.replace("/w/", "")) whisky = Whisky.query.get(whisky_id) else: whisky_slug = path.replace("/", "") new_whisky = Whisky(distillery=whisky_slug) new_slug = new_whisky.get_slug() whisky = Whisky.query.filter(Whisky.slug == new_slug).first() # feed temporary dictionary if whisky is not None: total_views[whisky.id] = total_views.get(whisky.id, 0) + int(views) # update db for whisky_id in total_views.keys(): new_whisky = Whisky.query.get(whisky_id) new_whisky.views = total_views[whisky_id] db.session.add(new_whisky) # commit db.session.commit()
def search(): whisky = Whisky(distillery=request.args['s']) row = Whisky.query.filter_by(slug=whisky.get_slug()).first() if row is None: return render_template('404.html', slug=request.args['s']) else: return redirect('/' + str(row.slug))
def upgrade(): file_name = Path('migrations', 'csv', 'whisky.csv') lines = list(reader(open(file_name, 'r'))) headers = lines.pop(0) headers.append('slug') for line in lines: whisky = Whisky(distillery=line[0]) line.append(whisky.get_slug()) data = [dict(zip(headers, line)) for line in lines] op.bulk_insert(Whisky.__table__, data)