示例#1
0
def surfs(request):
	context_dict = {}
	
	# If the admin is trying to create or delete a Surf, the page is refreshed
	flag = False
	if request.method == 'POST':
		
		# Is the admin trying to delete a surf?
		if	(
				'delete' in request.POST and
				'surf' in request.POST
			):
			
			# Get the surf that we're about to delete
			surf = Surf.get_surf(pk=request.POST['surf'])
			
			if type(surf) is Surf:
				# Add code here to delete related events and dings
			
				# Go ahead and delete the surf now that everything has be re-assigned
				surf.delete()
		
		# Is the admin trying to create a surf?
		elif 'name' in request.POST:
				
			surf = Surf.create(request.POST['name'], request.POST.get('description', ''))
			
			# If the surf wasn't created, throw a flag
			if surf == None:
				flag = True
			
			# If the surf was created, assign surfices to it
			else:
				surfices = []
				if 'surfices' in request.POST:
					# Loop through the passed pks and append the surf to surfs array
					for pk in request.POST.getlist('surfices'):
						surfices.append( Surfice.get_surfice(pk=pk) )
				surf.surfices = surfices
		
		# 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').prefetch_related('surfices')
	context_dict['surfs'] = surf_list
	
	# Query all the Surfices and add them to context_dict
	surfice_list = Surfice.get_surfices().order_by('name')
	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_surfs.html', context_dict)
示例#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)
示例#3
0
def admin(request):

	#user = LDAPBackend().get_user_model()

	context_dict = {}
	
	# Query for surfs and add them to context_dict
	surf_list = Surf.get_surfs().order_by('name')
	context_dict['surfs'] = surf_list
	
	# For each Surf, query for Surfices and add them to context_dict
	#for i, surf in enumerate(context_dict['surfs']):
		#context_dict['surfs'][i].surfices = surf_list[i].surfice_set.all()
	
	# Query for Surfices and add them to context_dict
	surfice_list = Surfice.get_surfices().order_by('name')
	context_dict['surfices'] = surfice_list
	
	# Query for Events and add them to context_dict
	event_list = Event.get_events()
	context_dict['events'] = event_list
	
	# Query the database for a list of all the events
	# Place them in context_dict
	event_list = Event.get_events()
	context_dict['events'] = event_list[:20]
	
	# Split events into future and past events
	context_dict['events_future'] = event_list.filter(timestamp__gt=timezone.now())[:20]
	context_dict['events_past'] = event_list.filter(timestamp__lte=timezone.now())[:20]
	
	# Query for 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()) )
	
	# print request.META['HTTP_USER_AGENT']
# 	ua_string = request.META['HTTP_USER_AGENT']
# 	user_agent = parse(ua_string)
	
	# Accessing user agent's browser attributes
	# print user_agent.browser  # returns Browser(family=u'Mobile Safari', version=(5, 1), version_string='5.1')
# 	print user_agent.browser.family  # returns 'Mobile Safari'
# 	print user_agent.browser.version  # returns (5, 1)
# 	print user_agent.browser.version_string   # returns '5.1'
# 
# 	# Accessing user agent's operating system properties
# 	print user_agent.os  # returns OperatingSystem(family=u'iOS', version=(5, 1), version_string='5.1')
# 	print user_agent.os.family  # returns 'iOS'
# 	print user_agent.os.version  # returns (5, 1)
# 	print user_agent.os.version_string  # returns '5.1'
# 	
# 	# Accessing user agent's device properties
# 	print user_agent.device  # returns Device(family='iPhone')
# 	print user_agent.device.family  # returns 'iPhone'
# 	
# 	print request.META['REMOTE_HOST']
# 	print request.META['REMOTE_ADDR']
	
	return render(request, 'surfice/base_admin.html', context_dict)