def obj_create(self, bundle, request=None, **kwargs): try: email = bundle.data['email'] salt = bundle.data['salt'] password_hash = bundle.data['password_hash'] except KeyError: raise exceptions.BadRequest('Specify email, salt and password_hash.') try: salt = crypto.from_string(salt) except (ValueError, TypeError, ): raise exceptions.BadRequest('Incorrect salt value.') try: password_hash = crypto.from_string(password_hash) except (ValueError, TypeError, ): raise exceptions.BadRequest('Incorrect password_hash value.') try: bundle.obj = self._meta.object_class.objects.create( email = email, username = email, password = crypto.make_password(password_hash, salt), ) except db.IntegrityError: raise exceptions.BadRequest('There\'s already user with this email.') except (ValueError, TypeError, ): raise exceptions.BadRequest('Invalid data provided (mismatched type).') models.UserProfile.objects.create( user=bundle.obj, ) bundle.data = {} # To prevent creation data e.g. `password_hash` population. return bundle
def full_hydrate(self, bundle): if not bundle.request.user.is_authenticated(): self.unauthorized_result(bundle=bundle) bundle = super(User, self).full_hydrate(bundle) bundle.obj.profile.data = bundle.data['data'] fields = set(bundle.data.keys()) if fields.intersection(self.PASSWORD_CHANGING_FIELDS): if fields.issuperset(self.PASSWORD_CHANGING_FIELDS): try: password_hash = crypto.from_string(bundle.data['new_password_hash']) except (ValueError, TypeError, ): raise exceptions.BadRequest('Incorrect new_password_hash value.') try: salt = crypto.from_string(bundle.data['new_salt']) except (ValueError, TypeError, ): raise exceptions.BadRequest('Incorrect new_salt value.') bundle.obj.password = crypto.make_password(password_hash, salt) else: raise exceptions.BadRequest('Both new_password_hash and new_salt fields should be presented.') bundle.data = {} # To prevent update data e.g. `password_hash` population. return bundle