Skip to content

Commit

Permalink
Merge 9f633c4 into 8974788
Browse files Browse the repository at this point in the history
  • Loading branch information
teleyinex committed Dec 20, 2016
2 parents 8974788 + 9f633c4 commit 1c514b1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
7 changes: 5 additions & 2 deletions pybossa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import logging
import humanize
from werkzeug.exceptions import Forbidden
from flask import Flask, url_for, request, render_template, \
flash, _app_ctx_stack
from flask.ext.login import current_user
Expand All @@ -28,7 +29,7 @@
from pybossa.extensions import *
from pybossa.ratelimit import get_view_rate_limit
from raven.contrib.flask import Sentry
from pybossa.util import pretty_date
from pybossa.util import pretty_date, handle_content_type
from pybossa.news import FEED_KEY as NEWS_FEED_KEY
from pybossa.news import get_news

Expand Down Expand Up @@ -443,7 +444,9 @@ def _server_error(e): # pragma: no cover

@app.errorhandler(403)
def _forbidden(e):
return render_template('403.html'), 403
response = dict(template='403.html', code=403,
description=Forbidden.description)
return handle_content_type(response)

@app.errorhandler(401)
def _unauthorized(e):
Expand Down
22 changes: 22 additions & 0 deletions pybossa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,34 @@
import codecs
import cStringIO
from flask import abort, request, make_response, current_app
from flask import render_template, jsonify
from functools import wraps
from flask.ext.login import current_user
from math import ceil
import json


def handle_content_type(data):
"""Return HTML or JSON based on request type."""
print type(data)
if request.headers['Content-Type'] == 'application/json':
if 'form' in data.keys():
del data['form']
if 'code' in data.keys():
return jsonify(data), data['code']
else:
return jsonify(data)
else:
template = data['template']
del data['template']
if 'code' in data.keys():
error_code = data['code']
del data['code']
return render_template(template, **data), error_code
else:
return render_template(template, **data)


def jsonpify(f):
"""Wrap JSONified output for JSONP."""
@wraps(f)
Expand Down
12 changes: 7 additions & 5 deletions pybossa/view/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import pybossa.model as model
from flask.ext.babel import gettext
from pybossa.core import signer, uploader, sentinel, newsletter
from pybossa.util import Pagination
from pybossa.util import Pagination, handle_content_type
from pybossa.util import get_user_signup_method
from pybossa.cache import users as cached_users
from pybossa.auth import ensure_authorized_to
Expand Down Expand Up @@ -124,10 +124,12 @@ def signin():
auth['facebook'] = True
if ('google' in current_app.blueprints): # pragma: no cover
auth['google'] = True
return render_template('account/signin.html',
title="Sign in",
form=form, auth=auth,
next=request.args.get('next'))
response = dict(template='account/signin.html',
title="Sign in",
form=form,
auth=auth,
next=request.args.get('next'))
return handle_content_type(response)
else:
# User already signed in, so redirect to home page
return redirect(url_for("home.home"))
Expand Down
4 changes: 2 additions & 2 deletions pybossa/view/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from sqlalchemy.exc import ProgrammingError

from pybossa.model.category import Category
from pybossa.util import admin_required, UnicodeWriter
from pybossa.util import admin_required, UnicodeWriter, handle_content_type
from pybossa.cache import projects as cached_projects
from pybossa.cache import categories as cached_cat
from pybossa.auth import ensure_authorized_to
Expand Down Expand Up @@ -66,7 +66,7 @@ def index():
"""List admin actions."""
key = NOTIFY_ADMIN + str(current_user.id)
sentinel.master.delete(key)
return render_template('/admin/index.html')
return handle_content_type(dict(template='/admin/index.html'))


@blueprint.route('/featured')
Expand Down

0 comments on commit 1c514b1

Please sign in to comment.