Example #1
0
def like_project(request):
	if request.user.is_authenticated():
		popularity = 0
		if request.GET:
			if 'project' in request.GET:
				project = request.GET['project']
				userp = request.user.profile
				if userp.liked_projects == "":
					userp.liked_projects = "[]"
					userp.save()
				projlist = json.decoder.JSONDecoder().decode(userp.liked_projects)
				if project in request.user.profile.liked_projects:
					#Unlike the project
					projlist.remove(project)
					userp.liked_projects = json.dumps(projlist)
					userp.save()
					db_interface.unlikeProject(project)
				else:
					#Like the project
					projlist.append(project)
					userp.liked_projects = json.dumps(projlist)
					userp.save()
					db_interface.likeProject(project)

				projectfile = db_interface.getProject(project)
				return HttpResponse(projectfile['popularity'])
			else:
				return HttpResponseRedirect('/')
		else:
			return HttpResponseRedirect('/')
	else:
		return HttpResponseRedirect('/')
Example #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)