<commit_before>from flask import request, jsonify, redirect
from server import app, sqldb, bcrypt
from .models import PostAccount, Post, PostFilter, PostStatus, PostTester, PostTargetEmail, SchoolMajorAccount, School, Major
from .models import PostAccountEmail, AnalyticsEvent
from sqlalchemy import desc, or_, case, exists, func, and_
from sqlalchemy.sql import select
import json
from datetime import date, datetime, timedelta
import uuid
import sys


"""
Example: JSON Encoding
{
    "account_id": "7900fffd-0223-4381-a61d-9a16a24ca4b7",
    "image_url": "https://i.imgur.com/CmhAG25.jpg",
    "post_url": "https://pennlabs.org/",
    "source": "Penn Labs",
    "subtitle": "A small subtitle",
    "time_label": "Today",
    "title": "Testing a new feature!",
    "start_date": "2019-05-23T08:00:00",
    "end_date": "2019-05-24T00:00:00",
    "filters": [
        {
            "type": "class",
            "filter": "2020"
        },
        {
            "type": "class",
            "filter": "2021"
        },
        {
            "type": "major",
            "filter": "CIS"
        }
    ],
    "testers": [
        "<EMAIL>"
    ],
    "emails": [
        "<EMAIL>",
        "<EMAIL>"
    ],
    "comments": "This is a post to test Penn Mobile. Please approve!"
}
"""


@app.route('/portal/post/new', methods=['POST'])
def create_post():
    data = request.get_json()

    try:
        account_id = data.get("account_id")
        account = PostAccount.get_account(account_id)
    except ValueError as e:
        return jsonify({"error": str(e)}), 400

    source = data.get("source")
    title = data.get("title")
    subtitle = data.get("subtitle")
    time_label = data.get("time_label")
    post_url = data.get("post_url")
    image_url = data.get("image_url")
    filters = list(data.get("filters"))
    testers = list(data.get("testers"))
    emails = list(data.get("emails"))

    start_date_str = data.get("start_date")
    end_date_str = data.get("end_date")

    if any(x is None for x in [image_url, start_date_str, end_date_str]):
        return jsonify({"error": "Parameter is missing"}), 400

    start_date = datetime.strptime(start_date_str, '%Y-%m-%dT%H:%M:%S')
    end_date = datetime.strptime(start_date_str, '%Y-%m-%dT%H:%M:%S')

    post = Post(account=account.id, source=source, title=title, subtitle=subtitle, time_label=time_label, post_url=post_url,
                image_url=image_url, start_date=start_date, end_date=end_date, filters=(True if filters else False),
                testers=(True if testers else False), emails=(True if emails else False))
    sqldb.session.add(post)
    sqldb.session.commit()<commit_msg>bug fix<commit_after>from flask import request, jsonify, redirect
from server import app, sqldb, bcrypt
from .models import PostAccount, Post, PostFilter, PostStatus, PostTester, PostTargetEmail, SchoolMajorAccount, School, Major
from .models import PostAccountEmail, AnalyticsEvent
from sqlalchemy import desc, or_, case, exists, func, and_
from sqlalchemy.sql import select
import json
from datetime import date, datetime, timedelta
import uuid
import sys


"""
Example: JSON Encoding
{
    "account_id": "7900fffd-0223-4381-a61d-9a16a24ca4b7",
    "image_url": "https://i.imgur.com/CmhAG25.jpg",
    "post_url": "https://pennlabs.org/",
    "source": "Penn Labs",
    "subtitle": "A small subtitle",
    "time_label": "Today",
    "title": "Testing a new feature!",
    "start_date": "2019-05-23T08:00:00",
    "end_date": "2019-05-24T00:00:00",
    "filters": [
        {
            "type": "class",
            "filter": "2020"
        },
        {
            "type": "class",
            "filter": "2021"
        },
        {
            "type": "major",
            "filter": "CIS"
        }
    ],
    "testers": [
        "<EMAIL>"
    ],
    "emails": [
        "<EMAIL>",
        "<EMAIL>"
    ],
    "comments": "This is a post to test Penn Mobile. Please approve!"
}
"""


@app.route('/portal/post/new', methods=['POST'])
def create_post():
    data = request.get_json()

    try:
        account_id = data.get("account_id")
        account = PostAccount.get_account(account_id)
    except ValueError as e:
        return jsonify({"error": str(e)}), 400

    source = data.get("source")
    title = data.get("title")
    subtitle = data.get("subtitle")
    time_label = data.get("time_label")
    post_url = data.get("post_url")
    image_url = data.get("image_url")
    filters = list(data.get("filters"))
    testers = list(data.get("testers"))
    emails = list(data.get("emails"))

    start_date_str = data.get("start_date")
    end_date_str = data.get("end_date")

    if any(x is None for x in [image_url, start_date_str, end_date_str]):
        return jsonify({"error": "Parameter is missing"}), 400

    start_date = datetime.strptime(start_date_str, '%Y-%m-%dT%H:%M:%S')
    end_date = datetime.strptime(end_date_str, '%Y-%m-%dT%H:%M:%S')

    post = Post(account=account.id, source=source, title=title, subtitle=subtitle, time_label=time_label, post_url=post_url,
                image_url=image_url, start_date=start_date, end_date=end_date, filters=(True if filters else False),
                testers=(True if testers else False), emails=(True if emails else False))
    sqldb.session.add(post)
    sqldb.session.commit()
{'fraction_non_alphanumeric': 0.15103578154425612, 'fraction_numerical': 0.02448210922787194, 'mean_word_length': 3.1106811145510838, 'pattern_counts': {'":': 42, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, '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': '4464709', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1779, 'n_words': 400}