import uuid
from brewman.models.auth import User

def groupfinder(user_id, request):
    if request is not None and 'perms' in request.session:
        perms = request.session['perms']
    else:
        if type(user_id) == str:
            user_id = uuid.UUID(user_id)
        perms = []
        user = User.by_id(user_id)
        for item in user.groups:
            for perm in item.roles:
                perms.append(perm.name)
        perms = f7(perms)
        if request is not None:
            request.session['perms'] = perms
    return perms


def f7(seq):
    seen = set()
    seen_add = seen.add
    return [x for x in seq if x not in seen and not seen_add(x)]