diff --git a/infoscreen/management/commands/importabb.py b/infoscreen/management/commands/importabb.py new file mode 100644 index 0000000..9e0a587 --- /dev/null +++ b/infoscreen/management/commands/importabb.py @@ -0,0 +1,46 @@ +from django.core.management.base import BaseCommand, CommandError +from django.db.models import Max +from django.conf import settings +from datetime import datetime, timedelta +from pytz import utc +from paramiko.client import SSHClient, AutoAddPolicy, RSAKey +from infoscreen.models import ABBJob +import qrcode + +class Command(BaseCommand): + help = 'import new abbjobs from oldish sikweb' + + def add_arguments(self, parser): + parser.add_argument('keypath') + + def handle(self, *args, **options): + last_id = ABBJob.objects.aggregate(Max('sw_id'))['sw_id__max'] or 0 + client = SSHClient() + client.set_missing_host_key_policy(AutoAddPolicy()) + key = RSAKey.from_private_key_file(options.get("keypath")) + client.connect("otax.ayy.fi", username="sik", pkey=key) + stdin, stdout, stderr = client.exec_command("./abbjobexport.sh {}".format(last_id)) + errors = stderr.read() + if len(errors) > 0: + print(errors) + client.close() + return + data = stdout.read().decode("latin1").strip().split("\n") # shame on latin1 otax!!! + for row in data: + if not row: + continue + cols = row.split('\t',maxsplit=2) + qr_url = gen_qr(cols[0]) + created = datetime.strptime(cols[1], "%Y-%m-%d %H:%M:%S").replace(tzinfo=utc) # todo parse to right timezone + ABBJob.objects.create( + sw_id=int(cols[0]), + created=created, + title=cols[2], + QR=qr_url) + +def gen_qr(sw_id): + img = qrcode.make("http://sahkoinsinoorikilta.fi/news/{}".format(sw_id)) + imgname = "abbjobs_{}.png".format(sw_id) + imgurl = "{}qr/{}".format(settings.MEDIA_URL,imgname) + img.save("{}qr/{}".format(settings.MEDIA_ROOT,imgname)) + return imgurl diff --git a/infoscreen/migrations/0001_initial.py b/infoscreen/migrations/0001_initial.py new file mode 100644 index 0000000..5254f3b --- /dev/null +++ b/infoscreen/migrations/0001_initial.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2016-08-31 17:54 +from __future__ import unicode_literals + +import datetime +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='ABBJob', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('sw_id', models.IntegerField(default=-1)), + ('title', models.CharField(max_length=255)), + ('QR', models.CharField(default='', max_length=255)), + ('created', models.DateTimeField(default=datetime.datetime.now)), + ], + ), + ] diff --git a/infoscreen/migrations/0002_auto_20160831_1757.py b/infoscreen/migrations/0002_auto_20160831_1757.py new file mode 100644 index 0000000..b21b61d --- /dev/null +++ b/infoscreen/migrations/0002_auto_20160831_1757.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2016-08-31 17:57 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('infoscreen', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='abbjob', + name='created', + field=models.DateTimeField(default=django.utils.timezone.now), + ), + ] diff --git a/infoscreen/models.py b/infoscreen/models.py index 71a8362..2d4d93a 100644 --- a/infoscreen/models.py +++ b/infoscreen/models.py @@ -1,3 +1,14 @@ from django.db import models +from django.utils import timezone -# Create your models here. +class ABBJob(models.Model): + sw_id = models.IntegerField(default=-1) + title = models.CharField(max_length=255) + QR = models.CharField(max_length=255, default="") + created = models.DateTimeField(default=timezone.now) + + def get_dict(self): + return { + "title": self.title, + "QR": self.QR, + } diff --git a/infoscreen/static/html/abb.html b/infoscreen/static/html/abb.html new file mode 100644 index 0000000..bf65a06 --- /dev/null +++ b/infoscreen/static/html/abb.html @@ -0,0 +1,11 @@ +
+
+
+
TYƖPAIKAT
+
+ +
+
{{ job.title }}
+
+
+
diff --git a/infoscreen/static/img/ABB_logo.png b/infoscreen/static/img/ABB_logo.png new file mode 100644 index 0000000..697de60 Binary files /dev/null and b/infoscreen/static/img/ABB_logo.png differ diff --git a/infoscreen/static/js/infoscreen_controllers.js b/infoscreen/static/js/infoscreen_controllers.js index 75dab24..322a0ec 100644 --- a/infoscreen/static/js/infoscreen_controllers.js +++ b/infoscreen/static/js/infoscreen_controllers.js @@ -9,8 +9,7 @@ app.controller('infoscreen_main', function($scope,$timeout){ template: "/static/html/test1.html?img=siklogo", onload: function(){$scope.imagepath = "/static/img/siklogo.jpg";} },{ - template: "/static/html/test1.html?img=teekkaribileet", - onload: function(){$scope.imagepath = "/static/img/teekkaribileet.jpg";} + template: "/static/html/abb.html" },{ template: "/static/html/test1.html?img=kaukkarit", onload: function(){$scope.imagepath = "/static/img/kaukkarit.jpg";} @@ -23,3 +22,9 @@ app.controller('infoscreen_main', function($scope,$timeout){ } $scope.next() }); +app.controller('ABBController', function($scope, $http){ + $scope.jobs = []; + $http.get("/infoscreen/abbjobs").then(function(response){ + $scope.jobs = response.data; + }) +}); diff --git a/infoscreen/views.py b/infoscreen/views.py index 8e41add..f76bd92 100644 --- a/infoscreen/views.py +++ b/infoscreen/views.py @@ -1,4 +1,19 @@ from django.shortcuts import render +from django.http import HttpResponse +from django.views.decorators.http import require_http_methods +from infoscreen.models import ABBJob +from django.utils import timezone +from datetime import datetime, timedelta +import json def index(request , *args, **kwargs): return render(request, 'infoscreen_index.html',{}) + + +# send abb jobs which have been created less than month ago +@require_http_methods(["GET"]) +def abb_job_list(request, *args, **kwargs): + limit = timezone.now() - timedelta(days=30) + jobs = ABBJob.objects.filter(created__gt=limit) + joblist = list(map(lambda j:j.get_dict(), jobs)) + return HttpResponse(json.dumps(joblist)) diff --git a/requirements.txt b/requirements.txt index ae5ad41..43e0785 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,6 @@ ptyprocess==0.5.1 pytz==2016.4 simplegeneric==0.8.1 traitlets==4.2.1 +paramiko==2.0.2 +qrcode==5.3 +Pillow==3.3.1 diff --git a/sikweb/urls.py b/sikweb/urls.py index 51ba699..ad7017e 100644 --- a/sikweb/urls.py +++ b/sikweb/urls.py @@ -31,6 +31,8 @@ from members.views import new_member_request from members.views import member_requests #infoscreen from infoscreen.views import index as infoindex +from infoscreen.views import abb_job_list + urlpatterns = [ # main @@ -51,4 +53,5 @@ urlpatterns = [ url(r'^members/api/request/(?P\d+)$', handle_mem_request), #infoscreen url(r'^infoscreen/$', infoindex), + url(r'^infoscreen/abbjobs$', abb_job_list), ]