<commit_before>"""
Instabus Views
"""
# pylint: disable=W0312

import datetime
import json
from time import time
import uuid

from flask import request, jsonify, session, json, Response
from functools import wraps
from redis import Redis

from backend.api import app, db
from backend.api.models import Checkin, DataPoint

redis = Redis()

def save_datapoint(request):
	get_post_field = lambda x: request.form[x]
	if not session.has_key('id'):
		session['id'] = int(uuid.uuid4())

	data_point_post_data = {
		'type': get_post_field('type'),
		'line': get_post_field('line'),
		'latitude': float(get_post_field('latitude')),
		'longitude': float(get_post_field('longitude')),
		'created': datetime.datetime.fromtimestamp(int(get_post_field('created'))), 
		'is_demo': int(get_post_field('is_demo')),
		'is_active': int(get_post_field('is_active')),
		'session': session['id'],
	}

	db.session.add(DataPoint(**data_point_post_data))
	db.session.commit()

@app.route('/api/datapoint', methods=['POST'])
def http_save_datapoint():
	""" Save and return data about datapoints."""
	save_datapoint(request)

	return jsonify(status='OK', message='Saved datapoint.')


@app.route('/api/datapoint/<line_no>')
def get_datapoint(line_no):
	# pylint: disable=E1101
	datapoints = DataPoint.query\
			.filter_by(line=line_no)\
			.all()
	datapoints_dict = [{
		'type': point.type,
		'line': point.line,
		'latitude': point.latitude,
		'longitude': point.longitude,
		'is_demo': point.is_demo,<commit_msg>Added checkout method.<commit_after>"""
Instabus Views
"""
# pylint: disable=W0312

import datetime
import json
from time import time
import uuid

from flask import request, jsonify, session, json, Response
from functools import wraps
from redis import Redis

from backend.api import app, db
from backend.api.models import Checkin, DataPoint

redis = Redis()

def save_datapoint(request):
	get_post_field = lambda x: request.form[x]
	if not session.has_key('id'):
		session['id'] = int(uuid.uuid4())

	data_point_post_data = {
		'type': get_post_field('type'),
		'line': get_post_field('line'),
		'latitude': float(get_post_field('latitude')),
		'longitude': float(get_post_field('longitude')),
		'created': datetime.datetime.fromtimestamp(int(get_post_field('created'))), 
		'is_demo': int(get_post_field('is_demo')),
		'is_active': int(get_post_field('is_active')),
		'session': session['id'],
	}

	db.session.add(DataPoint(**data_point_post_data))
	db.session.commit()

def deactivate_session(session_id):
	""" Checkout all datapoints with a given session-id. """
	# pylint: disable=E1101
	DataPoint.query.filter_by(session=session_id).update(
			{DataPoint.is_active: 0})
	db.session.commit()


@app.route('/api/datapoint', methods=['POST'])
def http_save_datapoint():
	""" Save and return data about datapoints."""
	save_datapoint(request)

	return jsonify(status='OK', message='Saved datapoint.')

@app.route('/api/checkout/<session_id>')
def checkout_specific_session(session_id):
	""" HTTP GET method for check-ing out a certain session id. """
	deactivate_session(session_id)
	return json.dumps({'status': 'OK'})

@app.route('/api/checkout')
def checkout():
	if 'id' not in session:
		return json.dumps({'status': 'OK'})

	deactivate_session(session['id'])

	return json.dumps({'status': 'OK'})

@app.route('/api/datapoint/<line_no>')
def get_datapoint(line_no):
	# pylint: disable=E1101
	datapoints = DataPoint.query\
			.filter_by(line=line_no)\
			.filter_by(is_active=1)\
			.all()
	datapoints_dict = [{
		'session': point.session,
		'type': point.type,
		'line': point.line,
		'latitude': point.latitude,
		'longitude': point.longitude,
		'is_demo': point.is_demo,
{'fraction_non_alphanumeric': 0.15852660300136426, 'fraction_numerical': 0.006548431105047749, 'mean_word_length': 5.982857142857143, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '616862', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1313, 'n_words': 267}