from core.views import SimpleCreateViewBase, SimpleUpdateViewBase, DetailedModelViewBase, ModelListViewBase
from .forms import *
from .access_checks import LoginRequiredMixin
from django.core.urlresolvers import reverse_lazy
from rest.serializers import WinoseUsersSerializer
from django.shortcuts import HttpResponseRedirect
from users.models import LAB_GROUP_ID, HEAD_OF_LAB_ID


class WinoseUsersListView(LoginRequiredMixin, ModelListViewBase):
    require_group = LAB_GROUP_ID
    serializer = WinoseUsersSerializer

    def get_context_data(self, **kwargs):
        ctx = super(WinoseUsersListView, self).get_context_data(**kwargs)
        ctx['edit_user_hint'] = True  # do not allow to deactived users
        return ctx

class WinoseUserDetailView(LoginRequiredMixin, DetailedModelViewBase):
    model = WinoseUser
    skip_fields = ['password']  # don't display pw hash in detail view, pointless
    require_group = LAB_GROUP_ID

    def get_context_data(self, **kwargs):
        ctx = super(WinoseUserDetailView, self).get_context_data(**kwargs)
        ctx['edit_user_hint'] = True
        return ctx


class WinoseUserUpdateView(LoginRequiredMixin, SimpleUpdateViewBase):
    model = WinoseUser
    require_group = LAB_GROUP_ID

    def get_form_kwargs(self):
        kw = super(WinoseUserUpdateView, self).get_form_kwargs()
        kw.update({'current_user': self.request.user})
        return kw

    def dispatch(self, *args, **kwargs):

        obj = self.get_object()
        is_self = obj.pk == self.request.user.pk
        # redirect if:
        # the user about to be edited is equal/higher on the access ladder and we're not
        # a sa or we're not editing our own profile
        if (obj.is_superuser or obj.user_group >= self.request.user.user_group) and \
                (not self.request.user.is_superuser and not is_self):
            return HttpResponseRedirect(redirect_to=reverse_lazy(WinoseUser.overview_url))
        return super(WinoseUserUpdateView, self).dispatch(*args, **kwargs)

    def get_form_class(self):
        # if editing own profile, hide groups and sa flags
        if self.request.user.pk == self.object.pk:
            return WinoseUserUpdateFormSelf
        else:
            if self.request.user.is_superuser:
                return WinoseUserUpdateFormOtherSA
            else:
                return WinoseUserUpdateForm


# create is the somewhat easy case here
# depending on sa rights or not, the user gets different forms
# which differ in hiding or showing the sa field
# the form itself takes care of privilege escalations for group membership
class WinoseUserCreateView(LoginRequiredMixin, SimpleCreateViewBase):
    model = WinoseUser

    require_group = LAB_GROUP_ID

    def get_form_class(self):
        if self.request.user.is_superuser:
            return WinoseUserCreateFormSA
        else:
            return WinoseUserCreateFormBase

    # pass current user to check for privilege escalation attempts
    def get_form_kwargs(self):
        kw = super(WinoseUserCreateView, self).get_form_kwargs()
        kw.update({'current_user': self.request.user})
        return kw

