From c0c7efa563edfce57f5df90d79d53bf8ab245c53 Mon Sep 17 00:00:00 2001 From: Klafyvel Date: Sun, 14 Jan 2018 19:05:53 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20mod=C3=A8les=20content,=20categor?= =?UTF-8?q?y,=20siteSettings,=20contentSettings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/migrations/0001_initial.py | 32 ++++++++++++++++++++++++ content/models.py | 24 +++++++++++++++++- settings/migrations/0001_initial.py | 32 ++++++++++++++++++++++++ settings/models.py | 38 ++++++++++++++++++++++++++++- site_tps/qaes.py | 26 ++++++++++++++++++++ site_tps/settings.py | 2 ++ 6 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 content/migrations/0001_initial.py create mode 100644 settings/migrations/0001_initial.py create mode 100644 site_tps/qaes.py diff --git a/content/migrations/0001_initial.py b/content/migrations/0001_initial.py new file mode 100644 index 0000000..b279fa6 --- /dev/null +++ b/content/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 2.0.1 on 2018-01-14 18:04 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0009_alter_user_last_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='Category', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('Nom de la catégorie', models.CharField(max_length=255)), + ], + ), + migrations.CreateModel( + name='Content', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('Nom du contenu', models.CharField(max_length=255)), + ('URL du contenu', models.URLField(editable=False)), + ('group_owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='auth.Group')), + ], + ), + ] diff --git a/content/models.py b/content/models.py index 71a8362..2f7611e 100644 --- a/content/models.py +++ b/content/models.py @@ -1,3 +1,25 @@ from django.db import models +from django.contrib.auth.models import Group -# Create your models here. + +class Category(models.Model): + name = models.CharField( + max_length=255, + name="Nom de la catégorie" + ) + + +class Content(models.Model): + name = models.CharField( + max_length=255, + name="Nom du contenu" + ) + group_owner = models.ForeignKey( + Group, + on_delete=models.CASCADE, + editable=False, + ) + content_url = models.URLField( + name='URL du contenu', + editable=False, + ) diff --git a/settings/migrations/0001_initial.py b/settings/migrations/0001_initial.py new file mode 100644 index 0000000..7d98541 --- /dev/null +++ b/settings/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 2.0.1 on 2018-01-14 18:04 + +from django.db import migrations, models +import settings.models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='ContentSettings', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('URL du FTP', models.URLField(max_length=255)), + ('Identifiant sur le FTP', models.CharField(max_length=255)), + ('Mot de passe', settings.models.AESEncryptedField(max_length=255)), + ], + ), + migrations.CreateModel( + name='SiteSettings', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('allow_upload', models.BooleanField(help_text="Autoriser l'upload de vidéos.")), + ('site_name', models.CharField(help_text='Nom du site', max_length=255)), + ], + ), + ] diff --git a/settings/models.py b/settings/models.py index 71a8362..2575479 100644 --- a/settings/models.py +++ b/settings/models.py @@ -1,3 +1,39 @@ from django.db import models +import binascii +import site_tps.qaes +from django.conf import settings -# Create your models here. + +class AESEncryptedField(models.CharField): + def save_form_data(self, instance, data): + setattr(instance, self.name, + binascii.b2a_base64(qaes.encrypt(settings.AES_KEY, data))) + + def value_from_object(self, obj): + return qaes.decrypt(settings.AES_KEY, + binascii.a2b_base64(getattr(obj, self.attname))) + + +class ContentSettings(models.Model): + ftp_url = models.URLField( + max_length=255, + name="URL du FTP", + ) + ftp_id = models.CharField( + max_length=255, + name="Identifiant sur le FTP", + ) + ftp_pass = AESEncryptedField( + max_length=255, + name="Mot de passe" + ) + + +class SiteSettings(models.Model): + allow_upload = models.BooleanField( + help_text="Autoriser l'upload de vidéos." + ) + site_name = models.CharField( + max_length=255, + help_text="Nom du site", + ) diff --git a/site_tps/qaes.py b/site_tps/qaes.py new file mode 100644 index 0000000..ee04139 --- /dev/null +++ b/site_tps/qaes.py @@ -0,0 +1,26 @@ +import string +from random import choice +from Crypto.Cipher import AES + +EOD = '`%EofD%`' # This should be something that will not occur in strings + + +def genstring(length=16, chars=string.printable): + return ''.join([choice(chars) for i in range(length)]) + + +def encrypt(key, s): + obj = AES.new(key) + datalength = len(s) + len(EOD) + if datalength < 16: + saltlength = 16 - datalength + else: + saltlength = 16 - datalength % 16 + ss = ''.join([s, EOD, genstring(saltlength)]) + return obj.encrypt(ss) + + +def decrypt(key, s): + obj = AES.new(key) + ss = obj.decrypt(s) + return ss.split(EOD)[0] diff --git a/site_tps/settings.py b/site_tps/settings.py index 05f4563..3b190e8 100644 --- a/site_tps/settings.py +++ b/site_tps/settings.py @@ -37,6 +37,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'settings', + 'content', ] MIDDLEWARE = [