def logout(req): #捕获异常 logger = logging.getLogger('mysiteApp.views') try: #将json数据转化为python数据格式 data = simplejson.loads(req.body) #获取用户令牌 customerToken = data['token'] token = Token() #依据用户令牌从数据库获取token token = Token.objects.get(token=customerToken) token.delete() result = {'successful': True, 'error': {'id': '', 'message': ''}} except Exception as e: logger.error(e.args) result = { 'successful': False, 'error': { 'id': '1024', 'message': e.args } } finally: logger.disabled = True return HttpResponse(simplejson.dumps(result), content_type="application/json")
def info_update(req): logger = logging.getLogger('mysiteApp.views') try: data = simplejson.loads(req.body) user = User() token = Token() token = Token.objects.get(token=data['token']) user = token.user if 'real_name' in data['user']: user.real_name = noneIfEmptyString(data['user']['real_name']) if 'height' in data['user']: user.height = noneIfEmptyString(data['user']['height']) if 'weight' in data['user']: user.weight = noneIfEmptyString(data['user']['weight']) if 'sex' in data['user']: user.sex = noneIfEmptyString(data['user']['sex']) if 'birthday' in data['user']: user.birthday = noneIfEmptyString(data['user']['birthday']) if 'email' in data['user']: user.email = noneIfEmptyString(data['user']['email']) user.save() result = {'successful': True, 'error': {'id': '', 'message': ''}} except Exception as e: logger.error(e.args) result = { 'successful': False, 'error': { 'id': '1024', 'message': e.args } } finally: logger.disabled = True return HttpResponse(simplejson.dumps(result), content_type="application/json")
def daily_data_upload(req): #获取日志 logger=logging.getLogger('mysiteApp.views') try: #将json数据转化为python数据格式 data = simplejson.loads(req.body) #通过令牌从数据库获取用户 token=Token() token=Token.objects.get(token=data['token']) user=User() user=token.user if ('steps' in data['data']): #创建空的singles list, stepslist = [] #从data中获取一系列singal中的数据 for tmp in data['data']['signal']: #创建一个model中的Single对象 steps = Steps() #将signal数据写入 steps.steps=tmp['steps'] #将时间戳写入 steps.timestamp=tmp['timestamp'] #将这个对象追加到singles list中 tepslist.append(signal) #批量创建对象,导入大量数据,提升性能,避免逐条插入 Signal.objects.bulk_create(signals) #构建一个表示成功的dict result = { 'successful':True, 'error':{ 'id':'', 'message': '' } } #将表示成功的dict以json返回到前段 #return HttpResponse(simplejson.dumps(result), content_type="application/json") #捕获异常 except Exception as e: logger.error(e.args) #构建一个表示失败的dict result={ 'successful': False, 'error' : { 'id' : '1024', 'message' : e.args } } finally: logger.disabled=True #返回结果 return HttpResponse(simplejson.dumps(result), content_type="application/json")
def password_update(req): logger = logging.getLogger('mysiteApp.views') try: data = simplejson.loads(req.body) user = User() token = Token() token = Token.objects.get(token=data['token']) user = token.user if (data['user']['old_password'] != user.password): raise myError('原密码输入错误!') user.password = data['user']['new_password'] user.save() result = {'successful': True, 'error': {'id': '', 'message': ''}} except myError as e: logger.error(e.value) result = { 'successful': False, 'error': { 'id': '3', 'message': e.value } } except Exception as e: logger.error(e.args) result = { 'successful': False, 'error': { 'id': '1024', 'message': e.args } } finally: logger.disabled = True return HttpResponse(simplejson.dumps(result), content_type="application/json")
def info(req): logger = logging.getLogger('mysiteApp.views') try: data = simplejson.loads(req.body) token = Token() token = Token.objects.get(token=data['token']) customerUser = User() customerUser = token.user result = { 'user': { "name": customerUser.name, "password": customerUser.password, "real_name": customerUser.real_name, "height": customerUser.height, "weight": customerUser.weight, "sex": customerUser.sex, "birthday": str(customerUser.birthday), "email": customerUser.email }, 'successful': True, 'error': { 'id': '', 'message': '' } } except Exception as e: logger.error(e.args) result = { 'successful': False, 'error': { 'id': '1024', 'message': e.args } } finally: logger.disabled = True return HttpResponse(simplejson.dumps(result), content_type="application/json")
def login(req): #获取日志 logger = logging.getLogger('mysiteApp.views') try: #将json数据转化为python数据格式 data = simplejson.loads(req.body) #将提交的用户名保存在name变量中 name = data['user']['name'] #将提交的用户密码保存在password变量中 password = data['user']['password'] #登录名与密码出现错误 customerUser = User() #根据用户名获取密码 customerUser = User.objects.get(name=name) #判断密码是否正确 if (password == customerUser.password): #密码正确则获取Token token = Token() token = Token.objects.filter(user=customerUser) #首次登陆token为空则删除 if (len(token) != 0): token.delete() else: #若密码不符则抛出异常 raise myError('登录名与密码出现错误!') #生成随机字符串 customerToken = ''.join( random.sample(string.ascii_letters + string.digits, 30)) #将信息写入token对象 token = Token() token.token = customerToken token.user = customerUser token.expire = '-1' #将Token对象写入数据库 token.save() #构建一个表示成功的dict,data中的token字段为生成的随机令牌 result = { 'data': { 'token': customerToken, #????? 'expire': -1 }, 'successful': True, 'error': { 'id': '', 'message': '' } } #捕获异常 except myError as e: logger.error(e.value) #构建一个表示失败的dict result = { 'successful': False, 'error': { 'id': '1', 'message': e.value } } except Exception as e: logger.error(e.args) #构建一个表示失败的dict result = { 'successful': False, 'error': { 'id': '1024', 'message': e.args } } finally: logger.disabled = True #将表示成功的dict以json返回到前段 return HttpResponse(simplejson.dumps(result), content_type="application/json")
def data_upload(req): #获取日志 logger=logging.getLogger('mysiteApp.views') try: #将json数据转化为python数据格式 data = simplejson.loads(req.body) #通过令牌从数据库获取用户 token=Token() token=Token.objects.get(token=data['token']) user=User() user=token.user if ('signal' in data['data']): #创建空的singles list, signals = [] #从data中获取一系列singal中的数据 for tmp in data['data']['signal']: #创建一个model中的Single对象 signal = Signal() #将signal数据写入 signal.signal_strength=tmp['signal_strength'] #将时间戳写入 signal.timestamp=tmp['timestamp'] #将这个对象追加到singles list中 signals.append(signal) #批量创建对象,导入大量数据,提升性能,避免逐条插入 Signal.objects.bulk_create(signals) if ('pressure' in data['data']): #创建空的pressures list pressures = [] #从data中获取一系列pressures中的数据 for tmp in data['data']['pressure']: #创建一个model中的pressures对象 pressure = Pressure() #将user数据写入,对应每个用户的压力 pressure.user=user #将device_id数据写入,对应每个设备的id pressure.device_id=data['device_id'] #将时间戳写入 pressure.timestamp=tmp['timestamp'] #将不同pressure传感器的数据写入 pressure.a=float(tmp['a']) pressure.b=float(tmp['b']) pressure.c=float(tmp['c']) pressure.d=float(tmp['d']) #将这个对象追加到pressures list中 pressures.append(pressure) #批量创建对象,导入大量数据,提升性能,避免逐条插入 Pressure.objects.bulk_create(pressures) if ('acceleration' in data['data']): #创建空的accelerations list accelerations = [] #从data中获取一系列acceleration中的数据 for tmp in data['data']['acceleration']: #创建一个model中的Acceleration对象 acceleration=Acceleration() #将user数据写入,对应每个用户的Acceleration acceleration.user=user #将device_id数据写入,对应每个设备的id acceleration.device_id=data['device_id'] #将时间戳写入 acceleration.timestamp=tmp['timestamp'] #将不同acceleration传感器的数据写入 acceleration.x=float(tmp['x']) acceleration.y=float(tmp['y']) acceleration.z=float(tmp['z']) #将这个对象追加到acceleration list中 accelerations.append(acceleration) #批量创建对象,导入大量数据,提升性能,避免逐条插入 Acceleration.objects.bulk_create(accelerations) if ('angleAcceleration' in data['data']): #创建空的angleAcceleration list angleAccelerations = [] #从data中获取一系列angleAcceleration中的数据 for tmp in data['data']['angleAcceleration'] : #创建一个model中的angleAcceleration对象 angleAcceleration = AngleAcceleration() #将user数据写入,对应每个用户的angleAcceleration angleAcceleration.user = user #将device_id数据写入,对应每个设备的id angleAcceleration.device_id = data['device_id'] #将时间戳写入 angleAcceleration.timestamp = tmp['timestamp'] #将不同angleAcceleration传感器的数据写入 angleAcceleration.x = float(tmp['x']) angleAcceleration.y = float(tmp['y']) angleAcceleration.z = float(tmp['z']) #将这个对象追加到angleAcceleration list中 angleAccelerations.append(angleAcceleration) #批量创建对象,导入大量数据,提升性能,避免逐条插入 AngleAcceleration.objects.bulk_create(angleAccelerations) #构建一个表示成功的dict result = { 'successful':True, 'error':{ 'id':'', 'message': '' } } #将表示成功的dict以json返回到前段 #return HttpResponse(simplejson.dumps(result), content_type="application/json") #捕获异常 except Exception as e: logger.error(e.args) #构建一个表示失败的dict result={ 'successful': False, 'error' : { 'id' : '1024', 'message' : e.args } } finally: logger.disabled=True #返回结果 return HttpResponse(simplejson.dumps(result), content_type="application/json")
def data_query(req): #获取日志 logger=logging.getLogger('mysiteApp.views') try: #将json数据转化为python数据格式 data = simplejson.loads(req.body) #通过令牌从数据库获取用户 token=Token() token=Token.objects.get(token=data['token']) user=User() user=token.user #创建一个model中的pressures对象 pressure=Pressure() #获取该对象的迭代器,筛选条件为特定用户 pressure=Pressure.objects.filter(user=user) #创建一个空的pressureList pressureList=[] #从迭代器中循环获取每一条记录的数据 for tmp in pressure: #将获取的数据组成一个dict追加到pressureList中 pressureList.append({'a':tmp.a, 'b':tmp.b, 'c':tmp.c, 'd':tmp.d}) #创建一个model中的Acceleration对象 acceleration=Acceleration() #获取该对象的迭代器 acceleration=Acceleration.objects.filter(user=user) #创建一个空的accelerationList accelerationList=[] #从迭代器中循环获取每一条记录的数据 for tmp in acceleration: #将获取的数据组成一个dict追加到accelerationList中 accelerationList.append({'x':tmp.x, 'y':tmp.y, 'z':tmp.z}) #创建一个model中的AngleAcceleration对象 angleAcceleration = AngleAcceleration() #获取该对象的迭代器 angleAcceleration = AngleAcceleration.objects.filter(user = user) #创建一个空的angleAccelerationList angleAccelerationList = [] #从迭代器中循环获取每一条记录的数据 for tmp in angleAcceleration : #将获取的数据组成一个dict追加到accelerationList中 angleAccelerationList.append({'x':tmp.x, 'y':tmp.y, 'z':tmp.z}) #构建一个表示成功的dict,data中的pressures,accelerations,angleAccelerationList为查询结果 result = { 'successful':True, 'data':{ 'pressures':pressureList, 'accelerations':accelerationList, 'angleAccelerations':angleAccelerationList, 'id':'', 'message': '' } } #捕获异常 except Exception as e: logger.error(e.args) #构建一个表示失败的dict result={ 'successful': False, 'error' : { 'id' : '1024', 'message' : e.args } } finally: #返回结果 logger.disabled=True return HttpResponse(simplejson.dumps(result), content_type="application/json")