示例#1
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)
示例#2
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)
示例#3
0
def index(request):
	if DEBUG: debug()
	
	# Query the database for a list of ALL surfices currently stored.
	# Order them by status in descending order
	# Retrieve the top 5 only (the "-" in front of it) - or all if less than 5
	# Place the list in our context_dict dictionary which
	# will be passed to the template engine
	#surfice_list = Surfice.objects.order_by('-status')[:5]
	#context_dict = {'surfices': surfice_list}
	
	# Query the database for a list of ALL surfices currently stored.
	surfice_list = Surfice.get_surfices().order_by('name')
	
	# Place the list in our context_dict dictionary which
	# will be passed to the template engine
	context_dict = {'surfices': surfice_list}
	
	# Split surfices into priority and regular surfices
	surfices_priority = []
	surfices_regular = []
	for surfice in surfice_list:
		# If the priority attribute isn't set, it's a regular surfice
		priority = int(surfice.data.get('priority', 0))
		
		# If it is set, and it's set to 1, add it to the priority list
		if priority == 1:
			surfices_priority.append(surfice)
		
		# If it's not 1, add it to the regular list
		else:
			surfices_regular.append(surfice)
	
	print surfices_regular
	context_dict['surfices_priority'] = surfices_priority
	context_dict['surfices_regular'] = surfices_regular
	
	# Get dings within past 24 hours for each surfice
	days = 1
	start = date.today() - timedelta(days)
	for i, surfice in enumerate(context_dict['surfices']):
		context_dict['surfices'][i].dings = surfice_list[i].ding_set.filter(timestamp__gte=date.today())
		context_dict['surfices'][i].events_future = surfice_list[i].event_set.filter(timestamp__gt=timezone.now()).order_by('-timestamp')[:10]
		context_dict['surfices'][i].events_past = surfice_list[i].get_events(days=7).filter(timestamp__lte=timezone.now())[:1]
		
		context_dict['surfices'][i].events = surfice_list[i].get_events(days=7)[:10]
		context_dict['surfices'][i].events_length = len(context_dict['surfices'][i].events_past) + len(context_dict['surfices'][i].events_future)
	
	# Query the database for a list of all the events
	# Place them in context_dict
	event_list = Event.get_events(days=7)
	context_dict['events'] = event_list[:10]
	
	# Split events into future and past events
	context_dict['events_future'] = event_list.filter(timestamp__gt=timezone.now())[:10]
	context_dict['events_past'] = event_list.filter(timestamp__lte=timezone.now())[:10]
	
	
	# Get a list of available statuses for reporting dings
	# Place them in 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
	
	# Query for surfs = add the list to our context dictionary.
	#surf_list = Surf.objects.order_by('-name')[:5]
	#context_dict = {'surfs': surf_list}
	
	# We loop through each category returned, and create a URL attribute.
	# This attribute stores an encoded URL
	#for surf in surf_list:
	#	surf.url = surf.name.replace(' ', '_')
	
	# The context is already figured out by the render() shortcut so...
	# Render the response and send it back!
	return render(request, 'surfice/base_index.html', context_dict)