<commit_before>import requests
import json

from requests.auth import HTTPBasicAuth

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.http import HttpResponse

from django.conf import settings


def about(request):
	return render(request, 'foreign/about_foreign.html',)

def methodology(request):
	return render(request, 'foreign/methodology.html',)

def incoming_arms(request):
	if request.GET.get('p'):
		p = int(request.GET.get('p'))
	else:
		p = 1
	url = "/".join([settings.FARA_ENDPOINT, "proposed-arms"])
	response = requests.get(url, params={"p":p, "key":settings.API_PASSWORD})
	data = response.json()
	data = data['results']
	docs = []

	page ={}
	page['this'] = p
	page['previous'] = p - 1
	page['next'] = p + 1
	page['total'] = data['page']['num_pages']

	count = 1
	results = []
	for d in data['proposed_sales']:
		if count % 2 != 0:
			row = "even"
		else:
			row = "odd"

		if d.has_key("date"):
			date = d["date"]
		else:
			print d, "Has no date"
			date =  None


		info ={
			"date": date,
			"id": d["id"],
			"title": d["title"],
			"row": row,
		}


		results.append(info)
		count += 1

	return render(request, 'foreign/incoming_arms.html', {"results":results, "page":page})

def arms_profile(request, doc_id):
	# need to build this
	url = "/".join([settings.FARA_ENDPOINT, "proposed-arms"])
	## problem
	response = requests.get(url, params={"doc_id":doc_id, "key":settings.API_PASSWORD})
	data = response.json()
	if data.has_key('date'):
		date = data['date']
	else:
		date = None
	results={
		'doc_id': doc_id,
		'title': data['title'],
		'date': date,
		'location': data['location'],
		'pdf_url': data['pdf_url'],
	}

	if data.has_key('location_id'):
		results['location_id'] = data['location_id']

	return render(request, 'foreign/arms_profile.html', results)



def incoming_fara(request):
	if request.GET.get('p'):
		p = int(request.GET.get('p'))
	else:
		p = 1
	url = "/".join([settings.FARA_ENDPOINT, "docs"])
	response = requests.get(url, params={"p":p,"key":settings.API_PASSWORD})
	data = response.json()
	page = {}
	page['this'] = p
	page['previous'] = p - 1
	page['next'] = p + 1
	page['total'] = data['page']['num_pages']

	table_info = make_doc_table(data, p)

	return render(request, 'foreign/incoming_fara.html', {"table_info":table_info, 'page':page})

def form_profile(request, form_id):
	url = "/".join([settings.FARA_ENDPOINT, "doc-profile", form_id])
	response = requests.get(url, params={"doc_id":form_id, "key":settings.API_PASSWORD})
	data = response.json()
	data = data['results']
	source_url = data["url"]
	reg_id = data["reg_id"]
	stamp_date = data["stamp_date"]
	doc_type = data["doc_type"]
	processed = data["processed"]
	view_doc_url= http_link(source_url)

	if data.has_key('reg_name'):
		registrant = data["reg_name"]
	else:
		registrant = None

	totals = {}
	if data.has_key('total_contribution'):
		totals['total_contribution'] = data['total_contribution']
	if data.has_key('total_contact'):
		totals['total_contact'] = data['total_contact']
	if data.has_key('total_payment'):
		totals['total_payment'] = data['total_payment']
	if data.has_key('total_disbursement'):
		totals['total_disbursement'] = data['total_disbursement']
	if data.has_key('amended'):
		amended = True
	else:
		amended = False

	client_list = []
	count = 1
	if data.has_key('clients'):
		for client in data['clients']:
			client_dict = {}
			# I like the look of the chart this way
			if count % 2 != 0:
				row = "even"
			else:
				row = "odd"
			count = count + 1
			client_dict = {"client_id": client["client_id"],
							"client_name": client["client_name"],
							"row": row,
							"location": client["location"],
							"location_id": client["location_id"],
			}
			if client.has_key("payment"):
				client_dict["payment"] = int(client["payment"])
			if client.has_key("contact"):
				client_dict["contact"] = int(client["contact"])
			if client.has_key("disbursement"):
				client_dict["disbursement"] = int(client["disbursement"])

			client_list.append(client_dict)
	if data.has_key('terminated_clients'):
		terminated_list = []
		for client in data['terminated_clients']:
			client_dict = {}
			# I like the look of the chart this way
			if count % 2 != 0:
				row = "even"
			else:
				row = "odd"
			count = count + 1
			client_id = client["client_id"]
			client_name = client["client_name"]


			client_dict = {"client_id": client_id,
							"client_name": client_name,
							"row": row,
							"location": client["location"],
							"location_id": client["location_id"],
			}
			if client.has_key("payment"):
				client_dict["payment"] = int(client["payment"])
			if client.has_key("contact"):
				client_dict["contact"] = int(client["contact"])
			if client.has_key("disbursement"):
				client_dict["disbursement"] = int(client["disbursement"])
			terminated_list.append(client_dict)

	download = 'http://fara.sunlightfoundation.com.s3.amazonaws.com/spreadsheets/forms/form_' + form_id + '.zip'
	r = requests.head(download)
	if r.status_code == requests.codes.ok:
		download = download
	else:
		download = None

	r = requests.head(view_doc_url)
	print r.status_code
	if r.status_code == requests.codes.ok:
		view_doc_url = download
	else:
		view_doc_url = None

	return render(request, 'foreign/form_profile.html', {
			"source_url": source_url,
			"stamp_date": stamp_date,
			"doc_type": doc_type,
			"registrant": registrant,
			"view_doc_url": view_doc_url,
			"clients": client_list,
			"terminated_clients": terminated_list,
			"processed": processed,
			"download": download,
			"reg_id": reg_id,
			"doc_id": form_id,
			"totals": totals,
			"amended": amended,
		})

def client_profile(request, client_id):
	url = "/".join([settings.FARA_ENDPOINT, "client-profile", client_id])
	response = requests.get(url, params={"key":settings.API_PASSWORD})
	data = response.json()
	data = data['results']

	results = {}
	results['client'] = data['client_name']
	results['client_id'] = client_id
	results['location'] = data['location']
	results['location_id'] = data['location_id']<commit_msg>fixing iframe<commit_after>import requests
import json

from requests.auth import HTTPBasicAuth

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.http import HttpResponse

from django.conf import settings


def about(request):
	return render(request, 'foreign/about_foreign.html',)

def methodology(request):
	return render(request, 'foreign/methodology.html',)

def incoming_arms(request):
	if request.GET.get('p'):
		p = int(request.GET.get('p'))
	else:
		p = 1
	url = "/".join([settings.FARA_ENDPOINT, "proposed-arms"])
	response = requests.get(url, params={"p":p, "key":settings.API_PASSWORD})
	data = response.json()
	data = data['results']
	docs = []

	page ={}
	page['this'] = p
	page['previous'] = p - 1
	page['next'] = p + 1
	page['total'] = data['page']['num_pages']

	count = 1
	results = []
	for d in data['proposed_sales']:
		if count % 2 != 0:
			row = "even"
		else:
			row = "odd"

		if d.has_key("date"):
			date = d["date"]
		else:
			print d, "Has no date"
			date =  None


		info ={
			"date": date,
			"id": d["id"],
			"title": d["title"],
			"row": row,
		}


		results.append(info)
		count += 1

	return render(request, 'foreign/incoming_arms.html', {"results":results, "page":page})

def arms_profile(request, doc_id):
	# need to build this
	url = "/".join([settings.FARA_ENDPOINT, "proposed-arms"])
	## problem
	response = requests.get(url, params={"doc_id":doc_id, "key":settings.API_PASSWORD})
	data = response.json()
	if data.has_key('date'):
		date = data['date']
	else:
		date = None
	results={
		'doc_id': doc_id,
		'title': data['title'],
		'date': date,
		'location': data['location'],
		'pdf_url': data['pdf_url'],
	}

	if data.has_key('location_id'):
		results['location_id'] = data['location_id']

	return render(request, 'foreign/arms_profile.html', results)



def incoming_fara(request):
	if request.GET.get('p'):
		p = int(request.GET.get('p'))
	else:
		p = 1
	url = "/".join([settings.FARA_ENDPOINT, "docs"])
	response = requests.get(url, params={"p":p,"key":settings.API_PASSWORD})
	data = response.json()
	page = {}
	page['this'] = p
	page['previous'] = p - 1
	page['next'] = p + 1
	page['total'] = data['page']['num_pages']

	table_info = make_doc_table(data, p)

	return render(request, 'foreign/incoming_fara.html', {"table_info":table_info, 'page':page})

def form_profile(request, form_id):
	url = "/".join([settings.FARA_ENDPOINT, "doc-profile", form_id])
	response = requests.get(url, params={"doc_id":form_id, "key":settings.API_PASSWORD})
	data = response.json()
	data = data['results']
	source_url = data["url"]
	reg_id = data["reg_id"]
	stamp_date = data["stamp_date"]
	doc_type = data["doc_type"]
	processed = data["processed"]
	view_doc_url= http_link(source_url)

	if data.has_key('reg_name'):
		registrant = data["reg_name"]
	else:
		registrant = None

	totals = {}
	if data.has_key('total_contribution'):
		totals['total_contribution'] = data['total_contribution']
	if data.has_key('total_contact'):
		totals['total_contact'] = data['total_contact']
	if data.has_key('total_payment'):
		totals['total_payment'] = data['total_payment']
	if data.has_key('total_disbursement'):
		totals['total_disbursement'] = data['total_disbursement']
	if data.has_key('amended'):
		amended = True
	else:
		amended = False

	client_list = []
	count = 1
	if data.has_key('clients'):
		for client in data['clients']:
			client_dict = {}
			# I like the look of the chart this way
			if count % 2 != 0:
				row = "even"
			else:
				row = "odd"
			count = count + 1
			client_dict = {"client_id": client["client_id"],
							"client_name": client["client_name"],
							"row": row,
							"location": client["location"],
							"location_id": client["location_id"],
			}
			if client.has_key("payment"):
				client_dict["payment"] = int(client["payment"])
			if client.has_key("contact"):
				client_dict["contact"] = int(client["contact"])
			if client.has_key("disbursement"):
				client_dict["disbursement"] = int(client["disbursement"])

			client_list.append(client_dict)
	if data.has_key('terminated_clients'):
		terminated_list = []
		for client in data['terminated_clients']:
			client_dict = {}
			# I like the look of the chart this way
			if count % 2 != 0:
				row = "even"
			else:
				row = "odd"
			count = count + 1
			client_id = client["client_id"]
			client_name = client["client_name"]


			client_dict = {"client_id": client_id,
							"client_name": client_name,
							"row": row,
							"location": client["location"],
							"location_id": client["location_id"],
			}
			if client.has_key("payment"):
				client_dict["payment"] = int(client["payment"])
			if client.has_key("contact"):
				client_dict["contact"] = int(client["contact"])
			if client.has_key("disbursement"):
				client_dict["disbursement"] = int(client["disbursement"])
			terminated_list.append(client_dict)

	download = 'http://fara.sunlightfoundation.com.s3.amazonaws.com/spreadsheets/forms/form_' + form_id + '.zip'
	r = requests.head(download)
	if r.status_code == requests.codes.ok:
		download = download
	else:
		download = None

	r = requests.head(source_url)
	if r.status_code == requests.codes.ok:
		source_url = download
	else:
		source_url = None

	return render(request, 'foreign/form_profile.html', {
			"source_url": source_url,
			"stamp_date": stamp_date,
			"doc_type": doc_type,
			"registrant": registrant,
			"view_doc_url": view_doc_url,
			"clients": client_list,
			"terminated_clients": terminated_list,
			"processed": processed,
			"download": download,
			"reg_id": reg_id,
			"doc_id": form_id,
			"totals": totals,
			"amended": amended,
		})

def client_profile(request, client_id):
	url = "/".join([settings.FARA_ENDPOINT, "client-profile", client_id])
	response = requests.get(url, params={"key":settings.API_PASSWORD})
	data = response.json()
	data = data['results']

	results = {}
	results['client'] = data['client_name']
	results['client_id'] = client_id
	results['location'] = data['location']
	results['location_id'] = data['location_id']
{'fraction_non_alphanumeric': 0.16835599505562424, 'fraction_numerical': 0.0029666254635352285, 'mean_word_length': 4.577205882352941, 'pattern_counts': {'":': 78, '<': 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': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1018732', 'n_tokens_mistral': 4823, 'n_tokens_neox': 4299, 'n_words': 874}