<commit_before>from __future__ import print_function
from os import environ

from flask import Flask, jsonify, request
from flask_socketio import SocketIO

app = Flask(__name__, static_url_path='/s', instance_relative_config=True)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
socketio = SocketIO(app)

# USE ONLY ENV VARS FOR config

from .errors import ServerError, BadDataError, NotFoundError
from .forms import SignupForm
from .models import db, Account, db_safety

app.secret_key = app.config['SECRET_KEY'] # For Flask

db.init_app(app)

@app.errorhandler(404)
def not_found(error):
    return handle_server_error(NotFoundError())

@app.errorhandler(405)
def method_not_allowed(error):
    # 405 is method not allowed
    # just pretend the endpoint doesn't exist
    # Can't call abort because Flask won't rehandle the reraised HTTPException
    # to prevent infinite loops
    return not_found(error)

@app.errorhandler(ServerError)
def handle_server_error(error):
    response = jsonify(error.to_dict())
    response.status_code = error.status_code
    return response

@socketio.on('connect')
def test_connect():
    print('got a connection')

@socketio.on('disconnect')
def test_disconnect():
    print('Client disconnected')

@app.route('/accounts', methods=['POST'])
def signup():
    try:
      form = SignupForm(csrf_enabled=False)
    except Exception as e: # Otherwise there's a 400 that's propagated
      raise BadDataError()
    if not form.validate_on_submit():
      raise BadDataError()
    username = form.username.data
    public_key = form.publicKey.data
    if Account.lookup_from_username(username) != None:
      raise ServerError('This account already exists!', status_code=409)
    with db_safety() as session:
        account_id = Account.create(session, username, public_key)
    return jsonify({'message': 'Account successfully created'})
<commit_msg>Implement add event endpoint.<commit_after>from __future__ import print_function
from os import environ

from flask import Flask, jsonify, request
from flask_socketio import SocketIO

app = Flask(__name__, static_url_path='/s', instance_relative_config=True)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
socketio = SocketIO(app)

# USE ONLY ENV VARS FOR config

from .errors import ServerError, BadDataError, NotFoundError
from .forms import SignupForm, AddEventForm
from .models import db, Account, db_safety

app.secret_key = app.config['SECRET_KEY'] # For Flask

db.init_app(app)

@app.errorhandler(404)
def not_found(error):
    return handle_server_error(NotFoundError())

@app.errorhandler(405)
def method_not_allowed(error):
    # 405 is method not allowed
    # just pretend the endpoint doesn't exist
    # Can't call abort because Flask won't rehandle the reraised HTTPException
    # to prevent infinite loops
    return not_found(error)

@app.errorhandler(ServerError)
def handle_server_error(error):
    response = jsonify(error.to_dict())
    response.status_code = error.status_code
    return response

@socketio.on('connect')
def test_connect():
    print('got a connection')

@socketio.on('disconnect')
def test_disconnect():
    print('Client disconnected')

@app.route('/accounts', methods=['POST'])
def signup():
    try:
      form = SignupForm(csrf_enabled=False)
    except Exception as e: # Otherwise there's a 400 that's propagated
      raise BadDataError()
    if not form.validate_on_submit():
      raise BadDataError()
    username = form.username.data
    public_key = form.publicKey.data
    if Account.lookup_from_username(username) != None:
      raise ServerError('This account already exists!', status_code=409)
    with db_safety() as session:
        account_id = Account.create(session, username, public_key)
    return jsonify({'message': 'Account successfully created'})

@app.route('/events', methods=['POST'])
def new_event():
    try:
      form = AddEventForm(csrf_enabled=False)
    except Exception as e: # Otherwise there's a 400 that's propagated
      raise BadDataError()
    if not form.validate_on_submit():
      raise BadDataError()
    name = form.name.data
    start = form.start.data
    end = form.end.data
    description = form.description.data
    with db_safety() as session:
        event_id = Event.create(session, name, start, end, description)
    return jsonify({'message': 'Event successfully created', 'event_id': event_id})

{'fraction_non_alphanumeric': 0.10183715128146972, 'fraction_numerical': 0.0074846904059877525, 'mean_word_length': 4.603557814485388, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1822689', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1319, 'n_words': 369}