Beispiel #1
0
def CanViewRoles(user, viewer):
    if not viewer:
        return False
    return (viewer.HasAnyRole(Roles.DelegateRoles())
            or viewer.HasAnyRole(Roles.AdminRoles()))
Beispiel #2
0
    def get(self):
        OAuthBaseHandler.get(self)
        if not self.auth_token:
            return

        response = self.GetWcaApi('/api/v0/me')
        if response.status != 200:
            self.response.set_status(response.status)
            logging.error('Error from WCA: ' + self.response.read())
            return

        # Save the account information we need.
        wca_info = json.loads(response.read())['me']
        self.session['wca_account_number'] = str(wca_info['id'])
        self.session['login_time'] = (
            datetime.datetime.now() -
            datetime.datetime.utcfromtimestamp(0)).total_seconds()
        user = User.get_by_id(str(
            wca_info['id'])) or User(id=str(wca_info['id']))
        if 'wca_id' in wca_info:
            user.wca_person = ndb.Key(Person, wca_info['wca_id'])
        else:
            del user.wca_person

        if 'name' in wca_info:
            user.name = wca_info['name']
        else:
            del user.name

        if 'email' in wca_info:
            user.email = wca_info['email']
        else:
            del user.email

        user.roles = [
            role for role in user.roles if role not in Roles.DelegateRoles()
        ]
        if 'delegate_status' in wca_info:
            if wca_info['delegate_status'] == 'senior_delegate':
                user.roles.append(Roles.SENIOR_DELEGATE)
            elif wca_info['delegate_status'] == 'delegate':
                user.roles.append(Roles.DELEGATE)
            elif wca_info['delegate_status'] == 'candidate_delegate':
                user.roles.append(Roles.CANDIDATE_DELEGATE)

        wca_id_user = User.get_by_id(wca_info['wca_id'])
        if wca_id_user:
            if wca_id_user.city and not user.city:
                user.city = wca_id_user.city
            if wca_id_user.state and not user.state:
                user.state = wca_id_user.state
            if wca_id_user.latitude and not user.latitude:
                user.latitude = wca_id_user.latitude
            if wca_id_user.longitude and not user.longitude:
                user.longitude = wca_id_user.longitude
            wca_id_user.key.delete()

        user.last_login = datetime.datetime.now()

        user.put()
        self.redirect(str(self.handler_data))
Beispiel #3
0
 def PermittedRoles(self):
   return Roles.AdminRoles() + Roles.DelegateRoles()
Beispiel #4
0
    def get(self):
        OAuthBaseHandler.GetTokenFromCode(self)
        if not self.auth_token:
            return

        response = self.GetWcaApi('/api/v0/me')
        if response.status != 200:
            self.response.set_status(response.status)
            logging.error('Error from WCA: ' + self.response.read())
            return

        # Save the account information we need.
        wca_info = json.loads(response.read())['me']
        self.session['wca_account_number'] = str(wca_info['id'])
        self.session['login_time'] = (
            datetime.datetime.now() -
            datetime.datetime.utcfromtimestamp(0)).total_seconds()
        user = User.get_by_id(str(
            wca_info['id'])) or User(id=str(wca_info['id']))
        if 'wca_id' in wca_info and wca_info['wca_id']:
            user.wca_person = ndb.Key(Person, wca_info['wca_id'])
            # If the user has a state on their account, we should update this on the
            # Person and Ranks as wel.
            if user.state:
                person = user.wca_person.get()
                person.state = user.state
                person.put()
                for rank_class in (RankSingle, RankAverage):
                    ndb.put_multi(
                        rank_class.query(
                            rank_class.person == person.key).fetch())
        else:
            del user.wca_person

        if 'name' in wca_info:
            user.name = wca_info['name']
        else:
            del user.name

        if 'email' in wca_info:
            user.email = wca_info['email']
        else:
            del user.email

        user.roles = [
            role for role in user.roles if role not in Roles.DelegateRoles()
        ]
        if 'delegate_status' in wca_info:
            if wca_info['delegate_status'] == 'senior_delegate':
                user.roles.append(Roles.SENIOR_DELEGATE)
            elif wca_info['delegate_status'] == 'delegate':
                user.roles.append(Roles.DELEGATE)
            elif wca_info['delegate_status'] == 'candidate_delegate':
                user.roles.append(Roles.CANDIDATE_DELEGATE)

        if wca_info['wca_id']:
            wca_id_user = User.get_by_id(wca_info['wca_id'])
        else:
            wca_id_user = None
        if wca_id_user:
            if wca_id_user.city and not user.city:
                user.city = wca_id_user.city
            if wca_id_user.state and not user.state:
                user.state = wca_id_user.state
            if wca_id_user.latitude and not user.latitude:
                user.latitude = wca_id_user.latitude
            if wca_id_user.longitude and not user.longitude:
                user.longitude = wca_id_user.longitude
            wca_id_user.key.delete()

        user.last_login = datetime.datetime.now()

        user.put()
        self.redirect(str(self.handler_data))