def token(request): if 'error' in request.GET: raise Exception(request.GET['error']) elif 'code' not in request.GET: raise Exception('No authorization code given') r = requests.post(settings.GLITCH_API_URL+'/oauth2/token?', { 'grant_type': 'authorization_code', 'code': request.GET['code'], 'client_id': settings.GLITCH_API_CREDENTIALS['key'], 'client_secret': settings.GLITCH_API_CREDENTIALS['secret'], 'redirect_uri': request.build_absolute_uri(reverse('glitchtools-users-token')), }, timeout=2) if r.status_code != 200: raise Exception(r.content) token = json.loads(r.content) auth = request.glitch_api.auth.check(oauth_token=token['access_token']) scope = GlitchUser.SCOPES_MAP[token['scope']] user = authenticate(username=auth['player_name'], tsid=auth['player_tsid'], token=token['access_token'], scope=scope) if user.glitch_user.scope < scope: update(user.glitch_user, token=token['access_token'], scope=scope) django_login(request, user) next = request.GET.get('state', '/') if not next.startswith('/'): next = '/' return HttpResponseRedirect(request.build_absolute_uri(next))
def set_features(self, raw_features): # Clean up the raw features strings, normalize to lowercase, and de-pluralize features = {} for count, feature in FEATURES_RE.findall(''.join(raw_features)): features[FEATURES_PLURALS.get(feature, feature).lower()] = int(count or 0) if not self.current_features or not self.current_features.features_equal(features): snapshot = StreetFeatureSnapshot.objects.create(street=self) for feature, count in features.iteritems(): StreetFeatureSnapshotItem.objects.create(snapshot=snapshot, feature=feature, count=count) update(self, current_features=snapshot)
def update_street_info(tsid): info = api.locations.streetInfo(street_tsid=tsid) street = Street.objects.get(tsid=tsid) street.set_features(info['features']) street.connections.exclude(tsid__in=info['connections']).delete() for connection_tsid in info['connections'].iterkeys(): try: connection_street = Street.objects.get(tsid=connection_tsid) except Street.DoesNotExist: vals = info['connections'][connection_tsid] connection_street = Street.objects.create(hub_id=vals['hub']['id'], tsid=connection_tsid, name=vals['name']) street.connections.add(connection_street) update(street, last_update=datetime.datetime.utcnow())