Example #1
0
def register_token():
    session = Session()
    token = session.query(Token).first()

    if token:
        return token, False

    url = api_method_url('project_tokens/')
    headers = {'User-Agent': 'Jet Django'}

    r = requests.request('POST', url, headers=headers)
    success = 200 <= r.status_code < 300

    if not success:
        logging.error('Register Token request error', r.status_code, r.reason)
        return None, False

    result = r.json()

    # TODO: add serializer
    token = result['token'].replace('-', '')
    date_add = datetime.strptime(result['date_add'][:-6],
                                 '%Y-%m-%dT%H:%M:%S.%f')

    token = Token(token=token, date_add=date_add)
    session.add(token)
    session.commit()

    return token, True
Example #2
0
def reset_token():
    session = Session()
    session.query(Token).delete()
    session.commit()

    return register_token()