Exemple #1
0
def statuses(request):
	context_dict = {}
	
	# If the admin is trying to create or delete a Status, the page is refreshed
	flag = False
	if request.method == 'POST':
		# Is the admin trying to delete a status?
		if	(
				'delete' in request.POST and
				'status' in request.POST and
				'new_status' in request.POST
			):
			
			# Get the status that we're about to delete
			status = Status.get_status(pk=request.POST['status'])
			
			# Get the new status that we're changing surfices to
			new_status = Status.get_status(pk=request.POST['new_status'])
			
			# Only continue if the new status actually exists and is not the same
			# as the one that's being deleted
			if type(new_status) is Status and new_status != status:
				
				# Get all the surfices associated with this status
				surfices = Surfice.get_surfices(status=status)
				
				# Now loop through all the surfices and change their status
				# to the status passed through POST
				for surfice in surfices:
					surfice.set_status(status=new_status)
				
				# Go ahead and delete the status now that everything has be re-assigned
				status.delete()
			
			# A new status wasn't selected or it doesn't exist, so don't do anything
			else:
				pass
				
		
		# Is the admin trying to create a status?
		elif 'name' in request.POST:
			
			data = {}
			if 'data' in request.POST:
				# Get the JSON data from POST
				data = json.loads(request.POST['data'])
			
			# Set the general data by passing in data as keyword arguments
			status = Status.create	(
										name = request.POST['name'],
										description = request.POST.get('description', ''),
										**data
									)
			if status == None:
				flag = True
		
		# Redirect to this view after submission to clear headers
		return HttpResponseRedirect('')
	
	# Query all the Statuses and add them to context_dict
	status_list = Status.get_statuses()
	status_list = sorted(status_list, key=lambda status: int( status.data.get('val', 9999) ))
	context_dict['statuses'] = status_list
	
	# Get the total number of dings for the navbar
	context_dict['dings_length'] = len( Ding.get_dings().filter(timestamp__gte=date.today()) )
	
	return render(request, 'surfice/base_statuses.html', context_dict)
Exemple #2
0
def surfices(request):
	context_dict = {}
	
	# If the admin is trying to create or delete a Surfice, the page is refreshed
	flag = False
	if request.method == 'POST':
		
		# Is the admin trying to delete a surfice?
		if	(
				'delete' in request.POST and
				'surfice' in request.POST
			):
			
			# Get the surfice that we're about to delete
			surfice = Surfice.get_surfice(pk=request.POST['surfice'])
			
			# Django automatically deletes all related objects
			# along with the surfice so go ahead and delete the surfice
			if type(surfice) is Surfice:
				surfice.delete()
			
			# The code below is equivalent to what the single delete()
			# function above is doing. Django automatically deletes all related
			# objects from the database. Below is just what it does explicitly
				
			## Get all the events and dings associated with this surf
			#events = Event.get_events(surfice=surfice)
			#dings = Ding.get_dings(surfice=surfice)
			
			## Now loop through all these events and delete them
			#for event in events:
			#	event.delete()
			
			## And loop through all these dings and delete them
			#for ding in dings:
			#	ding.delete()
			
			## Go ahead and delete the surfice now that everything associated with it has
			## been deleted
			#surfice.delete()
		
		# Is the admin trying to create a surfice?
		elif	(
					'name' in request.POST and
					'status' in request.POST
				):
			
			# Get the surf objects based on the pks that were passed
			surfs = []
			if 'surfs' in request.POST:
				# Loop through the passed pks and append the surf to surfs array
				for pk in request.POST.getlist('surfs'):
					surfs.append( Surf.get_surf(pk=pk) )
			
			# Get the status object
			status = Status.get_status(pk=request.POST['status'])
			
			# All objects have been gotten, so create the surfice
			surfice = Surfice.create(request.POST['name'], surfs, status, request.POST.get('description', ''))
			
			# Check to make sure a Surfice object was actually created
			if type(surfice) is not Surfice:
				flag = True
		
		# Redirect to this view after submission to clear headers
		return HttpResponseRedirect('')
			
	
	
	# Query for surfs and add them to context_dict
	surf_list = Surf.get_surfs().order_by('name')
	context_dict['surfs'] = surf_list
	
	# Query all the Surfices and add them to context_dict
	surfice_list = Surfice.get_surfices().order_by('name').prefetch_related('surfs')
	context_dict['surfices'] = surfice_list
	
	# Query all the Statuses and add them to context_dict
	status_list = Status.get_statuses()
	status_list = sorted(status_list, key=lambda status: int( status.data.get('val', 9999) ))
	context_dict['statuses'] = status_list
	
	# Get the total number of dings for the navbar
	context_dict['dings_length'] = len( Ding.get_dings().filter(timestamp__gte=date.today()) )
	
	return render(request, 'surfice/base_surfices.html', context_dict)
Exemple #3
0
def events(request, page, order_by=''):
	
	if request.method == 'POST':
		# """ Adds an event to the database
# 		
# 				If no timestamp is provided, it will fill in the current time
# 		
# 				INPUT
# 				request						A request object
# 					- status					The pk of a status
# 					- surfice					The pk of the surfice
# 					- description (optional)	The description of the Event
# 					- timestamp (optional)		The timestamp of the event
# 		
# 				RETURNS
# 				*errors
# 			"""

		errors = []

		# If status is set, go ahead and edit it
		if 'status' in request.POST and 'surfice' in request.POST:
	
			# Get the status object
			status = Status.get_status(pk=request.POST['status'])
	
			# Get the surfice object
			surfice = Surfice.get_surfice(pk=request.POST['surfice'])
	
			# If description is in request, set the description
			if 'description' in request.POST:
				description = request.POST['description']
			else:
				description = ''
	
			# If timestamp is in request, set the timestamp
			print request.POST['timestamp'].strip()
			if 'timestamp' in request.POST and request.POST['timestamp'].strip() != '':
		
				# Timestamp passed in format "01/21/2012 14:30:59"
				strp_time = time.strptime(request.POST['timestamp'], "%Y-%m-%dT%H:%M:%S")
		
				# Convert the time to a Django-acceptable timestamp
				timestamp = datetime.fromtimestamp(time.mktime(strp_time))
				
				print "TIMESTAMP"
				print timestamp
		
			# If timestamp is not set or is blank, use the current time
			else:
				timestamp = timezone.now()
			
			Event.create(surfice, status, description, timestamp)
	

		# If no status or surfice is set, throw an error
		else:
			errors.append("It's usually good to have a status to edit.")
		
		# Redirect to this view after submission to clear headers
		return HttpResponseRedirect('')
	

	context_dict = {}
	
	# Query for events and add them to context_dict
	event_list = Event.get_events(order_by=order_by)
	
	# Initialize paginator
	paginator = Paginator(event_list, 10)
	
	# Fill the events array with the current page
	try:
		context_dict['events'] = paginator.page(page)
	except PageNotAnInteger:
		# If page is not an integer, deliver the first page
		context_dict['events'] = paginator.page(1)
	except EmptyPage:
		# If page is out of range (e.g. 9999), deliver the last page of events
		context_dict['events'] = paginator.page(paginator.num_pages)
	
	# Query all the Surfices and add them to context_dict
	surfice_list = Surfice.get_surfices()
	context_dict['surfices'] = surfice_list
	
	# Query all the Statuses and add them to context_dict
	status_list = Status.get_statuses()
	status_list = sorted(status_list, key=lambda status: int( status.data.get('val', 9999) ))
	context_dict['statuses'] = status_list
	
	# Add order_by and its reverse to context_dict as well
	context_dict['order_by'] = order_by
	
	# If order_by is blank, set the reverse to nothing also
	if not order_by:
		context_dict['order_by_reverse'] = ''
	elif '-' in order_by:
		# Cut off the '-' in front of the ordering to reverse it
		context_dict['order_by_reverse'] = order_by[1:]
	else:
		# Add the '-' in front of the ordering to reverse it
		context_dict['order_by_reverse'] = '-' + order_by
	
	# Get the total number of dings for the navbar
	context_dict['dings_length'] = len( Ding.get_dings().filter(timestamp__gte=date.today()) )
	
	return render(request, 'surfice/base_events.html', context_dict)
Exemple #4
0
def debug():
	#print Event.get_events(end='2014-06-19')
	
	# Clear database of debug data first
	try: Surf.get_surf('Manual Surf').delete()
	except: pass
	try: Surf.get_surf('Auto Surf').delete()
	except: pass
	try: Surf.get_surf('Auto Surf Without Description').delete()
	except: pass
	try: Status.get_status('Status Without Description').delete()
	except: pass
	try: Status.get_status('Status With Description').delete()
	except: pass
	try: Status.get_status('Status2').delete()
	except: pass
	try: Surfice.get_surfice('Manual Surfice').delete()
	except: pass
	try: Surfice.get_surfice('Surfice').delete()
	except: pass
	try: Surfice.get_surfice('Surfice1').delete()
	except: pass
	try: Surfice.get_surfice('Surfice2').delete()
	except: pass
	
	
	# First test Surfs
	# First manually
	surf_manual = Surf()
	surf_manual.name = "Manual Surf"
	surf_manual.description = "A description of the manual surf"
	surf_manual.save_new()
	
	printv(surf_manual, "MANUAL SURF")
	
	# Now automatic Surf
	surf_auto_nodescr = Surf.create("Auto Surf Without Description")
	surf_auto = Surf.create("Auto Surf", "A Description for the Auto")
	printv(surf_auto_nodescr, "AUTO SURF W/O DESCRIPTION")
	printv(surf_auto, "AUTO SURF")
	
	# Now try to overwrite a Surf (it shouldn't work)
	surf_overwrite = Surf.create("Auto Surf")
	print "The following overwrite should not work:"
	try: printv(surf_overwrite, "SURF OVERWRITE")
	except: pass
	
	# Get a Surf by name
	surf_by_name = Surf.get_surf("Auto Surf")
	printv(surf_by_name, "GET AUTO SURF BY NAME")
	
	# Now delete the manual Surf
	surf_manual.delete()
	print "MANUAL SURF STILL SAVED AFTER DELETION? (Should be False)\n============================"
	print Surf.is_saved(name=surf_manual.name) # Test to see if it worked
	
	
	print "\n\n\n\n"
	
	
	# Second, create a new Status
	status_nodescr = Status.create("Status Without Description", color="#678434")
	status = Status.create("Status With Description", "A description", misc="haha")
	
	printv(status_nodescr, "STATUS W/O DESCRIPTION")
	printv(status, "STATUS WITH DESCRIPTION")
	print(status.data, "DATA of STATUS")
	
	# Get a status by name
	status_by_name = Status.get_status("Status Without Description")
	
	printv(status_by_name, "GET STATUS W/O DESCRIPTION BY NAME")
	
	# Delete the status without a description
	status_by_name.delete()
	
	print "NO DESCRIPTION STILL SAVED? (should be False)\n======================="
	print Status.is_saved(name="Status Without Description")
	
	status2 = Status.create("Status2", "A 2nd description", possible=5)






	# Now test Surfices
	# First manually
	surfice_manual = Surfice()
	surfice_manual.name = "Manual Surfice"
	surfice_manual.surf = surf_auto
	surfice_manual.description = "Manual Description"
	surfice_manual.status = status # I'm afraid this won't work at all
	surfice_manual.save_new()
	
	printv(surfice_manual, "MANUAL SURFICE")
	
	# Now Delete the manual one
	surfice_manual.delete()
	
	print "Is the Manual Surfice still saved (It should be False)\n========================"
	print Surfice.is_saved(name=surfice_manual.name)
	
	
	# Now Automatically
	surfice = Surfice.create("Surfice", surf_auto, status, "A Surfice description")
	surfice2 = Surfice.create("Surfice2", surf_auto, status, "A 2nd Surfice description")
	printv(surfice, "SURFICE")
	printv(surfice2, "SURFICE2")
	
	# Try to overwrite it
	surfice_overwrite = Surfice.create("Surfice", surf_auto, status)
	print surfice_overwrite
	
	# Get surfices
	print Surfice.get_surfices(surf=surf_auto) # All the surfices under surf_auto
	print Surfice.get_surfices(name="Surf") # All that contain "Surf"
	print Surfice.get_surfices() # All surfices
	
	
	surfice.set_surf(surf_auto_nodescr)
	surfice.set_description("I have a description now!")
	surfice.set_name("Surfice1")
	surfice.set_status(status2) # Just changes the status without making an event
	surfice.set_status(status, "What happened now?")
	surfice.set_status(status2, "Okay now we're back at status2")
	printv(surfice, "SURFICE1 UPDATED")
	
	
	
	# Delete everything
	try: surf_auto.delete()
	except: pass
	try: surf_auto_nodescr.delete()
	except: pass
	
	try: status_nodescr.delete()
	except: pass
	
	try: status.delete()
	except: pass
	try: status2.delete()
	except: pass
	
	try: surfice.delete()
	except: pass
	try: surfice2.delete()
	except: pass