示例#1
0
    def user_from_facebook_access_token(access_token):
        try:
            __unused = get_object("debug_token", fields="id", input_token=access_token, access_token=SOCIAL_SETTINGS.FACEBOOK_APP_TOKEN)
            facebook_data = get_object("me", fields="id", access_token=access_token)
        except FBTokenInvalidException:
            return None
        except FBPermissionException:
            return None
        except FBTokenExpiredException:
            return None
        try:
            user_facebook = UserFacebook.objects.get(facebook_id=facebook_data['id'])
            user_facebook.access_token = access_token
            user_facebook.save()
            return user_facebook.user
        except UserFacebook.DoesNotExist:
            pass
        from django_town.social.resources.user import UserResource
        with transaction.atomic():
            user_resource = UserResource().create(data={'facebook_access_token': access_token})

            user_facebook = UserFacebook(facebook_id=facebook_data['id'], user=user_resource.get_instance())
            user_facebook.access_token = access_token
            user_facebook.save()
            return user_resource.get_instance()
示例#2
0
    def data_with_facebook(self, access_token):
        # TODO apply facebook v2 api.
        profile = get_object('me', fields="id,name,bio,birthday,locale,timezone,gender,email,picture",
                             access_token=access_token)
        fb_id = profile['id']
        bio = profile.get('bio', '')
        photo_url = "http://graph.facebook.com/%s/picture?width=400&height=400" % fb_id \
            if not profile['picture']['data']['is_silhouette'] else None

        if photo_url:
            img_temp = NamedTemporaryFile()
            img_temp.write(urlopen(photo_url).read())
            img_temp.flush()
            img_temp.seek(0)
            photo = File(img_temp)
        else:
            photo = None

        name = profile['name']
        email = profile.get('email', '#' + fb_id + '@inva.lid')
        timezone = profile.get('timezone', 0)
        dob = datetime.datetime.strptime(profile['birthday'], "%m/%d/%Y") if 'birthday' in profile else None
        locale = profile.get('locale', 'en')[:2]
        gender = profile.get('gender', 'U')
        if gender == 'male':
            gender = 'M'
        elif gender == 'female':
            gender = 'F'
        data = {'name': name, 'dob': dob, 'locale': locale, 'email': email,
                'timezone_offset': timezone, 'gender': gender, 'bio': bio, 'fb_id': fb_id}
        if photo:
            files = {'photo': photo}
        else:
            files = None
        return data, files
示例#3
0
 def user_from_facebook_access_token(access_token):
     try:
         facebook_data = get_object("me", fields="id", access_token=access_token)
     except FBTokenInvalidException:
         return None
     except FBPermissionException:
         return None
     except FBTokenExpiredException:
         return None
     try:
         user_facebook = UserFacebook.objects.get(facebook_id=facebook_data['id'])
         user_facebook.access_token = access_token
         user_facebook.save()
         return user_facebook.user
     except UserFacebook.DoesNotExist:
         return None