From 571976cb2780ab0272b95b05bff860a3b8f04cc9 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 5 Oct 2014 01:25:45 +0200 Subject: split out utils.py --- app/utils.py | 39 +++++++++++++++++++++++++++++++++++++++ app/views.py | 35 +---------------------------------- 2 files changed, 40 insertions(+), 34 deletions(-) create mode 100644 app/utils.py diff --git a/app/utils.py b/app/utils.py new file mode 100644 index 0000000..a02ab2f --- /dev/null +++ b/app/utils.py @@ -0,0 +1,39 @@ +from app import app +from flask import url_for +from requests_oauthlib import OAuth2Session + +def authenticate(access_token): + token = {"scope": [""], "access_token": access_token, "token_type": "bearer"} + github = OAuth2Session(app.config['GITHUB_CLIENT_ID'], token=token) + user_data = github.get('https://api.github.com/user') + if user_data.status_code == 200: + return True + else: + return False + +def make_public_ticket(ticket): + new_ticket = ticket.copy() + new_ticket['uri'] = url_for('get_ticket', ticket_id=ticket['id'], _external=True) + return new_ticket + +def ticket_to_dict(ticket): + nt = { + 'id': ticket.id, + 'summary': ticket.summary, + 'body': ticket.body, + 'opened_at': ticket.opened_at.strftime('%Y-%m-%dT%H:%M:%S'), + 'status': ticket.status, + 'reason': ticket.reason, + 'opened_by': { + 'id': ticket.opened_by.id, + 'nickname': ticket.opened_by.nickname, + 'email': ticket.opened_by.email, + }, + } + + if ticket.updated_at: + nt['updated_at'] = ticket.updated_at.strftime('%Y-%m-%dT%H:%M:%S') + else: + nt['updated_at'] = None + + return nt diff --git a/app/views.py b/app/views.py index 3cc8891..acaf3f1 100644 --- a/app/views.py +++ b/app/views.py @@ -2,42 +2,9 @@ from flask import jsonify, abort, make_response, request, url_for, redirect, ses from requests_oauthlib import OAuth2Session from datetime import datetime from app import app, db, models +from app.utils import authenticate, ticket_to_dict, make_public_ticket import json -def make_public_ticket(ticket): - new_ticket = ticket.copy() - new_ticket['uri'] = url_for('get_ticket', ticket_id=ticket['id'], _external=True) - return new_ticket - -def ticket_to_dict(ticket): - nt = {} - nt['id'] = ticket.id - - nt['summary'] = ticket.summary - nt['body'] = ticket.body - - nt['opened_at'] = ticket.opened_at.strftime('%Y-%m-%dT%H:%M:%S') - - if ticket.updated_at: - nt['updated_at'] = ticket.updated_at.strftime('%Y-%m-%dT%H:%M:%S') - else: - nt['updated_at'] = None - - nt['status'] = ticket.status - - nt['reason'] = ticket.reason - - if ticket.opened_by: - nt['opened_by'] = { - 'id': ticket.opened_by.id, - 'nickname': ticket.opened_by.nickname, - 'email': ticket.opened_by.email, - } - else: - nt['opened_by'] = {'id': None, 'nickname': None, 'email': None} - - return nt - @app.route('/authorized') def authorized_callback(): github = OAuth2Session(app.config['GITHUB_CLIENT_ID'], state=session['oauth_state']) -- cgit v1.2.3-54-g00ecf