def user_create(opts): ''' Create a new user in the db. ''' try: if opts.name == '': return "User name is not set (-n)" if opts.email == '': return "User email is not set (-e)" if opts.phone == '' and opts.state != 9: return "User phone is not set (-p)" if "@" not in opts.email or "." not in opts.email: return "Invalid email address, try again" if (opts.phone.startswith("+") and len(opts.phone) == 12): pass else: if opts.state != 9: return "Invalid phone number format. Must be like '+12225558888' (no quotes)" if opts.team == '': opts.team = "default" if opts.state == 100: opts.state = 0 newuser = User.User() newuser.name = opts.name newuser.email = opts.email newuser.phone = opts.phone if opts.team != '': newuser.team = opts.team if opts.state != 100: newuser.state = opts.state newuser.save_user() # validate the phone number with twilio if opts.state != 9: valid_code = twilio.validate_phone(newuser) if valid_code == False: logging.error("Unable to get a validation code for new phone number") return newuser.print_user(opts.mobile) + "\nUnable to get a validation code. Please verify new phone number through Twilio website" elif valid_code == True: return newuser.print_user(opts.mobile) + "\nPhone has already been verified with Twilio" else: return newuser.print_user(opts.mobile) + "\nValidation Code: %s" % (valid_code) else: return newuser.print_user(opts.mobile) except Exception, e: logging.error("Failed to create new user: %s" % (e)) return "Failed to create user: %s" % (e.__str__())
def create(self): if self.target == "alert" or self.target == "alerts": if self.subject: self.subject = urllib.unquote_plus(self.subject) else: self.populate(1201, "No subject in alert creation") return if self.message: self.message = urllib.unquote_plus(self.message) else: self.populate(1202, "No message in alert creation") return if self.team: self.team = urllib.unquote_plus(self.team) else: self.team = "default" # check to see if this alert is a new one isNewAlert = True for a in Alert.fresh_alerts(): if a.subject == self.subject and a.message == self.message and a.team == self.team: isNewAlert = False if isNewAlert == True: # save new alert to the db newalert = Alert.Alert() newalert.subject = self.subject newalert.message = self.message newalert.team = self.team newalert.send_alert() self.populate(200, "OK") else: self.populate(200, "OK", json.dumps("Repeat alert")) elif self.target == "user" or self.target == "users": if self.name == None or len(self.name) <= 0: self.populate(1301, "No name parameter given in user creation") return if self.email == None or len( self.email) <= 0 or "@" not in self.email: self.populate( 1302, "No or invalid email parameter given in user creation") return if self.phone == None or len( self.phone) != 12 or self.phone.startswith("+"): if self.state != 9: self.populate( 1303, "No or invalid phone number parameter given in user creation" ) return if self.state == None or isinstance(self.state, int): self.state = 0 if self.team == None: self.team = "default" newuser = User.User() newuser.name = self.name newuser.email = self.email newuser.phone = self.phone newuser.state = self.state newuser.team = self.team newuser.save_user() if newuser.state != 9: valid_code = twilio.validate_phone(newuser) if valid_code == False: self.populate( 1401, "Unable to get a validation code for new phone number") return elif valid_code == True: self.populate( 1402, "Phone has already been verified with Twilio") return else: self.populate(1400, "Validation Code: %s" % (valid_code)) else: self.populate(200, "OK") return else: self.populate( 1101, "Invalid API create call: Missing valid target parameter") return
def create(self): if self.target == "alert" or self.target == "alerts": if self.subject: self.subject = urllib.unquote_plus(self.subject) else: self.populate(1201,"No subject in alert creation") return if self.message: self.message = urllib.unquote_plus(self.message) else: self.populate(1202,"No message in alert creation") return if self.team: self.team= urllib.unquote_plus(self.team) else: self.team="default" # check to see if this alert is a new one isNewAlert = True for a in Alert.fresh_alerts(): if a.subject == self.subject and a.message == self.message and a.team == self.team: isNewAlert = False if isNewAlert == True: # save new alert to the db newalert = Alert.Alert() newalert.subject = self.subject newalert.message = self.message newalert.team = self.team newalert.send_alert() self.populate(200,"OK") else: self.populate(200,"OK",json.dumps("Repeat alert")) elif self.target == "user" or self.target == "users": if self.name == None or len(self.name) <= 0: self.populate(1301,"No name parameter given in user creation") return if self.email == None or len(self.email) <= 0 or "@" not in self.email: self.populate(1302,"No or invalid email parameter given in user creation") return if self.phone == None or len(self.phone) != 12 or self.phone.startswith("+"): if self.state != 9: self.populate(1303,"No or invalid phone number parameter given in user creation") return if self.state == None or isinstance(self.state, int): self.state = 0 if self.team == None: self.team = "default" newuser = User.User() newuser.name = self.name newuser.email = self.email newuser.phone = self.phone newuser.state = self.state newuser.team = self.team newuser.save_user() if newuser.state != 9: valid_code = twilio.validate_phone(newuser) if valid_code == False: self.populate(1401,"Unable to get a validation code for new phone number") return elif valid_code == True: self.populate(1402,"Phone has already been verified with Twilio") return else: self.populate(1400,"Validation Code: %s" % (valid_code)) else: self.populate(200,"OK") return else: self.populate(1101,"Invalid API create call: Missing valid target parameter") return