From cb3e482dfcc154768d02384cdc909e24be15c2c3 Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Mon, 10 Jun 2024 22:26:55 +0200 Subject: [PATCH] musician dashboard get all resources --- .../templates/musician/dashboard2.html | 40 +++++++++ orchestra/contrib/musician/urls.py | 1 + orchestra/contrib/musician/views.py | 84 +++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 orchestra/contrib/musician/templates/musician/dashboard2.html diff --git a/orchestra/contrib/musician/templates/musician/dashboard2.html b/orchestra/contrib/musician/templates/musician/dashboard2.html new file mode 100644 index 00000000..cb70f99e --- /dev/null +++ b/orchestra/contrib/musician/templates/musician/dashboard2.html @@ -0,0 +1,40 @@ +{% extends "musician/base.html" %} +{% load i18n %} + +{% block content %} + +

{% trans "Welcome back" %} {{ profile.username }}

+{% if profile.last_login %} +

{% blocktrans with last_login=profile.last_login|date:"SHORT_DATE_FORMAT" %}Last time you logged in was: {{ last_login }}{% endblocktrans %}

+{% else %} +

{% trans "It's the first time you log into the system, welcome on board!" %}

+{% endif %} + +
+ {% for resource, usage in resource_usage.items %} +
+
+
{{ usage.verbose_name }}
+ {% include "musician/components/usage_progress_bar.html" with detail=usage.data %} + {% if usage.data.alert %} +
+ {{ usage.data.alert }} +
+ {% endif %} +
+
+ {% endfor %} +
+
+
{% trans "Notifications" %}
+ {% for message in notifications %} +

{{ message }}

+ {% empty %} +

{% trans "There is no notifications at this time." %}

+ {% endfor %} +
+
+
+ + +{% endblock %} diff --git a/orchestra/contrib/musician/urls.py b/orchestra/contrib/musician/urls.py index 8d56bda0..09775f12 100644 --- a/orchestra/contrib/musician/urls.py +++ b/orchestra/contrib/musician/urls.py @@ -17,6 +17,7 @@ urlpatterns = [ path('auth/login/', views.LoginView.as_view(), name='login'), path('auth/logout/', views.LogoutView.as_view(), name='logout'), path('dashboard/', views.DashboardView.as_view(), name='dashboard'), + path('dashboard2/', views.DashboardView2.as_view(), name='dashboard2'), path('domains//', views.DomainDetailView.as_view(), name='domain-detail'), path('domains//add-record/', views.DomainAddRecordView.as_view(), name='domain-add-record'), diff --git a/orchestra/contrib/musician/views.py b/orchestra/contrib/musician/views.py index ba8b98ca..2633c8f7 100644 --- a/orchestra/contrib/musician/views.py +++ b/orchestra/contrib/musician/views.py @@ -57,6 +57,90 @@ from .lists.views import * logger = logging.getLogger(__name__) +from django.db.models import Q +class DashboardView2(CustomContextMixin, UserTokenRequiredMixin, TemplateView): + template_name = "musician/dashboard2.html" + extra_context = { + # Translators: This message appears on the page title + 'title': _('Dashboard'), + } + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + + related_resources = self.get_all_resources() + # print([ x.resource for x in related_resources.filter(resource_id__verbose_name='mailbox-disk')]) + + + + # TODO(@slamora) update when backend supports notifications + notifications = [] + + # show resource usage based on plan definition + profile_type = context['profile'].type + + # TODO(@slamora) update when backend provides resource usage data + resource_usage = { + 'mailbox': self.get_mailbox_usage(profile_type), + } + + support_email = getattr(settings, "USER_SUPPORT_EMAIL", "suport@pangea.org") + support_email_anchor = format_html( + "{}", + support_email, + support_email, + ) + context.update({ + # 'domains': domains, + 'resource_usage': resource_usage, + 'notifications': notifications, + "support_email_anchor": support_email_anchor, + }) + + return context + + + def get_all_resources(self): + user = self.request.user + resources = Resource.objects.select_related('content_type') + resource_models = {r.content_type.model_class(): r.content_type_id for r in resources} + ct_id = resource_models[user._meta.model] + qset = Q(content_type_id=ct_id, object_id=user.id, resource__is_active=True) + for field, rel in user._meta.fields_map.items(): + try: + ct_id = resource_models[rel.related_model] + except KeyError: + pass + else: + manager = getattr(user, field) + ids = manager.values_list('id', flat=True) + qset = Q(qset) | Q(content_type_id=ct_id, object_id__in=ids, resource__is_active=True) + return ResourceData.objects.filter(qset) + + + def get_mailbox_usage(self, profile_type): + allowed_mailboxes = ALLOWED_RESOURCES[profile_type]['mailbox'] + total_mailboxes = len(self.orchestra.retrieve_mailbox_list()) + mailboxes_left = allowed_mailboxes - total_mailboxes + + alert = '' + if mailboxes_left < 0: + alert = format_html("{} extra mailboxes", mailboxes_left * -1) + elif mailboxes_left <= 1: + alert = format_html("{} mailbox left", mailboxes_left) + + return { + 'verbose_name': _('Mailboxes'), + 'data': { + 'used': total_mailboxes, + 'total': allowed_mailboxes, + 'alert': alert, + 'unit': 'mailboxes', + 'percent': get_bootstraped_percent(total_mailboxes, allowed_mailboxes), + }, + } + + class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView): template_name = "musician/dashboard.html" extra_context = {