示例#1
0
def authorize_callback_view(request, unpack=_unpack_callback):
    """Complete the OAuth dance after a user has authorized the app."""
    
    # This callback should not be called to handle login or signup.
    if request.session.get('twitter_oauth_is_authenticate'):
        return HTTPForbidden()
    # Unpack the request.
    return_value = unpack(request)
    if isinstance(return_value, HTTPFound):
        return return_value
    twitter_user, oauth_handler, access_permission = return_value
    # Update or create the ``twitter_account`` corresponding to ``twitter_user``,
    # relate it to the current ``request.user`` and save to the db.
    twitter_account = get_existing_twitter_account(twitter_user.id)
    if not twitter_account:
        twitter_account = TwitterAccount()
        twitter_account.twitter_id = twitter_user.id
    twitter_account.screen_name = twitter_user.screen_name
    twitter_account.oauth_token = oauth_handler.access_token.key
    twitter_account.oauth_token_secret = oauth_handler.access_token.secret
    twitter_account.access_permission = access_permission
    twitter_account.user = request.user
    save_to_db(twitter_account)
    # Redirect to the appropriate location.
    next_ = request.session.get('twitter_oauth_next')
    location = _get_redirect_url(request, 'connect', next_, user=request.user)
    return HTTPFound(location=location)
示例#2
0
def create_assignment_view(request, form_cls=Form):
    """Create an assignment."""
    
    form = form_cls(request, schema=CreateAssignment)
    if form.validate():
        d = form.data
        assignment = Assignment(title=d['title'], description=d['description'])
        assignment.story = request.context.story
        assignment.author = request.user
        save_to_db(assignment)
        # XXX create promote offer (n.b.: remember if statement so don't show
        # content to it's originator to RT and auth increment the count if
        # it's your own content)
        return assignment.__json__()
    request.response.status_int = 400
    return form.errors
示例#3
0
def authenticate_callback_view(request, unpack=_unpack_callback):
    """Complete the OAuth dance after a user has authenticated the app."""
    
    # This view should not be called by an authenticated user.
    if request.is_authenticated:
        return HTTPForbidden()
    
    # Unpack the request.
    return_value = unpack(request)
    if isinstance(return_value, HTTPFound):
        return return_value
    twitter_user, oauth_handler, access_permission = return_value
    # If there is an existing ``twitter_account`` then this is a login, so
    # update the ``twitter_account`` and generate a login event.
    existing = get_existing_twitter_account(twitter_user.id)
    if existing:
        twitter_account = existing
        twitter_account.profile.set_data_from_tweepy_user(twitter_user)
        user = twitter_account.user
        event = UserLoggedIn(request, user, data=twitter_user)
        action = 'login'
    else: # Otherwise, this is a signup, so insert a new ``user`` with a
        # ``twitter_account`` and generate a signup event.
        user = User()
        user.username = twitter_user.screen_name
        twitter_account = TwitterAccount()
        twitter_account.twitter_id = twitter_user.id
        twitter_account.user = user
        twitter_account.profile = TwitterProfile.create_from_tweepy_user(twitter_user)
        event = UserSignedUp(request, user, data=twitter_user)
        action = 'signup'
    # Update the twitter_account with the latest data, save to the db and
    # actually fire the event.
    twitter_account.screen_name = twitter_user.screen_name
    twitter_account.oauth_token = oauth_handler.access_token.key
    twitter_account.oauth_token_secret = oauth_handler.access_token.secret
    twitter_account.access_permission = access_permission
    save_to_db(twitter_account) # <!-- this saves the user along with it.
    request.registry.notify(event)
    # Actually log the user in and then redirect to the appropriate location.
    next_ = request.session.get('twitter_oauth_next')
    location = _get_redirect_url(request, action, next_, user=user)
    headers = remember(request, user.canonical_id)
    return HTTPFound(location=location, headers=headers)
示例#4
0
def create_offer_view(request, form_cls=Form):
    """Create a cover or a promote offer."""
    
    # Unpack the request / context.
    user = request.user
    offer_cls = request.context.model_cls
    assignment = request.context.assignment
    
    form = form_cls(request, schema=CreateOffer)
    if form.validate():
        # Don't let a user create two offers for the same thing.
        if offer_cls.query.filter_by(assignment=assignment, user=user).first():
            return HTTPBadRequest()
        # Create and save the offer.
        offer = form.bind(offer_cls(assignment=assignment, user=user))
        save_to_db(offer)
        return offer.__json__()
    request.response.status_int = 400
    return form.errors
示例#5
0
def authenticate_callback_view(request, unpack=_unpack_callback):
    """Complete the OAuth dance after a user has authenticated the app."""
    
    # This view should not be called by an authenticated user.
    if request.is_authenticated:
        return HTTPForbidden()
    
    # Unpack the request.
    return_value = unpack(request)
    if isinstance(return_value, HTTPFound):
        return return_value
    twitter_user, oauth_handler, access_permission = return_value
    # If there is an existing ``twitter_account`` then this is a login, so
    # update the ``twitter_account`` and generate a login event.
    existing = get_existing_twitter_account(twitter_user.id)
    if existing:
        twitter_account = existing
        twitter_account.profile.set_data_from_tweepy_user(twitter_user)
        user = twitter_account.user
        event = UserLoggedIn(request, user, data=twitter_user)
        action = 'login'
    else: # Otherwise, this is a signup, so insert a new ``user`` with a
        # ``twitter_account`` and generate a signup event.
        user = User()

        # check and see if the username is taken
        new_username = twitter_user.screen_name
        counter = 0
        while True:
            existing_user = User.query.filter(User.username==new_username)
            if existing_user.count() == 0:
                user.username = new_username
                break
            # username exists, so add a counter to the end and try again
            counter += 1
            new_username = '******' % (new_username, counter)

        # create user profile
        user.profile = Profile()
        twitter_account = TwitterAccount()
        
        twitter_account.profile = TwitterProfile.create_from_tweepy_user(twitter_user)
        tp = json.loads(twitter_account.profile.data_str)

        # set the display name
        if "name" in twitter_account.profile.data_str:
            try:
                user.profile.display_name = tp['name']
            except KeyError:
                user.profile.display_name = user.username

        twitter_account.twitter_id = twitter_user.id
        twitter_account.user = user

        # grab bio and location for user profile
        if "description" in twitter_account.profile.data_str:
            try:
                user.profile.bio = tp['description']
            except KeyError:
                pass
        
        if "location" in twitter_account.profile.data_str: 
            try:
                user.profile.formatted_location = tp['location']
            except KeyError:
                pass

        if "profile_image_url" in twitter_account.profile.data_str: 
            try:
                user.profile.image = tp['profile_image_url']
            except KeyError:
                pass
        
        event = UserSignedUp(request, user, data=twitter_user)
        action = 'signup'
    # Update the twitter_account with the latest data, save to the db and
    # actually fire the event.
    twitter_account.screen_name = twitter_user.screen_name
    twitter_account.oauth_token = oauth_handler.access_token.key
    twitter_account.oauth_token_secret = oauth_handler.access_token.secret
    twitter_account.access_permission = access_permission
    save_to_db(twitter_account) # <!-- this saves the user along with it.
    request.registry.notify(event)
    # Actually log the user in and then redirect to the appropriate location.
    next_ = request.session.get('twitter_oauth_next')
    location = _get_redirect_url(request, action, next_, user=user)
    headers = remember(request, user.canonical_id)
    return HTTPFound(location=location, headers=headers)
示例#6
0
def close_offer_view(request):
    """Close an offer."""
    
    request.context.closed = True
    save_to_db(request.context)
    return {'status': 'OK'}