def contribution_requests(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: o_c_r = Contribution_Request.select_open_by_tale_id_order_by_datetime( tale_id) for contribution_request in o_c_r: contribution_request['user_username'] = User.select_by_id( contribution_request['user_id'], 1)[0]['username'] contribution_request['datetime'] = aux.beautify_datetime( contribution_request['datetime']) c_c_r = Contribution_Request.select_closed_by_tale_id_order_by_datetime( tale_id) for contribution_request in c_c_r: contribution_request['user_username'] = User.select_by_id( contribution_request['user_id'], 1)[0]['username'] contribution_request['datetime'] = aux.beautify_datetime( contribution_request['datetime']) return aux.return_rendered_tale_template( tale, 'contribution_requests.html', open_contribution_requests_list=o_c_r, closed_contribution_requests_list=c_c_r) else: return redirect('/404')
def collaborations(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: chapters = Chapter.select_by_tale_id_order_by_date(tale_id) chapters_dict = dict() for chapter in chapters: date = aux.beautify_datetime(chapter['date']) if date not in chapters_dict: chapters_dict[date] = list() chapter['contributor_username'] = User.select_by_id( chapter['user_id'], 1)[0]['username'] chapters_dict[date].append(chapter) keys = list(chapters_dict.keys()) keys.sort(reverse=True) return aux.return_rendered_tale_template(tale, 'collaborations.html', contributions_object={ 'keys': keys, 'content': chapters_dict }) else: return redirect('/404')
def chapter_download_all(chapter_id): chapter = Chapter.select_by_id(chapter_id, 1) if len(chapter) is not 0: chapter = chapter[0] chapter_list = list() chapters_ids_list = list() previous_chapter_id = chapter_id tale = Tale.select_by_id(chapter['tale_id'], 1)[0] while previous_chapter_id is not -1: chapters_ids_list.append(previous_chapter_id) chapter = Chapter.select_by_id(previous_chapter_id, 1)[0] previous_chapter_id = chapter['previous_chapter_id'] for chapter_id in reversed(chapters_ids_list): chapter = Chapter.select_by_id(chapter_id, 1)[0] if request.method == 'POST': Chapter.update_download_count(chapter_id) chapter['datetime'] = aux.beautify_datetime(chapter['date']) chapter['contributor_username'] = User.select_by_id(chapter['user_id'], 1)[0]['username'] chapter_list.append(chapter) return render_template('fragment/downloadable_all.html', tale = tale, chapters = chapter_list) else: return redirect('/404')
def branches(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: chapters = Chapter.select_by_tale_id(tale_id) number_bundle = dict() for chapter in chapters: number = chapter['number'] if number not in number_bundle: number_bundle[number] = { 'number': number, 'contributions': list() } chapter['date'] = aux.beautify_datetime(chapter['date']) chapter['contributor_username'] = User.select_by_id( chapter['user_id'], 1)[0]['username'] number_bundle[number]['contributions'].append(chapter) return aux.return_rendered_tale_template(tale, 'branches.html', branches_object=number_bundle) else: return redirect('/404')
def profile(username): user = User.select_by_full_username(username, 1) if len(user) is not 0 and user[0]['is_valid_account']: user = user[0] user_logged_id = session.get('user_logged_id', None) user['signup_date'] = aux.beautify_datetime(user['signup_date']) user['is_email_visible'] = user[ 'is_email_visible'] or user_logged_id is user['id'] own_tales = Tale.select_viewable_by_creator_id_and_viewer_id( user['id'], user_logged_id) participated_tales = Tale.select_tales_other_creator( user['id'], user_logged_id) for tale in participated_tales: tale['creator'] = User.select_by_id(tale['creator_id'], 1)[0] return render_template( 'profile.html', user=user, own_tales=own_tales, participated_tales=participated_tales, ) else: return redirect('/404')
def tale(tale_id, chapter_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: if chapter_id is 0: chapter = Chapter.select_by_tale_id_and_previous_chapter_id( tale['id'], -1, 1) else: chapter = Chapter.select_by_id(chapter_id, 1) # no chapter with this ID if len(chapter) is 0: if chapter_id is 0: chapter = None else: return redirect('/404') else: chapter = chapter[0] next_chapters = Chapter.select_by_tale_id_and_previous_chapter_id( tale['id'], chapter['id']) chapter['datetime'] = aux.beautify_datetime(chapter['date']) chapter['contributor_username'] = User.select_by_id( chapter['user_id'], 1)[0]['username'] chapter['next_chapters'] = next_chapters chapter['is_editable'] = tale['creator_id'] is session.get( 'user_logged_id', None) tale['chapter'] = chapter return aux.return_rendered_tale_template(tale, 'tale.html') else: return redirect('/404')
def search_users(): content = request.args.get('c', '') sort_value = int(request.args.get('s', 1)) if len(content) < 3: return render_template('bad_search.html', search_page = 'users') users = User.select_by_username(content) users_list = list() for user in users: users_list.append({ 'id': user['id'], 'username': user['username'], 'signup_date': aux.beautify_datetime(user['signup_date']), 'ugly_signup_date': user['signup_date'], }) if sort_value is 2: users_list = sorted(users_list, key = lambda user: user['ugly_signup_date'], reverse = True) elif sort_value is 3: users_list = sorted(users_list, key = lambda user: user['ugly_signup_date']) return render_template( 'search_users.html', content = content, users = users_list, amount_of_users = len(users_list), sort_value = sort_value, )
def collaborations(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: chapters = Chapter.select_by_tale_id_order_by_date(tale_id) chapters_dict = dict() for chapter in chapters: date = aux.beautify_datetime(chapter['date']) if date not in chapters_dict: chapters_dict[date] = list() chapter['contributor_username'] = User.select_by_id(chapter['user_id'], 1)[0]['username'] chapters_dict[date].append(chapter) keys = list(chapters_dict.keys()) keys.sort(reverse = True) return aux.return_rendered_tale_template( tale, 'collaborations.html', contributions_object = { 'keys': keys, 'content': chapters_dict } ) else: return redirect('/404')
def tale(tale_id, chapter_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: if chapter_id is 0: chapter = Chapter.select_by_tale_id_and_previous_chapter_id(tale['id'], -1, 1) else: chapter = Chapter.select_by_id(chapter_id, 1) # no chapter with this ID if len(chapter) is 0: if chapter_id is 0: chapter = None else: return redirect('/404') else: chapter = chapter[0] next_chapters = Chapter.select_by_tale_id_and_previous_chapter_id(tale['id'], chapter['id']) chapter['datetime'] = aux.beautify_datetime(chapter['date']) chapter['contributor_username'] = User.select_by_id(chapter['user_id'], 1)[0]['username'] chapter['next_chapters'] = next_chapters chapter['is_editable'] = tale['creator_id'] is session.get('user_logged_id', None) tale['chapter'] = chapter return aux.return_rendered_tale_template(tale, 'tale.html') 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 contribution_requests(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: o_c_r = Contribution_Request.select_open_by_tale_id_order_by_datetime(tale_id) for contribution_request in o_c_r: contribution_request['user_username'] = User.select_by_id(contribution_request['user_id'], 1)[0]['username'] contribution_request['datetime'] = aux.beautify_datetime(contribution_request['datetime']) c_c_r = Contribution_Request.select_closed_by_tale_id_order_by_datetime(tale_id) for contribution_request in c_c_r: contribution_request['user_username'] = User.select_by_id(contribution_request['user_id'], 1)[0]['username'] contribution_request['datetime'] = aux.beautify_datetime(contribution_request['datetime']) return aux.return_rendered_tale_template( tale, 'contribution_requests.html', open_contribution_requests_list = o_c_r, closed_contribution_requests_list = c_c_r ) else: return redirect('/404')
def profile_edit_get(user_id): user = User.select_by_id(user_id, 1) if len(user) is not 0 and session.get('user_logged_id', None) is user[0]['id']: user = user[0] user['signup_date'] = aux.beautify_datetime(user['signup_date']) user['biography'] = user['biography'].replace("<br>", "\r\n") user['is_email_visible'] = True return render_template('profile_edit.html', user=user) else: return redirect('/404')
def chapter_download(chapter_id): chapter = Chapter.select_by_id(chapter_id, 1) if len(chapter) is not 0: chapter = chapter[0] chapter['datetime'] = aux.beautify_datetime(chapter['date']) chapter['contributor_username'] = User.select_by_id(chapter['user_id'], 1)[0]['username'] if request.method == 'POST': Chapter.update_download_count(chapter_id) return render_template('fragment/downloadable_chapter.html', chapter = chapter) else: return redirect('/404')
def collaboration(contribution_request_id): contribution_request = Contribution_Request.select_by_id(contribution_request_id, 1) if len(contribution_request) is not 0: contribution_request = contribution_request[0] tale = aux.is_visible_tale(contribution_request['tale_id'], session.get('user_logged_id', None)) if tale: contribution_request['user_username'] = User.select_by_id( contribution_request['user_id'], 1 )[0]['username'] contribution_request['datetime'] = aux.beautify_datetime(contribution_request['datetime']) return aux.return_rendered_tale_template(tale, 'collaboration.html', contribution = contribution_request) else: return redirect('/404') else: return redirect('/404')
def branches(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: chapters = Chapter.select_by_tale_id(tale_id) number_bundle = dict() for chapter in chapters: number = chapter['number'] if number not in number_bundle: number_bundle[number] = {'number': number, 'contributions': list()} chapter['date'] = aux.beautify_datetime(chapter['date']) chapter['contributor_username'] = User.select_by_id(chapter['user_id'], 1)[0]['username'] number_bundle[number]['contributions'].append(chapter) return aux.return_rendered_tale_template(tale, 'branches.html', branches_object = number_bundle) else: return redirect('/404')
def collaboration(contribution_request_id): contribution_request = Contribution_Request.select_by_id( contribution_request_id, 1) if len(contribution_request) is not 0: contribution_request = contribution_request[0] tale = aux.is_visible_tale(contribution_request['tale_id'], session.get('user_logged_id', None)) if tale: contribution_request['user_username'] = User.select_by_id( contribution_request['user_id'], 1)[0]['username'] contribution_request['datetime'] = aux.beautify_datetime( contribution_request['datetime']) return aux.return_rendered_tale_template( tale, 'collaboration.html', contribution=contribution_request) else: return redirect('/404') else: return redirect('/404')
def search_users(): content = request.args.get('c', '') sort_value = int(request.args.get('s', 1)) if len(content) < 3: return render_template('bad_search.html', search_page='users') users = User.select_by_username(content) users_list = list() for user in users: users_list.append({ 'id': user['id'], 'username': user['username'], 'signup_date': aux.beautify_datetime(user['signup_date']), 'ugly_signup_date': user['signup_date'], }) if sort_value is 2: users_list = sorted(users_list, key=lambda user: user['ugly_signup_date'], reverse=True) elif sort_value is 3: users_list = sorted(users_list, key=lambda user: user['ugly_signup_date']) return render_template( 'search_users.html', content=content, users=users_list, amount_of_users=len(users_list), sort_value=sort_value, )
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, )