2019-11-13 10:08:19 +00:00
|
|
|
from itertools import groupby
|
2019-11-13 11:26:35 +00:00
|
|
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2019-12-17 14:15:58 +00:00
|
|
|
from django.http import HttpResponse, 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-12-10 13:02:26 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-12-17 14:15:58 +00:00
|
|
|
from django.views import View
|
2019-10-30 12:05:46 +00:00
|
|
|
from django.views.generic.base import RedirectView, TemplateView
|
2019-12-06 09:28:34 +00:00
|
|
|
from django.views.generic.detail import DetailView
|
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-12-06 09:28:34 +00:00
|
|
|
from .mixins import (CustomContextMixin, ExtendedPaginationMixin,
|
|
|
|
UserTokenRequiredMixin)
|
2019-12-17 13:48:21 +00:00
|
|
|
from .models import (Bill, DatabaseService, MailinglistService, MailService,
|
2019-12-06 09:28:34 +00:00
|
|
|
PaymentSource, SaasService, UserAccount)
|
2019-12-12 14:01:51 +00:00
|
|
|
from .settings import ALLOWED_RESOURCES
|
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"
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('Dashboard'),
|
|
|
|
}
|
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)
|
2019-12-12 13:19:39 +00:00
|
|
|
domains = self.orchestra.retrieve_domain_list()
|
2019-10-30 13:08:14 +00:00
|
|
|
|
2019-12-10 13:02:26 +00:00
|
|
|
# TODO(@slamora) update when backend provides resource usage data
|
|
|
|
resource_usage = {
|
|
|
|
'disk': {
|
|
|
|
'verbose_name': _('Disk usage'),
|
|
|
|
'usage': 534,
|
|
|
|
'total': 1024,
|
|
|
|
'unit': 'MB',
|
|
|
|
'percent': 50,
|
|
|
|
},
|
|
|
|
'traffic': {
|
|
|
|
'verbose_name': _('Traffic'),
|
|
|
|
'usage': 300,
|
|
|
|
'total': 2048,
|
|
|
|
'unit': 'MB/month',
|
|
|
|
'percent': 25,
|
|
|
|
},
|
|
|
|
'mailbox': {
|
|
|
|
'verbose_name': _('Mailbox usage'),
|
|
|
|
'usage': 1,
|
|
|
|
'total': 2,
|
|
|
|
'unit': 'accounts',
|
|
|
|
'percent': 50,
|
|
|
|
},
|
|
|
|
}
|
2019-12-12 09:48:40 +00:00
|
|
|
|
2019-12-10 13:02:26 +00:00
|
|
|
# TODO(@slamora) update when backend supports notifications
|
|
|
|
notifications = []
|
|
|
|
|
2019-12-12 14:01:51 +00:00
|
|
|
# show resource usage based on plan definition
|
|
|
|
# TODO(@slamora): validate concept of limits with Pangea
|
|
|
|
profile_type = context['profile'].type
|
|
|
|
for domain in domains:
|
|
|
|
address_left = ALLOWED_RESOURCES[profile_type]['mailbox'] - len(domain.mails)
|
|
|
|
alert = None
|
|
|
|
if address_left == 1:
|
|
|
|
alert = 'warning'
|
|
|
|
elif address_left < 1:
|
|
|
|
alert = 'danger'
|
|
|
|
|
|
|
|
domain.address_left = {
|
|
|
|
'count': address_left,
|
|
|
|
'alert': alert,
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:08:14 +00:00
|
|
|
context.update({
|
2019-12-12 13:19:39 +00:00
|
|
|
'domains': domains,
|
2019-12-10 13:02:26 +00:00
|
|
|
'resource_usage': resource_usage,
|
|
|
|
'notifications': notifications,
|
2019-10-30 13:08:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
2019-10-29 09:58:54 +00:00
|
|
|
|
2019-11-20 19:07:35 +00:00
|
|
|
class ProfileView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
|
|
|
|
template_name = "musician/profile.html"
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('User profile'),
|
|
|
|
}
|
2019-11-20 19:07:35 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
try:
|
|
|
|
pay_source = self.orchestra.retrieve_service_list(
|
|
|
|
PaymentSource.api_name)[0]
|
|
|
|
except IndexError:
|
|
|
|
pay_source = {}
|
|
|
|
context.update({
|
|
|
|
'payment': PaymentSource.new_from_json(pay_source)
|
|
|
|
})
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2019-10-31 16:16:51 +00:00
|
|
|
class ServiceListView(CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin, ListView):
|
|
|
|
"""Base list view to all services"""
|
2019-11-13 11:26:35 +00:00
|
|
|
service_class = None
|
2020-01-07 12:53:59 +00:00
|
|
|
template_name = "musician/service_list.html"
|
2019-10-31 16:16:51 +00:00
|
|
|
|
2019-11-13 10:08:19 +00:00
|
|
|
def get_queryset(self):
|
2019-11-13 11:26:35 +00:00
|
|
|
if self.service_class is None or self.service_class.api_name is None:
|
2019-11-13 10:08:19 +00:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"ServiceListView requires a definiton of 'service'")
|
|
|
|
|
2019-12-17 10:57:59 +00:00
|
|
|
queryfilter = self.get_queryfilter()
|
2019-11-20 19:07:35 +00:00
|
|
|
json_qs = self.orchestra.retrieve_service_list(
|
2019-12-17 10:57:59 +00:00
|
|
|
self.service_class.api_name,
|
|
|
|
querystring=queryfilter,
|
|
|
|
)
|
2019-11-20 09:29:39 +00:00
|
|
|
return [self.service_class.new_from_json(data) for data in json_qs]
|
2019-11-13 10:08:19 +00:00
|
|
|
|
2019-12-17 10:57:59 +00:00
|
|
|
def get_queryfilter(self):
|
|
|
|
"""Does nothing by default. Should be implemented on subclasses"""
|
|
|
|
return ''
|
|
|
|
|
2019-11-13 10:08:19 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'service': self.service_class,
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2019-12-17 13:48:21 +00:00
|
|
|
class BillingView(ServiceListView):
|
|
|
|
service_class = Bill
|
|
|
|
template_name = "musician/billing.html"
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('Billing'),
|
|
|
|
}
|
2019-12-17 13:48:21 +00:00
|
|
|
|
|
|
|
|
2019-12-17 14:15:58 +00:00
|
|
|
class BillDownloadView(CustomContextMixin, UserTokenRequiredMixin, View):
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('Download bill'),
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:15:58 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
pk = self.kwargs.get('pk')
|
|
|
|
bill = self.orchestra.retrieve_bill_document(pk)
|
|
|
|
|
|
|
|
return HttpResponse(bill)
|
|
|
|
|
|
|
|
|
2019-11-13 10:08:19 +00:00
|
|
|
class MailView(ServiceListView):
|
|
|
|
service_class = MailService
|
2019-11-20 16:25:32 +00:00
|
|
|
template_name = "musician/mail.html"
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('Mail addresses'),
|
|
|
|
}
|
2019-11-13 10:08:19 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
def retrieve_mailbox(value):
|
|
|
|
mailboxes = value.get('mailboxes')
|
|
|
|
|
2019-12-13 12:49:12 +00:00
|
|
|
# forwarded address should not grouped
|
2019-11-13 10:08:19 +00:00
|
|
|
if len(mailboxes) == 0:
|
2019-12-13 12:49:12 +00:00
|
|
|
return value.get('name')
|
2019-11-13 10:08:19 +00:00
|
|
|
|
|
|
|
return mailboxes[0]['id']
|
|
|
|
|
2019-12-17 10:16:23 +00:00
|
|
|
# retrieve mails applying filters (if any)
|
|
|
|
queryfilter = self.get_queryfilter()
|
2019-11-13 10:08:19 +00:00
|
|
|
raw_data = self.orchestra.retrieve_service_list(
|
2019-12-17 10:16:23 +00:00
|
|
|
self.service_class.api_name,
|
|
|
|
querystring=queryfilter,
|
|
|
|
)
|
|
|
|
|
|
|
|
# group addresses with the same mailbox
|
2019-11-13 10:08:19 +00:00
|
|
|
addresses = []
|
|
|
|
for key, group in groupby(raw_data, retrieve_mailbox):
|
|
|
|
aliases = []
|
|
|
|
data = {}
|
|
|
|
for thing in group:
|
|
|
|
aliases.append(thing.pop('name'))
|
|
|
|
data = thing
|
|
|
|
|
|
|
|
data['names'] = aliases
|
2019-11-20 16:25:32 +00:00
|
|
|
addresses.append(self.service_class.new_from_json(data))
|
2019-10-31 16:16:51 +00:00
|
|
|
|
2019-11-13 10:08:19 +00:00
|
|
|
return addresses
|
2019-10-30 13:23:46 +00:00
|
|
|
|
2019-12-17 10:16:23 +00:00
|
|
|
def get_queryfilter(self):
|
|
|
|
"""Retrieve query params (if any) to filter queryset"""
|
|
|
|
domain_id = self.request.GET.get('domain')
|
2019-12-17 10:32:38 +00:00
|
|
|
if domain_id:
|
|
|
|
return "domain={}".format(domain_id)
|
2019-12-17 10:16:23 +00:00
|
|
|
|
2019-12-17 10:32:38 +00:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
domain_id = self.request.GET.get('domain')
|
|
|
|
if domain_id:
|
|
|
|
context.update({
|
|
|
|
'active_domain': self.orchestra.retrieve_domain(domain_id)
|
|
|
|
})
|
|
|
|
return context
|
2019-12-17 10:16:23 +00:00
|
|
|
|
2019-10-30 13:23:46 +00:00
|
|
|
|
2019-10-31 16:16:51 +00:00
|
|
|
class MailingListsView(ServiceListView):
|
2019-11-13 10:42:23 +00:00
|
|
|
service_class = MailinglistService
|
2019-12-04 11:37:35 +00:00
|
|
|
template_name = "musician/mailinglists.html"
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('Mailing lists'),
|
|
|
|
}
|
2019-10-31 13:09:49 +00:00
|
|
|
|
2019-10-30 13:23:46 +00:00
|
|
|
|
2019-12-17 10:57:59 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
domain_id = self.request.GET.get('domain')
|
|
|
|
if domain_id:
|
|
|
|
context.update({
|
|
|
|
'active_domain': self.orchestra.retrieve_domain(domain_id)
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_queryfilter(self):
|
|
|
|
"""Retrieve query params (if any) to filter queryset"""
|
|
|
|
# TODO(@slamora): this is not working because backend API
|
|
|
|
# doesn't support filtering by domain
|
|
|
|
domain_id = self.request.GET.get('domain')
|
|
|
|
if domain_id:
|
|
|
|
return "domain={}".format(domain_id)
|
|
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
2019-11-13 11:27:25 +00:00
|
|
|
class DatabasesView(ServiceListView):
|
2019-11-20 09:29:39 +00:00
|
|
|
template_name = "musician/databases.html"
|
2019-11-13 11:27:25 +00:00
|
|
|
service_class = DatabaseService
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('Databases'),
|
|
|
|
}
|
2019-10-30 13:23:46 +00:00
|
|
|
|
|
|
|
|
2019-12-06 09:28:34 +00:00
|
|
|
class SaasView(ServiceListView):
|
|
|
|
service_class = SaasService
|
2019-10-30 13:23:46 +00:00
|
|
|
template_name = "musician/saas.html"
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('Software as a Service'),
|
|
|
|
}
|
2019-10-30 13:23:46 +00:00
|
|
|
|
|
|
|
|
2019-12-13 14:08:01 +00:00
|
|
|
class DomainDetailView(CustomContextMixin, UserTokenRequiredMixin, DetailView):
|
|
|
|
template_name = "musician/domain_detail.html"
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('Domain details'),
|
|
|
|
}
|
2019-12-13 14:08:01 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2019-12-17 09:25:10 +00:00
|
|
|
# Return an empty list to avoid a request to retrieve all the
|
|
|
|
# user domains. We will get a 404 if the domain doesn't exists
|
|
|
|
# while invoking `get_object`
|
|
|
|
return []
|
2019-12-13 14:08:01 +00:00
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
if queryset is None:
|
|
|
|
queryset = self.get_queryset()
|
|
|
|
|
|
|
|
pk = self.kwargs.get(self.pk_url_kwarg)
|
2019-12-17 09:25:10 +00:00
|
|
|
domain = self.orchestra.retrieve_domain(pk)
|
2019-12-13 14:08:01 +00:00
|
|
|
|
|
|
|
return domain
|
|
|
|
|
|
|
|
|
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'
|
2020-01-07 12:53:59 +00:00
|
|
|
extra_context = {
|
|
|
|
# Translators: This message appears on the page title
|
|
|
|
'title': _('Login'),
|
|
|
|
'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)
|