def addDevice(request): """add a device for the logged in user Args: request: django request object Returns: on POST redirects to /devices on GET renders adddevice.html """ context = RequestContext(request) #get token and user id from the django user token, userId = getTokenAndId(request.user) #build apiClient api_client = ApiClient(apiKey=token, apiServer = settings.SAMI_SERVER_URL) #get devices types dictionary deviceTypes = getDeviceTypesForUser(token, userId) if not deviceTypes: print "addDevice: got empty device type list. call showDevices() again and early return" return showDevices(request) contextDict = {'active': 'devices', 'devicesTypes':deviceTypes} if (request.method == 'POST'): #user wants to add a device #retrieve device data from form deviceName = request.POST.get('deviceName', '') deviceType = request.POST.get('deviceType', '') optionManifest = request.POST.get('optionManifest', '') deviceApi = DevicesApi(api_client) #create a body for the post request of the api. Make sure not to send unicode strings. body = {'uid': userId.encode('ascii'), 'dtid':deviceType.encode('ascii'), 'name': deviceName.encode('ascii'), 'manifestVersion': 2, 'manifestVersionPolicy': optionManifest.encode('ascii') } #add the device deviceApi.addDevice(body) return HttpResponseRedirect('/devices') return render_to_response('adddevice.html', contextDict, context)
def showDevices(request): """shows the first ten devices for the logged in user Args: request: django request object Returns: Render devices.html """ context = RequestContext(request) #get token and user id from the django user token, userId = getTokenAndId(request.user) devicesEnvelope = getUserDevices(token, userId) #retrieve count and devices from the devices envelope count = devicesEnvelope.count devices = list(devicesEnvelope.data.devices) #get devices types pretty names to show on the html deviceTypes = getDeviceTypesForUser(token, userId) contextDict = {'active': 'devices', 'count': count, 'devices': devices, 'devicesTypes':deviceTypes} return render_to_response("devices.html", contextDict, context)
def showMessages(request): """show messages for the devices of the logged in samsung user Args: request: django request object Returns: render messages.html """ context = RequestContext(request) #get user_id and token. Needed to retrieve the messages token, userId = getTokenAndId(request.user) #build the message api apiClient = ApiClient(apiKey=token, apiServer = settings.SAMI_SERVER_URL) messagesApi = MessagesApi(apiClient) #get devices associated to a user devicesEnvelope = getUserDevices(token, userId) #build a string containing the devices ids separated by comma devices = ','.join (getDevicesIds(devicesEnvelope)) #get devices types pretty names to show on the html devicesTypes = getDeviceTypesForUser(token, userId) #retrieve the last 20 messages for this devices messageEnvelope = messagesApi.getNormalizedMessagesLast(devices, count = 20) #get the message count from the envelope and the messages count = messageEnvelope.count messages = messageEnvelope.data #get a dict containing the devices ids with an associated device name to show on the html devices = getDeviceIdDict(devicesEnvelope) contextDict = {'active': 'messages', 'count': count, 'messages': messages, 'devicesTypes':devicesTypes, 'devices':devices} return render_to_response("messages.html", contextDict, context)