<commit_before>#-*- coding: utf-8 -*- 
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
import urllib
import urllib2
import httplib
import hashlib
import datetime
from django.http import HttpResponse, HttpResponseBadRequest
from django.utils import simplejson as json_simple
import logging, random, hashlib, string
import dbmerge, os
from Crypto.Cipher import DES
import pymongo
from pymongo import Connection
import dbdecrypt
import decrypt
import sqlite3
import shutil
import time
from bson import json_util
import json, ast
import sys
import settings
from django.template import RequestContext
from django.shortcuts import redirect
from django.shortcuts import render_to_response
from django.utils.http import urlencode
from oauth2app.authenticate import Authenticator, AuthenticationException, JSONAuthenticator
import requests
from oauth2app.models import AccessRange, AccessToken
import apps.oauth2
from django.contrib.auth.decorators import login_required

import pdb
import traceback

upload_dir = settings.SERVER_UPLOAD_DIR

def insert_pds(profile, token, pds_json):
    try:
	#print "Attempting to post to PDS"
        # get pds location and user id
	request_path= "http://"+str(profile.pds_location)+"/api/personal_data/funf/?format=json&bearer_token="+str(token)+"&datastore_owner__uuid="+str(profile.uuid)
	payload = json.dumps(pds_json)
	r = requests.post(request_path, data=payload, headers= { "content-type": "application/json" })
	response = r.text
	#print "PDS POST response:"
	#print response
    except Exception as ex:
	#print ex
	raise Exception(ex)
    return response

def write_key(request):
    '''write the password used to encrypt funf database files to your PDS'''
    response = None
    try:
        token = request.GET['bearer_token']
        scope = "funf_write"
        scope = AccessRange.objects.get(key="funf_write")
        authenticator = Authenticator(scope=scope)
        try:
            # Validate the request.
            authenticator.validate(request)
        except AuthenticationException:
            # Return an error response.
            return authenticator.error_response(content="You didn't authenticate.")
        profile = authenticator.user.get_profile()
        profile.funf_password = json.loads(request.raw_post_data)['key']
        profile.save()
        response_content = json.dumps({'status':'success'})
        response = HttpResponse(content=response_content)        
    except Exception as ex:
        print "EXCEPTION:"
        print ex
        response = HttpResponseBadRequest('failed to write funf key')
    return response
 

def data(request):
    '''decrypt funf database files, and upload them to your PDS'''
    result = {}
    connection = None
    token = request.GET['bearer_token']

    if request.method == 'GET':
        template = {'token': token}
        return render_to_response('upload.html', template, RequestContext(request))
    pds = None
    scope = AccessRange.objects.get(key="funf_write")
    authenticator = JSONAuthenticator(scope=scope)
    try:
        # Validate the request.
        authenticator.validate(request)
    except AuthenticationException as e:
        # Return an error response.
        print e
        return authenticator.error_response(content="You didn't authenticate.")
    profile = authenticator.user.get_profile()
    funf_password = profile.funf_password
    print "Registry set_funf_data for uuid: %s" % profile.uuid

    for filename, file in request.FILES.items():
        try:
            file_path = upload_dir + file.name
            try:
                key = decrypt.key_from_password(str(funf_password))<commit_msg>pgbouncer<commit_after>#-*- coding: utf-8 -*- 
from django.shortcuts import render_to_response
from django.views.decorators.csrf import csrf_exempt
import urllib
import urllib2
import httplib
import hashlib
import datetime
from django.http import HttpResponse, HttpResponseBadRequest
from django.utils import simplejson as json_simple
import logging, random, hashlib, string
import dbmerge, os
from Crypto.Cipher import DES
import pymongo
from pymongo import Connection
import dbdecrypt
import decrypt
import sqlite3
import shutil
import time
from bson import json_util
import json, ast
import sys
import settings
from django.template import RequestContext
from django.shortcuts import redirect
from django.shortcuts import render_to_response
from django.utils.http import urlencode
from oauth2app.authenticate import Authenticator, AuthenticationException, JSONAuthenticator
import requests
from oauth2app.models import AccessRange, AccessToken
import apps.oauth2
from django.contrib.auth.decorators import login_required
import logging

import pdb
import traceback

upload_dir = settings.SERVER_UPLOAD_DIR

def insert_pds(profile, token, pds_json):
    try:
	#print "Attempting to post to PDS"
        # get pds location and user id
	request_path= "http://"+str(profile.pds_location)+"/api/personal_data/funf/?format=json&bearer_token="+str(token)+"&datastore_owner__uuid="+str(profile.uuid)
	payload = json.dumps(pds_json)
	logging.debug("data dumped: %s" % str(payload))
	r = requests.post(request_path, data=payload, headers= { "content-type": "application/json" })
	response = r.text
	#print "PDS POST response:"
	#print response
    except Exception as ex:
	#print ex
	raise Exception(ex)
    return response

def write_key(request):
    '''write the password used to encrypt funf database files to your PDS'''
    response = None
    try:
        token = request.GET['bearer_token']
        scope = "funf_write"
        scope = AccessRange.objects.get(key="funf_write")
        authenticator = Authenticator(scope=scope)
        try:
            # Validate the request.
            authenticator.validate(request)
        except AuthenticationException:
            # Return an error response.
            return authenticator.error_response(content="You didn't authenticate.")
        profile = authenticator.user.get_profile()
        profile.funf_password = json.loads(request.raw_post_data)['key']
        profile.save()
        response_content = json.dumps({'status':'success'})
        response = HttpResponse(content=response_content)        
    except Exception as ex:
        print "EXCEPTION:"
        print ex
        response = HttpResponseBadRequest('failed to write funf key')
    return response
 

def data(request):
    '''decrypt funf database files, and upload them to your PDS'''
    logging.debug("SENDING data to PDS")
    result = {}
    connection = None
    token = request.GET['bearer_token']

    if request.method == 'GET':
        template = {'token': token}
        return render_to_response('upload.html', template, RequestContext(request))
    pds = None
    scope = AccessRange.objects.get(key="funf_write")
    authenticator = JSONAuthenticator(scope=scope)
    try:
        # Validate the request.
        authenticator.validate(request)
    except AuthenticationException as e:
        # Return an error response.
        print e
        return authenticator.error_response(content="You didn't authenticate.")
    profile = authenticator.user.get_profile()
    funf_password = <PASSWORD>.<PASSWORD>f_password
    print "Registry set_funf_data for uuid: %s" % profile.uuid

    for filename, file in request.FILES.items():
        try:
            file_path = upload_dir + file.name
            try:
                key = decrypt.key_from_password(str(funf_password))
{'fraction_non_alphanumeric': 0.08452016504725143, 'fraction_numerical': 0.0015972314654598696, 'mean_word_length': 3.6991869918699187, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '1754314', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2106, 'n_words': 603}