Example #1
0
    def create(self, validated_data):

        if 'fullname' in validated_data:
            if validated_data['fullname'].find(' ') != -1:
                first_name, last_name = validated_data['fullname'].split(' ')
                validated_data['first_name'] = first_name
                validated_data['last_name'] = last_name

        user_serializer = UserSerializer(data=validated_data)

        user_serializer.is_valid(raise_exception=True)
        user = user_serializer.save()

        user_profile_data = {}
        user_profile_fields = ['gender', 'aliases']
        for field in user_profile_fields:
            if field in validated_data:
                user_profile_data[field] = validated_data[field]

        user_profile_data['user'] = user

        user_profile = UserProfile.objects.create(**user_profile_data)

        user_profile.save()

        if 'avatar_base64' in validated_data:
            avatar_base64 = b64decode(validated_data.get('avatar_base64'))
            img_filename = "%s.%s" % (str(uuid.uuid4()), "jpg")
            user_profile.avatar = ContentFile(avatar_base64, img_filename)
            user_profile.save()
        elif 'avatar_url' in validated_data:
            avatar_url = validated_data.get('avatar_url')
            response = urllib2.urlopen(avatar_url)
            content_type = response.info().getheader('Content-Type')
            extension = "jpg"
            if 'image' in content_type:
                parts = content_type.split('/')
                extension = parts[1] or 'jpg'
            img_filename = "%s.%s" % (str(uuid.uuid4()), extension)

            img_temp = NamedTemporaryFile(delete=True)
            img_temp.write(response.read())
            img_temp.flush()

            user_profile.avatar.save(img_filename, File(img_temp))

        access_token = generate_access_token(user)

        user_data = UserSerializer(user_profile.user).data
        self._data = {
            "access_token": str(access_token),
            'user': user_data,
        }

        return user_profile
Example #2
0
    def create(self, validated_data):
        user = validated_data.get('user')
        access_token = generate_access_token(user)

        user_data = UserSerializer(user).data
        self._data = {
            "access_token": str(access_token),
            'user': user_data,
        }

        return access_token
Example #3
0
    def create(self, validated_data):

        fb_id = validated_data.get('fb_id')
        fullname = validated_data.get('name')
        email = validated_data.get('email')
        gender = validated_data.get('gender')
        avatar_url = validated_data.get('avatar_url')
        fb_access_token = validated_data.get('fb_access_token')

        gender_id = 0
        if gender == 'male':
            gender_id = 1
        elif gender == 'female':
            gender_id = 2

        social_account = None
        try:
            social_account = SocialAccount.objects.get(provider='facebook', social_id=fb_id, authenticator=True)
            social_account.social_token = fb_access_token
            social_account.social_username = fullname
            social_account.save()
        except SocialAccount.DoesNotExist:
            user_profile_data = {
                'username': email,
                'email': email,
                'password': uuid.uuid4(),
                'fullname': fullname,
                'gender': gender_id,
                'avatar_url': avatar_url,
            }
            user_profile_serializer = SignupSerializer(data=user_profile_data)
            user_profile_serializer.is_valid(raise_exception=True)
            user_profile = user_profile_serializer.save()
            social_account = SocialAccount.objects.create(user_profile=user_profile,
                                                          social_username=fullname,
                                                          social_id=fb_id,
                                                          social_token=fb_access_token,
                                                          provider='facebook',
                                                          authenticator=True)

        user_profile = social_account.user_profile
        access_token = generate_access_token(user_profile.user)

        user_data = UserSerializer(user_profile.user).data

        self._data = {
            "access_token": str(access_token),
            'user': user_data,
        }

        return user_profile
Example #4
0
    def create(self, validated_data):
        key = validated_data.get('key')
        secret = validated_data.get('secret')
        fullname = validated_data.get('fullname')
        email = validated_data.get('email')
        twitter_id = validated_data.get('twitter_id')
        username = validated_data.get('username')
        social_username = validated_data.get('social_username')
        avatar_url = validated_data.get('avatar_url')

        social_account = None

        try:
            social_account = SocialAccount.objects.get(provider='twitter', social_id=twitter_id, authenticator=True)
            social_account.social_secret = secret
            social_account.social_token = key
            social_account.save()
        except SocialAccount.DoesNotExist:
            user_profile_data = {
                'username': username,
                'email': email,
                'password': uuid.uuid4(),
                'fullname': fullname,
                'avatar_url': avatar_url,
            }
            user_profile_serializer = SignupSerializer(data=user_profile_data)
            user_profile_serializer.is_valid(raise_exception=True)
            user_profile = user_profile_serializer.save()
            social_account = SocialAccount.objects.create(
                social_username=social_username,
                user_profile=user_profile,
                social_id=twitter_id,
                social_token=key,
                social_secret=secret,
                provider='twitter'
            )

        user_profile = social_account.user_profile
        access_token = generate_access_token(user_profile.user)

        user_data = UserSerializer(user_profile.user).data
        self._data = {
            "access_token": str(access_token),
            'user': user_data,
        }

        return user_profile