def stripe(self, request, *args, **kwargs): from crowdsourcing.serializers.utils import CreditCardSerializer, BankSerializer from crowdsourcing.payment import Stripe is_worker = request.data.get('is_worker', False) is_requester = request.data.get('is_requester', False) ssn_last_4 = request.data.get('ssn_last_4') bank_data = None credit_card = None if not is_worker and not is_requester: raise serializers.ValidationError(detail=daemo_error( "Please set either worker or requester to true.")) if is_requester: card_serializer = CreditCardSerializer( data=request.data.get('credit_card', {})) if not card_serializer.is_valid(): raise serializers.ValidationError( detail=card_serializer.errors) credit_card = request.data.get('credit_card') if is_worker: if not UserViewSet.is_whitelisted(request.user): raise serializers.ValidationError(detail=daemo_error( "You are not allowed to sign up as a worker at this time.") ) # TODO add support for other countries bank_data = request.data.get('bank', {}) bank_data.update({'currency': 'usd'}) bank_data.update({'country': 'US'}) bank_serializer = BankSerializer(data=bank_data) if not bank_serializer.is_valid(): raise serializers.ValidationError( detail=bank_serializer.errors) profile = request.user.profile account, customer = Stripe().create_account_and_customer( user=request.user, country_iso=profile.address.city.country.code, ip_address='8.8.8.8', is_worker=is_worker, is_requester=is_requester, credit_card=credit_card, bank=bank_data, ssn_last_4=ssn_last_4) if account is not None: profile.is_worker = True update_worker_cache([profile.user_id], constants.ACTION_UPDATE_PROFILE, 'is_worker', 1) if customer is not None: profile.is_requester = True update_worker_cache([profile.user_id], constants.ACTION_UPDATE_PROFILE, 'is_requester', 1) if account is not None or customer is not None: profile.save() return Response(data={"message": "Accounts and customer created"}, status=status.HTTP_201_CREATED) raise serializers.ValidationError(detail=daemo_error( "No accounts were created, something went wrong!"))
def destroy(self, request, pk=None, *args, **kwargs): instance = self.get_object() update_worker_cache([instance.worker_id], constants.ACTION_GROUP_REMOVE, value=instance.group_id) instance.delete() return Response(data={"pk": pk}, status=status.HTTP_200_OK)
def create_with_entries(self, requester, entries, *args, **kwargs): group = RequesterAccessControlGroup.objects.create( requester=requester, **self.validated_data) for entry in entries: d = {'group': group.id, 'worker': entry} entry_serializer = WorkerACESerializer(data=d) if entry_serializer.is_valid(): entry_serializer.create() else: raise ValueError('Invalid user ids') update_worker_cache(entries, constants.ACTION_GROUPADD, value=group.id) return group
def create_with_entries(self, requester, entries, *args, **kwargs): group = RequesterAccessControlGroup.objects.create(requester=requester, **self.validated_data) for entry in entries: d = { 'group': group.id, 'worker': entry } entry_serializer = WorkerACESerializer(data=d) if entry_serializer.is_valid(): entry_serializer.create() else: raise ValueError('Invalid user ids') update_worker_cache(entries, constants.ACTION_GROUP_ADD, value=group.id) return group
def stripe(self, request, *args, **kwargs): from crowdsourcing.serializers.utils import CreditCardSerializer, BankSerializer from crowdsourcing.payment import Stripe is_worker = request.data.get('is_worker', False) is_requester = request.data.get('is_requester', False) ssn_last_4 = request.data.get('ssn_last_4') bank_data = None credit_card = None if not is_worker and not is_requester: raise serializers.ValidationError(detail=daemo_error("Please set either worker or requester to true.")) if is_requester: card_serializer = CreditCardSerializer(data=request.data.get('credit_card', {})) if not card_serializer.is_valid(): raise serializers.ValidationError(detail=card_serializer.errors) credit_card = request.data.get('credit_card') if is_worker: if not UserViewSet.is_whitelisted(request.user): raise serializers.ValidationError( detail=daemo_error("You are not allowed to sign up as a worker at this time.")) # TODO add support for other countries bank_data = request.data.get('bank', {}) bank_data.update({'currency': 'usd'}) bank_data.update({'country': 'US'}) bank_serializer = BankSerializer(data=bank_data) if not bank_serializer.is_valid(): raise serializers.ValidationError(detail=bank_serializer.errors) profile = request.user.profile account, customer = Stripe().create_account_and_customer(user=request.user, country_iso=profile.address.city.country.code, ip_address='8.8.8.8', is_worker=is_worker, is_requester=is_requester, credit_card=credit_card, bank=bank_data, ssn_last_4=ssn_last_4) if account is not None: profile.is_worker = True update_worker_cache([profile.user_id], constants.ACTION_UPDATE_PROFILE, 'is_worker', 1) if customer is not None: profile.is_requester = True update_worker_cache([profile.user_id], constants.ACTION_UPDATE_PROFILE, 'is_requester', 1) if account is not None or customer is not None: profile.save() return Response(data={"message": "Accounts and customer created"}, status=status.HTTP_201_CREATED) raise serializers.ValidationError(detail=daemo_error("No accounts were created, something went wrong!"))
def update(self, **kwargs): user = self.validated_data.pop('user') if self.validated_data.get('location') is not None: location_data = self.validated_data.pop('location') city_name = None country_name = None country_code = None state_name = "" state_code = "" street_name = None postal_code = "" if 'city' in location_data: city_name = location_data.pop('city') if 'postal_code' in location_data: postal_code = location_data.pop('postal_code') if 'country' in location_data: country_name = location_data.pop('country') country_code = location_data.pop('country_code') if 'address' in location_data: street_name = location_data.pop('address') if 'state' in location_data: state_name = location_data.pop('state') state_code = location_data.pop('state_code') if country_name is not None and city_name is not None: country, created = models.Country.objects.get_or_create(name=country_name, code=country_code) city, created = models.City.objects.get_or_create(name=city_name, state=state_name, state_code=state_code, country=country) address, created = models.Address.objects.get_or_create(street=street_name, city=city, postal_code=postal_code) self.instance.address = address else: self.instance.address = None self.instance.gender = self.validated_data.get('gender', self.instance.gender) self.instance.birthday = self.validated_data.get('birthday', self.instance.birthday) self.instance.is_verified = self.validated_data.get('is_verified', self.instance.is_verified) self.instance.picture = self.validated_data.get('picture', self.instance.picture) self.instance.ethnicity = self.validated_data.get('ethnicity', self.instance.ethnicity) self.instance.purpose_of_use = self.validated_data.get('purpose_of_use', self.instance.purpose_of_use) self.instance.unspecified_responses = self.validated_data.get('unspecified_responses', self.instance.unspecified_responses) self.instance.education = self.validated_data.get('education', self.instance.education) self.instance.save() self.instance.user.first_name = user.get('first_name', self.instance.user.first_name) self.instance.user.last_name = user.get('last_name', self.instance.user.last_name) self.instance.user.save() update_worker_cache([self.instance.user_id], constants.ACTION_UPDATE_PROFILE, 'gender', self.instance.gender) if self.instance.birthday is not None: update_worker_cache([self.instance.user_id], constants.ACTION_UPDATE_PROFILE, 'birthday_year', self.instance.birthday.year) update_worker_cache([self.instance.user_id], constants.ACTION_UPDATE_PROFILE, 'ethnicity', self.instance.ethnicity) return self.instance
def create(self, **kwargs): print "in serial create" 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] print "in serial create - uname val done" 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.objects.create(user=user, is_worker=False, is_requester=False, handle=username) profile_data = { 'location': self.initial_data.get('location', {}), 'birthday': self.initial_data.get('birthday', None), 'user': {} } user_profile_serializer = UserProfileSerializer(instance=user_profile, data=profile_data, partial=True) if user_profile_serializer.is_valid(): user_profile = user_profile_serializer.update() update_worker_cache([user.id], constants.ACTION_UPDATE_PROFILE, 'is_worker', 0) update_worker_cache([user.id], constants.ACTION_UPDATE_PROFILE, 'is_requester', 0) # update_worker_cache([user.id], constants.ACTION_UPDATE_PROFILE, 'birthday_year', # user_profile.birthday.year) models.UserPreferences.objects.create(user=user) # if self.validated_data.get('is_requester', True): # user_profile.is_requester = True # user_profile.save() # requester_financial_account = models.FinancialAccount() # requester_financial_account.owner = user # requester_financial_account.type = models.FinancialAccount.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', True) or not has_profile_info: # user_profile.is_worker = True # user_profile.save() # worker_financial_account = models.FinancialAccount() # worker_financial_account.owner = user # worker_financial_account.type = models.FinancialAccount.TYPE_WORKER # worker_financial_account.save() ################Bir Commented out to discourse############# if settings.EMAIL_ENABLED: print "in 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.UserRegistration() registration_model.user = User.objects.get(id=user.id) registration_model.activation_key = activation_key send_activation_email(email=user.email, host=self.context['request'].get_host(), activation_key=activation_key) registration_model.save() # if settings.DISCOURSE_BASE_URL and settings.DISCOURSE_API_KEY: # try: # client = DiscourseClient( # settings.DISCOURSE_BASE_URL, # api_username='******', # api_key=settings.DISCOURSE_API_KEY) # client.create_user(name=user.get_full_name(), # username=user.profile.handle, # email=user.email, # password=self.initial_data.get('password1'), # active=True, # approved=True) # except Exception: # print("Failed to create Discourse user!") return user
def update(self, **kwargs): user = self.validated_data.pop('user') if self.validated_data.get('location') is not None: location_data = self.validated_data.pop('location') city_name = None country_name = None country_code = None state_name = "" state_code = "" street_name = None postal_code = "" if 'city' in location_data: city_name = location_data.pop('city') if 'postal_code' in location_data: postal_code = location_data.pop('postal_code') if 'country' in location_data: country_name = location_data.pop('country') country_code = location_data.pop('country_code') if 'address' in location_data: street_name = location_data.pop('address') if 'state' in location_data: state_name = location_data.pop('state') state_code = location_data.pop('state_code') if country_name is not None and city_name is not None: country, created = models.Country.objects.get_or_create( name=country_name, code=country_code) city, created = models.City.objects.get_or_create( name=city_name, state=state_name, state_code=state_code, country=country) address, created = models.Address.objects.get_or_create( street=street_name, city=city, postal_code=postal_code) self.instance.address = address else: self.instance.address = None self.instance.gender = self.validated_data.get('gender', self.instance.gender) self.instance.birthday = self.validated_data.get( 'birthday', self.instance.birthday) self.instance.is_verified = self.validated_data.get( 'is_verified', self.instance.is_verified) self.instance.picture = self.validated_data.get( 'picture', self.instance.picture) self.instance.ethnicity = self.validated_data.get( 'ethnicity', self.instance.ethnicity) self.instance.purpose_of_use = self.validated_data.get( 'purpose_of_use', self.instance.purpose_of_use) self.instance.unspecified_responses = self.validated_data.get( 'unspecified_responses', self.instance.unspecified_responses) self.instance.education = self.validated_data.get( 'education', self.instance.education) self.instance.save() self.instance.user.first_name = user.get('first_name', self.instance.user.first_name) self.instance.user.last_name = user.get('last_name', self.instance.user.last_name) self.instance.user.save() update_worker_cache([self.instance.user_id], constants.ACTION_UPDATE_PROFILE, 'gender', self.instance.gender) if self.instance.birthday is not None: update_worker_cache([self.instance.user_id], constants.ACTION_UPDATE_PROFILE, 'birthday_year', self.instance.birthday.year) update_worker_cache([self.instance.user_id], constants.ACTION_UPDATE_PROFILE, 'ethnicity', self.instance.ethnicity) return self.instance
def create(self, *args, **kwargs): instance = WorkerAccessControlEntry.objects.create(**self.validated_data) update_worker_cache([instance.worker_id], constants.ACTION_GROUP_ADD, value=instance.group_id) return instance
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.objects.create(user=user, is_worker=False, is_requester=False, handle=username) profile_data = { 'location': self.initial_data.get('location', {}), 'birthday': self.initial_data.get('birthday', None), 'user': {} } user_profile_serializer = UserProfileSerializer(instance=user_profile, data=profile_data, partial=True) if user_profile_serializer.is_valid(): user_profile = user_profile_serializer.update() update_worker_cache([user.id], constants.ACTION_UPDATE_PROFILE, 'is_worker', 0) update_worker_cache([user.id], constants.ACTION_UPDATE_PROFILE, 'is_requester', 0) # update_worker_cache([user.id], constants.ACTION_UPDATE_PROFILE, 'birthday_year', # user_profile.birthday.year) models.UserPreferences.objects.create(user=user) # if self.validated_data.get('is_requester', True): # user_profile.is_requester = True # user_profile.save() # requester_financial_account = models.FinancialAccount() # requester_financial_account.owner = user # requester_financial_account.type = models.FinancialAccount.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', True) or not has_profile_info: # user_profile.is_worker = True # user_profile.save() # worker_financial_account = models.FinancialAccount() # worker_financial_account.owner = user # worker_financial_account.type = models.FinancialAccount.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.UserRegistration() registration_model.user = User.objects.get(id=user.id) registration_model.activation_key = activation_key send_activation_email(email=user.email, host=self.context['request'].get_host(), activation_key=activation_key) registration_model.save() if settings.DISCOURSE_BASE_URL and settings.DISCOURSE_API_KEY: try: client = DiscourseClient( settings.DISCOURSE_BASE_URL, api_username='******', api_key=settings.DISCOURSE_API_KEY) client.create_user(name=user.get_full_name(), username=user.profile.handle, email=user.email, password=self.initial_data.get('password1'), active=True, approved=True) except Exception: print("Failed to create Discourse user!") return user