def put(self,id): apiInstance = api() if not apiInstance._isallowed(self): return if id=='': apiInstance.response(self,'{"status":"error","message":"Resource does not exist"}',404) return apiInstance = api() UserHelperInstance = UserModelHelper() aUser = UserHelperInstance.get('',id) if aUser is None: apiInstance.response(self,'{"status":"error","message":"Resource does not exist"}',404) return payload = json.loads(self.request.body) if payload['method']=='changerole': newrole = payload['role'] if newrole=='manager' or newrole=='user': aUser.role = newrole aUser.put() jsonUser = {"key":str(aUser.key().name()),"uid":aUser.uid,"email":aUser.email,"role":aUser.role} apiInstance.response(self,'{"status":"ok","message":"User role has been updated","user":'******'}') else: apiInstance.response(self,'{"errors":"role can be either or user or manager."}',401) else: apiInstance.response(self,'{"errors":"Invalid Resource."}',404) return
def _createcomment(self): payload = json.loads(self.request.body) apiInstance = api() try: repairid = payload['comment']['repairid'] comment = payload['comment']['text'] except: apiInstance.response( self, '{"error":"repairid and comment are mandatory."}', 401) return if len(comment) < 5: apiInstance.response(self, '{"error":"comment should be meaningful."}', 401) return RepairModelInstance = RepairModelHelper() reqUser = apiInstance.getRequestUser(self) aRepair = RepairModelInstance.get(repairid) ### Removing as (Comment on any Repairs at any time.) #if aRepair is None or (reqUser.role!='manager' and reqUser.email!=aRepair.assignedTo): # apiInstance.response(self,'{"error":"Invalid Resource ID passed."}',401) # return newComment = { 'uid': str(uuid4()), "ts": int(time.time()), "comment": comment, "user": reqUser.email } try: comments = json.loads(aRepair.comments) comments.append(newComment) except: comments = [] comments.append(newComment) aRepair.comments = json.dumps(comments) aRepair.put() repairObject = RepairModelInstance._tojson(aRepair) apiInstance.response( self, '{"message":"Comment Added.","repair":' + json.dumps(repairObject) + '}', 200) apiInstance = api()
def get(self, id): apiInstance = api() if id == 'signout': token = '' # Session Token apiInstance.response( self, '{"status":"ok","message":"Sign Out Successful","token":' + json.dumps(token) + '}') return requestUser = apiInstance.getRequestUser(self) if id == 'user': if requestUser is None: apiInstance.response(self, '{"errors":{"user": "******"}}', 401) return UHI = UtilitiesHelper() atoken = self.request.get('authorization') apiInstance.response( self, '{"user":{"email":' + json.dumps(requestUser.email) + ',"token":' + json.dumps(atoken) + ',"id":' + json.dumps(requestUser.uid) + ',"role":' + json.dumps(requestUser.role) + '}}') else: apiInstance.response(self, '{"error":{"url":"not found."}}', 404)
def post(self): apiInstance = api() if not apiInstance._isallowed(self): return payload = json.loads(self.request.body) repairValidatorInstance = repairValidator() validatorReponse = repairValidatorInstance.validate(payload,apiInstance,self) if len(validatorReponse['errors'])!=0: apiInstance.response(self,'{"errors":'+json.dumps(validatorReponse['errors'])+'}',401) return UtilitiesHelperInstance = UtilitiesHelper() repairDescription = UtilitiesHelperInstance.getValueofKey(payload['repair'],'descr') assignedTo = UtilitiesHelperInstance.getValueofKey(payload['repair'],'assignedTo') scheduledDate = UtilitiesHelperInstance.getValueofKey(payload['repair'],'scheduledDate') scheduledTime = UtilitiesHelperInstance.getValueofKey(payload['repair'],'scheduledTime') logging.info(scheduledDate) logging.info("::") requestUser = apiInstance.getRequestUser(self) createdBy = requestUser.email RepairHelperInstance = RepairModelHelper() aRepair = RepairHelperInstance.create(str(uuid4()),assignedTo,scheduledDate,scheduledTime,createdBy,repairDescription,validatorReponse['proposedStartTS']) repairObj = {"key":aRepair.uid} apiInstance.response(self,'{"message":"Repair Successfully added","repair":'+json.dumps(repairObj)+'}')
def delete(self,id): apiInstance = api() if not apiInstance._isallowed(self): return RepairModelInstance = RepairModelHelper() arepair = RepairModelInstance.get(id) if arepair is None: apiInstance.response(self,'{"errors":"Repair does not exist."}',404) return arepair.delete() apiInstance.response(self,'{"status":"ok","message":"Repair deleted."}',200)
def get(self,id): apiInstance = api() if not apiInstance._isallowed(self): return UserHelperInstance = UserModelHelper() if id!='': aUser = UserHelperInstance.get('',id) if aUser is None: apiInstance.response(self,'{"errors":{"message":"Resource does not exist"}}',404) return jsonUser = {"key":str(aUser.key().name()),"uid":aUser.uid,"email":aUser.email,"role":aUser.role} apiInstance.response(self,'{"status":"ok","message":"success","user":'******'}') return apiInstance.response(self,'{"status":"error","message":"id of user not passed"}')
def post(self, id): apiInstance = api() if id == 'login': payload = json.loads(self.request.body) try: id = payload['user']['email'] password = payload['user']['password'] except: apiInstance.response( self, '{"errors":"email and password are mandatory."}', 401) return UserHelperInstance = UserModelHelper() if len(id) < 3 or len(password) < 3: #Paramters invalid apiInstance.response( self, '{"errors" :"Lengths of email and password should be > 2 characters.' + id + '"}', 401) return aUser = UserHelperInstance.get(id) if aUser is None: #User Exists apiInstance.response( self, '{"errors" : "(' + id + ') has not been registered."}', 404) return UtilitiesInstance = UtilitiesHelper() if aUser.password == UtilitiesInstance.getonewayhash(password): token = UtilitiesInstance.AESencrypt( aUser.key().name() + '||' + str(datetime.datetime.now())) # Session Token apiInstance.response( self, '{"user":{"email":' + json.dumps(aUser.email) + ',"token":' + json.dumps(token) + ',"role":' + json.dumps(aUser.role) + '}}') return apiInstance.response(self, '{"errors" :"Invalid Credentials."}', 401) elif id == 'comment': self._createcomment() else: apiInstance.response(self, '{"errors":"api is invalid."}}', 404)
def get(self,id): apiInstance = api() RepairModelInstance = RepairModelHelper() reqUser = apiInstance.getRequestUser(self) if reqUser is None: apiInstance.response(self,'{"errors":"Unauthorized"}',401) return arepair = RepairModelInstance.get(id) if arepair is None: apiInstance.response(self,'{"errors":"Repair does not exist."}',404) return repairObject = RepairModelInstance._tojson(arepair) apiInstance.response(self,'{"repair":'+json.dumps(repairObject)+'}')
def delete(self,id): apiInstance = api() if not apiInstance._isallowed(self): return UserHelperInstance = UserModelHelper() reqUser = apiInstance.getRequestUser(self) aUser = UserHelperInstance.get('',id) if aUser is None: apiInstance.response(self,'{"status":"ok","message":"Resource does not exist"}',404) return if reqUser.email == aUser.email: apiInstance.response(self,'{"errors":"You cannot delete yourself."}',401) return aUser.delete() apiInstance.response(self,'{"status":"ok","message":"Resource deleted."}',200)
def get(self): apiInstance = api() if not apiInstance._isallowed(self): return UserHelperInstance = UserModelHelper() try: limit = int(self.request.get('limit')) except: limit = 10 try: offset = int(self.request.get('offset')) except: offset = 0 allusers = UserHelperInstance.list(limit,offset) alluserscount = UserHelperInstance.count() jsonArray = [] for i in range(0,len(allusers)): jsonArray.append({"uid":allusers[i].uid,"role":allusers[i].role,"email":allusers[i].email}) apiInstance.response(self,'{"users":'+json.dumps(jsonArray)+',"usercount":'+json.dumps(alluserscount)+'}')
def post(self): UserHelperInstance = UserModelHelper() apiInstance = api() payload = json.loads(self.request.body) try: email = payload['user']['email'] password = payload['user']['password'] uid = str(uuid4()) except: apiInstance.response(self,'{"errors":"email and password are mandatory."}',401) return if len(email)<3 or len(password)<3: #Parameters invalid apiInstance.response(self,'{"errors":"lengths of email and password need to be greater than 2 characters."}',401) else: #Paramters valid aUser = UserHelperInstance.get(email) if aUser is not None: #User Exists apiInstance.response(self,'{"errors":"('+email+') has already been taken."}',401) return else: #Create user and sign in if needed UtHI = UtilitiesHelper() passwordhash = UtHI.getonewayhash(password) aUser = UserHelperInstance.create(uid,passwordhash,email) UtilitiesInstance = UtilitiesHelper() token = UtilitiesInstance.AESencrypt(aUser.key().name()+'||'+ str(datetime.datetime.now())) # Session Token try: role = payload['user']['role'] aUser.role = role aUser.put() except: msg = 'No role passed' apiInstance.response(self,'{"message":"User added successfully.","user":{"role":'+json.dumps(aUser.role)+',"email":'+json.dumps(aUser.email)+',"token":'+json.dumps(token)+',"key":'+json.dumps(str(aUser.key().name()))+'}}')
def get(self): apiInstance = api() RepairModelInstance = RepairModelHelper() reqUser = apiInstance.getRequestUser(self) uemail = '' if reqUser is None: apiInstance.response(self,'{"errors":"Unauthorized"}',401) return elif reqUser.role!='manager': uemail = reqUser.email try: limit = int(self.request.get('limit')) except: limit = 10 try: offset = int(self.request.get('offset')) except: offset = 0 if offset==0: currentPage = 0 else: currentPage = int(offset/limit) assignedTo = self.request.get('assignedTo') status = self.request.get('status') frDt = self.request.get('frDt') frTm = self.request.get('frTm') toDt = self.request.get('toDt') toTm = self.request.get('toTm') if uemail=='': uemail=assignedTo UtilitiesHelperInstance = UtilitiesHelper() warnings = '' if frDt!='': anerror = UtilitiesHelperInstance._validatedate(frDt) if anerror!='': warnings += anerror frDt = '' if toDt!='': anerror = UtilitiesHelperInstance._validatedate(toDt) if anerror!='': warnings += anerror toDt='' if frTm!='': anerror = UtilitiesHelperInstance._validatetime(frTm) if anerror!='': warnings += anerror frTm='' if toTm!='': anerror = UtilitiesHelperInstance._validatetime(toTm) if anerror!='': warnings += anerror toTm='' ret = RepairModelInstance.list(limit,offset,uemail,status,frDt,frTm,toDt,toTm) filtesString = '"Fstatus":'+json.dumps(status) filtesString += ',"FassignedTo":'+json.dumps(assignedTo) filtesString += ',"FfrDt":'+json.dumps(frDt) filtesString += ',"FfrTm":'+json.dumps(frTm) filtesString += ',"FtoDt":'+json.dumps(toDt) filtesString += ',"FtoTm":'+json.dumps(toTm) if warnings!='': filtesString += ',"warnings":'+json.dumps(warnings) apiInstance.response(self,'{'+filtesString+',"repairs":'+json.dumps(ret['repairs'])+',"currentPage":'+json.dumps(currentPage)+',"hasMore":'+json.dumps(ret['hasMore'])+'}')
def put(self,id): apiInstance = api() RepairModelInstance = RepairModelHelper() reqUser = apiInstance.getRequestUser(self) if reqUser is None: apiInstance.response(self,'{"errors":"Unauthorized"}',401) return arepair = RepairModelInstance.get(id) if arepair is None: apiInstance.response(self,'{"errors":"Repair does not exist."}',404) return try: payload = json.loads(self.request.body) except: apiInstance.response(self,'{"errors":"Invalid Payload."}',404) return #logging.info(payload['method']) if payload['method']=='changestate': if reqUser.role=='manager': if (payload['state']=='COMPLETED' or payload['state']=='INCOMPLETE' or payload['state']=='APPROVED'): arepair.status = payload['state'] logging.info(arepair.status) if payload['state'] == 'INCOMPLETE' and arepair.scheduleDate!='': #Set assigned to none to avoid conflicts arepair.scheduleDate = '' arepair.scheduleTime = '' arepair.isScheduled = False arepair.put() repairObject = RepairModelInstance._tojson(arepair) apiInstance.response(self,'{"message":"State changed.","repair":'+json.dumps(repairObject)+'}',200) return else: apiInstance.response(self,'{"errors":"Allowed States are APPROVED, COMPLETED and INCOMPLETE"}',401) return elif arepair.assignedTo==reqUser.email: if arepair.status !='INCOMPLETE' and payload['state']!='COMPLETED': apiInstance.response(self,'{"errors":"Invalid State change requested for user.('+arepair.status+'->'+payload['state']+')"}',401) return else: arepair.status = payload['state'] arepair.put() repairObject = RepairModelInstance._tojson(arepair) apiInstance.response(self,'{"message":"State changed.","repair":'+json.dumps(repairObject)+'}',200) return else: apiInstance.response(self,'{"errors":"Invalid State change requested."}',401) return elif payload['method']=='update': repairValidatorInstance = repairValidator() validatorReponse = repairValidatorInstance.validate(payload,apiInstance,self) if len(validatorReponse['errors'])!=0: apiInstance.response(self,'{"errors":'+json.dumps(validatorReponse['errors'])+'}',401) return UtilitiesHelperInstance = UtilitiesHelper() repairDescription = UtilitiesHelperInstance.getValueofKey(payload['repair'],'descr') assignedTo = UtilitiesHelperInstance.getValueofKey(payload['repair'],'assignedTo') scheduledDate = UtilitiesHelperInstance.getValueofKey(payload['repair'],'scheduledDate') scheduledTime = UtilitiesHelperInstance.getValueofKey(payload['repair'],'scheduledTime') RepairHelperInstance = RepairModelHelper() arepair.assignedTo = assignedTo arepair.descr = repairDescription arepair.scheduleDate = scheduledDate if scheduledDate!='': tempval = int(scheduledDate.replace('-','')) arepair.scheduleDateINT = tempval if scheduledTime!='': tempval = int(scheduledTime.replace(':','')) arepair.scheduleTimeINT= tempval arepair.scheduleStart = int(validatorReponse['proposedStartTS']) arepair.scheduleTime = scheduledTime if scheduledDate=='': arepair.isScheduled = False arepair.put() repairObject = RepairModelInstance._tojson(arepair) apiInstance.response(self,'{"message":"Updated.","repair":'+json.dumps(repairObject)+'}',200) return apiInstance.response(self,'{"errors":"Invalid Put method"}',401)
def post(self,id): apiInstance = api() apiInstance.response(self,'{"status":"ok","message":"No Route."}')