Ejemplo n.º 1
0
 def ClubList(self, request):
   query = Club.query()
   if request.order == ClubListRequest.Order.TEXT:
     query = query.order(Club.name)
   elif request.order == ClubListRequest.Order.WHEN:
     query = query.order(-Club.creation_time)
   clubs = [entity.ToMessage() for entity in query.fetch(request.limit)]
   return ClubListResponse(clubs=clubs)
Ejemplo n.º 2
0
def auth(req, provider, token):  # pragma: no cover
    """
    ``GET`` @ |ta| ``/auth/<provider>/<token>``

    This function handles the authentication via social networks

    .. note::

        supports:

            - facebook
            - google


    :param req: the request
    :param provider: the provider, e.g., ``facebook``
    :param token: the token
    :return: a webApp2 Response object
    """

    # the pragma no cover is to skip the testing on this method, which can't be tested
    # get user infos
    d_user, token, error = GCAuth.handle_oauth_callback(token, provider)
    if error:
        raise AuthenticationError(error)
    # check if user exists..
    # logging.debug("%s %s %s" % (d_user, token, error))
    auth_id = str(provider) + ":" + d_user['id']
    user = User.get_by_auth_id(auth_id)
    email = d_user['email']
    # we check if users access with another social network
    user_via_mail = User.query(ndb.GenericProperty('email') == email).get()
    if user_via_mail:
        user_via_mail.add_auth_id(auth_id)
        user = user_via_mail
    # create the user..
    created = False
    if not user:
        if provider == 'google':
            created, user = User.create_user(auth_id, 
                                            # unique_properties=['email'],
                                             name=d_user.get('name', 'unknown'),
                                             nickname="",
                                             gender=d_user.get('gender', 'unknown')[0],
                                             picture=d_user.get('picture', None),
                                             avatar="",
                                             birthday=datetime.datetime.now(),
                                             country="",
                                             city="",
                                             language=d_user.get('locale', 'en'),
                                             email=d_user.get('email', '*****@*****.**'),
                                             phone="",
                                             active_club=None,
                                             owner_club=None,
                                             sensors=[])
        elif provider == 'facebook':
            created, user = User.create_user(auth_id, 
                                            # unique_properties=['email'],
                                             name=d_user.get('name', 'unknown'),
                                             nickname="",
                                             gender=d_user.get('gender', 'unknown')[0],
                                             picture="http://graph.facebook.com/%s/picture?type=large" % d_user.get(
                                                 'id', None),
                                             avatar="",
                                             birthday=datetime.datetime.now(),
                                             country="",
                                             city="",
                                             language=d_user.get('locale', 'en'),
                                             email=d_user.get('email', '*****@*****.**'),
                                             phone="",
                                             active_club=None,
                                             owner_club=None,
                                             sensors=[])
        else:
            raise AuthenticationError("provider not allowed")
        if not created:
            logging.error(
                "something is wrong with user %s with this token %s and this provider %s - unique %s" % (
                    d_user, token, provider, user))
            raise AuthenticationError(
                "Something is wrong with your account, these properties must be unique %s." % user)
        else:
            free_club = Club.query(Club.name == cfg.DEMO_CLUB).get()
            if free_club:
                courses = APIDB.get_club_courses(free_club)
                for course in courses:
                    APIDB.add_member_to_course(user, course, status="ACCEPTED")

    s_token = GCAuth.auth_user_token(user)
    # if we crate the response, then we need the cors stuff.
    # response = webapp2.Response(content_type='application/json', charset='UTF-8')
    # if created:
    # response.status = 201
    # cookie = GCAuth.get_secure_cookie(token)
    # response.set_cookie('gc_token', cookie, secure=False,
    # max_age=int(cfg.AUTH_TOKEN_MAX_AGE), domain="/")
    token = GCAuth.get_token(s_token)
    # resp.headers.update({
    #             'Access-Control-Allow-Origin': origin,
    #             'Access-Control-Allow-Credentials': 'true'})
    # response.write(json.dumps(token))
    deferred.defer(sync_user, user, s_token)
    return token