def updatePShare(id): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: pshare = PShare.query.filter(PShare.Id == id).first() if pshare is None: RETURNVALUE[CODE] = BackendErrorCode.PSHARE_NOT_EXIST_ERROR RETURNVALUE[MESSAGE] = BackendErrorMessage.PSHARE_NOT_EXIST_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) pshare.HEAT = pshare.HEAT + 1 log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = BackendErrorMessage.SYSTEM_ERROR log.error(e.message) return buildReturnValue(RETURNVALUE)
def login(auth): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: if auth.username != ADMIN: user = User.query.filter(User.Tel == auth.username).first() timeInterval = 12960000 #24 * 60 * 60 * 5 * 30 if (int(time.time()) - int(time.mktime(time.strptime(str(user.CreateTime), "%Y-%m-%d %H:%M:%S")))) > timeInterval: log.error("CheckSigKey: User %s sigkey invalid, time- %s", str(user.Tel), user.CreateTime) (errorCode, output) = sigKey(0, user.Tel, APP_ID) if errorCode != 0: RETURNVALUE[MESSAGE] = output RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR log.error("CheckSigKey: user %s sigkey create failed.", str(user.Tel)) return buildReturnValue(RETURNVALUE) log.info("CheckSigKey: user %s sigkey create success.", str(user.Tel)) user.SigKey = output user.CreateTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') data = {} data['sigKey'] = user.SigKey RETURNVALUE[VALUE].append(data) log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = BackendErrorMessage.SYSTEM_ERROR log.error(e.message) return buildReturnValue(RETURNVALUE)
def bindUserNAS(param, nasId, tel): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: userNas = UserNAS.query.filter(UserNAS.NasId == nasId, UserNAS.Tel == tel).first() if userNas is not None: RETURNVALUE[CODE] = BackendErrorCode.NAS_USER_IS_BIND_ERROR RETURNVALUE[MESSAGE] = BackendErrorMessage.NAS_USER_IS_BIND_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) userNas = UserNAS() userNas.Tel = tel userNas.NasId = nasId userNas.IsAdmin = param['isAdmin'] db.session.add(userNas) log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def removePShare(param): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: pshares = json.loads(param["params"].replace("'", '"').replace( "u", ' '))["pshares"] for pshare in pshares: if pshare.has_key('shareId'): shareId = pshare['shareId'] else: RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = "shareId necessary parameters" log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) if pshare.has_key('nasId'): nasId = pshare['nasId'] else: RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = "nasId necessary parameters" log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) if pshare.has_key('type'): typeFilter = PShare.Type == pshare['type'] #shareWith = str(pshare['shareWith']).replace("'", '"').replace("u", ' ') shareIdFilter = PShare.ShareId == shareId nasIdFilter = PShare.NasId == nasId if pshare.has_key('type'): PShare.query.filter(shareIdFilter, nasIdFilter, typeFilter).delete() else: PShare.query.filter(shareIdFilter, nasIdFilter).delete() #if type == 8: # PShare.query.filter(shareIdFilter, nasIdFilter, typeFilter).delete() #else: # if shareWith == 'None': # PShare.query.filter(shareIdFilter, nasIdFilter, typeFilter).delete() # else: # shareWithFilter = PShare.ShareWithHash == hash(str(shareWith)) # PShare.query.filter(shareIdFilter, nasIdFilter, typeFilter, shareWithFilter).delete() log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def updatePwd(tel, param): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: """ TO DO validata old pwd model """ # user = User.query.filter(User.Tel == param['tel']).first() # if user is None: # RETURNVALUE[MESSAGE] = "user does not exist." # RETURNVALUE[CODE] = 1 # else: # if user.Pwd != hashlib.md5(param['oldPwd']).hexdigest(): # RETURNVALUE[MESSAGE] = "User validation failure." # RETURNVALUE[CODE] = 1 # else: # user.Pwd = hashlib.md5(param['newPwd']).hexdigest() """ randomCode model """ user = User.query.filter(User.Tel == tel).first() if user is None: RETURNVALUE[MESSAGE] = BackendErrorMessage.USER_NOT_EXIST_ERROR RETURNVALUE[CODE] = BackendErrorCode.USER_NOT_EXIST_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) # check randomCode (userSession, errorCode, errorMessage) = checkRandomCodeIsValid(user.Tel, param['randomCode']) if errorCode != 0: RETURNVALUE[MESSAGE] = errorMessage RETURNVALUE[CODE] = errorCode log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) # update pwd user.Pwd = hashlib.md5(param['newPwd']).hexdigest() # remove userSession db.session.delete(userSession) log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def getRandomCode(areacode, tel): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: # create Random Code randomCode = createPhoneCode() # send Random Code code = [] code.append(randomCode) code.append(RANDOM_CODE_TIMEOUT) sendMessage = senMessage(code, areacode, tel) log.info(sendMessage) if sendMessage['result'] != 0: RETURNVALUE[MESSAGE] = sendMessage['errmsg'] RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) mobile = str(areacode) + str(tel) userSession = UserSession.query.filter(UserSession.Tel == mobile).first() if userSession is None: userSession = UserSession() # save random code userSession.Tel = mobile userSession.RandomCode = randomCode db.session.add(userSession) # build return value data = {} data['randomCode'] = randomCode RETURNVALUE[VALUE].append(data) log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def addPShare(param): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: pshare = PShare.query.filter(PShare.ShareId == param['shareId'], PShare.NasId == param['nasId']).first() if pshare is not None: RETURNVALUE[CODE] = BackendErrorCode.PSHARE_IS_EXIST_ERROR RETURNVALUE[MESSAGE] = BackendErrorMessage.PSHARE_IS_EXIST_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) pshare = PShare() pshare.Name = param['name'] #pshare.Pwd = hashlib.md5(param['pwd']).hexdigest() pshare.Pwd = param['pwd'] pshare.NasId = param['nasId'] pshare.ShareId = param['shareId'] pshare.Tel = param['tel'] pshare.Type = param['type'] #shareWith = param['shareWith'].replace("'", '"').replace("u", ' ') #pshare.ShareWith = shareWith #pshare.ShareWithHash = hash(shareWith) shareWith = param['shareWith'].replace("'", '"').replace("u", ' ') pshare.ShareWith = shareWith pshare.Notes = param['notes'] pshare.HEAT = param['heat'] pshare.Thumbnail = param['thumbnail'] db.session.add(pshare) log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def removePShareByIds(param): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: ids = param['ids'] idsFilter = [] for x in ids.split(","): idsFilter.append(x) rule = or_(*[PShare.Id == w for w in idsFilter]) PShare.query.filter(rule).delete() log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def getUserNASDevices(tel): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: UserNASList = UserNAS.query.filter(UserNAS.Tel == tel).all() for data in UserNASList: userNas = {} userNas['tel'] = data.Tel userNas['nasId'] = data.NasId userNas['isAdmin'] = data.IsAdmin RETURNVALUE[VALUE].append(userNas) log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def checkTel(param): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: user = User.query.filter(User.Tel == param).first() if user is not None: RETURNVALUE[CODE] = BackendErrorCode.USER_IS_REGISTERED_ERROR RETURNVALUE[MESSAGE] = BackendErrorMessage.USER_IS_REGISTERED_ERROR if RETURNVALUE[CODE] == 0: log.info(RETURNVALUE) else: log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def removeUserNAS(nasId, tel): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: userNas = UserNAS.query.filter(UserNAS.NasId == nasId, UserNAS.Tel == tel).first() if userNas is None: RETURNVALUE[CODE] = BackendErrorCode.NAS_IS_NOT_BIND_ERROR RETURNVALUE[MESSAGE] = BackendErrorMessage.NAS_IS_NOT_BIND_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) UserNAS.query.filter(UserNAS.NasId == nasId, UserNAS.Tel == tel).delete() log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def getPShares(param): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: page = param['page'] limit = param['limit'] sortField = param['sortField'] sortType = param['sortType'] nasId = param['nasId'] shareId = param['shareId'] tels = param['tels'] type = param['type'] shareWith = param['shareWith'] name = param['name'] if page is None: page = 1 if limit is None: limit = 20 if sortField == 0 or sortField is None: sort = PShare.Name elif sortField == 1: sort = PShare.CreateTime elif sortField == 2: sort = PShare.HEAT else: RETURNVALUE[CODE] = BackendErrorCode.PSHARE_IS_EXIST_ERROR RETURNVALUE[MESSAGE] = BackendErrorMessage.PSHARE_IS_EXIST_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) shareWithWords = [] if shareWith is not None: for x in shareWith.split(","): shareWithWords.append("%" + x.strip() + "%") telsWords = [] if tels is not None: for x in tels.split(","): telsWords.append("%" + x.strip() + "%") if nasId is None: nasIdFilter = PShare.NasId != 0 else: nasIdFilter = PShare.NasId == nasId if shareId is None: shareIdFilter = PShare.ShareId != 0 else: shareIdFilter = PShare.ShareId == shareId # if tel is None: # telFilter = PShare.Tel != 0 # else: # telFilter = PShare.Tel == tel if type is None: typeFilter = PShare.Type != 0 else: typeFilter = PShare.Type == type nameFilter = PShare.Name == name shareWithRule = or_( *[PShare.ShareWith.like(w) for w in shareWithWords]) telsRule = or_(*[PShare.Tel.like(w) for w in telsWords]) if sortType == 0 or sortType is None: # pshares = PShare.query.order_by(sort).limit(limit).all() # datas = PShare.query.filter(PShare.Name == name if name is not None else "*", PShare.NasId == nasId if nasId is not None else "*", PShare.Tel == tel if tel is not None else "*", PShare.Type == type if type is not None else "*").filter(rule).order_by(sort.desc()).paginate(page, limit, False).items if name is None: datas = PShare.query.filter( nasIdFilter, shareIdFilter, typeFilter).filter( shareWithRule).filter(telsRule).order_by( sort.desc()).paginate(page, limit, False).items else: datas = PShare.query.filter( nasIdFilter, shareIdFilter, typeFilter, nameFilter).filter( shareWithRule).filter(telsRule).order_by( sort.desc()).paginate(page, limit, False).items else: if name is None: datas = PShare.query.filter( nasIdFilter, shareIdFilter, typeFilter).filter( shareWithRule).filter(telsRule).order_by( sort.desc()).paginate(page, limit, False).items else: datas = PShare.query.filter( nasIdFilter, shareIdFilter, typeFilter, nameFilter).filter( shareWithRule).filter(telsRule).order_by( sort.desc()).paginate(page, limit, False).items # build return value for data in datas: pshare = {} pshare['id'] = data.Id pshare['name'] = data.Name pshare['pwd'] = data.Pwd pshare['nasId'] = data.NasId pshare['shareId'] = data.ShareId pshare['createTime'] = data.CreateTime.strftime( '%Y-%m-%d %H:%M:%S') pshare['tel'] = data.Tel pshare['type'] = data.Type pshare['shareWith'] = json.loads(data.ShareWith) pshare['notes'] = data.Notes pshare['heat'] = data.HEAT pshare['thumbnail'] = data.Thumbnail RETURNVALUE[VALUE].append(pshare) log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)
def register(param): RETURNVALUE = {} RETURNVALUE[VALUE] = [] RETURNVALUE[CODE] = 0 RETURNVALUE[MESSAGE] = None try: type = param['type'] #check user is registered if type == 0: if param['areaCode'] is None: RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = "areaCode necessary parameters when type = 1." log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) mobile = str(param['areaCode']) + str(param['tel']) user = User.query.filter(User.Tel == mobile).first() else: mobile = param['tel'] user = User.query.filter(User.Tel == mobile).first() if user is not None: if type == 1: user.Pwd = hashlib.md5(param['pwd']).hexdigest() RETURNVALUE[VALUE].append(user.SigKey) log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) RETURNVALUE[CODE] = BackendErrorCode.USER_IS_REGISTERED_ERROR RETURNVALUE[MESSAGE] = BackendErrorMessage.USER_IS_REGISTERED_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) user = User() user.Pwd = hashlib.md5(param['pwd']).hexdigest() user.Email = param['email'] user.Name = param['name'] user.Tel = mobile if type == 0: randomCode = param['randomCode'] # chec random code (userSession, errorCode, errorMessage) = checkRandomCodeIsValid(user.Tel, randomCode) if errorCode != 0: RETURNVALUE[CODE] = errorCode RETURNVALUE[MESSAGE] = errorMessage log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) else: nasDevices = NASDevices() nasDevices.NasId = mobile nasDevices.IP = updateIpToNumber(param['ip']) nasDevices.MAC = param['mac'] # Allocation PPDeviceID iReqID = allocationPPDeviceID(user, 0, user.Tel) if iReqID < 0 and iReqID != -17: RETURNVALUE[MESSAGE] = BackendErrorMessage.SYSTEM_ERROR RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) # createSigKey (errorCode, output) = sigKey(0, mobile, APP_ID) if errorCode != 0: RETURNVALUE[MESSAGE] = output RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR log.error(RETURNVALUE) return buildReturnValue(RETURNVALUE) # add user user.SigKey = output db.session.add(user) # add nas if type == 1: db.session.add(nasDevices) # remove userSession if type == 0: db.session.delete(userSession) RETURNVALUE[VALUE].append(user.SigKey) log.info(RETURNVALUE) return buildReturnValue(RETURNVALUE) except Exception as e: dbRollback(db) RETURNVALUE[CODE] = BackendErrorCode.SYSTEM_ERROR RETURNVALUE[MESSAGE] = e log.error(e.message) return buildReturnValue(RETURNVALUE)