def POST(self, jrequest, jresponse): """generate a nonce with given password and send the corresponding URL to the user. This round trip is intended to associate a password to an email address. """ assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: email = unicode(jrequest.getValue("email")[0]) raw_password = jrequest.getValue("password")[0].decode() raw_password2 = jrequest.getValue("password2")[0].decode() except TypeError: raise InvalidParams( message="email, password and password2 were mandatory.") if len(raw_password) < 8: raise InvalidParams( message="password should be eight characters or more") if raw_password != raw_password2: raise PasswordMismatch() match = re.search(r'^[\w.-\\\\+]+@[\w.-]+$', email) if not match: raise InvalidParams({"email": email}, "Malformed email '%s' address was given." % email) #email_user = None # check if OdenkiUser is loaded # try: # odenki_user = OdenkiUser.loadFromSession() # email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId) # except EntityNotFound: pass # check if EmailUser already exists. try: email_user = EmailUser.getByEmail(email) except EntityNotFound: email_user = EmailUser.createByEmail(email) assert isinstance(email_user, EmailUser) email_user.setNonce(email, raw_password) #EmailUser.deleteFromSession() from google.appengine.api import mail message = mail.EmailMessage() message.to = email message.body = "「みんなでおでんき」に関心をお持ちいただきありがとうございます。\n" + \ ("このメールアドレス %s" % email) + " でご登録いただくには次のページを開いて下さい。 \n " + \ ("http://%s/api/auth/Email/%s" % (jrequest.request.host, email_user.nonce)) + "\n" + \ "みんなでおでんきに登録しない場合はこのメールを無視して下さい。\n" message.sender = "*****@*****.**" message.subject = "みんなでおでんきへの登録確認メール" message.send() jresponse.setResultValue("EmailUser", email_user) jresponse.setResultValue("email", email)
def setPassword(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() # try: # session = gaesessions.get_current_session() # nonce = session[EMAIL_REGISTRATION_NONCE] # except Exception, e: # session.pop(EMAIL_REGISTRATION_NONCE); # raise UnexpectedState("Nonce is not stored in session data.") # assert isinstance(nonce, unicode) # email_user = EmailUser.getByNonce(nonce) try: odenki_user = OdenkiUser.loadFromSession() except EntityNotFound: odenki_user = None email_user = EmailUser.getByOdenkiId(odenki_user.odenkiId) assert isinstance(email_user, EmailUser) try: raw_password = jrequest.getValue("password")[0].decode() raw_password2 = jrequest.getValue("password2")[0].decode() assert len(raw_password) < 8 except Exception, e: raise InvalidParams( "setPassword method requires password and password2. %s" % e)
def POST(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() try: email = jrequest.getValue("email")[0] raw_password = jrequest.getValue("password")[0] except Exception: raise InvalidParams( "email and password are required for method=login.") try: email_user = EmailUser.getByEmail(email) except Exception: raise EntityNotFound("EmailUser entity is not found", {"email": email}) assert isinstance(email_user, EmailUser) email_user.matchPassword( raw_password) # raises PasswordMismatch if password not matches # email_user.saveToSession() assert email_user.odenkiId is not None odenki_user = OdenkiUser.getByOdenkiId(email_user.odenkiId) odenki_user.saveToSession() EmailUser.deleteFromSession() jresponse.setResultValue("EmailUser", email_user) jresponse.setResultValue("OdenkiUser", odenki_user)
def setOdenkiName(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() odenki_user = OdenkiUser.loadFromSession() try: new_odenki_name = jrequest.getValue("newOdenkiName")[0] assert isinstance(new_odenki_name, unicode) except: raise InvalidParams("setOdenkiName requires newOdenkiName") odenki_user.setOdenkiName(new_odenki_name) odenki_user.saveToSession()
def register(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() request = jrequest.request assert isinstance(request, Request) if len(request.params) < 3: raise InvalidParams( "method=register&x=y&z=w&moduleId=mid&sensorId=sid&sensorName=mysensor where x is product field, y is product string, z is serial field, is serial string" ) assert request.params.items()[0][0] == "method" assert request.params.items()[0][1] == "register" (product_name_field, product_name_string) = request.params.items()[1] (serial_number_field, serial_number_string) = request.params.items()[2] assert isinstance(product_name_field, str) product_name_field = unicode(product_name_field) assert isinstance(product_name_field, unicode) assert isinstance(serial_number_field, str) serial_number_field = unicode(serial_number_field) assert isinstance(serial_number_string, unicode) try: product_name_data = Data.prepare(product_name_field, product_name_string) serial_number_data = Data.prepare(serial_number_field, serial_number_string) module_id_string = request.get("moduleId") assert len(module_id_string) > 0 sensor_id_string = request.get("sensorId") assert len(sensor_id_string) > 0 module_id_data = Data.prepare("moduleId", request.get("moduleId")) sensor_id_data = Data.prepare("sensorId", request.get("sensorId")) existing_sensor = Sensor.getByProductNameSerialNumberModuleIdSensorId( product_name_data, serial_number_data, module_id_data, sensor_id_data) raise EntityExists( "Equipment", { product_name_field: product_name_string, serial_number_field: serial_number_string, "moduleId": module_id_string, "sensorId": sensor_id_string }) except EntityNotFound: pass odenki_user = OdenkiUser.loadFromSession() assert isinstance(odenki_user, OdenkiUser) sensor_name = request.get("sensorName") if len(sensor_name) == 0: sensor_name = unicode(sensor_name) sensor_name_data = Data.prepare("sensorName", sensor_name) sensor = Sensor.create(product_name_data, serial_number_data, module_id_data, sensor_id_data, sensor_name_data, odenki_user) jresponse.addResult(sensor)
def create(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) odenki_name = jrequest.request.get("odenkiName") odenki_name = odenki_name.decode() if odenki_name is None or len(odenki_name) == 0: raise InvalidParams("odenkiName is mandatory.") odenki_user = OdenkiUser.createNew() assert isinstance(odenki_user, OdenkiUser) odenki_user.odenkiName = odenki_name odenki_user.put() jresponse.addResult(odenki_user) jresponse.setId()
def GET(self, jrequest, jresponse): assert isinstance(jrequest, JsonRpcRequest) assert isinstance(jresponse, JsonRpcResponse) jresponse.setId() outlet_id = None relay_id = None card_type = None card_id = None try: outlet_id = unicode(jrequest.getValue("outletId")[0]) assert isinstance(outlet_id, unicode) relay_id = unicode(jrequest.getValue("relayId")[0]) assert isinstance(relay_id, unicode) card_id = unicode(jrequest.getValue("cardId")[0]) assert isinstance(card_id, unicode) card_type = unicode(jrequest.getValue("cardType")[0]) assert isinstance(card_type, unicode) except Exception, e: raise InvalidParams({"outletId":None, "relayId":None, "cardType":None, "cardId":None, "exception": e.__class__.__name__})