musicias show_history

This commit is contained in:
Jorge Pastor 2024-08-14 13:04:26 +02:00
parent 14ff506a0d
commit 20bcb83c83
6 changed files with 64 additions and 20 deletions

View file

@ -318,6 +318,10 @@ msgstr "És el primer cop que accedeixes, et donem la benvinguda!"
msgid " The disk space of resources is updated weekly "
msgstr "L'espai en disc dels recursos es va actualitzant setmanalment."
#: templates/musician/dashboard.html:47
msgid "Show history"
msgstr "Mostrar historial"
#: templates/musician/database_list.html:21
#: templates/musician/mailbox_list.html:30 templates/musician/saas_list.html:19
#: templates/musician/webapps/webapp_list.html:25

View file

@ -320,6 +320,10 @@ msgstr "Es la primera vez que accedes: ¡te damos la bienvenida!"
msgid " The disk space of resources is updated weekly "
msgstr "El espacio en disco de los recursos se actualiza semanalmente"
#: templates/musician/dashboard.html:47
msgid "Show history"
msgstr "Mostrar historial"
#: templates/musician/database_list.html:21
#: templates/musician/mailbox_list.html:30 templates/musician/saas_list.html:19
#: templates/musician/webapps/webapp_list.html:25

View file

@ -37,20 +37,22 @@
</div>
<ul class="list-group">
{% for name, obj_data in account.objects.items %}
<li class="list-group-item d-flex justify-content-between align-items-center">
<div class="row w-100 justify-content-between">
<div class="col-4">
{{ name }}
{% if obj_data.ac != None %}
<li class="list-group-item d-flex justify-content-between align-items-center">
<div class="row w-100 justify-content-between">
<div class="col-4">
{{ name }}
</div>
<div class="col-4 text-center">
<a href="{% url 'musician:dashboard-history' obj_data.ac.id %}" target="_blank">{% trans "Show history" %} <i class="fas fa-clock"></i></a>
</div>
<div class="col-3"></div>
<div class="col-1">
<span class="badge badge-primary badge-pill">{{ obj_data.ac.used }} {{ obj_data.ac.unit }}</span>
</div>
</div>
<div class="col-4 text-center">
<a href="{% url 'musician:dashboard-history' obj_data.ac.id %}" target="_blank">Show history <i class="fas fa-clock"></i></a>
</div>
<div class="col-3"></div>
<div class="col-1">
<span class="badge badge-primary badge-pill">{{ obj_data.ac.used }} {{ obj_data.ac.unit }}</span>
</div>
</div>
</li>
</li>
{% endif %}
{% endfor %}
</ul>
</div>

View file

@ -62,17 +62,48 @@ logger = logging.getLogger(__name__)
import json
from urllib.parse import parse_qs
from orchestra.contrib.resources.helpers import get_history_data
from django.http import HttpResponseNotFound, Http404
class HistoryView(CustomContextMixin, UserTokenRequiredMixin, View):
def get_object(self, pk):
related_resources = self.get_all_resources()
account = related_resources.filter(resource_id__verbose_name='account-disk').first()
account_trafic = related_resources.filter(resource_id__verbose_name='account-traffic').first()
account = getattr(account, "id", False) == pk
account_trafic = getattr(account_trafic, "id", False) == pk
if account == False and account_trafic == False:
raise Http404(f"Resource with id {pk} does not exist")
class HistoryView(View):
def get(self, request, pk, *args, **kwargs):
context = {
'ids': pk
}
self.get_object(pk)
return render(request, "musician/history.html", context)
# TODO: funcion de dashborad, mirar como no repetir esta funcion
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)
class HistoryDataView(View):
class HistoryDataView(CustomContextMixin, UserTokenRequiredMixin, View):
def get(self, request, pk, *args, **kwargs):
ids = [pk]
queryset = ResourceData.objects.filter(id__in=ids)
@ -111,7 +142,6 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
# TODO(@slamora) update when backend provides resource usage data
resource_usage = {
# 'account': self.get_account_usage(profile_type, account),
'mailbox': self.get_resource_usage(profile_type, mailboxes, 'mailbox'),
'database': self.get_resource_usage(profile_type, databases, 'database'),
'nextcloud': self.get_resource_usage(profile_type, nextcloud, 'nextcloud'),
@ -157,7 +187,7 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
rs_left = 0
alert = ''
progres_bar = False
if ALLOWED_RESOURCES[profile_type].get(name_resource):
progres_bar = True
limit_rs = ALLOWED_RESOURCES[profile_type][name_resource]
@ -184,15 +214,19 @@ class DashboardView(CustomContextMixin, UserTokenRequiredMixin, TemplateView):
}
def get_account_usage(self, profile_type, account, account_trafic):
total_size = 0
if account != None and getattr(account, "used") != None:
total_size = account.used
allowed_size = ALLOWED_RESOURCES[profile_type]['account']
total_size = account.used
size_left = allowed_size - total_size
unit = account.unit if account != None else "GiB"
alert = ''
if size_left < 0:
alert = format_html(f"<span class='text-danger'>{size_left * -1} {account.unit} extra</span>")
alert = format_html(f"<span class='text-danger'>{size_left * -1} {unit} extra</span>")
elif size_left <= 1:
alert = format_html(f"<span class='text-warning'>{size_left} {account.unit} available</span>")
alert = format_html(f"<span class='text-warning'>{size_left} {unit} available</span>")
return {
'verbose_name': _('Account'),