Ejemplo n.º 1
0
    def obj_create(self, bundle, **kwargs):
        required = [
            'username',
        ]
        check_required_params(bundle, required)

        bundle.obj.username = bundle.data['username']
        try:
            user = User.objects.get(username__exact=bundle.obj.username)
        except User.DoesNotExist:
            try:
                user = User.objects.get(email__exact=bundle.obj.username)
            except User.DoesNotExist:
                raise BadRequest(_(u'Username/email not found'))

        newpass = User.objects.make_random_password(length=8)
        user.set_password(newpass)
        user.save()
        if bundle.request.is_secure():
            prefix = 'https://'
        else:
            prefix = 'http://'

        emailer.send_oppia_email(
            template_html='profile/email/password_reset.html',
            template_text='profile/email/password_reset.txt',
            subject="Password reset",
            fail_silently=False,
            recipients=[user.email],
            new_password=newpass,
            site=prefix + bundle.request.META['SERVER_NAME'])

        return bundle
Ejemplo n.º 2
0
    def obj_create(self, bundle, **kwargs):
        required = ['first_name', 'last_name']
        check_required_params(bundle, required)

        # can't edit another users account
        if 'username' in bundle.data \
                and bundle.data['username'] != bundle.request.user:
            raise Unauthorized(_("You cannot edit another users profile"))

        bundle = self.process_profile_update(bundle)
        return bundle
Ejemplo n.º 3
0
    def obj_create(self, bundle, **kwargs):
        required = ['username', ]
        check_required_params(bundle, required)

        username = bundle.data['username']
        # find if username or email address
        user = User.objects.filter(Q(username=username) |
                                   Q(email__iexact=username)).first()

        if user is not None:
            prf = PasswordResetForm({'email': user.email})
            if prf.is_valid():
                prf.get_users(user.email)
                prf.save(request=bundle.request,
                         from_email=settings.SERVER_EMAIL,
                         use_https=bundle.request.is_secure())

        return bundle
Ejemplo n.º 4
0
    def obj_create(self, bundle, **kwargs):
        self_register = SettingProperties \
            .get_bool(constants.OPPIA_ALLOW_SELF_REGISTRATION,
                      settings.OPPIA_ALLOW_SELF_REGISTRATION)
        if not self_register:
            raise BadRequest(_(u'Registration is disabled on this server.'))
        required = [
            'username', 'password', 'passwordagain', 'first_name', 'last_name'
        ]
        check_required_params(bundle, required)

        data = {
            'username': bundle.data['username'],
            'password': bundle.data['password'],
            'password_again': bundle.data['passwordagain'],
            'email': bundle.data['email'] if 'email' in bundle.data else '',
            'first_name': bundle.data['first_name'],
            'last_name': bundle.data['last_name'],
        }

        custom_fields = CustomField.objects.all()
        for custom_field in custom_fields:
            try:
                data[custom_field.id] = bundle.data[custom_field.id]
            except KeyError:
                pass

        rf = RegisterForm(data)
        if not rf.is_valid():
            str = ""
            for key, value in rf.errors.items():
                for error in value:
                    str += error + "\n"
            raise BadRequest(str)
        else:
            username = bundle.data['username']
            password = bundle.data['password']
            email = bundle.data['email'] if 'email' in bundle.data else '',
            first_name = bundle.data['first_name']
            last_name = bundle.data['last_name']

        try:
            bundle.obj = User.objects.create_user(username=username,
                                                  password=password)
            bundle.obj.first_name = first_name
            bundle.obj.email = email
            bundle.obj.last_name = last_name
            bundle.obj.save()
        except IntegrityError:
            raise BadRequest(
                _(u'Username "%s" already in use, please select another' %
                  username))

        self.process_register_base_profile(bundle)

        self.process_register_custom_fields(bundle)

        u = authenticate(username=username, password=password)
        if u is not None and u.is_active:
            login(bundle.request, u)
            # Add to tracker
            tracker = Tracker()
            tracker.user = u
            tracker.type = 'register'
            tracker.ip = bundle.request.META.get('REMOTE_ADDR',
                                                 DEFAULT_IP_ADDRESS)
            tracker.agent = bundle.request.META.get('HTTP_USER_AGENT',
                                                    'unknown')
            tracker.save()
        key = ApiKey.objects.get(user=u)
        bundle.data['api_key'] = key.key

        del bundle.data['passwordagain']
        del bundle.data['password']
        del bundle.data['first_name']
        del bundle.data['last_name']
        return bundle