Skip to content

Commit

Permalink
Merge pull request #506 from Turbo87/cleanup
Browse files Browse the repository at this point in the history
Simplfiy and cleanup backend code
  • Loading branch information
Turbo87 authored Oct 25, 2016
2 parents 83dc750 + 8925de3 commit 965a4be
Show file tree
Hide file tree
Showing 42 changed files with 388 additions and 433 deletions.
3 changes: 2 additions & 1 deletion ember/.ember-cli
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
Setting `disableAnalytics` to true will prevent any data from being sent.
*/
"disableAnalytics": false,
"outputPath": "../skylines/frontend/static"
"outputPath": "../skylines/frontend/static",
"proxy": "http://localhost:5000/"
}
4 changes: 2 additions & 2 deletions ember/app/authenticators/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default Base.extend({
ajax: Ember.inject.service(),

authenticate(email, password) {
return this.get('ajax').request('/session', { method: 'PUT', json: { email, password } })
return this.get('ajax').request('/api/session', { method: 'PUT', json: { email, password } })
.then(() => this.get('ajax').request('/api/settings/'))
.then(settings => ({ settings }));
},
Expand All @@ -16,6 +16,6 @@ export default Base.extend({
},

invalidate(/* data */) {
return this.get('ajax').request('/session', { method: 'DELETE' });
return this.get('ajax').request('/api/session', { method: 'DELETE' });
},
});
3 changes: 3 additions & 0 deletions ember/app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ export default Ember.Route.extend(ApplicationRouteMixin, {

sessionAuthenticated() {
const attemptedTransition = this.get('session.attemptedTransition');
const inLoginRoute = this.controllerFor('application').get('inLoginRoute');

if (attemptedTransition) {
attemptedTransition.retry();
this.set('session.attemptedTransition', null);
} else if (inLoginRoute) {
this.transitionTo('index');
}
},
});
12 changes: 11 additions & 1 deletion ember/app/routes/login.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import Ember from 'ember';
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin';

export default Ember.Route.extend(UnauthenticatedRouteMixin);
export default Ember.Route.extend(UnauthenticatedRouteMixin, {
setupController() {
this._super(...arguments);
this.controllerFor('application').set('inLoginRoute', true);
},

resetController() {
this._super(...arguments);
this.controllerFor('application').set('inLoginRoute', false);
},
});
2 changes: 1 addition & 1 deletion ember/app/utils/map-click-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ const MapClickHandler = Ember.Object.extend({
* @param {Number} lat Latitude.
*/
getLocationInfo(lon, lat) {
let req = $.ajax(`/api/v0/mapitems?lon=${lon}&lat=${lat}`);
let req = $.ajax(`/api/mapitems?lon=${lon}&lat=${lat}`);
req.done(data => this.showLocationData(data));
req.fail(() => this.showLocationData(null));
},
Expand Down
1 change: 0 additions & 1 deletion ember/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"Flot": "flot#0.8.3",
"flot-marks": "https://github.com/TobiasLohner/flot-marks.git#f09ded70f5a229a38ba0b9cfa92dbb448ca4daaf",
"jquery": "1.10.2",
"jQuery-ajaxTransport-XDomainRequest": "jquery-ajaxtransport-xdomainrequest#1.0.3",
"sidebar-v2": "0.2.1",
"eonasdan-bootstrap-datetimepicker": "https://github.com/TobiasLohner/bootstrap-datetimepicker.git#c36342415a1be8fa013548402bf01718ca93d454",
"BigScreen": "bigscreen#2.0.4"
Expand Down
2 changes: 0 additions & 2 deletions ember/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ module.exports = function(defaults) {
// please specify an object with the list of modules as keys
// along with the exports of each module as its value.

app.import('bower_components/jQuery-ajaxTransport-XDomainRequest/jquery.xdomainrequest.min.js');

app.import('vendor/openlayers/ol3cesium.js');
app.import('vendor/openlayers/ol.css');

Expand Down
2 changes: 0 additions & 2 deletions skylines/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def register(app):
from .airports import airports_blueprint
from .airspace import airspace_blueprint
from .clubs import clubs_blueprint
from .mapitems import mapitems_blueprint
from .search import search_blueprint
from .users import users
from .user import user
Expand All @@ -42,7 +41,6 @@ def require_user_agent():
app.register_blueprint(airports_blueprint)
app.register_blueprint(airspace_blueprint)
app.register_blueprint(clubs_blueprint)
app.register_blueprint(mapitems_blueprint)
app.register_blueprint(search_blueprint)
app.register_blueprint(user)
app.register_blueprint(users)
Expand Down
5 changes: 2 additions & 3 deletions skylines/api/views/airports.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
}


@airports_blueprint.route('/airports/')
@airports_blueprint.route('/airports', endpoint='list')
@airports_blueprint.route('/airports', strict_slashes=False)
@use_args(bbox_args)
def _list(args):
def list(args):
airports = api.get_airports_by_bbox(args['bbox'])
return jsonify(airports)

Expand Down
5 changes: 2 additions & 3 deletions skylines/api/views/airspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
airspace_blueprint = Blueprint('airspace', 'skylines')


@airspace_blueprint.route('/airspace/')
@airspace_blueprint.route('/airspace', endpoint='list')
def _list():
@airspace_blueprint.route('/airspace', strict_slashes=False)
def list():
location = parse_location(request.args)
return jsonify(api.get_airspaces_by_location(location))
3 changes: 1 addition & 2 deletions skylines/api/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
user = Blueprint('user', 'skylines')


@user.route('/user/')
@user.route('/user')
@user.route('/user', strict_slashes=False)
@oauth.required()
def read():
user = User.get(request.user_id)
Expand Down
5 changes: 2 additions & 3 deletions skylines/api/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
users = Blueprint('users', 'skylines')


@users.route('/users/')
@users.route('/users', endpoint='list')
@users.route('/users', strict_slashes=False)
@use_args(pagination_args)
def _list(args):
def list(args):
offset = (args['page'] - 1) * args['per_page']
limit = args['per_page']

Expand Down
5 changes: 2 additions & 3 deletions skylines/api/views/waves.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
waves_blueprint = Blueprint('waves', 'skylines')


@waves_blueprint.route('/mountain_wave_project/')
@waves_blueprint.route('/mountain_wave_project', endpoint='list')
def _list():
@waves_blueprint.route('/mountain_wave_project', strict_slashes=False)
def list():
location = parse_location(request.args)
return jsonify(api.get_waves_by_location(location))
27 changes: 11 additions & 16 deletions skylines/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import config

from flask import Flask
from raven.contrib.flask import Sentry

from skylines.api.middleware import HTTPMethodOverrideMiddleware

Expand All @@ -23,20 +22,18 @@ def __init__(self, name='skylines', config_file=None, *args, **kw):

def add_sqlalchemy(self):
""" Create and configure SQLAlchemy extension """
from skylines.database import db, migrate
from skylines.database import db
db.init_app(self)
migrate.init_app(self, db)

def add_cache(self):
""" Create and attach Cache extension """
from flask.ext.cache import Cache
self.cache = Cache(self, with_jinja2_ext=False)
from skylines.frontend.cache import cache
cache.init_app(self)

def add_login_manager(self):
""" Create and attach Login extension """
from flask.ext.login import LoginManager
self.login_manager = LoginManager()
self.login_manager.init_app(self)
from skylines.frontend.login import login_manager
login_manager.init_app(self)

def add_logging_handlers(self):
if self.debug: return
Expand All @@ -61,33 +58,29 @@ def add_logging_handlers(self):
self.logger.addHandler(file_handler)

def add_sentry(self):
sentry_dsn = self.config.get('SENTRY_DSN')
if sentry_dsn:
Sentry(self, dsn=sentry_dsn)
from skylines.sentry import sentry
sentry.init_app(self)

def add_celery(self):
from skylines.worker.celery import celery
celery.init_app(self)
return celery

def initialize_lib(self):
def load_egm96(self):
from skylines.lib.geoid import load_geoid
load_geoid(self)


def create_app(*args, **kw):
app = SkyLines(*args, **kw)
app.add_sqlalchemy()
app.add_cache()
app.initialize_lib()
app.add_sentry()
return app


def create_http_app(*args, **kw):
app = create_app(*args, **kw)

app.add_logging_handlers()
app.add_sentry()
app.add_celery()

return app
Expand All @@ -96,6 +89,8 @@ def create_http_app(*args, **kw):
def create_frontend_app(*args, **kw):
app = create_http_app('skylines.frontend', *args, **kw)

app.add_cache()
app.load_egm96()
app.add_login_manager()

import skylines.frontend.views
Expand Down
7 changes: 5 additions & 2 deletions skylines/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys

from flask.ext.script import Manager
from flask.ext.migrate import MigrateCommand
from flask.ext.migrate import Migrate, MigrateCommand

from .shell import Shell
from .server import Server, APIServer
Expand All @@ -19,6 +19,7 @@
from .search import Search

from skylines.app import create_app
from skylines.database import db
from config import to_envvar


Expand All @@ -27,7 +28,9 @@ def _create_app(config):
print 'Config file "{}" not found.'.format(config)
sys.exit(1)

return create_app()
app = create_app()
app.migrate = Migrate(app, db)
return app


manager = Manager(_create_app)
Expand Down
2 changes: 0 additions & 2 deletions skylines/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.migrate import Migrate


def query(cls, **kw):
Expand All @@ -20,7 +19,6 @@ def exists(cls, **kw):


db = SQLAlchemy(session_options=dict(expire_on_commit=False))
migrate = Migrate()

db.Model.flask_query = db.Model.query
db.Model.query = classmethod(query)
Expand Down
3 changes: 3 additions & 0 deletions skylines/frontend/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flask.ext.cache import Cache

cache = Cache()
23 changes: 23 additions & 0 deletions skylines/frontend/login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import base64

from flask.ext.login import LoginManager

from skylines.model import User

login_manager = LoginManager()


@login_manager.user_loader
def load_user(user_id):
return User.get(user_id)


@login_manager.header_loader
def load_user_from_header(header_val):
try:
header_val = header_val.replace('Basic ', '', 1)
header_val = base64.b64decode(header_val)
email, password = header_val.split(':', 1)
return User.by_credentials(email, password)
except:
return None
39 changes: 21 additions & 18 deletions skylines/frontend/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .flight import flight_blueprint
from .flights import flights_blueprint
from .livetrack24 import lt24_blueprint
from .mapitems import mapitems_blueprint
from .notifications import notifications_blueprint
from .ranking import ranking_blueprint
from .search import search_blueprint
Expand All @@ -31,25 +32,27 @@ def register(app):
register_i18n(app)
register_login(app)

app.register_blueprint(about_blueprint)
app.register_blueprint(airport_blueprint)
app.register_blueprint(aircraft_models_blueprint)
app.register_blueprint(assets_blueprint)
app.register_blueprint(club_blueprint)
app.register_blueprint(clubs_blueprint)
app.register_blueprint(files_blueprint)
app.register_blueprint(flight_blueprint)
app.register_blueprint(flights_blueprint)
app.register_blueprint(lt24_blueprint)
app.register_blueprint(notifications_blueprint)
app.register_blueprint(ranking_blueprint)
app.register_blueprint(search_blueprint)
app.register_blueprint(settings_blueprint)
app.register_blueprint(statistics_blueprint)
app.register_blueprint(timeline_blueprint)
app.register_blueprint(track_blueprint)
app.register_blueprint(tracking_blueprint)
app.register_blueprint(upload_blueprint)
app.register_blueprint(user_blueprint)
app.register_blueprint(users_blueprint)
app.register_blueprint(widgets_blueprint)

app.register_blueprint(about_blueprint, url_prefix='/api')
app.register_blueprint(airport_blueprint, url_prefix='/api')
app.register_blueprint(aircraft_models_blueprint, url_prefix='/api')
app.register_blueprint(club_blueprint, url_prefix='/api')
app.register_blueprint(clubs_blueprint, url_prefix='/api')
app.register_blueprint(flight_blueprint, url_prefix='/api')
app.register_blueprint(flights_blueprint, url_prefix='/api')
app.register_blueprint(mapitems_blueprint, url_prefix='/api')
app.register_blueprint(notifications_blueprint, url_prefix='/api')
app.register_blueprint(ranking_blueprint, url_prefix='/api')
app.register_blueprint(search_blueprint, url_prefix='/api')
app.register_blueprint(settings_blueprint, url_prefix='/api')
app.register_blueprint(statistics_blueprint, url_prefix='/api')
app.register_blueprint(timeline_blueprint, url_prefix='/api')
app.register_blueprint(track_blueprint, url_prefix='/api')
app.register_blueprint(tracking_blueprint, url_prefix='/api')
app.register_blueprint(upload_blueprint, url_prefix='/api')
app.register_blueprint(user_blueprint, url_prefix='/api')
app.register_blueprint(users_blueprint, url_prefix='/api')
6 changes: 3 additions & 3 deletions skylines/frontend/views/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
about_blueprint = Blueprint('about', 'skylines')


@about_blueprint.route('/api/imprint')
@about_blueprint.route('/imprint')
def imprint():
content = current_app.config.get(
'SKYLINES_IMPRINT',
Expand All @@ -14,7 +14,7 @@ def imprint():
return jsonify(content=content)


@about_blueprint.route('/api/team')
@about_blueprint.route('/team')
def skylines_team():
path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'..', '..', '..', 'AUTHORS.md')
Expand All @@ -24,7 +24,7 @@ def skylines_team():
return jsonify(content=content)


@about_blueprint.route('/api/license')
@about_blueprint.route('/license')
def license():
path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'..', '..', '..', 'LICENSE')
Expand Down
2 changes: 1 addition & 1 deletion skylines/frontend/views/aircraft_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
aircraft_models_blueprint = Blueprint('aircraft_models', 'skylines')


@aircraft_models_blueprint.route('/api/aircraft-models', strict_slashes=False)
@aircraft_models_blueprint.route('/aircraft-models', strict_slashes=False)
def index():
models = AircraftModel.query() \
.order_by(AircraftModel.kind) \
Expand Down
Loading

0 comments on commit 965a4be

Please sign in to comment.