api.py
Python script, ASCII text executable
1import uuid 2from models import * 3from app import app, db, bcrypt 4from misc_utils import * 5from common import git_command 6import os 7import shutil 8import config 9import flask 10import git 11import subprocess 12from flask_httpauth import HTTPBasicAuth 13import zlib 14import re 15import datetime 16 17auth = HTTPBasicAuth(realm=config.AUTH_REALM + " Data API") 18 19api_app = flask.Blueprint("api_app", __name__, template_folder="templates/api/", url_prefix="/data-api/") 20 21 22@auth.verify_password 23def verify_password(username, password): 24user = User.query.filter_by(username=username).first() 25 26if user and bcrypt.check_password_hash(user.password_hashed, password): 27flask.g.user = username 28return True 29 30return False 31 32 33@api_app.route("/", methods=["GET"]) 34def welcome(): 35response = flask.make_response(flask.render_template("welcome.xml")) 36response.headers["Content-Type"] = "application/xml" 37return response 38 39 40@api_app.route("/user/<username>/", methods=["GET"]) 41def get_user(username): 42response = flask.make_response(flask.render_template("user.xml", user=db.session.get(User, username))) 43response.headers["Content-Type"] = "application/xml" 44return response 45 46 47@api_app.route("/user/<username>/repositories", methods=["GET"]) 48@auth.login_required(optional=True) 49def get_user_repositories(username): 50user = db.session.get(User, username) 51page = flask.request.args.get("page", 1) 52page_size = flask.request.args.get("page_size", 64) 53if hasattr(flask.g, "user") and flask.g.user == username: 54repositories = Repo.query.filter_by(owner=user).paginate(page=page, per_page=page_size) 55else: 56repositories = Repo.query.filter_by(owner=user, visibility=2).paginate(page=page, per_page=page_size) 57response = flask.make_response( 58flask.render_template("user-repositories.xml", user=user, 59repositories=repositories.items, page=page, page_size=page_size) 60) 61response.headers["Content-Type"] = "application/xml" 62return response 63 64 65app.register_blueprint(api_app) 66