def handle(self, request, data): email = data.get("email") try: res = keystoneapi.get_user(email) if not (res.get("success") or res.get("result")): return None user = res.get("result") user_id = user.get('id') email_activation_code = generate_sms_activation_code() data = {"email_activation_code": email_activation_code, "email_activation_code_time": timeutils.strtime()} # email confirmation about the change send_templated_email("JioCloud account password change confirmation", [email], html_template="signup/password_change_verification.html", template_context={ "name": user.get("first_name") if user.get("first_name") else user.get("name"), "reset_password_path": reverse_lazy('horizon_jiocloud:resetPassword', kwargs={'user_id':user_id, 'code':email_activation_code}), "SITE_URL": getattr(settings, "SITE_URL", "http://127.0.0.1"), }) # push changes to keystone database response = keystoneapi.update_user(user_id, data) except Exception as e: LOG.exception(e) return render_to_response('signup/forgot_password_success.html', {'email':settings.DEFAULT_FROM_EMAIL, 'user_id': user_id})
def resendSms(request, user_id): url = reverse_lazy('horizon_jiocloud:user_sms_activation', kwargs={'user_id':user_id}) # This method serves re-sending SMS multiple times # to cater to SMS sending failures. # If the OTP is expired, a new one will be generated try: res = keystoneapi.get_user(user_id) if not (res.get("success") or res.get("result")): raise Exception('Unable to get user details') except Exception as e: LOG.exception(e) return http.HttpResponseRedirect(url + "?resentSms=True&resentSuccess=False") user = res.get("result") phone = user.get("phone") sms_activation_code = user.get("sms_activation_code") sms_activation_code_time = user.get("sms_activation_code_time") try: if is_sms_expired(sms_activation_code_time): sms_activation_code = generate_sms_activation_code() data = {"sms_activation_code": sms_activation_code, "sms_activation_code_time": timeutils.strtime()} # push changes to keystone database response = keystoneapi.update_user(user_id, data) generate_and_send_sms(phone, sms_activation_code) except Exception as e: LOG.exception(e) return http.HttpResponseRedirect(url + "?resentSms=True&resentSuccess=False") return http.HttpResponseRedirect(url + "?resentSms=True&resentSuccess=True")
def handle(self, request, data): phone = data.get("phone") request.session["phone"] = phone if not self.fields.has_key("sms_activation_code"): # No "sms_activation_code" field in form # So generate OTP to start phone number change process; # send SMS containing the OTP and store OTP details # in database for later verification sms_activation_code = generate_sms_activation_code() data = {"sms_activation_code": sms_activation_code, "sms_activation_code_time": timeutils.strtime()} try: generate_and_send_sms(phone, sms_activation_code) messages.success(request, 'Please enter the SMS code to change phone number') except Exception as ex: LOG.exception(ex) self.set_non_field_errors([ self.get_default_error_message() ]) return None else: # check if the code submitted in the form and that in the # database match code = data.get("sms_activation_code") sms_activation_code = self.user.get("sms_activation_code") if sms_activation_code not in [code]: self.set_non_field_errors(["Invalid Activation Code"]) return None # codes matched. so we will change the phone number # in database. # sms_activation_code no more required so unset it # in database. data["sms_activation_code"] = "" request.session["phone"] = "" try: # email confirmation about the change send_templated_email("JioCloud account phone number changed", [self.user.get("email")], html_template="change_phone/phone_change_cofirmation_email.html", template_context={ "name": self.user.get("first_name") if self.user.get("first_name") else self.user.get("name"), "phone": phone, }) messages.success(request, 'Phone number has been updated successfully.') except Exception as ex: LOG.exception(ex) try: # push changes to keystone database response = keystoneapi.update_user(self.request.user.id, data) except Exception: response = exceptions.handle(request, ignore=True) messages.error(request, 'Unable to change phone number.') return shortcuts.redirect(request.build_absolute_uri())
def handle(self, request, data): new_email = data.get("email") email_activation_code = generate_sms_activation_code() data = {"email_activation_code": email_activation_code, "email_activation_code_time": timeutils.strtime()} try: # email confirmation about the change send_templated_email("JioCloud account email change confirmation", [new_email], html_template="change_email/email_change_verification.html", template_context={ "name": self.user.get("first_name") if self.user.get("first_name") else self.user.get("name"), "email_activation_path": reverse('horizon:settings:email:index') + 'activate/' + email_activation_code + '?email=' + new_email, }) # push changes to keystone database response = keystoneapi.update_user(self.request.user.id, data) messages.success(request, 'Please click the link sent to your new email to activate it') except Exception as ex: LOG.exception(ex) response = exceptions.handle(request, ignore=True) messages.error(request, 'Unable to change email.') return shortcuts.redirect(request.build_absolute_uri())
def to_primitive(value, convert_instances=False, convert_datetime=True, level=0, max_depth=3): """Convert a complex object into primitives. Handy for JSON serialization. We can optionally handle instances, but since this is a recursive function, we could have cyclical data structures. To handle cyclical data structures we could track the actual objects visited in a set, but not all objects are hashable. Instead we just track the depth of the object inspections and don't go too deep. Therefore, convert_instances=True is lossy ... be aware. """ # handle obvious types first - order of basic types determined by running # full tests on nova project, resulting in the following counts: # 572754 <type 'NoneType'> # 460353 <type 'int'> # 379632 <type 'unicode'> # 274610 <type 'str'> # 199918 <type 'dict'> # 114200 <type 'datetime.datetime'> # 51817 <type 'bool'> # 26164 <type 'list'> # 6491 <type 'float'> # 283 <type 'tuple'> # 19 <type 'long'> if isinstance(value, _simple_types): return value if isinstance(value, datetime.datetime): if convert_datetime: return timeutils.strtime(value) else: return value # value of itertools.count doesn't get caught by nasty_type_tests # and results in infinite loop when list(value) is called. if type(value) == itertools.count: return six.text_type(value) # FIXME(vish): Workaround for LP bug 852095. Without this workaround, # tests that raise an exception in a mocked method that # has a @wrap_exception with a notifier will fail. If # we up the dependency to 0.5.4 (when it is released) we # can remove this workaround. if getattr(value, '__module__', None) == 'mox': return 'mock' if level > max_depth: return '?' # The try block may not be necessary after the class check above, # but just in case ... try: recursive = functools.partial(to_primitive, convert_instances=convert_instances, convert_datetime=convert_datetime, level=level, max_depth=max_depth) if isinstance(value, dict): return dict((k, recursive(v)) for k, v in six.iteritems(value)) elif isinstance(value, (list, tuple)): return [recursive(lv) for lv in value] # It's not clear why xmlrpclib created their own DateTime type, but # for our purposes, make it a datetime type which is explicitly # handled if isinstance(value, xmlrpclib.DateTime): value = datetime.datetime(*tuple(value.timetuple())[:6]) if convert_datetime and isinstance(value, datetime.datetime): return timeutils.strtime(value) elif isinstance(value, gettextutils.Message): return value.data elif hasattr(value, 'iteritems'): return recursive(dict(value.iteritems()), level=level + 1) elif hasattr(value, '__iter__'): return recursive(list(value)) elif convert_instances and hasattr(value, '__dict__'): # Likely an instance of something. Watch for cycles. # Ignore class member vars. return recursive(value.__dict__, level=level + 1) elif netaddr and isinstance(value, netaddr.IPAddress): return six.text_type(value) else: if any(test(value) for test in _nasty_type_tests): return six.text_type(value) return value except TypeError: # Class objects are tricky since they may define something like # __iter__ defined but it isn't callable as list(). return six.text_type(value)
def to_primitive(value, convert_instances=False, convert_datetime=True, level=0, max_depth=3): """Convert a complex object into primitives. Handy for JSON serialization. We can optionally handle instances, but since this is a recursive function, we could have cyclical data structures. To handle cyclical data structures we could track the actual objects visited in a set, but not all objects are hashable. Instead we just track the depth of the object inspections and don't go too deep. Therefore, convert_instances=True is lossy ... be aware. """ # handle obvious types first - order of basic types determined by running # full tests on nova project, resulting in the following counts: # 572754 <type 'NoneType'> # 460353 <type 'int'> # 379632 <type 'unicode'> # 274610 <type 'str'> # 199918 <type 'dict'> # 114200 <type 'datetime.datetime'> # 51817 <type 'bool'> # 26164 <type 'list'> # 6491 <type 'float'> # 283 <type 'tuple'> # 19 <type 'long'> if isinstance(value, _simple_types): return value if isinstance(value, datetime.datetime): if convert_datetime: return timeutils.strtime(value) else: return value # value of itertools.count doesn't get caught by nasty_type_tests # and results in infinite loop when list(value) is called. if type(value) == itertools.count: return six.text_type(value) # FIXME(vish): Workaround for LP bug 852095. Without this workaround, # tests that raise an exception in a mocked method that # has a @wrap_exception with a notifier will fail. If # we up the dependency to 0.5.4 (when it is released) we # can remove this workaround. if getattr(value, '__module__', None) == 'mox': return 'mock' if level > max_depth: return '?' # The try block may not be necessary after the class check above, # but just in case ... try: recursive = functools.partial(to_primitive, convert_instances=convert_instances, convert_datetime=convert_datetime, level=level, max_depth=max_depth) if isinstance(value, dict): return dict((k, recursive(v)) for k, v in value.iteritems()) elif isinstance(value, (list, tuple)): return [recursive(lv) for lv in value] # It's not clear why xmlrpclib created their own DateTime type, but # for our purposes, make it a datetime type which is explicitly # handled if isinstance(value, xmlrpclib.DateTime): value = datetime.datetime(*tuple(value.timetuple())[:6]) if convert_datetime and isinstance(value, datetime.datetime): return timeutils.strtime(value) elif hasattr(value, 'iteritems'): return recursive(dict(value.iteritems()), level=level + 1) elif hasattr(value, '__iter__'): return recursive(list(value)) elif convert_instances and hasattr(value, '__dict__'): # Likely an instance of something. Watch for cycles. # Ignore class member vars. return recursive(value.__dict__, level=level + 1) else: if any(test(value) for test in _nasty_type_tests): return six.text_type(value) return value except TypeError: # Class objects are tricky since they may define something like # __iter__ defined but it isn't callable as list(). return six.text_type(value)
def handle(self, request, data): res = {} user_id = None password = data.get("password") email = data.get("email") first_name = data.get("first_name") # last_name = data.get("last_name") # company = data.get("company") # address = data.get("address") # country_code = data.get("country_code") # country = data.get("country") # country_list = self.get_country(country) # if country_list: # country_name = country_list[0].name_ascii # state = data.get("state") # state_list = self.get_state(state) # if state_list: # state_name = state_list[0].name_ascii # city = data.get("city") # city_list = self.get_city(city) # if city_list: # city_name = city_list[0].name_ascii # pin = data.get("pincode") phone = data.get("phone") sms_activation_code = generate_sms_activation_code() _data = {"password": password, "email": email, "first_name": first_name, # "last_name": last_name, # "country": country, "address": address, "city": city, # "state": state, "pin": pin, "company": company, # "country_code": country_code, "phone": phone, "sms_activation_code": sms_activation_code, "sms_activation_code_time": timeutils.strtime()} try: res = keystoneapi.create_user(_data) except Exception as ex: LOG.exception(ex) self.set_non_field_errors([self.get_default_error_message()]) return None if not res.get("success"): self.set_non_field_errors([ res.get("error", self.get_default_error_message() ) ]) return None else: user = res["result"] user_id = user.get("id") try: send_registration_email(user, "signup/account_activation_email.html") except Exception as ex: LOG.exception(ex) try: generate_and_send_sms(phone, sms_activation_code) except Exception as ex: ##TODO(SM):Delete user from keystone ? LOG.exception(ex) self.set_non_field_errors([ self.get_default_error_message() ]) return None # return http.HttpResponseRedirect( # reverse_lazy( # 'user_sms_activation', # kwargs={"user_id": user_id} # )) return render_to_response('signup/signup_success.html', {'email':settings.DEFAULT_FROM_EMAIL, 'user_id': user_id})