From a577a226561f0a33cb227c1efdafb14de4895cc6 Mon Sep 17 00:00:00 2001
From: Christian Dresen <c.dresen@fh-muenster.de>
Date: Tue, 14 Mar 2017 21:41:00 +0100
Subject: [PATCH] Changing to mysql step 1

---
 docker-compose.yml           |  2 ++
 www/Dockerfile               |  5 +++++
 www/web/warppay/views.py     | 10 ++++++----
 www/web/warpzone/settings.py | 26 ++++++++++++++++++++++++--
 4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/docker-compose.yml b/docker-compose.yml
index cd60f77..591ac2d 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -14,8 +14,10 @@ services:
         entrypoint: /opt/entrypoint_dev.sh
         depends_on:
             - ldap
+            - mysql
         links:
             - ldap
+            - mysql
 
     mysql:
         image: mariadb:latest
diff --git a/www/Dockerfile b/www/Dockerfile
index b67b34e..289e167 100644
--- a/www/Dockerfile
+++ b/www/Dockerfile
@@ -51,6 +51,11 @@ RUN chmod +x /opt/entrypoint*
 ADD web /opt/warpinfra/
 ADD nginx /opt/nginx
 
+RUN apt-get update && apt-get install -y libmysqlclient-dev
+RUN pip3 install django-mysql mysqlclient
+
+
+
 EXPOSE 8000 443 80
 
 VOLUME ["/opt/nginx", "/opt/warpinfra"]
diff --git a/www/web/warppay/views.py b/www/web/warppay/views.py
index 4bc40c7..91c7908 100644
--- a/www/web/warppay/views.py
+++ b/www/web/warppay/views.py
@@ -55,12 +55,15 @@ def user_list(request, user_id = 0):
             user = UserCredit.objects.get(uid=user_id)
             if "credit" in request.data:
                 user.credit = request.data['credit']
-            if "card_id" in request.data:
+            elif "card_id" in request.data:
                 # ToDo: Diskussion: Direkt Karte Ändern?
                 try:
                     ldap_user = LdapUser.objects.get(uid=str(request.data['uid']))
-                    ldap_user.card_id = request.data['card_id']
-                    ldap_user.save()
+                    if not ldap_user.card_id:
+                        ldap_user.card_id = request.data['card_id']
+                        ldap_user.save()
+                    else:
+                        return Response(status=status.HTTP_403_FORBIDDEN)
                 except:
                     pass
                 user.card_id = request.data['card_id']
@@ -92,7 +95,6 @@ def user_list(request, user_id = 0):
 
 def sync_users():
     for user in LdapUser.objects.all():
-        print(user)
         try:
             u = UserCredit.objects.get(uid=user.uid)
             if user.card_id:
diff --git a/www/web/warpzone/settings.py b/www/web/warpzone/settings.py
index e01782d..b0f7bb2 100644
--- a/www/web/warpzone/settings.py
+++ b/www/web/warpzone/settings.py
@@ -15,6 +15,13 @@ config.read('/etc/warpinfra/config.ini')
 # COMMON
 APPS = [app.strip() for app in config.get('common','APPS').split(",")]
 
+# MYSQL
+MYSQL_HOST = config.get('mysql','MYSQL_HOST')
+MYSQL_PORT = config.get('mysql','MYSQL_PORT')
+MYSQL_USER = config.get('mysql','MYSQL_USER')
+MYSQL_PASS = config.get('mysql','MYSQL_PASS')
+MYSQL_NAME = config.get('mysql','MYSQL_NAME')
+
 # LDAP
 LDAP_HOST = "ldap://"+config.get('ldap','LDAP_HOST')
 LDAP_BIND_DN = config.get('ldap','LDAP_BIND_DN')
@@ -67,6 +74,7 @@ INSTALLED_APPS = [
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'django.contrib.humanize',
+    'django_mysql',
     'bootstrapform',
     'bootstrap3_datetime',
     'rest_framework',
@@ -118,10 +126,24 @@ LOCALE_PATHS = (
 # https://docs.djangoproject.com/en/1.8/ref/settings/#databases
 
 DATABASES = {
+
     'default': {
-        'ENGINE': 'django.db.backends.sqlite3',
-        'NAME': '/opt/database/warpzone.db'
+        'ENGINE': 'django.db.backends.mysql',
+        'NAME': MYSQL_NAME,
+        'USER': MYSQL_USER,
+        'PASSWORD': MYSQL_PASS,
+        'HOST': MYSQL_HOST,
+        'PORT': MYSQL_PORT,
+        'OPTIONS': {
+            'init_command': "SET sql_mode='STRICT_TRANS_TABLES', innodb_strict_mode=1",
+            'charset': 'utf8mb4',
+        },
     },
+
+#    'default': {
+#        'ENGINE': 'django.db.backends.sqlite3',
+#        'NAME': '/opt/database/warpzone.db'
+#    },
     'ldap': {
         'ENGINE': 'ldapdb.backends.ldap',
         'NAME': LDAP_HOST,
-- 
GitLab