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 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 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 contributors(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: contributors = Chapter.select_by_tale_id(tale_id) contributors_dict = dict() creator = User.select_by_id(tale['creator_id'], 1)[0] contributors_dict[creator['id']] = { 'username': creator['username'], 'count': 0 } for c in contributors: contributor = User.select_by_id(c['user_id'], 1)[0] if contributor['id'] not in contributors_dict: contributors_dict[contributor['id']] = { 'username': contributor['username'], 'count': 0 } contributors_dict[contributor['id']]['count'] += 1 return aux.return_rendered_tale_template( tale, 'contributors.html', contributors_object=contributors_dict) else: return redirect('/404')
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 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 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 unstar(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if request.is_xhr and tale and 'user_logged_id' in session: Star.delete_by_user_id(session['user_logged_id']) return jsonify({'stars': tale['stars'] - 1}) else: abort(404)
def star(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if request.is_xhr and tale and 'user_logged_id' in session: new_star = Star(session['user_logged_id'], tale_id, aux.get_current_datetime_as_string()) new_star.insert() return jsonify({'stars': tale['stars'] + 1}) elif 'user_logged_id' not in session: return jsonify({'error' : '/join/?redirect=/tale/' + str(tale_id) + '/0'}) else: abort(404)
def follow(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if request.is_xhr and tale and 'user_logged_id' in session: new_follow = Follow(session['user_logged_id'], tale_id) new_follow.insert() return jsonify({'followers': tale['followers'] + 1}) elif 'user_logged_id' not in session: return jsonify({'error' : '/join/?redirect=/tale/' + str(tale_id) + '/0'}) else: abort(404)
def follow(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if request.is_xhr and tale and 'user_logged_id' in session: new_follow = Follow(session['user_logged_id'], tale_id) new_follow.insert() return jsonify({'followers': tale['followers'] + 1}) elif 'user_logged_id' not in session: return jsonify( {'error': '/join/?redirect=/tale/' + str(tale_id) + '/0'}) else: abort(404)
def star(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if request.is_xhr and tale and 'user_logged_id' in session: new_star = Star(session['user_logged_id'], tale_id, aux.get_current_datetime_as_string()) new_star.insert() return jsonify({'stars': tale['stars'] + 1}) elif 'user_logged_id' not in session: return jsonify( {'error': '/join/?redirect=/tale/' + str(tale_id) + '/0'}) else: abort(404)
def collaboration_add_get(tale_id, chapter_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) chapter = Chapter.select_by_id(chapter_id, 1) if tale and 'user_logged_id' in session and ( ((len(chapter) is not 0 and int(chapter[0]['tale_id']) is tale_id)) or (chapter_id is 0 and len(Chapter.select_by_tale_id_and_previous_chapter_id(tale_id, -1)) is 0)): return render_template('collaboration_add.html', tale_id = tale_id, chapter_id = chapter_id) elif 'user_logged_id' not in session: return redirect('/join?redirect=/collaboration/add/' + str(tale_id) + '/' + str(chapter_id)) else: return redirect('/404')
def collaboration_add_get(tale_id, chapter_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) chapter = Chapter.select_by_id(chapter_id, 1) if tale and 'user_logged_id' in session and ( ((len(chapter) is not 0 and int(chapter[0]['tale_id']) is tale_id)) or (chapter_id is 0 and len(Chapter.select_by_tale_id_and_previous_chapter_id( tale_id, -1)) is 0)): return render_template('collaboration_add.html', tale_id=tale_id, chapter_id=chapter_id) elif 'user_logged_id' not in session: return redirect('/join?redirect=/collaboration/add/' + str(tale_id) + '/' + str(chapter_id)) 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 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 contributors(tale_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) if tale: contributors = Chapter.select_by_tale_id(tale_id) contributors_dict = dict() creator = User.select_by_id(tale['creator_id'], 1)[0] contributors_dict[creator['id']] = {'username': creator['username'], 'count': 0} for c in contributors: contributor = User.select_by_id(c['user_id'], 1)[0] if contributor['id'] not in contributors_dict: contributors_dict[contributor['id']] = {'username': contributor['username'], 'count': 0} contributors_dict[contributor['id']]['count'] += 1 return aux.return_rendered_tale_template( tale, 'contributors.html', contributors_object = contributors_dict ) else: return redirect('/404')
def collaboration_add_post(tale_id, chapter_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) chapter = Chapter.select_by_id(chapter_id, 1) if request.is_xhr and tale and 'user_logged_id' in session and ( ((len(chapter) is not 0 and int(chapter[0]['tale_id']) is tale_id)) or (chapter_id is 0 and len(Chapter.select_by_tale_id_and_previous_chapter_id(tale_id, -1)) is 0)): creator = tale['creator_id'] user_id = session['user_logged_id'] title = request.form.get('collaboration-add-title', '') content = request.form.get('collaboration-add-content', '') language = session.get('language', 'en') error_list = list() if not Contribution_Request.is_title_valid(title): error_list.append(strings.STRINGS[language]['INVALID_TITLE']) if not Contribution_Request.is_content_valid(content): error_list.append(strings.STRINGS[language]['INVALID_CONTENT']) if len(error_list) is not 0: return make_response(jsonify(error_list = error_list), 400) else: email_object = {} creator_username = User.select_by_id(user_id, 1)[0]['username'] if creator is user_id: if chapter_id is 0: new_chapter = Chapter( user_id, tale_id, 1, title, content, aux.get_current_datetime_as_string(), -1 ) new_chapter.insert() email_object = strings.construct_new_chapter_email_object( language, tale, creator_username, 1, 0 ) else: chapter = Chapter.select_by_id(chapter_id, 1)[0] date = aux.get_current_datetime_as_string() new_chapter = Chapter( user_id, tale_id, chapter['number'] + 1, title, content, date, chapter['id'] ) new_chapter.insert() new_chapter_id = Chapter.select_by_date(date, 1)[0]['id'] email_object = strings.construct_new_chapter_email_object( language, tale, creator_username, chapter['number'] + 1, new_chapter_id ) else: chapter = Chapter.select_by_id(chapter_id, 1)[0] new_contribution_request = Contribution_Request( user_id, tale_id, chapter['number'] + 1, title, content, aux.get_current_datetime_as_string(), chapter['id'] ) new_contribution_request.insert() Tale.update_contribution_request_count(tale_id, 1) email_object = strings.construct_new_contribution_request_email_object( language, tale, creator_username ) aux.send_email_to_followers(tale_id, email_object['title'], email_object['body']) return jsonify(url = '/tale/' + str(tale_id) + '/' + str(chapter_id)) else: return redirect('/404')
def collaboration_add_post(tale_id, chapter_id): tale = aux.is_visible_tale(tale_id, session.get('user_logged_id', None)) chapter = Chapter.select_by_id(chapter_id, 1) if request.is_xhr and tale and 'user_logged_id' in session and ( ((len(chapter) is not 0 and int(chapter[0]['tale_id']) is tale_id)) or (chapter_id is 0 and len(Chapter.select_by_tale_id_and_previous_chapter_id( tale_id, -1)) is 0)): creator = tale['creator_id'] user_id = session['user_logged_id'] title = request.form.get('collaboration-add-title', '') content = request.form.get('collaboration-add-content', '') language = session.get('language', 'en') error_list = list() if not Contribution_Request.is_title_valid(title): error_list.append(strings.STRINGS[language]['INVALID_TITLE']) if not Contribution_Request.is_content_valid(content): error_list.append(strings.STRINGS[language]['INVALID_CONTENT']) if len(error_list) is not 0: return make_response(jsonify(error_list=error_list), 400) else: email_object = {} creator_username = User.select_by_id(user_id, 1)[0]['username'] if creator is user_id: if chapter_id is 0: new_chapter = Chapter(user_id, tale_id, 1, title, content, aux.get_current_datetime_as_string(), -1) new_chapter.insert() email_object = strings.construct_new_chapter_email_object( language, tale, creator_username, 1, 0) else: chapter = Chapter.select_by_id(chapter_id, 1)[0] date = aux.get_current_datetime_as_string() new_chapter = Chapter(user_id, tale_id, chapter['number'] + 1, title, content, date, chapter['id']) new_chapter.insert() new_chapter_id = Chapter.select_by_date(date, 1)[0]['id'] email_object = strings.construct_new_chapter_email_object( language, tale, creator_username, chapter['number'] + 1, new_chapter_id) else: chapter = Chapter.select_by_id(chapter_id, 1)[0] new_contribution_request = Contribution_Request( user_id, tale_id, chapter['number'] + 1, title, content, aux.get_current_datetime_as_string(), chapter['id']) new_contribution_request.insert() Tale.update_contribution_request_count(tale_id, 1) email_object = strings.construct_new_contribution_request_email_object( language, tale, creator_username) aux.send_email_to_followers(tale_id, email_object['title'], email_object['body']) return jsonify(url='/tale/' + str(tale_id) + '/' + str(chapter_id)) else: return redirect('/404')