<commit_before># talkatv - Commenting backend for static pages
# Copyright (C) 2012  talkatv contributors, see AUTHORS
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from flask import request, json, abort, g

from talkatv import app, db
from talkatv.decorators import require_active_login
from talkatv.models import Item, Comment, User
from talkatv.tools.cors import jsonify, allow_all_origins
from talkatv.notification import send_comment_notification
from talkatv.item import get_or_add_item


@app.route('/api/comments', methods=['GET', 'POST', 'OPTIONS'])
@require_active_login(['POST'])
def api_comments():
    if request.method == 'POST':
        post_data = json.loads(request.data)

        item = Item.query.filter_by(id=post_data['item']).first()

        if not item:
            return abort(404)

        user = User.query.filter_by(id=g.user.id).first()

        if not user:
            return abort(400)

        comment = Comment(item, user, post_data['comment'])
        db.session.add(comment)
        db.session.commit()

        emails = []

        for c in item.comments.all():
            emails.append(c.user.email)

        if item.site:
            emails.append(item.site.owner.email)

        emails = set(emails)

        app.logger.debug('Sending notification about {0} to {1}'.format(
            comment.text,
            ', '.join(emails)))

        for email in emails:
            # Send comment notification to users that posted on the page
            send_comment_notification(
                    email,
                    g.user,
                    comment,
                    item)

        app.logger.debug(request.data)

        return jsonify(status='OK', _allow_origin_cb=allow_all_origins)

    if not request.args.get('item_url'):
        return abort(400)<commit_msg>Fixed error that occured when Chromium issues OPTIONS<commit_after># talkatv - Commenting backend for static pages
# Copyright (C) 2012  talkatv contributors, see AUTHORS
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from flask import request, json, abort, g

from talkatv import app, db
from talkatv.decorators import require_active_login
from talkatv.models import Item, Comment, User
from talkatv.tools.cors import jsonify, allow_all_origins
from talkatv.notification import send_comment_notification
from talkatv.item import get_or_add_item


@app.route('/api/comments', methods=['GET', 'POST', 'OPTIONS'])
@require_active_login(['POST'])
def api_comments():
    if request.method == 'POST':
        post_data = json.loads(request.data)

        item = Item.query.filter_by(id=post_data['item']).first()

        if not item:
            return abort(404)

        user = User.query.filter_by(id=g.user.id).first()

        if not user:
            return abort(400)

        comment = Comment(item, user, post_data['comment'])
        db.session.add(comment)
        db.session.commit()

        emails = []

        for c in item.comments.all():
            emails.append(c.user.email)

        if item.site:
            emails.append(item.site.owner.email)

        emails = set(emails)

        app.logger.debug('Sending notification about {0} to {1}'.format(
            comment.text,
            ', '.join(emails)))

        for email in emails:
            # Send comment notification to users that posted on the page
            send_comment_notification(
                    email,
                    g.user,
                    comment,
                    item)

        app.logger.debug(request.data)

        return jsonify(status='OK', _allow_origin_cb=allow_all_origins)

    if not request.args.get('item_url'):
        return jsonify(error='Item not found', _allow_origin_cb=allow_all_origins)
{'fraction_non_alphanumeric': 0.08977203954004438, 'fraction_numerical': 0.0058503126891264875, 'mean_word_length': 2.76461655277145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3669775', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1348, 'n_words': 469}