def _admin_episodejokes(episode_code): from flask import request details = request.form.get('details', None) if request.method == 'POST': episode_joke_id = request.form.get('episode_joke_id', None) ej = EpisodeJoke.get(id=int(episode_joke_id)) ej.details = details ej.save() return '%s' % ej.id if request.method == 'PUT': joke_code = request.form.get('joke_code', None) joke_type = request.form.get('type', None) joke = Joke.get(code=int(joke_code)) episode = Episode.get(code=episode_code) code = 's%se%sj%s' % (str(episode.season).zfill(2), str( episode.episode).zfill(2), joke.code) context = {} context['ej'] = EpisodeJoke(joke=joke, episode=episode, joke_type=joke_type, details=details, code=code) context['ej'].save() return render_template('_episodejoke_form_row.html', **context)
def _parse_episodejokes(sheet): """ Parses joke sheet. Imports episodejokes. Will not update. """ episodes = list(Episode.select()) for row in sheet: for episode in episodes: joke = Joke.get(Joke.code == row['code']) if episode.code == 's02e03': episode.title = '\xc2\xa1Amigos!' if episode.code == 's01e13': episode.title = 'Beef Consomm\xc3\xa9' if row[episode.title] in ['1', 'f', 'b']: ej_dict = {} ej_dict['joke'] = joke ej_dict['episode'] = episode ej_dict['joke_type'] = row[episode.title] ej_dict['code'] = '%sj%s' % (ej_dict['episode'].code, ej_dict['joke'].code) try: EpisodeJoke.get(EpisodeJoke.code == ej_dict['code']) except EpisodeJoke.DoesNotExist: EpisodeJoke(**ej_dict).save()
def _parse_episodejoke_details(sheet, sheet_num): """ Parses the details, origin and connection sheets. Adds data to existing episodejokes. """ FIELDS = [None, None, None, 'details', None, 'connection'] field = FIELDS[int(sheet_num)] episodes = list(Episode.select()) for row in sheet: for episode in episodes: joke = Joke.get(Joke.code == row['code']) if episode.code == 's02e03': episode.title = '\xc2\xa1Amigos!' if episode.code == 's01e13': episode.title = 'Beef Consomm\xc3\xa9' if row[episode.title]: if field: ej_code = '%sj%s' % (episode.code, joke.code) payload = {} payload[field] = row[episode.title] EpisodeJoke.update(**payload).where( EpisodeJoke.code == ej_code).execute()
def setup_tables(): """ Creates the tables for the Arrested Development database. """ db.connect() Joke.create_table() Episode.create_table() EpisodeJoke.create_table() JokeConnection.create_table()
def _admin_episodejokes(episode_code): from flask import request details = request.form.get('details', None) if request.method == 'POST': episode_joke_id = request.form.get('episode_joke_id', None) ej = EpisodeJoke.get(id=int(episode_joke_id)) ej.details = details ej.save() return '%s' % ej.id if request.method == 'PUT': joke_code = request.form.get('joke_code', None) joke_type = request.form.get('type', None) joke = Joke.get(code=int(joke_code)) episode = Episode.get(code=episode_code) code = 's%se%sj%s' % ( str(episode.season).zfill(2), str(episode.episode).zfill(2), joke.code ) context = {} context['ej'] = EpisodeJoke(joke=joke, episode=episode, joke_type=joke_type, details=details, code=code) context['ej'].save() return render_template('_episodejoke_form_row.html', **context)
def _episode_detail(episode_code): context = make_context() context['episode'] = Episode.get(Episode.code == episode_code) context['jokes'] = {} context['joke_count'] = 0 for joke in EpisodeJoke.select().where(EpisodeJoke.episode == context['episode']): group = joke.joke.primary_character if group not in app_config.PRIMARY_CHARACTER_LIST: group = 'Miscellaneous' if group not in context['jokes']: context['jokes'][group] = [] context['jokes'][group].append(joke) context['joke_count'] += 1 context['seasons'] = _all_seasons() context['group_order'] = [g for g in app_config.PRIMARY_CHARACTER_LIST if g in context['jokes']] try: context['next'] = Episode.get(number=context['episode'].number + 1) except Episode.DoesNotExist: context['next'] = None try: context['prev'] = Episode.get(number=context['episode'].number - 1) except Episode.DoesNotExist: context['prev'] = None return render_template('episode_detail.html', **context)
def _admin_episodes(episode_code): from flask import request if request.method == 'GET': context = {} context['episode'] = Episode.get(code=episode_code) context['episodejokes'] = EpisodeJoke.select().join(Episode).where(Episode.code == episode_code) context['jokes'] = Joke.select().order_by(Joke.primary_character) context['seasons'] = _all_seasons() try: context['next'] = Episode.get(number=context['episode'].number + 1) except Episode.DoesNotExist: context['next'] = None try: context['prev'] = Episode.get(number=context['episode'].number - 1) except Episode.DoesNotExist: context['prev'] = None return render_template('admin_episode_detail.html', **context) if request.method == 'PUT': e = Episode.get(code=episode_code) e.blurb = request.form.get('blurb', None) e.save() return '%s' % e.id
def _admin_episodes(episode_code): from flask import request if request.method == 'GET': context = {} context['episode'] = Episode.get(code=episode_code) context['episodejokes'] = EpisodeJoke.select().join(Episode).where( Episode.code == episode_code) context['jokes'] = Joke.select().order_by(Joke.primary_character) context['seasons'] = _all_seasons() try: context['next'] = Episode.get(number=context['episode'].number + 1) except Episode.DoesNotExist: context['next'] = None try: context['prev'] = Episode.get(number=context['episode'].number - 1) except Episode.DoesNotExist: context['prev'] = None return render_template('admin_episode_detail.html', **context) if request.method == 'PUT': e = Episode.get(code=episode_code) e.blurb = request.form.get('blurb', None) e.save() return '%s' % e.id
def _episode_detail(episode_code): context = make_context() context['episode'] = Episode.get(Episode.code == episode_code) context['jokes'] = {} context['joke_count'] = 0 for joke in EpisodeJoke.select().where( EpisodeJoke.episode == context['episode']): group = joke.joke.primary_character if group not in app_config.PRIMARY_CHARACTER_LIST: group = 'Miscellaneous' if group not in context['jokes']: context['jokes'][group] = [] context['jokes'][group].append(joke) context['joke_count'] += 1 context['seasons'] = _all_seasons() context['group_order'] = [ g for g in app_config.PRIMARY_CHARACTER_LIST if g in context['jokes'] ] try: context['next'] = Episode.get(number=context['episode'].number + 1) except Episode.DoesNotExist: context['next'] = None try: context['prev'] = Episode.get(number=context['episode'].number - 1) except Episode.DoesNotExist: context['prev'] = None return render_template('episode_detail.html', **context)
def _admin_output(): output = {} output['joke_main'] = '' output['joke_details'] = '' output['joke_connections'] = '' for joke in Joke.select(): for episode in Episode.select().where(Episode.season == 4).order_by( Episode.number): try: ej = EpisodeJoke.get(episode=episode, joke=joke) output['joke_main'] += '%s\t' % ej.joke_type output['joke_details'] += '\'%s\t' % ej.details if ej.connections(): output['joke_connections'] += '\'%s\t' % ej.connections( )[0]['text'] else: output['joke_connections'] += '\t' except EpisodeJoke.DoesNotExist: output['joke_main'] += '\t' output['joke_details'] += '\t' output['joke_connections'] += '\t' output['joke_main'] += '\n' output['joke_details'] += '\n' output['joke_connections'] += '\n' return render_template('_output.html', **output)
def build_connections(): for episode_joke in EpisodeJoke.select(): if episode_joke.connection: joke1 = episode_joke.joke joke2 = Joke.get(Joke.text == episode_joke.connection.strip()) episode = episode_joke.episode if joke2.code < joke1.code: tmp = joke2 joke2 = joke1 joke1 = tmp JokeConnection(joke1=joke1, joke2=joke2, episode=episode).save()
def _joke_detail(joke_code): context = make_context() context['joke'] = Joke.get(Joke.code == int(joke_code)) context['episodejokes'] = EpisodeJoke.select().where( EpisodeJoke.joke == context['joke']) context['episodejokes'] = sorted(context['episodejokes'], key=lambda ej: ej.episode.code) context['seasons'] = _all_seasons() with open('www/live-data/jokes.json') as f: data = json.load(f) group_order = data['group_order'] joke_data = data['jokes'] connections = data['connections'] connected_joke_codes = [int(joke_code)] def filter_connections(c): if c['joke1_code'] == int(joke_code) or c['joke2_code'] == int( joke_code): connected_joke_codes.append(c['joke1_code']) connected_joke_codes.append(c['joke2_code']) return True return False connections = filter(filter_connections, connections) def filter_jokes(c): return c['code'] in connected_joke_codes for group, jokes in joke_data.items(): joke_data[group] = filter(filter_jokes, jokes) if len(joke_data[group]) == 0: del joke_data[group] group_order.remove(group) context['group_order'] = Markup(json.dumps(group_order)) context['joke_data'] = Markup(json.dumps(joke_data)) context['connection_data'] = Markup(json.dumps(connections)) context['episodes'] = Markup(json.dumps(data['episodes'])) group = context['joke'].primary_character if group not in app_config.PRIMARY_CHARACTER_LIST: group = 'Miscellaneous' context['group'] = group return render_template('joke_detail.html', **context)
def build_regression_csv(): """ Builds an episode-based CSV for @stiles to do our regression. Outputs a list of episodes with joke counts and ratings. """ with open('data/regression.csv', 'wb') as csvfile: regressionwriter = csv.DictWriter(csvfile, ['episode_name', 'episode_id', 'jokes', 'rating']) regressionwriter.writerow({'episode_name': 'episode_name', 'episode_id': 'episode_id', 'jokes': 'jokes', 'rating': 'rating'}) for episode in Episode.select(): episode_dict = {} episode_dict['episode_name'] = episode.title.encode('utf-8') episode_dict['episode_id'] = episode.number episode_dict['rating'] = episode.rating episode_dict['jokes'] = EpisodeJoke.select().where(EpisodeJoke.episode == episode).count() regressionwriter.writerow(episode_dict)
def _parse_episodejoke_details(sheet, sheet_num): """ Parses the details, origin and connection sheets. Adds data to existing episodejokes. """ FIELDS = [None, None, None, 'details', None, 'connection'] field = FIELDS[int(sheet_num)] episodes = list(Episode.select()) for row in sheet: for episode in episodes: joke = Joke.get(Joke.code == row['code']) if episode.code == 's02e03': episode.title = '\xc2\xa1Amigos!' if episode.code == 's01e13': episode.title = 'Beef Consomm\xc3\xa9' if row[episode.title]: if field: ej_code = '%sj%s' % (episode.code, joke.code) payload = {} payload[field] = row[episode.title] EpisodeJoke.update(**payload).where(EpisodeJoke.code == ej_code).execute()
def _joke_detail(joke_code): context = make_context() context['joke'] = Joke.get(Joke.code == int(joke_code)) context['episodejokes'] = EpisodeJoke.select().where(EpisodeJoke.joke == context['joke']) context['episodejokes'] = sorted(context['episodejokes'], key=lambda ej: ej.episode.code) context['seasons'] = _all_seasons() with open('www/live-data/jokes.json') as f: data = json.load(f) group_order = data['group_order'] joke_data = data['jokes'] connections = data['connections'] connected_joke_codes = [int(joke_code)] def filter_connections(c): if c['joke1_code'] == int(joke_code) or c['joke2_code'] == int(joke_code): connected_joke_codes.append(c['joke1_code']) connected_joke_codes.append(c['joke2_code']) return True return False connections = filter(filter_connections, connections) def filter_jokes(c): return c['code'] in connected_joke_codes for group, jokes in joke_data.items(): joke_data[group] = filter(filter_jokes, jokes) if len(joke_data[group]) == 0: del joke_data[group] group_order.remove(group) context['group_order'] = Markup(json.dumps(group_order)) context['joke_data'] = Markup(json.dumps(joke_data)) context['connection_data'] = Markup(json.dumps(connections)) context['episodes'] = Markup(json.dumps(data['episodes'])) group = context['joke'].primary_character if group not in app_config.PRIMARY_CHARACTER_LIST: group = 'Miscellaneous' context['group'] = group return render_template('joke_detail.html', **context)
def _admin_jokeconnections(episode_code): from flask import request if request.method == 'POST': pass if request.method == 'PUT': payload = {} ej = EpisodeJoke.get(id=int(request.form.get('episode_joke_id'))) payload['joke1'] = ej.joke payload['joke2'] = Joke.get(code=int(request.form.get('joke_code'))) payload['episode'] = ej.episode j = JokeConnection(**payload) j.save() return(""" <br/> <a class="related kill-related" href="#" data-jc-id="%s">×</a> <a class="related" href="#joke-%s">%s →</a>""" % (j.id, j.joke2.code, j.joke2.text))
def _admin_jokeconnections(episode_code): from flask import request if request.method == 'POST': pass if request.method == 'PUT': payload = {} ej = EpisodeJoke.get(id=int(request.form.get('episode_joke_id'))) payload['joke1'] = ej.joke payload['joke2'] = Joke.get(code=int(request.form.get('joke_code'))) payload['episode'] = ej.episode j = JokeConnection(**payload) j.save() return (""" <br/> <a class="related kill-related" href="#" data-jc-id="%s">×</a> <a class="related" href="#joke-%s">%s →</a>""" % (j.id, j.joke2.code, j.joke2.text))
def build_regression_csv(): """ Builds an episode-based CSV for @stiles to do our regression. Outputs a list of episodes with joke counts and ratings. """ with open('data/regression.csv', 'wb') as csvfile: regressionwriter = csv.DictWriter( csvfile, ['episode_name', 'episode_id', 'jokes', 'rating']) regressionwriter.writerow({ 'episode_name': 'episode_name', 'episode_id': 'episode_id', 'jokes': 'jokes', 'rating': 'rating' }) for episode in Episode.select(): episode_dict = {} episode_dict['episode_name'] = episode.title.encode('utf-8') episode_dict['episode_id'] = episode.number episode_dict['rating'] = episode.rating episode_dict['jokes'] = EpisodeJoke.select().where( EpisodeJoke.episode == episode).count() regressionwriter.writerow(episode_dict)
def _admin_output(): output = {} output['joke_main'] = '' output['joke_details'] = '' output['joke_connections'] = '' for joke in Joke.select(): for episode in Episode.select().where(Episode.season == 4).order_by(Episode.number): try: ej = EpisodeJoke.get(episode=episode, joke=joke) output['joke_main'] += '%s\t' % ej.joke_type output['joke_details'] += '\'%s\t' % ej.details if ej.connections(): output['joke_connections'] += '\'%s\t' % ej.connections()[0]['text'] else: output['joke_connections'] += '\t' except EpisodeJoke.DoesNotExist: output['joke_main'] += '\t' output['joke_details'] += '\t' output['joke_connections'] += '\t' output['joke_main'] += '\n' output['joke_details'] += '\n' output['joke_connections'] += '\n' return render_template('_output.html', **output)
def _admin_episodejokes_delete(episode_code, episode_joke_id): from flask import request if request.method == 'DELETE': EpisodeJoke.delete().where( EpisodeJoke.id == int(episode_joke_id)).execute() return episode_joke_id
def write_jokes_json(): """ Writes the JSON for @onyxfish and @alykat's viz. Produces a list of jokes and within that a list of episodejokes where the joke appears, sorted by episode index number. """ payload = { 'group_order': app_config.PRIMARY_CHARACTER_LIST, 'jokes': {}, 'connections': [], 'episodes': {} } for joke in Joke.select().order_by(Joke.primary_character): if joke.primary_character not in payload['group_order']: joke.primary_character = 'Miscellaneous' if joke.primary_character not in payload['jokes']: payload['jokes'][joke.primary_character] = [] joke_dict = joke.__dict__['_data'] del joke_dict['id'] del joke_dict['blurb'] joke_dict['episodejokes'] = [] for ej in EpisodeJoke.select().join(Joke).where(Joke.code == joke.code): episode_dict = ej.__dict__['_data'] episode_dict['episode_number'] = ej.episode.number for k in ['episode', 'joke', 'id', 'origin']: del episode_dict[k] joke_dict['episodejokes'].append(episode_dict) joke_dict['episodejokes'] = sorted(joke_dict['episodejokes'], key=lambda ej: ej['episode_number']) payload['jokes'][joke.primary_character].append(joke_dict) if app_config.IMPORT_NEW_SEASON is True: episodes = Episode().select().order_by(Episode.number) else: episodes = Episode().select().where(Episode.season != 4).order_by(Episode.number) for episode in episodes: episode_dict = episode.__dict__['_data'] episode_dict['run_date'] = episode_dict['run_date'].strftime('%Y-%m-%d') for k in ['blurb', 'id', 'production_code', 'rating', 'tvdb_image']: del episode_dict[k] payload['episodes'][episode.number] = episode_dict for primary_character, jokes in payload['jokes'].items(): payload['jokes'][primary_character] = sorted(jokes, key=lambda j: j['episodejokes'][0]['episode_number']) for connection in JokeConnection.select(): payload['connections'].append({ 'joke1_code': connection.joke1.code, 'joke2_code': connection.joke2.code, 'episode_number': connection.episode.number }) payload['connections'] = sorted(payload['connections'], key=lambda c: c['joke1_code']) with open('www/live-data/jokes.json', 'wb') as jokefile: jokefile.write(json.dumps(payload))
def write_jokes_json(): """ Writes the JSON for @onyxfish and @alykat's viz. Produces a list of jokes and within that a list of episodejokes where the joke appears, sorted by episode index number. """ payload = { 'group_order': app_config.PRIMARY_CHARACTER_LIST, 'jokes': {}, 'connections': [], 'episodes': {} } for joke in Joke.select().order_by(Joke.primary_character): if joke.primary_character not in payload['group_order']: joke.primary_character = 'Miscellaneous' if joke.primary_character not in payload['jokes']: payload['jokes'][joke.primary_character] = [] joke_dict = joke.__dict__['_data'] del joke_dict['id'] del joke_dict['blurb'] joke_dict['episodejokes'] = [] for ej in EpisodeJoke.select().join(Joke).where( Joke.code == joke.code): episode_dict = ej.__dict__['_data'] episode_dict['episode_number'] = ej.episode.number for k in ['episode', 'joke', 'id', 'origin']: del episode_dict[k] joke_dict['episodejokes'].append(episode_dict) joke_dict['episodejokes'] = sorted(joke_dict['episodejokes'], key=lambda ej: ej['episode_number']) payload['jokes'][joke.primary_character].append(joke_dict) if app_config.IMPORT_NEW_SEASON is True: episodes = Episode().select().order_by(Episode.number) else: episodes = Episode().select().where(Episode.season != 4).order_by( Episode.number) for episode in episodes: episode_dict = episode.__dict__['_data'] episode_dict['run_date'] = episode_dict['run_date'].strftime( '%Y-%m-%d') for k in ['blurb', 'id', 'production_code', 'rating', 'tvdb_image']: del episode_dict[k] payload['episodes'][episode.number] = episode_dict for primary_character, jokes in payload['jokes'].items(): payload['jokes'][primary_character] = sorted( jokes, key=lambda j: j['episodejokes'][0]['episode_number']) for connection in JokeConnection.select(): payload['connections'].append({ 'joke1_code': connection.joke1.code, 'joke2_code': connection.joke2.code, 'episode_number': connection.episode.number }) payload['connections'] = sorted(payload['connections'], key=lambda c: c['joke1_code']) with open('www/live-data/jokes.json', 'wb') as jokefile: jokefile.write(json.dumps(payload))
def _admin_episodejokes_delete(episode_code, episode_joke_id): from flask import request if request.method == 'DELETE': EpisodeJoke.delete().where(EpisodeJoke.id == int(episode_joke_id)).execute() return episode_joke_id