Exemplo n.º 1
0
    def create(self, **kwargs):
        username = ''

        validated_username = self.validated_data['first_name'].lower() + '.' + self.validated_data['last_name'].lower()
        username_check = User.objects.filter(username=validated_username).count()

        if username_check == 0 and len(validated_username) <= settings.USERNAME_MAX_LENGTH:
            username = validated_username
        else:
            username = get_next_unique_id(User, 'username', validated_username)
            if len(username) > settings.USERNAME_MAX_LENGTH:
                if len(self.validated_data['email']) <= settings.USERNAME_MAX_LENGTH:
                    username = self.validated_data['email']
                else:
                    username = uuid.uuid4().hex[:settings.USERNAME_MAX_LENGTH]

        user = User.objects.create_user(username, self.validated_data.get('email'),
                                        self.initial_data.get('password1'))

        if settings.EMAIL_ENABLED:
            user.is_active = 0
        user.first_name = self.validated_data['first_name']
        user.last_name = self.validated_data['last_name']
        user.save()
        user_profile = models.UserProfile()
        user_profile.user = user
        user_profile.save()

        if 'is_worker' in self.validated_data or self.validated_data['is_worker']:
            worker = models.Worker()
            worker.profile = user_profile
            worker.alias = username
            worker.save()

        if 'is_requester' in self.validated_data or self.validated_data['is_requester']:
            requester = models.Requester()
            requester.profile = user_profile
            requester.alias = username
            requester.save()

        if settings.EMAIL_ENABLED:
            salt = hashlib.sha1(str(random.random()).encode('utf-8')).hexdigest()[:5]
            if isinstance(username, str):
                username = username.encode('utf-8')
            activation_key = hashlib.sha1(salt.encode('utf-8') + username).hexdigest()
            registration_model = models.RegistrationModel()
            registration_model.user = User.objects.get(id=user.id)
            registration_model.activation_key = activation_key
            send_activation_email_sendgrid(email=user.email, host=self.context['request'].get_host(),
                                           activation_key=activation_key)
            registration_model.save()
        return user
Exemplo n.º 2
0
    def create(self, **kwargs):
        username = ''

        validated_username = self.validated_data['first_name'].lower(
        ) + '.' + self.validated_data['last_name'].lower()
        username_check = User.objects.filter(
            username=validated_username).count()

        if username_check == 0 and len(
                validated_username) <= settings.USERNAME_MAX_LENGTH:
            username = validated_username
        else:
            username = get_next_unique_id(User, 'username', validated_username)
            if len(username) > settings.USERNAME_MAX_LENGTH:
                if len(self.validated_data['email']
                       ) <= settings.USERNAME_MAX_LENGTH:
                    username = self.validated_data['email']
                else:
                    username = uuid.uuid4().hex[:settings.USERNAME_MAX_LENGTH]

        user = User.objects.create_user(username,
                                        self.validated_data.get('email'),
                                        self.initial_data.get('password1'))

        if settings.EMAIL_ENABLED:
            user.is_active = 0
        user.first_name = self.validated_data['first_name']
        user.last_name = self.validated_data['last_name']
        user.save()
        user_profile = models.UserProfile()
        user_profile.user = user
        user_profile.save()

        if self.validated_data.get('is_requester', False):
            requester = models.Requester()
            requester.profile = user_profile
            requester.alias = username
            requester.save()
            '''
                For experimental phase only, to be removed later.
                {begin experiment}
            '''
            try:
                if self.initial_data.get('experiment_fields', False):
                    requester_experiment = experimental_models.RequesterExperiment(
                    )
                    requester_experiment.requester = requester
                    requester_experiment.has_prototype = self.initial_data[
                        'experiment_fields'].get('has_prototype', True)
                    requester_experiment.has_boomerang = self.initial_data[
                        'experiment_fields'].get('has_boomerang', True)
                    requester_experiment.pool = self.initial_data[
                        'experiment_fields'].get('pool', 0)
                    requester_experiment.save()
                else:
                    experimental_models.WorkerExperiment.objects.create(
                        requester=requester)
            except Exception as e:
                pass  # it's ok to fail silently here
            '''
                {end experiment}
            '''

        has_profile_info = self.validated_data.get(
            'is_requester', False) or self.validated_data.get(
                'is_worker', False)

        if self.validated_data.get('is_worker', False) or not has_profile_info:
            worker = models.Worker()
            worker.profile = user_profile
            worker.alias = username
            worker.save()
            '''
                For experimental phase only, to be removed later.
                {begin experiment}
            '''
            try:
                if self.initial_data.get('experiment_fields', False):
                    worker_experiment = experimental_models.WorkerExperiment()
                    worker_experiment.worker = worker
                    worker_experiment.has_prototype = self.initial_data[
                        'experiment_fields'].get('has_prototype', True)
                    worker_experiment.sorting_type = self.initial_data[
                        'experiment_fields'].get('sorting_type', 1)
                    worker_experiment.is_subject_to_cascade= self.initial_data['experiment_fields']\
                        .get('is_subject_to_cascade', True)
                    worker_experiment.pool = self.initial_data[
                        'experiment_fields'].get('pool', 0)
                    worker_experiment.save()
                else:
                    experimental_models.WorkerExperiment.objects.create(
                        worker=worker)
            except Exception as e:
                pass  # it's ok to fail silently here
            '''
                {end experiment}
            '''
        if settings.EMAIL_ENABLED:
            salt = hashlib.sha1(str(
                random.random()).encode('utf-8')).hexdigest()[:5]
            if isinstance(username, str):
                username = username.encode('utf-8')
            activation_key = hashlib.sha1(salt.encode('utf-8') +
                                          username).hexdigest()
            registration_model = models.RegistrationModel()
            registration_model.user = User.objects.get(id=user.id)
            registration_model.activation_key = activation_key
            send_activation_email_sendgrid(
                email=user.email,
                host=self.context['request'].get_host(),
                activation_key=activation_key)
            registration_model.save()
        return user
Exemplo n.º 3
0
    def create(self, **kwargs):
        username = ''

        validated_username = self.validated_data['first_name'].lower() + '.' + self.validated_data['last_name'].lower()
        username_check = User.objects.filter(username=validated_username).count()

        if username_check == 0 and len(validated_username) <= settings.USERNAME_MAX_LENGTH:
            username = validated_username
        else:
            username = get_next_unique_id(User, 'username', validated_username)
            if len(username) > settings.USERNAME_MAX_LENGTH:
                if len(self.validated_data['email']) <= settings.USERNAME_MAX_LENGTH:
                    username = self.validated_data['email']
                else:
                    username = uuid.uuid4().hex[:settings.USERNAME_MAX_LENGTH]

        user = User.objects.create_user(username, self.validated_data.get('email'),
                                        self.initial_data.get('password1'))

        if settings.EMAIL_ENABLED:
            user.is_active = 0
        user.first_name = self.validated_data['first_name']
        user.last_name = self.validated_data['last_name']
        user.save()
        user_profile = models.UserProfile()
        user_profile.user = user
        user_profile.save()

        if self.validated_data.get('is_requester', False):
            requester = models.Requester()
            requester.profile = user_profile
            requester.alias = username
            requester.save()
            '''
                For experimental phase only, to be removed later.
                {begin experiment}
            '''
            try:
                if self.initial_data.get('experiment_fields', False):
                    requester_experiment = experimental_models.RequesterExperiment()
                    requester_experiment.requester = requester
                    requester_experiment.has_prototype = self.initial_data['experiment_fields'].get('has_prototype', True)
                    requester_experiment.has_boomerang  = self.initial_data['experiment_fields'].get('has_boomerang', True)
                    requester_experiment.pool = self.initial_data['experiment_fields'].get('pool', 0)
                    requester_experiment.save()
                else:
                    experimental_models.WorkerExperiment.objects.create(requester=requester)
            except Exception as e:
                pass # it's ok to fail silently here

            '''
                {end experiment}
            '''
            
        has_profile_info = self.validated_data.get('is_requester', False) or self.validated_data.get('is_worker', False)

        if self.validated_data.get('is_worker', False) or not has_profile_info:
            worker = models.Worker()
            worker.profile = user_profile
            worker.alias = username
            worker.save()
            '''
                For experimental phase only, to be removed later.
                {begin experiment}
            '''
            try:
                if self.initial_data.get('experiment_fields', False):
                    worker_experiment = experimental_models.WorkerExperiment()
                    worker_experiment.worker = worker
                    worker_experiment.has_prototype = self.initial_data['experiment_fields'].get('has_prototype', True)
                    worker_experiment.sorting_type  = self.initial_data['experiment_fields'].get('sorting_type', 1)
                    worker_experiment.is_subject_to_cascade= self.initial_data['experiment_fields']\
                        .get('is_subject_to_cascade', True)
                    worker_experiment.pool = self.initial_data['experiment_fields'].get('pool', 0)
                    worker_experiment.save()
                else:
                    experimental_models.WorkerExperiment.objects.create(worker=worker)
            except Exception as e:
                pass # it's ok to fail silently here

            '''
                {end experiment}
            '''
        if settings.EMAIL_ENABLED:
            salt = hashlib.sha1(str(random.random()).encode('utf-8')).hexdigest()[:5]
            if isinstance(username, str):
                username = username.encode('utf-8')
            activation_key = hashlib.sha1(salt.encode('utf-8') + username).hexdigest()
            registration_model = models.RegistrationModel()
            registration_model.user = User.objects.get(id=user.id)
            registration_model.activation_key = activation_key
            send_activation_email_sendgrid(email=user.email, host=self.context['request'].get_host(),
                                           activation_key=activation_key)
            registration_model.save()
        return user
Exemplo n.º 4
0
    def create(self, **kwargs):
        username = ''

        validated_username = self.validated_data['first_name'].lower(
        ) + '.' + self.validated_data['last_name'].lower()
        username_check = User.objects.filter(
            username=validated_username).count()

        if username_check == 0 and len(
                validated_username) <= settings.USERNAME_MAX_LENGTH:
            username = validated_username
        else:
            username = get_next_unique_id(User, 'username', validated_username)
            if len(username) > settings.USERNAME_MAX_LENGTH:
                if len(self.validated_data['email']
                       ) <= settings.USERNAME_MAX_LENGTH:
                    username = self.validated_data['email']
                else:
                    username = uuid.uuid4().hex[:settings.USERNAME_MAX_LENGTH]

        user = User.objects.create_user(username,
                                        self.validated_data.get('email'),
                                        self.initial_data.get('password1'))

        if settings.EMAIL_ENABLED:
            user.is_active = 0
        user.first_name = self.validated_data['first_name']
        user.last_name = self.validated_data['last_name']
        user.save()
        user_profile = models.UserProfile()
        user_profile.user = user
        user_profile.save()
        user_financial_account = models.FinancialAccount()
        user_financial_account.owner = user_profile
        user_financial_account.type = 'general'
        user_financial_account.save()

        if self.validated_data.get('is_requester', True):
            requester = models.Requester()
            requester.profile = user_profile
            requester.alias = username
            requester.save()
            requester_financial_account = models.FinancialAccount()
            requester_financial_account.owner = user_profile
            requester_financial_account.type = 'requester'
            requester_financial_account.save()

        has_profile_info = self.validated_data.get(
            'is_requester', False) or self.validated_data.get(
                'is_worker', False)

        if self.validated_data.get('is_worker', False) or not has_profile_info:
            worker = models.Worker()
            worker.profile = user_profile
            worker.alias = username
            worker.save()
            worker_financial_account = models.FinancialAccount()
            worker_financial_account.owner = user_profile
            worker_financial_account.type = 'worker'
            worker_financial_account.save()

        if settings.EMAIL_ENABLED:
            salt = hashlib.sha1(str(
                random.random()).encode('utf-8')).hexdigest()[:5]
            if isinstance(username, str):
                username = username.encode('utf-8')
            activation_key = hashlib.sha1(salt.encode('utf-8') +
                                          username).hexdigest()
            registration_model = models.RegistrationModel()
            registration_model.user = User.objects.get(id=user.id)
            registration_model.activation_key = activation_key
            send_activation_email_sendgrid(
                email=user.email,
                host=self.context['request'].get_host(),
                activation_key=activation_key)
            registration_model.save()
        return user