from django.shortcuts import render from django.contrib.auth.decorators import login_required from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth import login as auth_login from django.db.models import Q from django.views.generic import CreateView from django.urls import reverse_lazy, reverse from django.contrib import messages from django.http import HttpResponseRedirect from whoisdb.models import ASNumber, InetNum from domains.models import Domain from rrequests.models import Request from .forms import CustomUserCreationForm @login_required def profile(request): pwForm = None if request.method == "POST": if request.POST.get("submit", None) == "pwchange": pwForm = PasswordChangeForm(user=request.user, data=request.POST) if pwForm.is_valid(): pwForm.save() auth_login(request, pwForm.user) messages.success(request, "Password changed") return HttpResponseRedirect(reverse("user:profile")) if not pwForm: pwForm = PasswordChangeForm(user=request.user) return render(request, "registration/profile.html", {'pwForm': pwForm}) @login_required def dashboard(request): mnts = request.user.maintainer_set.all() ownMnts = request.user.maintainer_set.filter(rir=False, lir=False).all().distinct() # if account only has rir/lir objects, show them if ownMnts.count() == 0: ownMnts = mnts asns = ASNumber.objects.filter(Q(mnt_by__in=ownMnts) | Q(mnt_lower__in=ownMnts)).distinct() inetnums = InetNum.objects.filter(Q(mnt_by__in=ownMnts) | Q(mnt_lower__in=ownMnts)).distinct() domains = Domain.objects.filter(mnt_by__in=mnts).distinct() rrequests = Request.objects.filter((Q(provider__in=mnts) | Q(applicant__in=mnts)) & Q(status=Request.STATE_OPEN)).distinct() return render(request, "dncore/dashboard.html", {"asns": asns, "inetnums": inetnums, "domains": domains, 'rrequests': rrequests}) def index(request): if request.user.is_authenticated(): return HttpResponseRedirect(reverse("dashboard")) return render(request, "index.html", {}) class RegisterUser(CreateView): template_name = "dncore/registration.html" form_class = CustomUserCreationForm success_url = reverse_lazy("user:login") def form_valid(self, form): ret = super(RegisterUser, self).form_valid(form) messages.success(self.request, "You successfully registered as user %s and can now log in!" % form.instance.username) return ret