Revision bef3bf46 snf-astakos-app/astakos/im/target/twitter.py

b/snf-astakos-app/astakos/im/target/twitter.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
......
49 49
from astakos.im.settings import TWITTER_KEY, TWITTER_SECRET, INVITATIONS_ENABLED, IM_MODULES
50 50

  
51 51
# It's probably a good idea to put your consumer's OAuth token and
52
# OAuth secret into your project's settings. 
52
# OAuth secret into your project's settings.
53 53
consumer = oauth.Consumer(TWITTER_KEY, TWITTER_SECRET)
54 54
client = oauth.Client(consumer)
55 55

  
......
72 72
    request_token = dict(urlparse.parse_qsl(content))
73 73
    if request.GET.get('next'):
74 74
        request_token['next'] = request.GET['next']
75
    
75

  
76 76
    # Step 2. Store the request token in a session for later use.
77 77
    response = HttpResponse()
78 78
    request.session['Twitter-Request-Token'] = value=json.dumps(request_token)
79
    
79

  
80 80
    # Step 3. Redirect the user to the authentication URL.
81 81
    url = "%s?oauth_token=%s" % (authenticate_url, request_token['oauth_token'])
82 82
    response['Location'] = url
83 83
    response.status_code = 302
84
    
84

  
85 85
    return response
86 86

  
87 87
@requires_anonymous
......
91 91
    if not data:
92 92
        raise Exception("Request token cookie not found.")
93 93
    del request.session['Twitter-Request-Token']
94
    
94

  
95 95
    request_token = json.loads(data)
96 96
    if not hasattr(request_token, '__getitem__'):
97 97
        raise BadRequest('Invalid data formating')
......
101 101
    except:
102 102
        raise BadRequest('Invalid request token cookie formatting')
103 103
    client = oauth.Client(consumer, token)
104
    
104

  
105 105
    # Step 2. Request the authorized access token from Twitter.
106 106
    resp, content = client.request(access_token_url, "GET")
107 107
    if resp['status'] != '200':
108 108
        raise Exception("Invalid response from Twitter.")
109
    
109

  
110 110
    """
111 111
    This is what you'll get back from Twitter. Note that it includes the
112 112
    user's user_id and screen_name.
113 113
    {
114 114
        'oauth_token_secret': 'IcJXPiJh8be3BjDWW50uCY31chyhsMHEhqJVsphC3M',
115
        'user_id': '120889797', 
115
        'user_id': '120889797',
116 116
        'oauth_token': '120889797-H5zNnM3qE0iFoTTpNEHIz3noL9FKzXiOxwtnyVOD',
117 117
        'screen_name': 'heyismysiteup'
118 118
    }
119 119
    """
120 120
    access_token = dict(urlparse.parse_qsl(content))
121
    
121

  
122 122
    # Step 3. Lookup the user or create them if they don't exist.
123
    
123

  
124 124
    # When creating the user I just use their screen_name@twitter.com
125 125
    # for their email and the oauth_token_secret for their password.
126
    # These two things will likely never be used. Alternatively, you 
127
    # can prompt them for their email here. Either way, the password 
126
    # These two things will likely never be used. Alternatively, you
127
    # can prompt them for their email here. Either way, the password
128 128
    # should never be used.
129 129
    screen_name = access_token['screen_name']
130 130
    next = request_token.get('next')
131
    
131

  
132 132
    # check first if user with that email is registered
133 133
    # and if not create one
134 134
    user = None
135 135
    email = request.session.pop('email')
136
    
136

  
137 137
    if email: # signup mode
138
        if not reserved_screen_name(screen_name): 
138
        if not reserved_screen_name(screen_name):
139 139
            try:
140 140
                user = AstakosUser.objects.get(email = email)
141 141
            except AstakosUser.DoesNotExist, e:
......
166 166
            return prepare_response(request, user, next)
167 167
        elif user and not user.is_active:
168 168
            messages.add_message(request, messages.ERROR, 'Inactive account: %s' % user.email)
169
    ip = request.META.get('REMOTE_ADDR',
170
            request.META.get('HTTP_X_REAL_IP', None))
169 171
    return render_response(login_template,
170
                   form = LocalUserCreationForm(ip=request.META['REMOTE_ADDR']),
172
                   form = LocalUserCreationForm(ip=ip),
171 173
                   context_instance=get_context(request, extra_context))
172 174

  
173 175
def reserved_screen_name(screen_name):
......
178 180
    except AstakosUser.DoesNotExist, e:
179 181
        return False
180 182

  
181
def create_user(request, form, backend=None, post_data={}, next = None, on_failure='im/signup.html', on_success='im/signup_complete.html', extra_context={}): 
183
def create_user(request, form, backend=None, post_data={}, next = None, on_failure='im/signup.html', on_success='im/signup_complete.html', extra_context={}):
182 184
    """
183 185
    Create a user.
184
    
186

  
185 187
    The user activation will be delegated to the backend specified by the ``backend`` keyword argument
186 188
    if present, otherwise to the ``astakos.im.backends.InvitationBackend``
187 189
    if settings.ASTAKOS_INVITATIONS_ENABLED is True or ``astakos.im.backends.SimpleBackend`` if not
188 190
    (see backends);
189
    
191

  
190 192
    Upon successful user creation if ``next`` url parameter is present the user is redirected there
191 193
    otherwise renders the ``on_success`` template (if exists) or im/signup_complete.html.
192
    
194

  
193 195
    On unsuccessful creation, renders the ``on_failure`` template (if exists) or im/signup.html with an error message.
194
    
196

  
195 197
    **Arguments**
196
    
198

  
197 199
    ``on_failure``
198 200
        A custom template to render in case of failure. This is optional;
199 201
        if not specified, this will default to ``im/signup.html``.
200
    
202

  
201 203
    ``on_success``
202 204
        A custom template to render in case of success. This is optional;
203 205
        if not specified, this will default to ``im/signup_complete.html``.
204
    
206

  
205 207
    ``extra_context``
206 208
        An dictionary of variables to add to the template context.
207
    
209

  
208 210
    **Template:**
209
    
211

  
210 212
    im/signup.html or ``on_failure`` keyword argument.
211 213
    im/signup_complete.html or ``on_success`` keyword argument.
212 214
    """
......
230 232
        messages.add_message(request, messages.ERROR, e)
231 233
    for provider in IM_MODULES:
232 234
        extra_context['%s_form' % provider] = backend.get_signup_form(provider)
235
    ip = request.META.get('REMOTE_ADDR',
236
            request.META.get('HTTP_X_REAL_IP', None))
233 237
    return render_response(on_failure,
234
                           form = LocalUserCreationForm(ip=request.META['REMOTE_ADDR']),
235
                           context_instance=get_context(request, extra_context))
238
                           form = LocalUserCreationForm(ip=ip),
239
                           context_instance=get_context(request, extra_context))

Also available in: Unified diff