Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
hacking on the frontend
  • Loading branch information
dfm committed Sep 30, 2016
1 parent d63db82 commit b56a716
Show file tree
Hide file tree
Showing 12 changed files with 951 additions and 32 deletions.
Binary file added logo/logo.pxm
Binary file not shown.
4 changes: 2 additions & 2 deletions osrc/api.py
Expand Up @@ -45,7 +45,7 @@ def error_handler_403(e):

@api.route("/<username>", strict_slashes=False)
@jsonp
def user(username):
def user(username=None):
stats = user_stats(username)
if stats is None:
return flask.abort(404)
Expand All @@ -56,7 +56,7 @@ def user(username):

@api.route("/<username>/<reponame>", strict_slashes=False)
@jsonp
def repo(username, reponame):
def repo(username=None, reponame=None):
stats = repo_stats(username, reponame)
if stats is None:
return flask.abort(404)
Expand Down
109 changes: 108 additions & 1 deletion osrc/frontend.py
@@ -1,6 +1,15 @@
# -*- coding: utf-8 -*-

import json
import flask
import string
import random
import requests
from sqlalchemy import func
from urllib.parse import urlencode

from .models import db, User
from .utils import load_resource
from .stats import user_stats, repo_stats

__all__ = ["frontend"]
Expand All @@ -13,7 +22,17 @@ def user(username):
stats = user_stats(username)
if stats is None:
return flask.abort(404)
return flask.render_template("user.html", stats=stats)
with load_resource("event_verbs.json") as f:
event_verbs = json.load(f)
with load_resource("event_actions.json") as f:
event_actions = json.load(f)
return flask.render_template(
"user.html",
stats=stats,
event_verbs=event_verbs,
event_actions=event_actions,
enumerate=enumerate,
)


@frontend.route("/<username>/<reponame>", strict_slashes=False)
Expand All @@ -22,3 +41,91 @@ def repo(username=None, reponame=None):
if stats is None:
return flask.abort(404)
return flask.render_template("repo.html", stats=stats)


@frontend.route("/optout/<username>", strict_slashes=False)
def optout(username=None):
return flask.render_template("optout.html", username=username)


@frontend.route("/optout/<username>/login")
def optout_login(username):
state = "".join([random.choice(string.ascii_uppercase + string.digits)
for x in range(24)])
flask.session["state"] = state
params = dict(
client_id=flask.current_app.config["GITHUB_ID"],
redirect_uri=flask.url_for(".optout_callback", username=username,
_external=True),
state=state,
)
return flask.redirect("https://github.com/login/oauth/authorize?{0}"
.format(urlencode(params)))


@frontend.route("/optout/<username>/callback")
def optout_callback(username):
state1 = flask.session.get("state")
state2 = flask.request.args.get("state")
code = flask.request.args.get("code")
if state1 is None or state2 is None or code is None or state1 != state2:
flask.flash("Couldn't authorize access.")
return flask.redirect(flask.url_for(".optout_error",
username=username))

# Get an access token.
params = dict(
client_id=flask.current_app.config["GITHUB_ID"],
client_secret=flask.current_app.config["GITHUB_SECRET"],
code=code,
)
r = requests.post("https://github.com/login/oauth/access_token",
data=params, headers={"Accept": "application/json"})
if r.status_code != requests.codes.ok:
flask.flash("Couldn't acquire an access token from GitHub.")
return flask.redirect(flask.url_for(".optout_error",
username=username))
data = r.json()
access = data.get("access_token", None)
if access is None:
flask.flash("No access token returned.")
return flask.redirect(flask.url_for(".optout_error",
username=username))

# Check the username.
r = requests.get("https://api.github.com/user",
params={"access_token": access})
if r.status_code != requests.codes.ok:
flask.flash("Couldn't get user information.")
return flask.redirect(flask.url_for(".optout_error",
username=username))
data = r.json()
login = data.get("login", None)
if login is None or login.lower() != username.lower():
flask.flash("You have to log in as '{0}' in order to opt-out."
.format(username))
return flask.redirect(flask.url_for(".optout_error",
username=username))

# Save the opt-out to the database.
user = User.query.filter(
func.lower(User.login) == func.lower(username)).first()
if user is None:
flask.flash("Unknown user: '{0}'.".format(username))
return flask.redirect(flask.url_for(".optout_error",
username=username))
user.active = False
db.session.add(user)
db.session.commit()

return flask.redirect(flask.url_for(".optout_success", username=username))


@frontend.route("/optout/<username>/error")
def optout_error(username):
return flask.render_template("optout-error.html", username=username)


@frontend.route("/optout/<username>/success")
def optout_success(username):
return flask.render_template("optout-success.html")
10 changes: 5 additions & 5 deletions osrc/static/d3.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions osrc/static/jquery.min.js

Large diffs are not rendered by default.

Binary file added osrc/static/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 70 additions & 14 deletions osrc/static/osrc.css
Expand Up @@ -5,7 +5,6 @@ html {

body {
font-family: Georgia, "Times New Roman", Times, serif;
/* font-family: "Helvetica Neue", helvetica, sans-serif; */
font-size: 16px;
line-height: 27px;
background: rgb(236, 236, 234);
Expand All @@ -27,6 +26,30 @@ a {
margin: 50px 0 30px;
}

.footer {
margin-top: 100px;
padding: 30px 0;
border-top: 1px solid #ddd;
width: 100%;
text-align: center;

font-size: 12px;
line-height: 18px;
color: #999;
}

#logo {
display: block;
position: fixed;
top: 10px;
left: 10px;
width: 50px;
height: 50px;
background-image: url('/static/logo.png?v=2');
background-size: 50px 50px;
background-repeat: no-repeat;
}

.avatar {
margin: 0 0 10px;
}
Expand All @@ -49,23 +72,56 @@ a {
overflow: hidden;
}

.titles ul {
h2 {
color: #928452;
text-align: center;
text-transform: uppercase;
font-family: Arial, Helvetica, sans-serif;
font-size: 1.1em;
font-weight: 500;
letter-spacing: .1em;
line-height: 150%;
padding-top: 30px;
}

p {
text-align:justify;
}

#friends {
text-align: center;
padding: 10px 0;
height: 50px;
}

.friend {
padding: 0 5px;
width: 50px;
height: 50px;
border-radius: 4px;
}

/* plotting */
.hist-block {
text-align: center;
}

.hist {
display: inline-block;
list-style: none;
margin: 0;
padding: 0;
line-height: 22px;
font-size: 16px;
margin: 10px 20px 0;
}

.left-titles {
text-align: right;
rect {
fill-opacity: .8;
}

.right-titles {
text-align: left;
color: #cb4b16;
/* font-weight: bold; */
/* color: #859900; */
text {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 11px;
color: #222;
}

line.axis {
stroke: #222;
stroke-width: 1px;
}
4 changes: 2 additions & 2 deletions osrc/static/plot.js
Expand Up @@ -133,7 +133,7 @@
.innerRadius(0),
plo = d3.layout.pie()
.sort(null)
.value(function(d) { return d; });
.value(function(d) { return d.count; });

sel.enter().append("svg")

Expand Down Expand Up @@ -164,7 +164,7 @@
})
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) { return d.data; })
.text(function(d, i) { return d.data.count; })
.style("stroke", function(d, i) { return cb(i); });

sel.exit().remove();
Expand Down

0 comments on commit b56a716

Please sign in to comment.