2019-10-30 13:06:55 +00:00
|
|
|
|
2019-10-30 12:05:46 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2019-10-10 07:18:34 +00:00
|
|
|
from django.shortcuts import render
|
2019-10-29 09:58:54 +00:00
|
|
|
from django.urls import reverse_lazy
|
2019-10-31 09:46:54 +00:00
|
|
|
from django.utils.http import is_safe_url
|
2019-10-30 12:05:46 +00:00
|
|
|
from django.views.generic.base import RedirectView, TemplateView
|
2019-10-29 09:58:54 +00:00
|
|
|
from django.views.generic.edit import FormView
|
2019-10-31 13:08:49 +00:00
|
|
|
from django.views.generic.list import ListView
|
2019-10-10 07:18:34 +00:00
|
|
|
|
2019-10-29 09:58:54 +00:00
|
|
|
from . import api, get_version
|
2019-10-30 13:06:55 +00:00
|
|
|
from .auth import login as auth_login
|
|
|
|
from .auth import logout as auth_logout
|
2019-10-29 09:58:54 +00:00
|
|
|
from .forms import LoginForm
|
2019-10-30 13:06:55 +00:00
|
|
|
from .mixins import CustomContextMixin, UserTokenRequiredMixin
|
2019-10-25 11:33:37 +00:00
|
|
|
|
|
|
|
|
2019-10-30 13:06:55 +00:00
|
|
|
class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
|
2019-10-25 11:33:37 +00:00
|
|
|
template_name = "musician/dashboard.html"
|
2019-10-29 09:58:54 +00:00
|
|
|
|
2019-10-30 13:08:14 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
# TODO retrieve all data needed from orchestra
|
2019-10-31 13:08:49 +00:00
|
|
|
raw_domains = self.orchestra.retrieve_service_list('domain')
|
2019-10-30 13:08:14 +00:00
|
|
|
|
|
|
|
context.update({
|
|
|
|
'domains': raw_domains
|
|
|
|
})
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
2019-10-29 09:58:54 +00:00
|
|
|
|
2019-10-30 13:23:46 +00:00
|
|
|
class MailView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
|
|
|
|
template_name = "musician/mail.html"
|
|
|
|
|
|
|
|
|
2019-10-31 13:09:49 +00:00
|
|
|
class MailingListsView(CustomContextMixin, UserTokenRequiredMixin, ListView):
|
2019-10-30 13:23:46 +00:00
|
|
|
template_name = "musician/mailinglists.html"
|
2019-10-31 13:09:49 +00:00
|
|
|
paginate_by = 20
|
|
|
|
paginate_by_kwarg = 'per_page'
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return self.orchestra.retrieve_service_list('mailinglist')
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'page_param': self.page_kwarg,
|
|
|
|
'per_page_values': [5, 10, 20, 50],
|
|
|
|
'per_page_param': self.paginate_by_kwarg,
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_paginate_by(self, queryset):
|
|
|
|
per_page = self.request.GET.get(self.paginate_by_kwarg) or self.paginate_by
|
|
|
|
try:
|
|
|
|
paginate_by = int(per_page)
|
|
|
|
except ValueError:
|
|
|
|
paginate_by = self.paginate_by
|
|
|
|
return paginate_by
|
2019-10-30 13:23:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DatabasesView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
|
|
|
|
template_name = "musician/databases.html"
|
|
|
|
|
|
|
|
|
|
|
|
class SaasView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
|
|
|
|
template_name = "musician/saas.html"
|
|
|
|
|
|
|
|
|
2019-10-29 09:58:54 +00:00
|
|
|
class LoginView(FormView):
|
|
|
|
template_name = 'auth/login.html'
|
|
|
|
form_class = LoginForm
|
|
|
|
success_url = reverse_lazy('musician:dashboard')
|
2019-10-31 09:46:54 +00:00
|
|
|
redirect_field_name = 'next'
|
2019-10-29 09:58:54 +00:00
|
|
|
extra_context = {'version': get_version()}
|
2019-10-30 12:05:46 +00:00
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
kwargs['request'] = self.request
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
"""Security check complete. Log the user in."""
|
|
|
|
auth_login(self.request, form.username, form.token)
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
2019-10-31 09:46:54 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
url = self.get_redirect_url()
|
|
|
|
return url or self.success_url
|
|
|
|
|
|
|
|
def get_redirect_url(self):
|
|
|
|
"""Return the user-originating redirect URL if it's safe."""
|
|
|
|
redirect_to = self.request.POST.get(
|
|
|
|
self.redirect_field_name,
|
|
|
|
self.request.GET.get(self.redirect_field_name, '')
|
|
|
|
)
|
|
|
|
url_is_safe = is_safe_url(
|
|
|
|
url=redirect_to,
|
|
|
|
allowed_hosts={self.request.get_host()},
|
|
|
|
require_https=self.request.is_secure(),
|
|
|
|
)
|
|
|
|
return redirect_to if url_is_safe else ''
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
self.redirect_field_name: self.get_redirect_url(),
|
|
|
|
**(self.extra_context or {})
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
2019-10-30 12:05:46 +00:00
|
|
|
|
|
|
|
class LogoutView(RedirectView):
|
|
|
|
"""
|
|
|
|
Log out the user.
|
|
|
|
"""
|
|
|
|
permanent = False
|
|
|
|
pattern_name = 'musician:login'
|
|
|
|
|
|
|
|
def get_redirect_url(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Logs out the user.
|
|
|
|
"""
|
|
|
|
auth_logout(self.request)
|
|
|
|
return super().get_redirect_url(*args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
"""Logout may be done via POST."""
|
|
|
|
return self.get(request, *args, **kwargs)
|