summaryrefslogtreecommitdiff
path: root/archaeological_files/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_files/models.py')
-rw-r--r--archaeological_files/models.py124
1 files changed, 123 insertions, 1 deletions
diff --git a/archaeological_files/models.py b/archaeological_files/models.py
index 68a65f6de..90f60fe64 100644
--- a/archaeological_files/models.py
+++ b/archaeological_files/models.py
@@ -24,7 +24,8 @@ from django.contrib.gis.db import models
from django.utils.translation import ugettext_lazy as _, ugettext
from ishtar_common.models import GeneralType, BaseHistorizedItem, \
- HistoricalRecords, OwnPerms, Person, Organization, Department, Town
+ HistoricalRecords, OwnPerms, Person, Organization, Department, Town, \
+ Dashboard
class FileType(GeneralType):
class Meta:
@@ -183,3 +184,124 @@ class FileByDepartment(models.Model):
class Meta:
managed = False
db_table = 'file_department'
+
+class FileDashboard:
+ def __init__(self):
+ main_dashboard = Dashboard(File)
+
+ self.total_number = main_dashboard.total_number
+
+ types = File.objects.values('file_type', 'file_type__label')
+ self.types = types.annotate(number=Count('pk')).order_by('file_type')
+
+ 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(
+ {'date':"date_trunc('month', creation_date)"})
+ self.by_month = by_month.values('date')\
+ .annotate(number=Count('pk')).order_by('-date')
+
+ # research
+ self.research = {}
+ prog_type = FileType.objects.get(txt_idx='prog')
+ 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,
+ department__isnull=False)\
+ .values('department__label')\
+ .annotate(number=Count('file'))\
+ .order_by('department__label')
+ FileTown = File.towns.through
+ 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,
+ department__isnull=False)\
+ .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,
+ department__isnull=False)\
+ .values('department__label')\
+ .annotate(number=Sum('file__total_surface'))\
+ .order_by('department__label')