示例#1
0
文件: views.py 项目: aucms/tsar
def event_response(request):
	c = {}
	c.update(csrf(request))
	# Receives data with information about which MemberResposne must be modified
	# or created. For this we need:
	#	. the member_id
	#	. the eventrole_id
	#	. the action
	# If the member_id is missing, we use the currently logged on member.
	# TODO: Perhaps it's better to always pass the member_id on with the other two from the view...

	print 'in event_response'
	# Ensure we're getting an AJAX POST.
	if not request.is_ajax():
		print 'not AJAX'
		return False

	print 'check member'
	# Check whether we've been handed a member_id:
	try:
		member_id = request.POST['member_id']
		cm_responding = False
		print 'member_id sent through post: {}'.format(member_id)
	except:
		member_id = request.user.member.id
		cm_responding = True
		print 'member_id set to current member'

	# Get the eventrole_id and action:
	print 'get eventrole_id'
	print request.POST
	try:
		eventrole_id = request.POST['eventrole_id']
	except:
		print 'no eventrole_id'
		return HttpResponse(json.dumps({ 'type': 'error', 'message': 'The POST data is missing the eventrole_id.'}))
	print 'eventrole_id: {}'.format(eventrole_id)
	action = request.POST['action']
	if action == 'attend':
		act = 'Y'
	elif action == 'absent':
		act = 'N'

	print 'action: {} or {}'.format(action,act)
	print 'eventrole.id: {}'.format(eventrole_id)

	# Get the relevant objects:
	member = Member.objects.get(id=member_id)
	event_role = get_object_or_404(EventRole, id=eventrole_id)
	#event_role = EventRole.objects.get(event_role__id=eventrole_id)

	# See if there already exists a member response:
	try:
		mr = MemberResponse.objects.get(event_role=event_role, member=member)
		print '>> MemberResponse {} found'.format(mr)
		mr.response=act
		mr.time_responded=timezone.now()
	# Otherwise create a new one:
	except:
		mr = MemberResponse(event_role=event_role, member=member, response=act)
		print '>> No MemberResponse found. Creating a new one: {}'.format(mr)

	# Clean and save:
	try:
		mr.clean_fields()
		mr.save()
		print '>> SAVED'
	except:
		print '>> FAILED'
		return HttpResponse('Failed: could not save member response')

	##print 'Hér kemur JSON útgáfan:'
	##data = json.loads(request.POST['data'])
	##pprint.pprint(data)

	print mr.time_responded
	data = {
		'user_id': member.user.id,
		'user_name': member.__unicode__(),
		'username': member.user.username,
		'cm_responding': cm_responding,
		'event_id': event_role.event.id,
		'role_id': event_role.role.id,
		'eventrole_id': event_role.id,
		'action': action,
		'time_responded': mr.time_responded,
	}
	print 'Data: {}'.format(data)
	jsondata = json.dumps(data, cls=DjangoJSONEncoder)
	print 'JSON: ',jsondata
	print 'XXXXX responding'
	#return HttpResponse(json.dumps(data))##, mimetype='application/javascript')
	return HttpResponse(jsondata)
示例#2
0
文件: views.py 项目: snjokall/tsar
def event_response(request):
	print 'in event_response'
	if not request.user.is_authenticated():
		cm = User.objects.get(id=2)
	else:
		cm = request.user.member
	print cm
	if request.is_ajax():
		#import pprint
		action = request.POST['action']
		if action == 'attend':
			act = 'Y'
		elif action == 'absent':
			act = 'N'
		eventrole_id = request.POST['eventrole']

		print 'action: {} or {}'.format(action,act)
		print 'eventrole.id: {}'.format(eventrole_id)

		print 'OK, now for my second act...'
		#TODO: read current member info from login!!!
		#mr = MemberResponse(event_role__id=eventrole_id, member=cm, response='Y')
		#event = get_object_or_404(Event, id=pk)
		#print event
		event_role = get_object_or_404(EventRole, id=eventrole_id)
		#event_role = EventRole.objects.get(event_role__id=14)
		print event_role.id
		try:
			mr = MemberResponse.objects.get(event_role=event_role, member=cm)
			print '>> MemberResponse {} found'.format(mr)
			mr.response=act
		except:
			mr = MemberResponse(event_role=event_role, member=cm, response=act)
			print '>> No MemberResponse found. Creating a new one: {}'.format(mr)

		try:
			mr.clean_fields()
			mr.save()
			print '>> SAVED'
		except:
			print '>> FAILED'
			return HttpResponse('Failed: could not save member response')
		##print 'Hér kemur JSON útgáfan:'
		##data = json.loads(request.POST['data'])
		##pprint.pprint(data)
	#else:
		#print 'not ajax'
	#TODO: Have to return the change so that the script can update the attendance list.
	data = {
		'user_id': cm.user.id,
		'user_name': cm.__unicode__(),
		'username': cm.user.username,
		'event_id': event_role.event.id,
		'role_id': event_role.role.id,
		'eventrole_id': event_role.id,
		'action': action,
	}
	print 'Data: {}'.format(data)
	jsondata = json.dumps(data)
	print 'JSON: ',jsondata
	print 'XXXXX responding'
	return HttpResponse(json.dumps(data))##, mimetype='application/javascript')
示例#3
0
文件: views.py 项目: ZmjbS/tsar
def checkin_edit(request):
	print('Checkin in progress!')
	if request.is_ajax():
		print('Request is AJAX')
		data = request.POST['data']
		event_id = request.POST['event_id']
		new_data = json.loads(data)
		# Lets find out if the member already has an attendance logged
		for row in new_data:
			print(row[u'role_id'])
			print(row[u'member'])
			try:
				ma = MemberAttendance.objects.get(event_role_id=row[u'role_id'], member_id=row[u'member'])
				print(row[u'id'])
				if "checkin" in row[u'id']:
					if row[u'value'] == "":
						print('Value is empty')
						ma.delete()
						print('DELETED')
					else:
						print('Time to save this checkin time'+row[u'value'])
						ma.time_checkin= row[u'value']
						ma.save()
				elif "checkout" in row[u'id']:
					if row[u'value'] == "":
						print('Value is empty')
						ma.time_checkout = None
						ma.save()
					else:
						print('Time to save this checkout time'+row[u'value'])
						ma.time_checkout= row[u'value']
						ma.save()
			except Exception as e:
				print('>> No MemberAttendance found. Creating a new one: {}')
				ma = MemberAttendance(event_role_id=row[u'role_id'], member_id=row[u'member'], attendance="Y",time_checkin = datetime.now())
				try: 
					mr = MemberResponse.objects.get(event_role_id=row[u'role_id'], member_id=row[u'member'])
				except Exception as e:
					mr = MemberResponse(event_role_id=row[u'role_id'], member_id=row[u'member'], response="Y",time_responded = datetime.now())
				try: 
					mi = MemberInvitation.objects.get(event_role_id=row[u'role_id'], member_id=row[u'member'])
				except Exception as e:
					mi = MemberInvitation(event_role_id=row[u'role_id'], member_id=row[u'member'])
				
				ma.clean_fields()
				mr.clean_fields()
				mi.clean_fields()
				ma.save()
				mr.save()
				mi.save()
		
	else:
		print('Request is not AJAX')


	datab = {
		'action': data
		}
	#jsondata = json.dumps(datab)
	print('After if')
	return HttpResponse(json.dumps(data))