By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 api.py

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