示例#1
0
def createtable_view(request, apikey, tablename):
	result = {}
	if db_interface.getProjectFromKey(apikey) is None:
		result['error'] = "incorrect api key"
	else:
		project_name = db_interface.getProjectFromKey(apikey)
		if db_interface.checkTableExists(project_name, tablename) is True:
			result['error'] = "table name already taken"
		else:
			default_tabletype = db_interface.getTabletypeDefault(project_name)
			db_interface.createTable(project_name, tablename, default_tabletype)
			result['error'] = ""
			result['result'] = "success"
			result['project_name'] = project_name
			result['table_name'] = tablename
			result['table_type'] = default_tabletype
			return JsonResponse(result)

	#Indicates failure
	result['result'] = "failure"
	result['project_name'] = ""
	result['table_name'] = ""
	result['table_type'] = ""
	return JsonResponse(result)
示例#2
0
def project_settings(request, projectname):
	#For navbar control and user access verification
	if request.user.is_authenticated():
		user = request.user.get_username()
		access = db_interface.getProjectAccess(projectname) 
		if access is None:
			raise Http404("Project does not exist.n")
		elif access == "private":
			myproject = True
			projectlist = db_interface.getUserProjects(user) 
			if projectname in projectlist:
				projectlist.remove(projectname)
			else:
				raise Http404("User access denied.")
		elif access == "public":
			projectlist = db_interface.getUserProjects(user)
			if projectname in projectlist:
				myproject = True
				projectlist.remove(projectname)
			else:
				myproject = False

	else:
		return HttpResponseRedirect('/login?next=/log/%s' % projectname)

	#Verify that project exists
	tables = db_interface.gettables(projectname)
	if len(tables) == 0:
		raise Http404("Project does not exist.")
	else:
		projectfile = db_interface.getProject(projectname)
		revisedtables = []
		#Cut off x characters from tablename to display
		#x = length of project name and the hyphen
		length = len(projectname) + 1
		for table in tables:
			revisedtables.append(table[length:])

		#Recently viewed project handler
		addRecentProject(request.user.profile, projectname)

	#Verify post results and handle forms
	issues = []
	issues2 = []
	newname = quanqual = discretecontinuous = ""
	edit_name = projectname
	key_reset = False
	edit_description = projectfile['description']
	edit_access_final = projectfile['access']
	edit_default_tabletype = projectfile['default_tabletype']
	if request.method == "POST":
		if request.POST['formtype'] == 'create_table':
			edit_first = True
			first = False
			newname = request.POST['name']
			quanqual = request.POST['quanqual']
			if len(newname) > 50 or len(newname) < 3:
				issues.append("name_length")
			if re.match('^\w+$', newname) is None and len(newname) != 0:
				issues.append("name_char")
			if newname in revisedtables:
				issues.append("name_taken")
			if len(issues) == 0:
				#Create the table type to help with graphing later
				db_interface.createTable(projectname, newname, quanqual)
				print('emergency')
				return HttpResponseRedirect('/log/%s/%s/' % (projectname, newname))
		elif request.POST['formtype'] == 'edit_project':
			first = True
			edit_first = False
			edit_name = request.POST['edit_name']
			edit_description = request.POST['edit_description']
			edit_access = request.POST.getlist('acc2[]')
			edit_default_tabletype = request.POST['edit_quanqual']

			if len(edit_name) < 4 or len(edit_name) > 50:
				#Project name must be 4-50 characters long.
				issues2.append("name_length")
			if re.match('^\w+$', edit_name) is None and len(edit_name) != 0:
				#Project name can only contain characters and numbers and underscores.
				issues2.append("name_char")
			if len(edit_description) > 500:
				#Description is optional
				#If description must be under 500 characters long.
				issues2.append("description_length")
			if edit_name != projectname and db_interface.checkProjectExists(edit_name):
				issues2.append("name_taken")

			#Update the project:
			if len(issues2) == 0:
				if "public" in edit_access:
					edit_access_final = "public"
				else:
					edit_access_final = "private"
				db_interface.updateProject(projectname, edit_name, edit_access_final,
					edit_description, edit_default_tabletype)
				return HttpResponseRedirect('/log/%s' % edit_name)
		elif request.POST['formtype'] == "reset_key":
			#Reset the project's secret key
			key_reset = True
			first = True
			edit_first = True
			db_interface.resetKey(projectname)
		elif request.POST['formtype'] == "change_status":
			first = True
			edit_first = True
			if projectfile['status'] == "running":
				db_interface.changeStatus(projectname, "stopped")
			elif projectfile['status'] == "stopped":
				db_interface.changeStatus(projectname, "running")
			return HttpResponseRedirect('/log/%s' % projectname)
		elif request.POST['formtype'] == "delete_project":
			db_interface.deleteProject(projectname)
			return HttpResponseRedirect('/account/')
	else:
		quanqual = db_interface.getTabletypeDefault(projectname)
		first = True
		edit_first = True

	#Check if the user has liked the project
	userp = request.user.profile
	projlist = json.decoder.JSONDecoder().decode(userp.liked_projects)
	if projectname in projlist:
		liked = True
	else:
		liked = False

	context = {
		'specific_project' : projectname, 
		'project_description' : projectfile['description'],
		'project_funds' : centsToUSD(projectfile['usd_cents']),
		'project_access' : projectfile['access'],
		'project_free_logs' : projectfile['free_logs'],
		'project_date_created' : timeSince(projectfile['datecreated']),
		'project_last_added_free_logs' : timeSince(projectfile['last_added_free_logs']),
		'secret_key' : projectfile['secret_key'],
		'project_total_logs' : projectfile['total_logs'],
		'project_popularity' : projectfile['popularity'],
		'project_status' : projectfile['status'],
		'tablelist' : revisedtables,
		'username' : user,
		'first' : first,
		'edit_first' : edit_first,
		'reset_key' : key_reset,
		'newname' : newname,
		'quanqual' : quanqual,
		'discretecontinuous' : discretecontinuous,
		'edit_name': edit_name,
		'edit_description' : edit_description,
		'edit_access_final' : edit_access_final,
		'edit_default_tabletype' : edit_default_tabletype,
		'issues' : issues,
		'issues2' : issues2,
		'projectlist' : projectlist,
		'lenprojectlist' : len(projectlist),
		'myproject' : myproject,
		'liked' : liked,
	}

	return render(request, "tablemanager/project_details.html", context)
示例#3
0
def projectlog(request, projectname, tablename):
	#For navbar control and user access verification
	if request.user.is_authenticated():
		user = request.user.get_username()
		access = db_interface.getProjectAccess(projectname)
		if projectname + "-" + tablename not in db_interface.gettables(projectname):
			return HttpResponseRedirect('/log/%s' % (projectname, ))
		if access is None:
			raise Http404("Project does not exist.")
		elif access == "private":
			myproject = True
			projectlist = db_interface.getUserProjects(user) 
			if projectname in projectlist:
				projectlist.remove(projectname)
			else:
				raise Http404("User access denied.")
		elif access == "public":
			projectlist = db_interface.getUserProjects(user)
			if projectname in projectlist:
				myproject = True
				projectlist.remove(projectname)
			else:
				myproject = False

	else:
		return HttpResponseRedirect('/login?next=/log/%s/%s/' % (projectname, tablename))

	#Recently viewed project handler
	addRecentProject(request.user.profile, projectname)

	issues = []
	issues2 = []
	newname = quanqual = discretecontinuous = ""
	tables = db_interface.gettables(projectname)
	name = projectname + '-' + tablename
	revisedtables = []
	#Cut off x characters from tablename to display
	#x = length of project name and the hyphen
	length = len(projectname) + 1
	for table in tables:
		revisedtables.append(table[length:])

	edit_name = tablename
	edit_tabletype = db_interface.getTabletype(projectname, tablename)
	default_tab = "analysis"

	if request.method == "POST":
		if request.POST['formtype'] == 'create_table':
			first = False
			edit_first = True
			newname = request.POST['name']
			quanqual = request.POST['quanqual']
			if len(newname) > 50 or len(newname) < 3:
				issues.append("name_length")
			if re.match('^\w+$', newname) is None and len(newname) != 0:
				issues.append("name_char")
			if newname in revisedtables:
				issues.append("name_taken")

			if len(issues) == 0:
				#Create the table type to help with graphing later
				db_interface.createTable(projectname, newname, quanqual)
				return HttpResponseRedirect('/log/%s/%s/' % (projectname, newname))
		elif request.POST['formtype'] == 'edit_table':
			first = True
			edit_first = False
			edit_name = request.POST['edit_name']
			edit_tabletype = request.POST['edit_quanqual']

			if len(edit_name) < 3 or len(edit_name) > 50:
				#Table name must be 4-50 characters long.
				issues2.append("name_length")
			if re.match('^\w+$', edit_name) is None and len(edit_name) != 0:
				#Table name can only contain characters and numbers and underscores.
				issues2.append("name_char")
			if edit_name in revisedtables and edit_name != tablename:
				issues2.append("name_taken")

			#Update the project:
			if len(issues2) == 0:
				db_interface.updateTable(projectname, tablename, edit_name, edit_tabletype)
				return HttpResponseRedirect('/log/%s/%s/' % (projectname, edit_name))
			else:
				default_tab = "settings"
		elif request.POST['formtype'] == 'delete_table':
			db_interface.deleteTable(projectname, tablename)
			return HttpResponseRedirect('/log/%s' % projectname)
	else:
		quanqual = db_interface.getTabletypeDefault(projectname)
		first = True
		edit_first = True

	logset = db_interface.findlogs(projectname, tablename, 100)
	total_in_logset = len(logset)
	loglists = []
	for i in range(10):
		newlist = []
		for l in range(10):
			if 10*i+l < total_in_logset:
				newlist.append(logset[(10*i)+l])
			else:
				newlist.append("")
		loglists.append(newlist)

	finalloglist = zip(loglists[0], loglists[1], loglists[2],
		loglists[3], loglists[4], loglists[5], loglists[6],
		loglists[7], loglists[8], loglists[9])

	rows =  db_interface.getFrequency_limit(projectname, tablename, 10)
	log_row = []
	log_count = []
	for row in rows:
		if 'Other' in row:
			log_row.append("Other")
			log_count.append(row['Other'])
		else:
			log_row.append(row['log'])
			log_count.append(row['count'])
	freq_log = zip(log_row, log_count)

	tot_logs = db_interface.getlogcount(projectname, tablename)
	ch1_data = []
	type_amt = 0
	ch3_data = []

	ch2_data = db_interface.getTimeGraph_alltime(projectname, tablename)
	if edit_tabletype == "quantitative_continuous":
		ch3_data = db_interface.getHistogram(projectname, tablename)
	else:
		if tot_logs > 0:
			rows2 = db_interface.getFrequency_limit(projectname, tablename, 4)
			for row in rows2:
				type_amt = type_amt + 1
				if 'Other' in row:
					ch1_data.append("Other")
					percentage = (float(row['Other'])/tot_logs)*100
					ch1_data.append(round(percentage))
				else:
					if row['log'] is not None:
						if row['log'].isspace():
							ch1_data.append("blank")
						else:
							ch1_data.append(row['log'])
					percentage = (float(row['count'])/tot_logs)*100
					ch1_data.append(round(percentage))

	context = {
		'specific_project' : projectname, 
		'tablelist' : revisedtables,
		'specific_table' : tablename,
		'logs' : finalloglist,
		'logcount' : tot_logs,
		'username' : user,
		'first' : first,
		'newname' : newname,
		'quanqual' : quanqual,
		'discretecontinuous' : discretecontinuous,
		'issues' : issues,
		'projectlist' : projectlist,
		'lenprojectlist' : len(projectlist),
		'myproject' : myproject,
		'freq_log' : freq_log,
		'ch1_data' : ch1_data,
		'type_amt' : type_amt,
		'tooltipTemplate' : '"<%=label%>: <%= value %>%"',
		'endtime' : int(time.time()),
		'ch2_data' : ch2_data[0],
		'time_scale' : ch2_data[1],
		'edit_first' : edit_first,
		'issues2' : issues2,
		'edit_name' : edit_name,
		'edit_tabletype' : edit_tabletype,
		'default_tab' : default_tab,
		'ch3_data' : ch3_data,
	}

	if name in tables:
		return render(request, 'tablemanager/table_view.html', context)
	else:
		#raise Http404("Table in this project does not exist.")
		return render(request, 'tablemanager/table_does_not_exist.html', context)