aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/caf/managers.py
diff options
context:
space:
mode:
authorMatthew Lemon <lemon@matthewlemon.com>2020-03-06 16:05:05 +0000
committerMatthew Lemon <lemon@matthewlemon.com>2020-03-06 16:05:05 +0000
commitff85a615721dc048046b212fe07edbced8fe20fc (patch)
tree442e0503cfe69ddd907c94073361f8cdbbaebd69 /ctrack/caf/managers.py
parentb69b8f797c710718df3cb24c5ec83d7d6dd1d0df (diff)
first attempt at a custom manager for applicablesystem model
Diffstat (limited to '')
-rw-r--r--ctrack/caf/managers.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/ctrack/caf/managers.py b/ctrack/caf/managers.py
new file mode 100644
index 0000000..3aea117
--- /dev/null
+++ b/ctrack/caf/managers.py
@@ -0,0 +1,25 @@
+from django.db import models
+
+from ctrack.organisations.models import Organisation, Person
+
+
+class ApplicableSystemManager(models.Manager):
+ def with_primary_contact(self):
+ """
+ Add in the name of the primary nis contact to the context.
+ Using Custom Managers Django docs for an example.
+ """
+ from django.db import connection
+ with connection.cursor() as cursor:
+ cursor.execute("""
+ SELECT a.id, a.name, o.id, c.id, sm.descriptor, p.id, o.name
+ FROM caf_applicablesystem a, organisations_organisation o, organisations_person p, caf_caf c, organisations_submode sm
+ WHERE a.organisation_id = o.id AND a.caf_id = c.id AND p.organisation_id = o.id AND o.submode_id = sm.id AND p.primary_nis_contact = False;
+ """)
+ result_list = []
+ for row in cursor.fetchall():
+ org = Organisation.objects.get(pk=row[2])
+ ass = self.model(id=row[0], name=row[1], organisation=org)
+ ass.nis_contact = Person.objects.get(pk=row[5])
+ result_list.append(ass)
+ return result_list