Пример #1
0
def create_default_facebook_profile(request):
    '''
    Called by both facebook_login (if connecting through the website) and synchronise_facebook_profile (if using facebook.require_add in events_fb)
    This is the place to send emails!
    '''

    #
    # Create User
    #
    new_user = User( username = str(uuid.uuid4())[:30] )

    # basic user created, now get some information about him
    fb_profile = request.facebook.users.getInfo([request.facebook.uid], ['first_name', 'last_name', 'pic_square', 'profile_url', 'current_location', 'email'])[0]

    new_user.first_name = fb_profile['first_name']
    new_user.last_name = fb_profile['last_name']
    new_user.email = fb_profile['email']

    new_user.put()

    #
    # Create Profile
    #
    try:
        zip = int(fb_profile['current_location']['zip'])
    except:
        zip = 0
        
    try:
        current_location_city = fb_profile['current_location']['city']
        current_location_state = fb_profile['current_location']['state']
        current_location_country = fb_profile['current_location']['country']
    except:
        current_location_city = None
        current_location_state = None
        current_location_country = None

    new_profile = FacebookProfile(
        user = new_user,
        uid = str(request.facebook.uid),
        pic_square = fb_profile['pic_square'],
        profile_url = fb_profile['profile_url'],
        current_location_city = current_location_city,
        current_location_state = current_location_state,
        current_location_country = current_location_country,
        current_location_zip = zip,
        site = Site.objects.get_current(),
        )

    new_profile.put()
    
    logging.debug("Created user: %s"%new_profile.user)

    if getattr(settings, 'SOCIAL_SEND_EMAIL_ON_NEW_PROFILE', False):
        if not new_user.email:
            logging.debug("Not sending email - user hasn't specified email")
        else:
            logging.debug("Sending email to: %s" % new_user.email)
            
            try:
                from django.core.mail import send_mail
                subject = render_to_string('socialregistration/new_profile_email_subject.txt',
                                               { 'site': new_profile.site })
                # Email subject *must not* contain newlines
                subject = ''.join(subject.splitlines())

                message = render_to_string('socialregistration/new_profile_email.txt',
                                           { 'user': new_user,
                                             'profile': new_profile })

                send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [new_user.email])

            except NotImplementedError:
                logging.error("Failed to send email to %s; subject: %s" %(new_user.email, subject))

    return new_profile