# Path: threebot/utils.py
# @login_required
# def get_my_orgs(request, an_user=None):
#     if request is not None and an_user is None:
#         an_user = request.user
# 
#     default_org, created = Organization.objects.get_or_create(slug='3bot', name="3bot")
# 
#     if not OrganizationUser.objects.filter(organization=default_org, user=an_user).exists():
#         # we create an OrganizationUser, so each user is member of our default_org
#         # if default_org was just created, we also mark this user as admin and create an OrganizationOwner
#         org_user = OrganizationUser(organization=default_org, user=an_user, is_admin=created)
#         org_user.save()
#         if created:
#             org_owner = OrganizationOwner(organization=default_org, organization_user=org_user)
#             org_owner.save()
# 
#     orgs = Organization.objects.filter(users=an_user)
# 
#     return orgs
# 
# def filter_workflow_log_history(workflow=None, teams=None, exit_code=None, user=None, worker=None, quantity=None):
#     """returns a queryset of workflow-logs filtered by given parameters"""
#     _filter = {}
#     if workflow:
#         _filter['workflow'] = workflow
#     if teams:
#         _filter['workflow__owner__in'] = teams
#     if user:
#         _filter['performed_by'] = user
#     if worker:
#         _filter['performed_on'] = worker
# 
#     logs = WorkflowLog.objects.filter(**_filter).select_related('workflow', 'performed_by', 'performed_on')[:quantity]
#     return logs


from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import logout
from django.conf import settings
from organizations.models import Organization
from threebot.utils import get_my_orgs, filter_workflow_log_history
def chooseorg(request, template='threebot/chooseorg.html'):
    orgs = Organization.objects.filter(users=request.user)

    return render(request, template, {'orgs': orgs})


def user_login(request, template='threebot/login.html'):
    next = request.GET.get("next", getattr(settings, "LOGIN_REDIRECT_URL", "/"))
    auth_form = AuthenticationForm(None, request.POST or None)

    if request.user.is_authenticated():
        # no need to login again, just redirect
        return redirect(next)

    if auth_form.is_valid():
        # The form itself handles authentication and checking to make sure passowrd and such are supplied.
        next = request.POST.get("next", getattr(settings, "LOGIN_REDIRECT_URL", "/"))
        login(request, auth_form.get_user())
        return HttpResponseRedirect(next)

    return render(request, template, {'auth_form': auth_form})


def user_logout(request):
    logout(request)
    return redirect("/")


@login_required
def index(request, template='threebot/index.html'):

# Path: threebot/utils.py
# @login_required
# def get_my_orgs(request, an_user=None):
#     if request is not None and an_user is None:
#         an_user = request.user
# 
#     default_org, created = Organization.objects.get_or_create(slug='3bot', name="3bot")
# 
#     if not OrganizationUser.objects.filter(organization=default_org, user=an_user).exists():
#         # we create an OrganizationUser, so each user is member of our default_org
#         # if default_org was just created, we also mark this user as admin and create an OrganizationOwner
#         org_user = OrganizationUser(organization=default_org, user=an_user, is_admin=created)
#         org_user.save()
#         if created:
#             org_owner = OrganizationOwner(organization=default_org, organization_user=org_user)
#             org_owner.save()
# 
#     orgs = Organization.objects.filter(users=an_user)
# 
#     return orgs
# 
# def filter_workflow_log_history(workflow=None, teams=None, exit_code=None, user=None, worker=None, quantity=None):
#     """returns a queryset of workflow-logs filtered by given parameters"""
#     _filter = {}
#     if workflow:
#         _filter['workflow'] = workflow
#     if teams:
#         _filter['workflow__owner__in'] = teams
#     if user:
#         _filter['performed_by'] = user
#     if worker:
#         _filter['performed_on'] = worker
# 
#     logs = WorkflowLog.objects.filter(**_filter).select_related('workflow', 'performed_by', 'performed_on')[:quantity]
#     return logs


# Path: threebot/views/base.py
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import logout
from django.conf import settings
from organizations.models import Organization
from threebot.utils import get_my_orgs, filter_workflow_log_history

def chooseorg(request, template='threebot/chooseorg.html'):
    orgs = Organization.objects.filter(users=request.user)

    return render(request, template, {'orgs': orgs})


def user_login(request, template='threebot/login.html'):
    next = request.GET.get("next", getattr(settings, "LOGIN_REDIRECT_URL", "/"))
    auth_form = AuthenticationForm(None, request.POST or None)

    if request.user.is_authenticated():
        # no need to login again, just redirect
        return redirect(next)

    if auth_form.is_valid():
        # The form itself handles authentication and checking to make sure passowrd and such are supplied.
        next = request.POST.get("next", getattr(settings, "LOGIN_REDIRECT_URL", "/"))
        login(request, auth_form.get_user())
        return HttpResponseRedirect(next)

    return render(request, template, {'auth_form': auth_form})


def user_logout(request):
    logout(request)
    return redirect("/")


@login_required
def index(request, template='threebot/index.html'):

    orgs = get_my_orgs(request)