summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerghei Mihai <smihai@entrouvert.com>2019-07-03 13:13:11 (GMT)
committerSerghei Mihai <smihai@entrouvert.com>2019-07-08 13:58:50 (GMT)
commitcf926ad5dc7b4a4b41770bf162bef780d5a8c4c8 (patch)
treed84225446d4936222a4b12c821f3fc9e4402cc5d
parent2bd1df4f2a20f7a6a79eb5723a8da34fa7ca76eb (diff)
downloadpublik-client-scripts-cf926ad5dc7b4a4b41770bf162bef780d5a8c4c8.zip
publik-client-scripts-cf926ad5dc7b4a4b41770bf162bef780d5a8c4c8.tar.gz
publik-client-scripts-cf926ad5dc7b4a4b41770bf162bef780d5a8c4c8.tar.bz2
orleans: add script to sync creche events (#34554)v1.6
-rw-r--r--clients/orleans/sync-chreche-events.py42
-rw-r--r--debian/control8
-rw-r--r--debian/publik-orleans-scripts.cron.hourly7
-rw-r--r--debian/publik-orleans-scripts.dirs1
-rw-r--r--debian/publik-orleans-scripts.install1
5 files changed, 58 insertions, 1 deletions
diff --git a/clients/orleans/sync-chreche-events.py b/clients/orleans/sync-chreche-events.py
new file mode 100644
index 0000000..91c0f27
--- /dev/null
+++ b/clients/orleans/sync-chreche-events.py
@@ -0,0 +1,42 @@
+import csv
+import datetime
+import os
+import zipfile
+import sys
+
+from django.db import transaction
+from django.utils import timezone
+from chrono.agendas.models import Agenda, Event, Booking
+
+if len(sys.argv) < 2:
+ sys.exit(0)
+
+if not os.path.exists(sys.argv[1]):
+ sys.exit(0)
+
+FILE_PATH = sys.argv[1]
+FILE_ENCODING = 'ISO-8859-1'
+
+with zipfile.ZipFile(FILE_PATH) as data:
+ csv_fd = data.open('extract_prcit_planning_creche.csv')
+ reader = csv.reader(csv_fd, delimiter=';')
+
+ # skip header line
+ reader.next()
+ with transaction.atomic():
+ for row in reader:
+ agenda_name = u'%s - %s' % (row[10].decode(FILE_ENCODING), row[11].decode(FILE_ENCODING))
+ agenda, created = Agenda.objects.get_or_create(label=agenda_name)
+ event_start_dt = '%s %s' % (row[1], row[2])
+ # delete all old events
+ Event.objects.filter(agenda=agenda, start_datetime__lt=timezone.now()).delete()
+ dt = timezone.datetime.strptime(event_start_dt, '%Y%m%d %H%M')
+ dt = timezone.make_aware(dt)
+ # handle cases when places column is empty
+ try:
+ places = int(row[8])
+ if places < 0:
+ raise ValueError
+ except ValueError:
+ places = 0
+ Event.objects.update_or_create(agenda=agenda, start_datetime=dt, defaults={'places': places})
diff --git a/debian/control b/debian/control
index ba0826a..580c62c 100644
--- a/debian/control
+++ b/debian/control
@@ -10,7 +10,8 @@ Package: publik-client-scripts
Architecture: all
Depends: ${misc:Depends},
publik-montpellier-scripts,
- publik-strasbourg-scripts
+ publik-strasbourg-scripts,
+ publik-orleans-scripts
Description: Meta package with all Publik client scripts
Package: publik-strasbourg-scripts
@@ -22,3 +23,8 @@ Package: publik-montpellier-scripts
Architecture: all
Depends: ${misc:Depends}
Description: Publik scripts for Montpellier
+
+Package: publik-orleans-scripts
+Architecture: all
+Depends: ${misc:Depends}
+Description: Publik scripts for Orléans
diff --git a/debian/publik-orleans-scripts.cron.hourly b/debian/publik-orleans-scripts.cron.hourly
new file mode 100644
index 0000000..905bb9b
--- /dev/null
+++ b/debian/publik-orleans-scripts.cron.hourly
@@ -0,0 +1,7 @@
+#! /bin/sh
+
+test -x /usr/bin/chrono-manage || exit 0
+
+CHRONO_TENANT_NAME=$(cd /var/lib/chrono/tenants/ && ls -1d *orleans*) || exit 0
+
+sudo -u chrono /usr/bin/chrono-manage tenant_command runscript -d $CHRONO_TENANT_NAME /usr/lib/publik/clients/orleans/sync-creche-events.py /var/lib/chrono/tenants/${CHRONO_TENANT_NAME}/media/family/exports_dispo_creche.zip
diff --git a/debian/publik-orleans-scripts.dirs b/debian/publik-orleans-scripts.dirs
new file mode 100644
index 0000000..4173003
--- /dev/null
+++ b/debian/publik-orleans-scripts.dirs
@@ -0,0 +1 @@
+/usr/lib/publik/clients/orleans \ No newline at end of file
diff --git a/debian/publik-orleans-scripts.install b/debian/publik-orleans-scripts.install
new file mode 100644
index 0000000..96e9f89
--- /dev/null
+++ b/debian/publik-orleans-scripts.install
@@ -0,0 +1 @@
+usr/lib/publik/clients/orleans \ No newline at end of file