def index(): return render_template( 'index.html', genres = Genre.select_top_ten(), top10_tales = aux.get_ten_best_tales(), top10_daily_tales = aux.get_ten_best_daily_tales() )
def tale_add_get(): if 'user_logged_id' in session: return render_template('tale_add.html', genres=Genre.select_all(), licenses=License.select_all()) else: return redirect('/404')
def return_rendered_tale_template( tale, template, contributions_object = None, branches_object = None, contributors_object = None, genres_list = None, licenses_list = None, open_contribution_requests_list = None, closed_contribution_requests_list = None, contribution = None ): this_user_gave_star = did_the_logged_user_give_star(tale['id']) this_user_is_following = did_the_logged_user_follow(tale['id']) tale_genres = Tale_Genre.select_by_tale_id(tale['id']) tale_genres_list = list() for tale_genre in tale_genres: genre = Genre.select_by_id(tale_genre[1], 1)[0] tale_genres_list.append(genre) tale['genres'] = tale_genres_list return render_template( template, creator = User.select_by_id(tale['creator_id'], 1)[0], tale = tale, license = License.select_by_id(tale['license_id'], 1)[0], contributions = contributions_object, branches = branches_object, contributors = contributors_object, this_user_gave_star = this_user_gave_star, this_user_is_following = this_user_is_following, genres = genres_list, licenses = licenses_list, open_contribution_requests = open_contribution_requests_list, closed_contribution_requests = closed_contribution_requests_list, contribution = contribution ).encode('utf-8')
def tale_edit_get(tale_id): tale = Tale.select_by_id(tale_id, 1) if len(tale) is not 0 and session.get('user_logged_id', None) is tale[0]['creator_id']: tale = tale[0] tale_genres = Tale_Genre.select_by_tale_id(tale_id) tale_genres_ids_set = set() for tale_genre in tale_genres: tale_genres_ids_set.add(tale_genre[1]) genres = Genre.select_all() genres_list = list() for genre in genres: genre['checked'] = True if genre[ 'id'] in tale_genres_ids_set else False genres_list.append(genre) licenses = License.select_all() licenses_list = list() for license in licenses: license['selected'] = True if tale['license_id'] is license[ 'id'] else False licenses_list.append(license) return aux.return_rendered_tale_template(tale, 'tale_edit.html', genres_list=genres_list, licenses_list=licenses_list) else: return redirect('/404')
def tale_edit_post(tale_id): tale = Tale.select_by_id(tale_id, 1) if request.is_xhr and len(tale) is not 0 and session.get('user_logged_id', None) is tale[0]['creator_id']: tale = tale[0] title = request.form.get('tale-edit-title', '') description = request.form.get('tale-edit-description', '') category = 2 if int(request.form.get('tale-edit-type', 1)) is not 1 else 1 genres = request.form.getlist('tale-edit-genres') license_id = request.form.get('tale-edit-license', -1) creator_id = session['user_logged_id'] language = session.get('language', 'en') error_list = list() if tale['title'] == title or Tale.is_title_available(creator_id, title): if not Tale.is_title_valid(title): error_list.append(strings.STRINGS[language]['INVALID_TITLE']) else: error_list.append(strings.STRINGS[language]['TITLE_TAKEN']) if not Tale.is_description_valid(description): error_list.append(strings.STRINGS[language]['INVALID_DESCRIPTION']) if not License.is_license_id_valid(license_id): error_list.append(strings.STRINGS[language]['INVALID_LICENSE']) if len(genres) is not 0: for genre_id in genres: if not Genre.is_genre_id_valid(genre_id): error_list.append(strings.STRINGS[language]['INVALID_GENRE']) break else: error_list.append(strings.STRINGS[language]['NO_GENRES']) if len(error_list) is not 0: return make_response(jsonify(error_list = error_list), 400) else: tale_last_category = int(tale['category']) if tale_last_category is not category: if tale_last_category is 1: contributors = Tale.select_contributors_id(tale_id) for contributor_id in contributors: new_invitation = Invitation(creator_id, contributor_id, tale_id) new_invitation.insert() else: Invitation.delete_by_tale_id(tale_id) Tale.update_all(tale_id, title, description, category, license_id) Tale_Genre.delete_by_tale_id(tale_id) for genre_id in genres: new_tale_genre = Tale_Genre(tale_id, genre_id) new_tale_genre.insert() return jsonify(url = '/tale/' + str(tale_id) + '/0') else: return redirect('/404')
def tale_edit_get(tale_id): tale = Tale.select_by_id(tale_id, 1) if len(tale) is not 0 and session.get('user_logged_id', None) is tale[0]['creator_id']: tale = tale[0] tale_genres = Tale_Genre.select_by_tale_id(tale_id) tale_genres_ids_set = set() for tale_genre in tale_genres: tale_genres_ids_set.add(tale_genre[1]) genres = Genre.select_all() genres_list = list() for genre in genres: genre['checked'] = True if genre['id'] in tale_genres_ids_set else False genres_list.append(genre) licenses = License.select_all() licenses_list = list() for license in licenses: license['selected'] = True if tale['license_id'] is license['id'] else False licenses_list.append(license) return aux.return_rendered_tale_template( tale, 'tale_edit.html', genres_list = genres_list, licenses_list = licenses_list ) else: return redirect('/404')
def show(genre_id): schema = GenreSchema() genre = Genre.get(id=genre_id) if not genre: abort(404) return schema.dumps(genre)
def delete(category_id): category = Genre.get(id=category_id) if not category: abort(404) category.delete() db.commit() return '', 204
def tale_add_post(): if request.is_xhr and 'user_logged_id' in session: title = request.form.get('tale-add-title', '') description = request.form.get('tale-add-description', '') category = 2 if int(request.form.get('tale-add-type', 1)) is not 1 else 1 genres = request.form.getlist('tale-add-genres') license_id = request.form.get('tale-add-license', -1) creator_id = session['user_logged_id'] language = session.get('language', 'en') error_list = list() if Tale.is_title_available(creator_id, title): if not Tale.is_title_valid(title): error_list.append(strings.STRINGS[language]['INVALID_TITLE']) else: error_list.append(strings.STRINGS[language]['TITLE_TAKEN']) if not Tale.is_description_valid(description): error_list.append(strings.STRINGS[language]['INVALID_DESCRIPTION']) if not License.is_license_id_valid(license_id): error_list.append(strings.STRINGS[language]['INVALID_LICENSE']) if len(genres) is not 0: for genre_id in genres: if not Genre.is_genre_id_valid(genre_id): error_list.append( strings.STRINGS[language]['INVALID_GENRE']) break else: error_list.append(strings.STRINGS[language]['NO_GENRES']) if len(error_list) is not 0: return make_response(jsonify(error_list=error_list), 400) else: new_tale = Tale(title, description, category, creator_id, license_id, aux.get_current_datetime_as_string()) new_tale.insert() tale_id = Tale.select_by_creator_id_and_full_title( creator_id, title, 1)[0]['id'] for genre_id in genres: new_tale_genre = Tale_Genre(tale_id, genre_id) new_tale_genre.insert() if category is 2: invitation = Invitation(creator_id, creator_id, tale_id) invitation.insert() return jsonify(url='/tale/' + str(tale_id) + '/0') else: return redirect('/404')
def search_tales(): content = request.args.get('c', '') genre_id = int(request.args.get('g', -1)) if len(content) < 3 and genre_id is -1: return render_template('bad_search.html', search_page = 'tales') sort_value = int(request.args.get('s', 1)) user_logged_id = session.get('user_logged_id', None) tales = Tale.select_viewable_by_title_and_creator_id( content, user_logged_id ) if genre_id is -1 else Tale.select_viewable_by_title_creator_id_and_genre_id( content, user_logged_id, genre_id ) tales_per_genre = dict() tales_list = list() for tale in tales: tale_genres = Tale_Genre.select_by_tale_id(tale['id']) tale_genres_list = list() for tale_genre in tale_genres: genre = Genre.select_by_id(tale_genre[1], 1)[0] tale_genres_list.append(genre) if genre['type'] in tales_per_genre: tales_per_genre[genre['type']]['count'] += 1 else: tales_per_genre[genre['type']] = {'count': 1, 'id': genre['id']} last_update = Tale.select_last_update(tale['id'], 1)[0][0] tale['genres'] = tale_genres_list tale['creator_username'] = User.select_by_id(tale['creator_id'], 1)[0]['username'] tale['last_update'] = False if last_update is None else aux.beautify_datetime(last_update) tale['ugly_last_update'] = datetime(1, 1, 1, 15, 11) if last_update is None else last_update tales_list.append(tale) if sort_value is 2: tales_list = sorted(tales_list, key = lambda tale: tale['stars'], reverse = True) elif sort_value is 3: tales_list = sorted(tales_list, key = lambda tale: tale['stars']) elif sort_value is 4: tales_list = sorted(tales_list, key = lambda tale: tale['ugly_last_update'], reverse = True) elif sort_value is 5: tales_list = sorted(tales_list, key = lambda tale: tale['ugly_last_update']) return render_template( 'search_tales.html', content = content, tales = tales_list, amount_of_tales = len(tales_list), tales_per_genre = tales_per_genre, sort_value = sort_value, genre_id = genre_id, )
def save_song_to_db(song, db): """ Save song in database """ try: cursor = db.cursor() song_name = song.name artists = song.artists lyrics = song.lyrics album_name = song.album source = song.source poster_url = song.image_link mp3_links = song.mp3_links release_date = song.released_date youtube_id = song.youtube_id genres = song.genres album_id = None artist_ids = [] if album_name is not None: album_id = Album(album_name).insert(cursor).id for artist in (artists or [{'name': None, type: None}]): artist_id = Artist(artist['name'], artist['type']) \ .insert(cursor).id if artist['type'] == 'singer': artist_ids.append(artist_id) song_id = Song(song_name, lyrics, album_id, poster_url, release_date, youtube_id, artist_ids=artist_ids).insert(cursor).id for artist_id in artist_ids: SongArtist(song_id, artist_id).insert(cursor) if genres: for genre in genres: genre_id = Genre(genre).insert(cursor).id SongGenre(song_id, genre_id).insert(cursor) for quality in mp3_links: url = mp3_links.get(quality) Mp3s(song_id, url, source, quality).insert(cursor) db.commit() except IOError as error: print("Error while inserting new song", error)
def save_rankings_to_db(ranking, app): """ Save ranking in song_rankings table """ db = get_db(app) try: cursor = db.cursor() for rank in ranking: song_name = rank.name artist_name = rank.artist youtube_id = rank.youtube_id source = rank.source song_rank = rank.ranking week = rank.week genre = rank.genre artist_ids = [] for artist in artist_name: artist_id = Artist(artist, 'singer') \ .insert(cursor).id artist_ids.append(artist_id) song_id = Song(song_name, None, None, None, None, youtube_id, artist_ids=artist_ids).insert(cursor).id genre_id = Genre(genre).insert(cursor).id SongGenre(song_id, genre_id).insert(cursor) for artist_id in artist_ids: SongArtist(song_id, artist_id).insert(cursor) SongRankings( song_id, artist_id, source, song_rank, week, ).insert(cursor) except IOError as error: print('Error while inserting new ranking ', error) finally: db.commit() db.close()
def tale_add_post(): if request.is_xhr and 'user_logged_id' in session: title = request.form.get('tale-add-title', '') description = request.form.get('tale-add-description', '') category = 2 if int(request.form.get('tale-add-type', 1)) is not 1 else 1 genres = request.form.getlist('tale-add-genres') license_id = request.form.get('tale-add-license', -1) creator_id = session['user_logged_id'] language = session.get('language', 'en') error_list = list() if Tale.is_title_available(creator_id, title): if not Tale.is_title_valid(title): error_list.append(strings.STRINGS[language]['INVALID_TITLE']) else: error_list.append(strings.STRINGS[language]['TITLE_TAKEN']) if not Tale.is_description_valid(description): error_list.append(strings.STRINGS[language]['INVALID_DESCRIPTION']) if not License.is_license_id_valid(license_id): error_list.append(strings.STRINGS[language]['INVALID_LICENSE']) if len(genres) is not 0: for genre_id in genres: if not Genre.is_genre_id_valid(genre_id): error_list.append(strings.STRINGS[language]['INVALID_GENRE']) break else: error_list.append(strings.STRINGS[language]['NO_GENRES']) if len(error_list) is not 0: return make_response(jsonify(error_list = error_list), 400) else: new_tale = Tale(title, description, category, creator_id, license_id, aux.get_current_datetime_as_string()) new_tale.insert() tale_id = Tale.select_by_creator_id_and_full_title(creator_id, title, 1)[0]['id'] for genre_id in genres: new_tale_genre = Tale_Genre(tale_id, genre_id) new_tale_genre.insert() if category is 2: invitation = Invitation(creator_id, creator_id, tale_id) invitation.insert() return jsonify(url = '/tale/' + str(tale_id) + '/0') else: return redirect('/404')
def create(): schema = GenreSchema() try: data = schema.load(request.get_json()) genre = Genre(**data) db.commit() except ValidationError as err: return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 return schema.dumps(genre), 201
def update(genre_id): schema = GenreSchema() genre = Genre.get(id=genre_id) if not genre: abort(404) try: data = schema.load(request.get_json()) genre.set(**data) db.commit() except ValidationError as err: return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 return schema.dumps(genre)
from pony.orm import db_session from app import db from models.Book import Book from models.Genre import Genre from models.User import User, UserSchema from models.Author import Author db.drop_all_tables(with_all_data=True) db.create_tables() with db_session(): Sci_fi = Genre(name='Sci fi') Fantasy = Genre(name='Fantasy') Non_Fiction = Genre(name='Non Fiction') Fiction = Genre(name='Fiction') Memoir = Genre(name='Memoir') Novel = Genre(name='Novel') Crime_Fiction = Genre(name='Crime Fiction') Literary_fiction = Genre(name='Literary fiction') Autobiography = Genre(name='Autobiography') Mystery_fiction = Genre(name='Mystery fiction') Anthology = Genre(name='Anthology') Magical_Realism = Genre(name='Magical Realism') Gothic_fiction = Genre(name='Gothic fiction') Alternate_history = Genre(name='Alternate history') History = Genre(name='History') Graphic_novel = Genre(name='Graphic novel') Hard_science_fiction = Genre(name='Hard science fiction') Self_help_book = Genre(name='Self help book') Mystery = Genre(name='Mystery')
def tale_add_get(): if 'user_logged_id' in session: return render_template('tale_add.html', genres = Genre.select_all(), licenses = License.select_all()) else: return redirect('/404')
def index(): schema = GenreSchema(many=True) genres = Genre.select() return schema.dumps(genres)
def index(): return render_template('index.html', genres=Genre.select_top_ten(), top10_tales=aux.get_ten_best_tales(), top10_daily_tales=aux.get_ten_best_daily_tales())
def search_tales(): content = request.args.get('c', '') genre_id = int(request.args.get('g', -1)) if len(content) < 3 and genre_id is -1: return render_template('bad_search.html', search_page='tales') sort_value = int(request.args.get('s', 1)) user_logged_id = session.get('user_logged_id', None) tales = Tale.select_viewable_by_title_and_creator_id( content, user_logged_id ) if genre_id is -1 else Tale.select_viewable_by_title_creator_id_and_genre_id( content, user_logged_id, genre_id) tales_per_genre = dict() tales_list = list() for tale in tales: tale_genres = Tale_Genre.select_by_tale_id(tale['id']) tale_genres_list = list() for tale_genre in tale_genres: genre = Genre.select_by_id(tale_genre[1], 1)[0] tale_genres_list.append(genre) if genre['type'] in tales_per_genre: tales_per_genre[genre['type']]['count'] += 1 else: tales_per_genre[genre['type']] = { 'count': 1, 'id': genre['id'] } last_update = Tale.select_last_update(tale['id'], 1)[0][0] tale['genres'] = tale_genres_list tale['creator_username'] = User.select_by_id(tale['creator_id'], 1)[0]['username'] tale[ 'last_update'] = False if last_update is None else aux.beautify_datetime( last_update) tale['ugly_last_update'] = datetime( 1, 1, 1, 15, 11) if last_update is None else last_update tales_list.append(tale) if sort_value is 2: tales_list = sorted(tales_list, key=lambda tale: tale['stars'], reverse=True) elif sort_value is 3: tales_list = sorted(tales_list, key=lambda tale: tale['stars']) elif sort_value is 4: tales_list = sorted(tales_list, key=lambda tale: tale['ugly_last_update'], reverse=True) elif sort_value is 5: tales_list = sorted(tales_list, key=lambda tale: tale['ugly_last_update']) return render_template( 'search_tales.html', content=content, tales=tales_list, amount_of_tales=len(tales_list), tales_per_genre=tales_per_genre, sort_value=sort_value, genre_id=genre_id, )
def tale_edit_post(tale_id): tale = Tale.select_by_id(tale_id, 1) if request.is_xhr and len(tale) is not 0 and session.get( 'user_logged_id', None) is tale[0]['creator_id']: tale = tale[0] title = request.form.get('tale-edit-title', '') description = request.form.get('tale-edit-description', '') category = 2 if int(request.form.get('tale-edit-type', 1)) is not 1 else 1 genres = request.form.getlist('tale-edit-genres') license_id = request.form.get('tale-edit-license', -1) creator_id = session['user_logged_id'] language = session.get('language', 'en') error_list = list() if tale['title'] == title or Tale.is_title_available( creator_id, title): if not Tale.is_title_valid(title): error_list.append(strings.STRINGS[language]['INVALID_TITLE']) else: error_list.append(strings.STRINGS[language]['TITLE_TAKEN']) if not Tale.is_description_valid(description): error_list.append(strings.STRINGS[language]['INVALID_DESCRIPTION']) if not License.is_license_id_valid(license_id): error_list.append(strings.STRINGS[language]['INVALID_LICENSE']) if len(genres) is not 0: for genre_id in genres: if not Genre.is_genre_id_valid(genre_id): error_list.append( strings.STRINGS[language]['INVALID_GENRE']) break else: error_list.append(strings.STRINGS[language]['NO_GENRES']) if len(error_list) is not 0: return make_response(jsonify(error_list=error_list), 400) else: tale_last_category = int(tale['category']) if tale_last_category is not category: if tale_last_category is 1: contributors = Tale.select_contributors_id(tale_id) for contributor_id in contributors: new_invitation = Invitation(creator_id, contributor_id, tale_id) new_invitation.insert() else: Invitation.delete_by_tale_id(tale_id) Tale.update_all(tale_id, title, description, category, license_id) Tale_Genre.delete_by_tale_id(tale_id) for genre_id in genres: new_tale_genre = Tale_Genre(tale_id, genre_id) new_tale_genre.insert() return jsonify(url='/tale/' + str(tale_id) + '/0') else: return redirect('/404')
async def get_by_pk(cls, pk: int) -> Optional["Genre"]: row = await cls.pool.fetch(cls.SELECT_BY_ID, pk) return Genre(*(row[0])) if row else None