Skip to content

Commit

Permalink
Merge branch 'master' into docker
Browse files Browse the repository at this point in the history
  • Loading branch information
thebrianzeng committed Apr 17, 2015
2 parents 49e72e8 + d1e8bd4 commit 85f2224
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 118 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ Data for density is provided by CUIT in coordination with ESC.



## Style guide

Make sure to conform to [AirBnb's brilliant style guide](https://github.com/airbnb/javascript) when writing javascript.



# app structure
Expand Down
4 changes: 2 additions & 2 deletions density/config/flask_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from sys import exit
from datetime import datetime

from consul import Consul
from flask.json import JSONEncoder

# dictionary the flask app configures itself from
config = {
Expand Down Expand Up @@ -53,7 +55,6 @@
exit(1)

else: # prod w/ consul
from consul import Consul
kv = Consul().kv # initalize client to KV store

for consul_key, config_key in consul_configurations:
Expand Down Expand Up @@ -87,7 +88,6 @@

""" Creates a json encoder that returns ISO 8601 strings for datetimes
http://flask.pocoo.org/snippets/119/ """
from flask.json import JSONEncoder


class ISO8601Encoder(JSONEncoder):
Expand Down
24 changes: 11 additions & 13 deletions density/density.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
from flask import Flask, g, jsonify, render_template, json, request
from flask_mail import Message, Mail

from config import flask_config
app = Flask(__name__)
app.config.update(**flask_config.config)

# do import early to check that all env variables are present
if not app.debug:
mail = Mail(app)

# change the default JSON encoder to handle datetime's properly
app.json_encoder = flask_config.ISO8601Encoder

# library imports
import psycopg2
Expand All @@ -25,6 +15,14 @@
import re
from functools import wraps

app = Flask(__name__)
app.config.update(**flask_config.config)
if not app.debug:
mail = Mail(app)

# change the default JSON encoder to handle datetime's properly
app.json_encoder = flask_config.ISO8601Encoder

with open('data/capacity_group.json') as json_data:
FULL_CAP_DATA = json.load(json_data)['data']

Expand Down Expand Up @@ -212,9 +210,9 @@ def auth():
if not regex:
return render_template('auth.html',
success=False,
reason=("You need to log in with your "
"Columbia or Barnard email! You "
"logged in with: " + email))
reason="Please log in with your " +
"Columbia or Barnard email. You logged " +
"in with: " + email)

# Get UNI and ask database for code.
uni = regex.group('uni')
Expand Down
102 changes: 102 additions & 0 deletions density/static/js/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// vanilla js document.ready bc we are kool kids
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('map').addEventListener('load', function() {

//Iterate through locations and display their data
var data = document.getElementById('data');
var locations = JSON.parse(data.dataset.locations);
var map = document.getElementById('map');
var innerSvg = map.contentDocument;
var popup = document.getElementById('locationPopup');

var initialOffsetTop = map.getBoundingClientRect().top + window.scrollY;
var initialOffsetLeft = map.getBoundingClientRect().left + window.scrollX;

//Create a mapping from parent_id --> text representation
var buildingStrings = {};

//Create a mapping from parent_id --> list full %s for elements inside (e.g. floors of a building)
var buildingFloors = {};

//Create a mapping from parent_id --> query (http://....hours)
var buildingHours = {};

locations.forEach(function(location) {

// We're skipping this out of non-use
if (location.name === 'Butler Library stk') {
return;
}
if (buildingFloors[location.parentId] == null) {
buildingFloors[location.parentId] = {};
}
if (buildingStrings[location.parentId] == null) {
buildingStrings[location.parentId] = '<b>' + location.parentName + '</b><br />' +
'<em>Click to view hours</em><br /><br />';
}
if (buildingHours[location.parentId] == null) {
buildingHours[location.parentId] = 'https://www.google.com/#q=' + escape('Columbia University ' + location.parentName + ' hours');
}

buildingFloors[location.parentId][location.name] = location.fullness;
buildingStrings[location.parentId] += location.name + ': ' + location.fullness + '%' + '<br />';
});

//Iterate through mappings keys to set opacity for the element by fullness and better data on mouseover
var buildings = Object.keys(buildingFloors);

buildings.forEach(function(building) {
var list = buildingFloors[building];

//find average fullness for location (this is for locations w/ multiple floors)
var totalFullness = 0;
for (var entry in list) {
totalFullness += buildingFloors[building][entry];
}

//number of pixels offset to separate popup from mouse
var mouseOffset = 4;
var numFloorsInBuilding = Object.keys(list).length;

var percent = (totalFullness / numFloorsInBuilding); //Should be from [0, 100]
var buildingElement = innerSvg.getElementById('parent_' + building); //e.g. parent_43
if(buildingElement != null) {
buildingElement.style.opacity = percent / 100;
}

//opens new window with google search for hours
buildingElement.onmousedown = function(event) {
var location = this.id.replace('parent_', '');
var query = buildingHours[location];
window.open(query);
}

//displays popup at location of mouse
buildingElement.onmouseenter = function(event) {
popup.style.visibility = 'visible';
var leftPosition = (initialOffsetLeft + event.clientX -
(popup.offsetWidth/2)).toString() + 'px'; // divide by 2 centers it (half of width)
var topPosition = (initialOffsetTop - mouseOffset + event.clientY -
(popup.offsetHeight)).toString() + 'px';
popup.style.top = topPosition;
popup.style.left = leftPosition;
var location = this.id.replace('parent_', '');
popup.innerHTML = buildingStrings[location];
}

buildingElement.onmouseout = function() {
popup.style.visibility = 'hidden';
}

//moves popup with mouse
buildingElement.onmousemove = function(event) {
var leftPosition = (initialOffsetLeft + event.clientX -
(popup.offsetWidth/2)).toString() + 'px'; // divide by 2 centers it (half of width)
var topPosition = (initialOffsetTop - mouseOffset + event.clientY -
(popup.offsetHeight)).toString() + 'px';
popup.style.top = topPosition;
popup.style.left = leftPosition;
}
});
});
});
4 changes: 2 additions & 2 deletions density/templates/auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ <h1> Welcome {{ uni }}! </h1>
<h5> Your code is: {{ code }} </h5>
{% else %}
{% if reason %}
<h1 style="color:red"> {{ reason }} </h1>
<h3> {{ reason }} </h3>
{% endif %}

<h3>Getting access to the API</h3>
<h5>To use the API, you will need a Columbia or Barnard e-mail address.</h5>
<h5>To use the API, you will need a Columbia or Barnard email address.</h5>
<button id='signinButton' class="btn btn-primary blue-btn" onclick='login()' style="margin-bottom: 1em;">
Get access
</button>
Expand Down
2 changes: 1 addition & 1 deletion density/templates/docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ <h3>Errors</h3>
<li>"Invalid authentication token."

<ul>
<li>An expired or improper authentication token was used with your request. Ensure you're using the most recent token generated with your e-mail.</li>
<li>An expired or improper authentication token was used with your request. Ensure you're using the most recent token generated with your email.</li>
</ul>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion density/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<!--Javascript Files-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="{{ url_for('static', filename='js/expand.js') }}"
<script src="{{ url_for('static', filename='js/expand.js') }}"></script>
{% block scripts %}{% endblock %}
</div>
</div>
Expand Down
106 changes: 7 additions & 99 deletions density/templates/map.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{% extends "layout.html" %}
{% block body %}
<article id="data" data-locations='{{locations|tojson}}'></article>

<div class="logoContainer">
<div class="container">
<div class="text-center">
Expand All @@ -24,105 +26,6 @@
</div>

<!-- Script for populating map colors -->
<script type="text/javascript">
document.getElementById('map').addEventListener("load", function(){
//Iterate through locations and display their data
var locations = {{ locations|tojson }};
var map = document.getElementById('map');
var innerSvg = map.contentDocument;
var defs = document.getElementById('gradientFills');
var popup = document.getElementById('locationPopup');

var initialOffsetTop = map.getBoundingClientRect().top + window.scrollY;
var initialOffsetLeft = map.getBoundingClientRect().left + window.scrollX;

//Create a mapping from parent_id --> text representation
var parentToText = new Object();
//Create a mapping from parent_id --> list full %s for elements inside (e.g. floors of a building)
var parentToList = new Object();
//Create a mapping from parent_id --> query (http://....hours)
var parentToQuery = new Object();

for(var i = 0; i < locations.length; i++){
if(locations[i].name != "Butler Library stk"){ //We're skipping this out of non-use]
if(parentToList[locations[i].parentId] == null){
parentToList[locations[i].parentId] = new Object();
}
if(parentToText[locations[i].parentId] == null){
parentToText[locations[i].parentId] = "<b>" + locations[i].parentName + "</b><br />" +
"<em>Click to view hours</em><br /><br />";
}
if(parentToQuery[locations[i].parentId] == null){
parentToQuery[locations[i].parentId] = "https://www.google.com/#q=" + escape("Columbia University " + locations[i].parentName + " hours");
}

parentToList[locations[i].parentId][locations[i].name] = locations[i].fullness;
parentToText[locations[i].parentId] += locations[i].name + ": " + locations[i].fullness + "%" + "<br />";
}
}

//Iterate through mappings keys to set opacity for the element by fullness and better data on mouseover
var keySet = Object.keys(parentToList);
for(var key in keySet){
var keyValue = keySet[key];
var list = parentToList[keyValue];

//find average fullness for location (this is for locations w/ multiple floors)
var count = 0;
var totalFullness = 0;
for(var entry in list){
totalFullness += parentToList[keyValue][entry];
count++;
}

//number of pixels offset to separate popup from mouse
var mouseOffset = 4;

if(count != 0){
var percent = (totalFullness / count); //Should be from [0, 100]
var buildingElement = innerSvg.getElementById('parent_' + keyValue); //e.g. parent_43
if(buildingElement != null){
buildingElement.style.opacity = percent / 100;
}

//opens new window with google search for hours
buildingElement.onmousedown = function(event){
var location = this.id.replace("parent_", "");
var query = parentToQuery[location];
window.open(query);
}

//displays popup at location of mouse
buildingElement.onmouseenter = function(event){
popup.style.visibility = 'visible';
var leftPosition = (initialOffsetLeft + event.clientX -
(popup.offsetWidth/2)).toString() + "px";
var topPosition = (initialOffsetTop - mouseOffset + event.clientY -
(popup.offsetHeight)).toString() + "px";
popup.style.top = topPosition;
popup.style.left = leftPosition;
var location = this.id.replace("parent_", "");
popup.innerHTML = parentToText[location];
}

buildingElement.onmouseout = function(){
popup.style.visibility = 'hidden';
}

//moves popup with mouse
buildingElement.onmousemove = function(event){
var leftPosition = (initialOffsetLeft + event.clientX -
(popup.offsetWidth/2)).toString() + "px";
var topPosition = (initialOffsetTop - mouseOffset + event.clientY -
(popup.offsetHeight)).toString() + "px";
popup.style.top = topPosition;
popup.style.left = leftPosition;
}
}
}
});
</script>


<div class="thanksContainer">
<div class="container">
Expand All @@ -131,3 +34,8 @@ <h4>Special thanks to <img class="logo escLogo" src="{{ url_for('static', filena
</div>
</div>
{% endblock %}

{% block scripts %}
<script src="{{ url_for('static', filename='js/map.js') }}"></script>
{% endblock %}

0 comments on commit 85f2224

Please sign in to comment.