from flask import redirect, url_for, flash
from flask import current_user
import functools

# Use only with @login_required.
def privilege_required(priv):
	def privilege_decorator(f):
		@functools.wraps(f)
		def wrapper():
			if not current_user.priv(priv):
				flash(f'Cette page est protégée par le privilège <code>{priv}'+
					'</code>', 'error')
				return redirect(url_for('index'))
			else:
				f()
		return wrapper
	return privilege_decorator
