<commit_before>from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS, cross_origin
from OpenSSL import SSL
from admin import *
from PIL import Image
import requests
import re
import cStringIO
import json
import os
import uuid

context = SSL.Context(SSL.SSLv23_METHOD)

app = Flask(__name__)
cors = CORS(app, resources={r'/api/*': {'origins': '*'}})

app.config['BASE_URL'] = 'https://localhost:5000/'
app.config['DB_FOLDER'] = 'db/'
app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg', 'gif'])

# Code slightly modified from https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
# The backend will be independent from the frontend.
# Flask will help us with building a REST API for the front-end.
@app.route('/api/login', methods=['POST'])
def login(): 
    is_admin = False
    for admin in ADMIN_USERS:   
        if request.data == admin:
            is_admin = True

    if is_admin:
        print(ADMIN_RESPONSE)
        return ADMIN_RESPONSE
    else:
        login_request = requests.post('http://inscricoes.ncuritiba2017.com.br/login', data = request.data)
        return login_request._content

@app.route('/api/db/<path:filename>')
def serve_db(filename):
    return send_from_directory(app.config['DB_FOLDER'], filename)

@app.route('/api/uploads/<path:filename>')
def serve_images(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

@app.route('/api/edit_activity/<activity_id>', methods=['POST'])
def edit_activity(activity_id):
    data = json.loads(request.data)
    if data['admin_hash'] == ADMIN_HASH:

        if 'image' in data: 
            image_data = re.sub('^data:image/.+;base64,', '', data['image']).decode('base64')
            image = Image.open(cStringIO.StringIO(image_data))
            image_path = os.path.join(app.config['UPLOAD_FOLDER'], str(activity_id) + '.' + (image.format).lower())    
            image.save(image_path, format=image.format)
            data['image'] = app.config['BASE_URL'] + 'api/' + image_path

        with open('db/activities.json', 'r') as activities:
            db_data = json.load(activities)

        index = 0
        found = False
        for i in db_data:
            if i['id'] == int(activity_id):
                found = True
                break
            index = index + 1

        if not found:
            db_data.append({})
            activity_id = uuid.uuid4()
            data['id'] = str(uuid.uuid4())

        for key, value in data.items():
            db_data[index][key] = value

        with open('db/activities.json', 'w') as activities:
            json.dump(db_data, activities)

        return 'Done.'

    else:

        return Response('Bad credentials.', 401)

@app.route('/api/delete_activity/<int:activity_id>', methods=['POST'])
def delete_activity(activity_id):<commit_msg>Removes everything regarding program.json<commit_after>from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS, cross_origin
from OpenSSL import SSL
from admin import *
from PIL import Image
import requests
import re
import cStringIO
import json
import os
import uuid

context = SSL.Context(SSL.SSLv23_METHOD)

app = Flask(__name__)
cors = CORS(app, resources={r'/api/*': {'origins': '*'}})

app.config['BASE_URL'] = 'https://localhost:5000/'
app.config['DB_FOLDER'] = 'db/'
app.config['UPLOAD_FOLDER'] = 'uploads/'
app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg', 'gif'])

# Code slightly modified from https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask
# The backend will be independent from the frontend.
# Flask will help us with building a REST API for the front-end.
@app.route('/api/login', methods=['POST'])
def login(): 
    is_admin = False
    for admin in ADMIN_USERS:   
        if request.data == admin:
            is_admin = True

    if is_admin:
        print(ADMIN_RESPONSE)
        return ADMIN_RESPONSE
    else:
        login_request = requests.post('http://inscricoes.ncuritiba2017.com.br/login', data = request.data)
        return login_request._content

@app.route('/api/db/<path:filename>')
def serve_db(filename):
    return send_from_directory(app.config['DB_FOLDER'], filename)

@app.route('/api/uploads/<path:filename>')
def serve_images(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

@app.route('/api/edit_activity/<activity_id>', methods=['POST'])
def edit_activity(activity_id):
    data = json.loads(request.data)
    if data['admin_hash'] == ADMIN_HASH:

        if 'image' in data: 
            image_data = re.sub('^data:image/.+;base64,', '', data['image']).decode('base64')
            image = Image.open(cStringIO.StringIO(image_data))
            image_path = os.path.join(app.config['UPLOAD_FOLDER'], str(activity_id) + '.' + (image.format).lower())    
            image.save(image_path, format=image.format)
            data['image'] = app.config['BASE_URL'] + 'api/' + image_path

        with open('db/activities.json', 'r') as activities:
            db_data = json.load(activities)

        index = 0
        found = False
        for i in db_data:
            if i['id'] == int(activity_id):
                found = True
                break
            index = index + 1

        if not found:
            db_data.append({})
            activity_id = uuid.uuid4()
            data['id'] = str(uuid.uuid4())

        for key, value in data.items():
            db_data[index][key] = value

        with open('db/activities.json', 'w') as activities:
            json.dump(db_data, activities)

        return 'Done.'

    else:

        return Response('Bad credentials.', 401)

@app.route('/api/delete_activity/<activity_id>', methods=['POST'])
def delete_activity(activity_id):
{'fraction_non_alphanumeric': 0.14217524718718036, 'fraction_numerical': 0.007159904534606206, 'mean_word_length': 3.094207955338451, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2672624', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1862, 'n_words': 408}