def tale_delete(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] creator = User.select_by_id(tale['creator_id'], 1)[0] email_object = strings.construct_delete_tale_email_object( session.get('language', 'en'), tale, creator) aux.send_email_to_followers(tale['id'], email_object['title'], email_object['body']) Chapter.delete_by_tale_id(tale['id']) Contribution_Request.delete_by_tale_id(tale['id']) Follow.delete_by_tale_id(tale['id']) Invitation.delete_by_tale_id(tale['id']) Star.delete_by_tale_id(tale['id']) Tale_Genre.delete_by_tale_id(tale['id']) Tale.delete(tale['id']) return jsonify(url='/profile/' + creator['username']) else: 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 tale_delete(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] creator = User.select_by_id(tale['creator_id'], 1)[0] email_object = strings.construct_delete_tale_email_object( session.get('language', 'en'), tale, creator ) aux.send_email_to_followers(tale['id'], email_object['title'], email_object['body']) Chapter.delete_by_tale_id(tale['id']) Contribution_Request.delete_by_tale_id(tale['id']) Follow.delete_by_tale_id(tale['id']) Invitation.delete_by_tale_id(tale['id']) Star.delete_by_tale_id(tale['id']) Tale_Genre.delete_by_tale_id(tale['id']) Tale.delete(tale['id']) return jsonify(url = '/profile/' + creator['username']) else: 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 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 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 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 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 chapter_edit_get(chapter_id): chapter = Chapter.select_by_id(chapter_id, 1) if len(chapter) is not 0 and session.get('user_logged_id', None) is chapter[0]['user_id']: return render_template('chapter_edit.html', chapter = chapter[0]) 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 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 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 contribution_requests_accept(): contribution_request_id = request.form.get('contribution_request_id', -1) contribution_request = Contribution_Request.select_by_id( contribution_request_id, 1) if len(contribution_request) is not 0: contribution_request = contribution_request[0] tale = Tale.select_by_id(contribution_request['tale_id'], 1)[0] if tale['creator_id'] is session.get('user_logged_id', None): Contribution_Request.update_was_accepted( contribution_request['id'], True) Tale.update_contribution_request_count( contribution_request['tale_id'], -1) new_chapter = Chapter( contribution_request['user_id'], contribution_request['tale_id'], contribution_request['number'], contribution_request['title'], contribution_request['content'], contribution_request['datetime'], contribution_request['previous_chapter_id'], ) new_chapter.insert() creator = User.select_by_id(tale['creator_id'], 1)[0] contributor = User.select_by_id(contribution_request['user_id'], 1)[0] email_object = strings.construct_contribution_request_accepted_email_object( session.get('language', 'en'), tale, creator, contributor, contribution_request['id']) aux.send_email_to_followers(tale['id'], email_object['title'], email_object['body']) return redirect('/tale/' + str(tale['id']) + '/0') else: return redirect('/404') else: return redirect('/404')
def contribution_requests_accept(): contribution_request_id = request.form.get('contribution_request_id', -1) contribution_request = Contribution_Request.select_by_id(contribution_request_id, 1) if len(contribution_request) is not 0: contribution_request = contribution_request[0] tale = Tale.select_by_id(contribution_request['tale_id'], 1)[0] if tale['creator_id'] is session.get('user_logged_id', None): Contribution_Request.update_was_accepted(contribution_request['id'], True) Tale.update_contribution_request_count(contribution_request['tale_id'], -1) new_chapter = Chapter( contribution_request['user_id'], contribution_request['tale_id'], contribution_request['number'], contribution_request['title'], contribution_request['content'], contribution_request['datetime'], contribution_request['previous_chapter_id'], ) new_chapter.insert() creator = User.select_by_id(tale['creator_id'], 1)[0] contributor = User.select_by_id(contribution_request['user_id'], 1)[0] email_object = strings.construct_contribution_request_accepted_email_object( session.get('language', 'en'), tale, creator, contributor, contribution_request['id'] ) aux.send_email_to_followers(tale['id'], email_object['title'], email_object['body']) return redirect('/tale/' + str(tale['id']) + '/0') else: return redirect('/404') else: return redirect('/404')
def chapter_edit_post(chapter_id): chapter = Chapter.select_by_id(chapter_id, 1) creator_id = session.get('user_logged_id', None) if request.is_xhr and len(chapter) is not 0 and creator_id is chapter[0]['user_id']: chapter = chapter[0] title = request.form.get('chapter-edit-title', '') content = request.form.get('chapter-edit-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: Chapter.update_title_and_content(chapter_id, title, content) tale = Tale.select_by_id(chapter['tale_id'], 1)[0] email_object = strings.construct_updated_chapter_email_object( language, tale, User.select_by_id(creator_id, 1)[0]['username'], chapter['number'], chapter['id'] ) aux.send_email_to_followers(tale['id'], email_object['title'], email_object['body']) return jsonify(url = '/tale/' + str(chapter['tale_id']) + '/' + str(chapter_id)) 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 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')
def __init__(self, mdFile): Chapter.__init__(self, mdFile) self.parse(mdFile)
def parse_chapter(self, response): ##Infos :: tab2 = response.css("#tab-2") texts = tab2.css('#leftis').get() texts = texts.split("\n") chapterRes = Chapter.select().where( (Chapter.title_en == texts[3].split("</strong> ")[1]) & (Chapter.edition_number == int(texts[6].split("</strong> ")[1]))) if chapterRes: print("Chapter " + str(texts[6].split("</strong> ")[1]) + " of " + texts[3].split("</strong> ")[1] + " is already exist!") return else: mangaRes = Manga.get( Manga.title_en == texts[3].split("</strong> ")[1]) if (mangaRes): chapter = Chapter() edition = texts[6].split("</strong> ")[1] chapter.edition_number = edition chapter.views = texts[8].split("</strong> ")[1] chapter.title_en = mangaRes.title_en chapter.title = mangaRes.title chapter.cover = mangaRes.cover chapter.about = mangaRes.about chapter.manga_id = mangaRes.id chapter.save() ##Add Pages images = response.css('#sqmang > img') for image in images: imageSRC = image.xpath("@src").get() if Page.select().where((Page.image == imageSRC) & (Page.chapter_id == chapter.id)): continue page = Page() page.chapter_id = chapter.id page.image = imageSRC page.save() print("Chapter " + str(texts[6].split("</strong> ")[1]) + " of " + texts[3].split("</strong> ")[1] + " has been added") else: print(texts[3].split("</strong> ")[1] + " Manga Dosen't exist")