From b8ec4a8daf3ca973abefef2e1baa833a01f2bdcd Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 26 Jul 2011 13:17:37 +0200 Subject: Archaeological file dashboard (closes #521) --- ishtar/ishtar_base/models.py | 134 +++++++++++++++----- ishtar/templates/dashboard_file.html | 231 +++++++++++++++++++++++++++-------- 2 files changed, 288 insertions(+), 77 deletions(-) diff --git a/ishtar/ishtar_base/models.py b/ishtar/ishtar_base/models.py index 6f29196ce..e686b6ca2 100644 --- a/ishtar/ishtar_base/models.py +++ b/ishtar/ishtar_base/models.py @@ -28,7 +28,7 @@ from django.utils.translation import ugettext_lazy as _, ugettext from django.utils.safestring import SafeUnicode, mark_safe from django.core.urlresolvers import reverse, NoReverseMatch from django.db.utils import DatabaseError -from django.db.models import Q, Max, Count +from django.db.models import Q, Max, Count, Sum from django.db.models.signals import m2m_changed from django.contrib.auth.models import User @@ -360,8 +360,8 @@ class UserDashboard: def __init__(self): types = IshtarUser.objects.values('person__person_type', 'person__person_type__label') - self.types = types.annotate(number=Count('pk')).order_by( - 'person__person_type') + self.types = types.annotate(number=Count('pk'))\ + .order_by('person__person_type') class FileDashboard: def __init__(self): @@ -372,36 +372,114 @@ class FileDashboard: types = File.objects.values('file_type', 'file_type__label') self.types = types.annotate(number=Count('pk')).order_by('file_type') - self.by_years = main_dashboard.values + by_year = File.objects.extra( + {'date':"date_trunc('year', creation_date)"}) + self.by_year = by_year.values('date')\ + .annotate(number=Count('pk')).order_by('-date') now = datetime.date.today() limit = datetime.date(now.year, now.month, 1) - datetime.timedelta(365) by_month = File.objects.filter(creation_date__gt=limit).extra( - {'month':"date_trunc('month', creation_date)"}) - self.by_month = by_month.values('month').annotate(number=Count('pk') - ).order_by('-month') + {'date':"date_trunc('month', creation_date)"}) + self.by_month = by_month.values('date')\ + .annotate(number=Count('pk')).order_by('-date') - # programmed - self.prog = {} + # research + self.research = {} prog_type = FileType.objects.get(txt_idx='prog') - progs = File.objects.filter(file_type=prog_type) - self.prog['total_number'] = progs.count() - by_year = progs.extra({'year':"date_trunc('year', creation_date)"}) - self.prog['by_year'] = by_year.values('year').annotate(number=Count('pk') - ).order_by('-year') - by_month = progs.filter(creation_date__gt=limit).extra( - {'month':"date_trunc('month', creation_date)"}) - self.prog['by_month'] = by_month.values('month').annotate( - number=Count('pk')).order_by('-month') - - self.prog['by_dpts'] = FileByDepartment.objects.filter( - file__file_type=prog_type).values('department' - ).annotate(number=Count('file')).order_by('-number') + researchs = File.objects.filter(file_type=prog_type) + self.research['total_number'] = researchs.count() + by_year = researchs.extra({'date':"date_trunc('year', creation_date)"}) + self.research['by_year'] = by_year.values('date')\ + .annotate(number=Count('pk'))\ + .order_by('-date') + by_month = researchs.filter(creation_date__gt=limit)\ + .extra({'date':"date_trunc('month', creation_date)"}) + self.research['by_month'] = by_month.values('date')\ + .annotate(number=Count('pk'))\ + .order_by('-date') + + self.research['by_dpt'] = FileByDepartment.objects\ + .filter(file__file_type=prog_type)\ + .values('department__label')\ + .annotate(number=Count('file'))\ + .order_by('department__label') FileTown = File.towns.through - self.prog['towns'] = FileTown.objects.filter( - file__file_type=prog_type).values('town' - ).annotate(number=Count('file')).order_by('-number') - + self.research['towns'] = FileTown.objects\ + .filter(file__file_type=prog_type)\ + .values('town__name')\ + .annotate(number=Count('file'))\ + .order_by('-number','town__name')[:10] + + # rescue + rescue_type = FileType.objects.get(txt_idx='preventive') + rescues = File.objects.filter(file_type=rescue_type) + self.rescue = {} + self.rescue['total_number'] = rescues.count() + self.rescue['saisine'] = rescues.values('saisine_type__label')\ + .annotate(number=Count('pk'))\ + .order_by('saisine_type__label') + self.rescue['administrative_act'] = AdministrativeAct.objects\ + .filter(associated_file__isnull=False)\ + .values('act_type__label')\ + .annotate(number=Count('pk'))\ + .order_by('act_type__pk') + + by_year = rescues.extra({'date':"date_trunc('year', creation_date)"}) + self.rescue['by_year'] = by_year.values('date')\ + .annotate(number=Count('pk'))\ + .order_by('-date') + by_month = rescues.filter(creation_date__gt=limit)\ + .extra({'date':"date_trunc('month', creation_date)"}) + self.rescue['by_month'] = by_month.values('date')\ + .annotate(number=Count('pk'))\ + .order_by('-date') + + self.rescue['by_dpt'] = FileByDepartment.objects\ + .filter(file__file_type=rescue_type)\ + .values('department__label')\ + .annotate(number=Count('file'))\ + .order_by('department__label') + self.rescue['towns'] = FileTown.objects\ + .filter(file__file_type=rescue_type)\ + .values('town__name')\ + .annotate(number=Count('file'))\ + .order_by('-number','town__name')[:10] + + self.rescue['with_associated_operation'] = rescues\ + .filter(operations__isnull=False).count() + + self.rescue['with_associated_operation_percent'] = round( + float(self.rescue['with_associated_operation'])\ + /self.rescue['total_number']*100, 2) + + by_year_operationnal = rescues.filter(operations__isnull=False)\ + .extra({'date':"date_trunc('year', creation_date)"}) + by_year_operationnal = by_year_operationnal.values('date')\ + .annotate(number=Count('pk'))\ + .order_by('-date') + percents, idx = [], 0 + for dct in self.rescue['by_year']: + if idx > len(by_year_operationnal): + break + if by_year_operationnal[idx]['date'] != dct['date'] or\ + not dct['number']: + continue + val = round(float(by_year_operationnal[idx]['number'])/\ + dct['number']*100, 2) + percents.append({'date':dct['date'], 'number':val}) + self.rescue['operational_by_year'] = percents + + self.rescue['surface_by_town'] = FileTown.objects\ + .filter(file__file_type=rescue_type)\ + .values('town__name')\ + .annotate(number=Sum('file__total_surface'))\ + .order_by('-number','town__name')[:10] + self.rescue['surface_by_dpt'] = FileByDepartment.objects\ + .filter(file__file_type=rescue_type)\ + .values('department__label')\ + .annotate(number=Sum('file__total_surface'))\ + .order_by('department__label') class Dashboard: def __init__(self, model): @@ -411,8 +489,8 @@ class Dashboard: # last edited - created self.recents, self.lasts = [], [] for last_lst, modif_type in ((self.lasts, '+'), (self.recents, '~')): - last_ids = history_model.objects.values('id').annotate(hd=\ - Max('history_date')) + last_ids = history_model.objects.values('id')\ + .annotate(hd=Max('history_date')) last_ids = last_ids.filter(history_type=modif_type) if self.model == Item: last_ids = last_ids.filter(downstream_treatment_id__isnull=True) diff --git a/ishtar/templates/dashboard_file.html b/ishtar/templates/dashboard_file.html index e710dbe16..cebd147f4 100644 --- a/ishtar/templates/dashboard_file.html +++ b/ishtar/templates/dashboard_file.html @@ -6,77 +6,210 @@ {% endblock %} {% block content %}
-{% for lbl, dashboard in items %} +

{% trans "Archaeological files" %}

-

{{lbl}}

-

{% trans "Numbers" %}

+

{% trans "Global informations" %}

+

{% trans "Total:" %} {{dashboard.total_number}}

+ {% for type in dashboard.types %} +

{{type.file_type__label}}{% trans ":"%} {{type.number}}

+ {% endfor %}
- {% for idx, lbl, values in dashboard.values %} - - - {% for value in values %}{% endfor%} + + + {% for year in dashboard.by_year %}{% endfor %} + + + {% for year in dashboard.by_year %}{% endfor%} + +
{{lbl}}{{value}}
{% trans "By year"%}
{{year.date.year}}
{{year.number}}
+
+ +
+ + + + {% for month in dashboard.by_month %}{% endfor %} + + + {% for month in dashboard.by_month %}{% endfor%} + +
{% trans "By month"%}
{{month.date|date:"F Y"|capfirst}}
{{month.number}}
+
+ +
+
+ +

{% trans "Research archaeology" %}

+ +

{% trans "Total:" %} {{dashboard.research.total_number}}

+
+ + + + {% for year in dashboard.research.by_year %}{% endfor %} + + + {% for year in dashboard.research.by_year %}{% endfor%} + +
{% trans "By year"%}
{{year.date.year}}
{{year.number}}
+
+ +
+ + + + {% for month in dashboard.research.by_month %}{% endfor %} + + + {% for month in dashboard.research.by_month %}{% endfor%} - {% endfor%}
{% trans "By month"%}
{{month.date|date:"F Y"|capfirst}}
{{month.number}}
- {% if dashboard.years %} -

{% trans "By years" %}

-
    -
  • {% trans "Average:" %} {{dashboard.average}}
  • -
  • {% trans "Variance:" %} {{dashboard.variance}}
  • -
  • {% trans "Standard deviation:" %} {{dashboard.standard_deviation}}
  • -
  • {% trans "Median:" %} {{dashboard.median}}
  • -
  • {% trans "Mode:" %} {{dashboard.mode}}
  • -
- {% endif %} - {% if dashboard.operation_average %} -

{% trans "By operations" %}

-
    -
  • {% trans "Average:" %} {{dashboard.operation_average}}
  • -
  • {% trans "Variance:" %} {{dashboard.operation_variance}}
  • -
  • {% trans "Standard deviation:" %} {{dashboard.operation_standard_deviation}}
  • -
  • {% trans "Median:" %} {{dashboard.operation_median}}
  • -
  • {% trans "Mode:" %} {{dashboard.operation_mode}}
  • -
- {% endif %} -

{% trans "Created last" %}

+
- - {% for item in dashboard.lasts %} - - - - {% endfor %} + + + {% for dpt in dashboard.research.by_dpt %}{% endfor %} + + + {% for dpt in dashboard.research.by_dpt %}{% endfor%} +
{{lbl}}{% trans "Created" %}
{{item}}{{item.history_date}}{% if item.get_show_url %}{%trans "Show"%}{%endif%}
{% trans "By department"%}
{{dpt.department__label}}
{{dpt.number}}
-

{% trans "Recent changes" %}

+
- - {% for item in dashboard.recents %} - - - - {% endfor %} + + + {% for town in dashboard.research.towns %}{% endfor %} + + + {% for town in dashboard.research.towns %}{% endfor%} +
{{lbl}}{% trans "Modified" %}
{{item}}{{item.history_date}}{% if item.get_show_url %}{%trans "Show"%}{%endif%}
{% trans "Main towns"%}
{{town.town__name}}
{{town.number}}
+
-{% endfor%}
-

{% trans "Users" %}

+ +

{% trans "Rescue archaeology" %}

+ +

{% trans "Total:" %} {{dashboard.rescue.total_number}}

+ +
+ + + + {% for saisine in dashboard.rescue.saisine %}{% endfor %} + + + {% for saisine in dashboard.rescue.saisine %}{% endfor%} + +
{% trans "By saisine type"%}
{{saisine.saisine_type__label}}
{{saisine.number}}
+
+ +
+ + + + {% for act in dashboard.rescue.administrative_act %}{% endfor %} + + + {% for act in dashboard.rescue.administrative_act %}{% endfor%} + +
{% trans "By administrative act"%}
{{act.act_type__label}}
{{act.number}}
+
+ +
+ + + + {% for year in dashboard.rescue.by_year %}{% endfor %} + + + {% for year in dashboard.rescue.by_year %}{% endfor%} + +
{% trans "By year"%}
{{year.date.year}}
{{year.number}}
+
+ +
+ + + + {% for month in dashboard.rescue.by_month %}{% endfor %} + + + {% for month in dashboard.rescue.by_month %}{% endfor%} + +
{% trans "By month"%}
{{month.date|date:"F Y"|capfirst}}
{{month.number}}
+
+ +

{% trans "Archaeological files linked to at least one operation:" %} {{dashboard.rescue.with_associated_operation}}

+

{% trans "Archaeological files linked to at least one operation (%):" %} {{dashboard.rescue.with_associated_operation_percent}}

+ +
+ + + + {% for year in dashboard.rescue.operational_by_year %}{% endfor %} + + + {% for year in dashboard.rescue.operational_by_year %}{% endfor%} + +
{% trans "Archaeological files linked to at least one operation (%)"%}
{{year.date.year}}
{{year.number}}
+
+ +
+ + + + {% for dpt in dashboard.rescue.by_dpt %}{% endfor %} + + + {% for dpt in dashboard.rescue.by_dpt %}{% endfor%} + +
{% trans "By department"%}
{{dpt.department__label}}
{{dpt.number}}
+
+ +
+ + + + {% for dpt in dashboard.rescue.surface_by_dpt %}{% endfor %} + + + {% for dpt in dashboard.rescue.surface_by_dpt %}{% endfor%} + +
{% trans "Surface by department (m²)"%}
{{dpt.department__label}}
{{dpt.number}}
+
+
- - {% for user_type in ishtar_users.types %} + + + {% for town in dashboard.rescue.towns %}{% endfor %} + - - + {% for town in dashboard.rescue.towns %}{% endfor%} - {% endfor%}
{% trans "User type" %}{% trans "Number" %}
{% trans "Main towns by number"%}
{{town.town__name}}
{{user_type.person__person_type__label}}{{user_type.number}}{{town.number}}
+ +
+ + + + {% for town in dashboard.rescue.surface_by_town %}{% endfor %} + + + {% for town in dashboard.rescue.surface_by_town %}{% endfor%} + +
{% trans "Main towns by surface (m²)"%}
{{town.town__name}}
{{town.number}}
+
+ +
{% endblock %} -- cgit v1.2.3