示例#1
0
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')
示例#2
0
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')
示例#3
0
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')
示例#4
0
def contribution_requests_refuse():
    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'], False)
            Tale.update_contribution_request_count(
                contribution_request['tale_id'], -1)

            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_refused_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(contribution_request['tale_id']) +
                            '/0')
        else:
            return redirect('/404')
    else:
        return redirect('/404')
示例#5
0
def contribution_requests_refuse():
	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'], False)
			Tale.update_contribution_request_count(contribution_request['tale_id'], -1)

			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_refused_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(contribution_request['tale_id']) + '/0')
		else:
			return redirect('/404')
	else:
		return redirect('/404')
示例#6
0
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,
	)
示例#7
0
def get_ten_best_daily_tales():
	tales = Tale.select_top_ten_order_by_star_count_today()
	tales_list = list()

	for tale in tales:
		last_update = Tale.select_last_update(tale['id'])[0][0]

		tale['creator'] = User.select_by_id(tale['creator_id'], 1)[0]
		tale['last_update'] = False if last_update is None else last_update
		tale['chapters'] = Tale.select_chapters_count(tale['id'])[0][0]
		tales_list.append(tale)

	return tales_list
示例#8
0
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')
示例#9
0
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')
示例#10
0
def invite_post(tale_id):
	username = request.form.get('invite-username', '')
	user = User.select_by_email(username, 1)
	tale = Tale.select_by_id(tale_id, 1)

	if len(user) is 0:
		user = User.select_by_full_username(username, 1)

	if len(user) is not 0 and len(tale) is not 0 and session.get('user_logged_id', None) is tale[0]['creator_id']:
		user = user[0]
		tale = tale[0]
		new_invitation = Invitation(session['user_logged_id'], user['id'], tale_id)
		new_invitation.insert()

		creator = User.select_by_id(tale['creator_id'], 1)[0]

		email_object = strings.construct_tale_invitation_email_object(
			session.get('language', 'en'),
			user,
			tale,
			creator
		)

		aux.send_email(email_object['title'], user['email'], email_object['body'])

	return redirect('/tale/' + str(tale_id) + '/0')
示例#11
0
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')
示例#12
0
def invite_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']:
		return aux.return_rendered_tale_template(tale[0], 'invite.html')
	else:
		return redirect('/404')
示例#13
0
def invite_post(tale_id):
    username = request.form.get('invite-username', '')
    user = User.select_by_email(username, 1)
    tale = Tale.select_by_id(tale_id, 1)

    if len(user) is 0:
        user = User.select_by_full_username(username, 1)

    if len(user) is not 0 and len(tale) is not 0 and session.get(
            'user_logged_id', None) is tale[0]['creator_id']:
        user = user[0]
        tale = tale[0]
        new_invitation = Invitation(session['user_logged_id'], user['id'],
                                    tale_id)
        new_invitation.insert()

        creator = User.select_by_id(tale['creator_id'], 1)[0]

        email_object = strings.construct_tale_invitation_email_object(
            session.get('language', 'en'), user, tale, creator)

        aux.send_email(email_object['title'], user['email'],
                       email_object['body'])

    return redirect('/tale/' + str(tale_id) + '/0')
示例#14
0
def invite_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']:
        return aux.return_rendered_tale_template(tale[0], 'invite.html')
    else:
        return redirect('/404')
示例#15
0
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')
示例#16
0
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')
示例#17
0
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')
示例#18
0
def is_visible_tale(tale_id, user_logged_id):
	tale = Tale.select_by_id(tale_id, 1)

	if len(tale) is not 0:
		tale = tale[0]
		# private tale
		if tale['category'] is 2:
			invitations = Invitation.select_by_tale_id(tale['id'])
			allowed_users_id = set([tale['creator_id']])

			for invitation in invitations:
				allowed_users_id.add(invitation[2])

			if user_logged_id not in allowed_users_id:
				return False
		return tale
	else:
		return False
示例#19
0
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')
示例#20
0
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')
示例#21
0
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,
    )
示例#22
0
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')
示例#23
0
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')