from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect, render
from django.urls import reverse
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView, UpdateView
from dfirtrack_main.forms import DivisionForm
from dfirtrack_main.logger.default_logger import debug_logger
from dfirtrack_main.models import Division

class DivisionList(LoginRequiredMixin, ListView):
    login_url = '/login'
    model = Division
    template_name = 'dfirtrack_main/division/division_list.html'
    context_object_name = 'division_list'

    def get_queryset(self):
        debug_logger(str(self.request.user), " DIVISION_LIST_ENTERED")
        return Division.objects.order_by('division_name')

class DivisionDetail(LoginRequiredMixin, DetailView):
    login_url = '/login'
    model = Division
    template_name = 'dfirtrack_main/division/division_detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        division = self.object
        division.logger(str(self.request.user), " DIVISION_DETAIL_ENTERED")
        return context

class DivisionCreate(LoginRequiredMixin, CreateView):
    login_url = '/login'
    model = Division
    form_class = DivisionForm
    template_name = 'dfirtrack_main/division/division_add.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        debug_logger(str(request.user), " DIVISION_ADD_ENTERED")
        return render(request, self.template_name, {'form': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            division = form.save(commit=False)
            division.save()
            division.logger(str(request.user), " DIVISION_ADD_EXECUTED")
            messages.success(request, 'Division added')
            return redirect(reverse('division_detail', args=(division.division_id,)))
        else:
            return render(request, self.template_name, {'form': form})

class DivisionUpdate(LoginRequiredMixin, UpdateView):
    login_url = '/login'
    model = Division
    form_class = DivisionForm
    template_name = 'dfirtrack_main/division/division_edit.html'

    def get(self, request, *args, **kwargs):
        division = self.get_object()
        form = self.form_class(instance=division)
        division.logger(str(request.user), " DIVISION_EDIT_ENTERED")
        return render(request, self.template_name, {'form': form})

    def post(self, request, *args, **kwargs):
        division = self.get_object()
        form = self.form_class(request.POST, instance=division)
        if form.is_valid():
            division = form.save(commit=False)
            division.save()
            division.logger(str(request.user), " DIVISION_EDIT_EXECUTED")
            messages.success(request, 'Division edited')
            return redirect(reverse('division_detail', args=(division.division_id,)))
        else:
            return render(request, self.template_name, {'form': form})

[((757, 799), 'dfirtrack_main.models.Division.objects.order_by', 'Division.objects.order_by', (['"""division_name"""'], {}), "('division_name')\n", (782, 799), False, 'from dfirtrack_main.models import Division\n'), ((1548, 1599), 'django.shortcuts.render', 'render', (['request', 'self.template_name', "{'form': form}"], {}), "(request, self.template_name, {'form': form})\n", (1554, 1599), False, 'from django.shortcuts import redirect, render\n'), ((2508, 2559), 'django.shortcuts.render', 'render', (['request', 'self.template_name', "{'form': form}"], {}), "(request, self.template_name, {'form': form})\n", (2514, 2559), False, 'from django.shortcuts import redirect, render\n'), ((1880, 1923), 'django.contrib.messages.success', 'messages.success', (['request', '"""Division added"""'], {}), "(request, 'Division added')\n", (1896, 1923), False, 'from django.contrib import messages\n'), ((2043, 2094), 'django.shortcuts.render', 'render', (['request', 'self.template_name', "{'form': form}"], {}), "(request, self.template_name, {'form': form})\n", (2049, 2094), False, 'from django.shortcuts import redirect, render\n'), ((2897, 2941), 'django.contrib.messages.success', 'messages.success', (['request', '"""Division edited"""'], {}), "(request, 'Division edited')\n", (2913, 2941), False, 'from django.contrib import messages\n'), ((3061, 3112), 'django.shortcuts.render', 'render', (['request', 'self.template_name', "{'form': form}"], {}), "(request, self.template_name, {'form': form})\n", (3067, 3112), False, 'from django.shortcuts import redirect, render\n'), ((1952, 2008), 'django.urls.reverse', 'reverse', (['"""division_detail"""'], {'args': '(division.division_id,)'}), "('division_detail', args=(division.division_id,))\n", (1959, 2008), False, 'from django.urls import reverse\n'), ((2970, 3026), 'django.urls.reverse', 'reverse', (['"""division_detail"""'], {'args': '(division.division_id,)'}), "('division_detail', args=(division.division_id,))\n", (2977, 3026), False, 'from django.urls import reverse\n')]