'Decorator to log user actions'

@classmethod
def log_this(cls, f):

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        start_dttm = datetime.now()
        user_id = None
        if g.user:
            user_id = g.user.get_id()
        d = request.args.to_dict()
        post_data = (request.form or {})
        d.update(post_data)
        d.update(kwargs)
        slice_id = d.get(u'slice_id', 0)
        try:
            slice_id = (int(slice_id) if slice_id else 0)
        except ValueError:
            slice_id = 0
        params = u''
        try:
            params = json.dumps(d)
        except:
            pass
        stats_logger.incr(f.__name__)
        value = f(*args, **kwargs)
        sesh = db.session()
        log = cls(action=f.__name__, json=params, dashboard_id=(d.get(u'dashboard_id') or None), slice_id=slice_id, duration_ms=((datetime.now() - start_dttm).total_seconds() * 1000), referrer=(request.referrer[:1000] if request.referrer else None), user_id=user_id)
        sesh.add(log)
        sesh.commit()
        return value
    return wrapper
