from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.views import LoginView from django.shortcuts import get_object_or_404 from django.urls import reverse_lazy from django.views.generic import (CreateView, DeleteView, ListView, RedirectView, TemplateView, UpdateView) from django_scopes import scope, scopes_disabled from scopedsites.models import Domain from leftists.forms import LinkForm from leftists.models import ShortLink class ShortLinkRedirectView(RedirectView): def get_redirect_url(self, *args, **kwargs): sl = get_object_or_404(ShortLink, location=kwargs.get('link')) sl.click() return sl.to class CoolerLoginView(LoginView): redirect_authenticated_user = True next_page = reverse_lazy('interface.overview') class OverView(LoginRequiredMixin, TemplateView): template_name = 'interface/overview.html' def get_context_data(self, **kwargs): with scopes_disabled(): ctx = super().get_context_data(**kwargs) ds = Domain.objects.all() ctx.setdefault('domains', ds) links = [] for d in ds: [links.append(l) for l in d.links.all()] ctx.setdefault('links', links) return ctx class LinkListView(LoginRequiredMixin, ListView): template_name = 'interface/linkedlist.html' model = ShortLink def get(self, request, *args, **kwargs): with scopes_disabled(): d = Domain.objects.get(fqdn__contains=kwargs.get('domain')) with scope(domain=d): return super().get(request, *args, **kwargs) class LinkCreateView(LoginRequiredMixin, CreateView): template_name = 'interface/create_link.html' model = ShortLink form_class = LinkForm success_url = reverse_lazy('interface.overview') class LinkUpdateView(LoginRequiredMixin, UpdateView): template_name = 'interface/basic_form.html' model = ShortLink form_class = LinkForm success_url = reverse_lazy('interface.overview') class LinkDeleteView(LoginRequiredMixin, DeleteView): template_name = 'interface/delete_link.html' model = ShortLink success_url = reverse_lazy('interface.overview')