decorators.py 1.24 KB
Newer Older
Cédric Bonhomme's avatar
Cédric Bonhomme committed
1 2 3 4 5
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# ***** BEGIN LICENSE BLOCK *****
# This file is part of EV3WebController.
6
# Copyright (c) 2014-2015 Cédric Bonhomme.
Cédric Bonhomme's avatar
Cédric Bonhomme committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
# All rights reserved.
#
#
#
# ***** END LICENSE BLOCK *****

import json
from functools import wraps
from threading import Thread
from flask import Response, request, session, jsonify, current_app
from web.lib.utils import default_handler

def async(f):
    """
    This decorator enables to send email in a new thread.
    This prevent the server to freeze.
    """
    def wrapper(*args, **kwargs):
        thr = Thread(target = f, args = args, kwargs = kwargs)
        thr.start()
    return wrapper


def to_response(func):
    """Will cast results of func as a result, and try to extract
    a status_code for the Response object"""
    def wrapper(*args, **kwargs):
        status_code = 200
        result = func(*args, **kwargs)
        if isinstance(result, Response):
            return result
        if isinstance(result, list) and len(result) == 1:
            result = result[0]
        elif isinstance(result, tuple):
            result, status_code = result
        return Response(json.dumps(result, default=default_handler),
                        status=status_code)
    return wrapper