From c1d79b5bb3c71b907739dd3f6a7fdf674069af0f Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 13 Dec 2017 12:34:46 +0300 Subject: [PATCH] fix --- access/migrations/0001_initial.py | 2 +- ...212_2307.py => 0002_auto_20171213_1230.py} | 8 +-- achievements/migrations/0001_initial.py | 2 +- ...212_2307.py => 0002_auto_20171213_1230.py} | 4 +- courses/migrations/0001_initial.py | 2 +- ...212_2307.py => 0002_auto_20171213_1230.py} | 4 +- finance/migrations/0001_initial.py | 4 +- library/migrations/0001_initial.py | 2 +- maps/migrations/0001_initial.py | 4 +- storage/api.py | 55 +++++++++++++++++++ storage/migrations/0001_initial.py | 3 +- storage/models.py | 20 +++++++ storage/tests.py | 13 ++++- storage/views.py | 47 ---------------- 14 files changed, 105 insertions(+), 65 deletions(-) rename access/migrations/{0002_auto_20171212_2307.py => 0002_auto_20171213_1230.py} (97%) rename achievements/migrations/{0002_auto_20171212_2307.py => 0002_auto_20171213_1230.py} (97%) rename courses/migrations/{0002_auto_20171212_2307.py => 0002_auto_20171213_1230.py} (95%) create mode 100644 storage/api.py delete mode 100644 storage/views.py diff --git a/access/migrations/0001_initial.py b/access/migrations/0001_initial.py index e29a62b..bcfe877 100644 --- a/access/migrations/0001_initial.py +++ b/access/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals import access.models.user diff --git a/access/migrations/0002_auto_20171212_2307.py b/access/migrations/0002_auto_20171213_1230.py similarity index 97% rename from access/migrations/0002_auto_20171212_2307.py rename to access/migrations/0002_auto_20171213_1230.py index 014ab17..3492030 100644 --- a/access/migrations/0002_auto_20171212_2307.py +++ b/access/migrations/0002_auto_20171213_1230.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals from django.conf import settings @@ -12,10 +12,10 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('courses', '0002_auto_20171212_2307'), - ('auth', '0008_alter_user_username_max_length'), - ('maps', '0001_initial'), ('access', '0001_initial'), + ('maps', '0001_initial'), + ('auth', '0008_alter_user_username_max_length'), + ('courses', '0002_auto_20171213_1230'), ] operations = [ diff --git a/achievements/migrations/0001_initial.py b/achievements/migrations/0001_initial.py index ce12385..9c5a33f 100644 --- a/achievements/migrations/0001_initial.py +++ b/achievements/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals from django.db import migrations, models diff --git a/achievements/migrations/0002_auto_20171212_2307.py b/achievements/migrations/0002_auto_20171213_1230.py similarity index 97% rename from achievements/migrations/0002_auto_20171212_2307.py rename to achievements/migrations/0002_auto_20171213_1230.py index 917b715..3c81e1f 100644 --- a/achievements/migrations/0002_auto_20171212_2307.py +++ b/achievements/migrations/0002_auto_20171213_1230.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals from django.conf import settings @@ -13,8 +13,8 @@ class Migration(migrations.Migration): dependencies = [ ('courses', '0001_initial'), - ('achievements', '0001_initial'), migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('achievements', '0001_initial'), ] operations = [ diff --git a/courses/migrations/0001_initial.py b/courses/migrations/0001_initial.py index 6f88e5b..69ec748 100644 --- a/courses/migrations/0001_initial.py +++ b/courses/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals from django.db import migrations, models diff --git a/courses/migrations/0002_auto_20171212_2307.py b/courses/migrations/0002_auto_20171213_1230.py similarity index 95% rename from courses/migrations/0002_auto_20171212_2307.py rename to courses/migrations/0002_auto_20171213_1230.py index 3eea03f..bd82c9e 100644 --- a/courses/migrations/0002_auto_20171212_2307.py +++ b/courses/migrations/0002_auto_20171213_1230.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals from django.conf import settings @@ -12,8 +12,8 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('courses', '0001_initial'), ('maps', '0001_initial'), + ('courses', '0001_initial'), migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] diff --git a/finance/migrations/0001_initial.py b/finance/migrations/0001_initial.py index 3e1cb53..5ef26b6 100644 --- a/finance/migrations/0001_initial.py +++ b/finance/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals from django.conf import settings @@ -12,7 +12,7 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('courses', '0002_auto_20171212_2307'), + ('courses', '0002_auto_20171213_1230'), migrations.swappable_dependency(settings.AUTH_USER_MODEL), ('yandex_money', '0002_auto_20171128_1150'), ] diff --git a/library/migrations/0001_initial.py b/library/migrations/0001_initial.py index e5e02b8..c57fc97 100644 --- a/library/migrations/0001_initial.py +++ b/library/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals import datetime diff --git a/maps/migrations/0001_initial.py b/maps/migrations/0001_initial.py index baa45fb..67a3360 100644 --- a/maps/migrations/0001_initial.py +++ b/maps/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals from django.db import migrations, models @@ -73,6 +73,6 @@ class Migration(migrations.Migration): ), migrations.AlterUniqueTogether( name='pivotcoursemap', - unique_together=set([('map_course', 'route'), ('sort', 'route')]), + unique_together=set([('sort', 'route'), ('map_course', 'route')]), ), ] diff --git a/storage/api.py b/storage/api.py new file mode 100644 index 0000000..81ea7fa --- /dev/null +++ b/storage/api.py @@ -0,0 +1,55 @@ +import random +import string + +from storage.models import Comment, File + + +def upload_file(original=None, name=None, base64=None) -> File: + key = ''.join(random.choice(string.ascii_letters) for _x in range(15)) + if original: + new_file = File.objects.create(key=key, original=original) + else: + new_file = File.objects.upload_as_base64(base64) + + if name: + new_file.name = name + new_file.save() + return new_file + + +def add_comment(text: str, email: str, files=[]) -> Comment: + """ + :param text: sting + :param email: string + :param files: {name?: string, original?: File, base64?: string}[] одно из двух последних свойств должно быть указано + :return: Comment + """ + key = ''.join(random.choice(string.ascii_letters) for _x in range(15)) + comment = Comment.objects.create( + text=text, + email=email, + key=key, + ) + + for file in files: + new_file = upload_file(**file) + comment.files.add(new_file) + + return comment + + +def get_comment(key): + comment = Comment.objects.get(key=key) + return comment + + +def update_comment(key, **kwargs): + comment = Comment.objects.get(key=key) + comment.__dict__.update(kwargs) + comment.save() + return comment + + +def delete_comment(key): + comment = Comment.objects.get(key=key).delete() + return comment diff --git a/storage/migrations/0001_initial.py b/storage/migrations/0001_initial.py index 93b6f87..c24c80f 100644 --- a/storage/migrations/0001_initial.py +++ b/storage/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.6 on 2017-12-12 23:07 +# Generated by Django 1.11.6 on 2017-12-13 12:30 from __future__ import unicode_literals from django.db import migrations, models @@ -30,6 +30,7 @@ class Migration(migrations.Migration): name='File', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('key', models.CharField(max_length=15, unique=True, verbose_name='Внешний ключ')), ('original', models.FileField(max_length=255, upload_to='files', verbose_name='Файл')), ('name', models.CharField(blank=True, max_length=255, null=True, verbose_name='Видимое имя файла')), ], diff --git a/storage/models.py b/storage/models.py index 140ed99..b03aff1 100755 --- a/storage/models.py +++ b/storage/models.py @@ -1,11 +1,31 @@ # encoding=utf-8 +import base64 +import random +import string + +from django.core.files.base import ContentFile from django.db import models +class FileManager(models.Manager): + + def upload_as_base64(self, file_base64): + key = ''.join(random.choice(string.ascii_letters) for _x in range(15)) + if "data:" in file_base64: + my_str = file_base64[file_base64.index("base64,") + 7:] + ext = my_str.split('/')[-1] + file = self.create(key, original=ContentFile(base64.b64decode(my_str), name='time.' + ext)) + return file + raise ValueError() + + class File(models.Model): + key = models.CharField(max_length=15, verbose_name="Внешний ключ", unique=True) original = models.FileField(max_length=255, verbose_name='Файл', upload_to="files") name = models.CharField(max_length=255, null=True, blank=True, verbose_name='Видимое имя файла') + objects = FileManager() + def __str__(self): return '%s' % self.original diff --git a/storage/tests.py b/storage/tests.py index 5883796..6b886ea 100644 --- a/storage/tests.py +++ b/storage/tests.py @@ -1,7 +1,9 @@ from django.test import TestCase -from storage.views import add_comment, delete_comment, update_comment, get_comment +from storage.api import add_comment, delete_comment, update_comment, get_comment from django.core.files.uploadedfile import SimpleUploadedFile +from storage.models import Comment + class CommentTestCase(TestCase): def setUp(self): @@ -26,3 +28,12 @@ class CommentTestCase(TestCase): comment2 = add_comment(text=token, email="artem4000@gmail.com", files=[object_for_upload]) self.assertEqual(comment2.files.count(), 1) self.assertEqual(comment2.files.all()[0].name, file_name) + + def test_comment_delete(self): + delete_comment(self.first_comment.key) + try: + comment = get_comment(self.first_comment.id) + except Comment.DoesNotExist: + comment = None + + self.assertIsNone(comment) diff --git a/storage/views.py b/storage/views.py deleted file mode 100644 index 027fe46..0000000 --- a/storage/views.py +++ /dev/null @@ -1,47 +0,0 @@ -import random -import string - -from storage.models import Comment, File - - -def add_comment(text: str, email: str, files=None) -> Comment: - """ - :param text: sting - :param email: string - :param files: {name?: string, file?: File, base64?: string}[] одно из двух последних свойств должно быть указано - :return: Comment - """ - key = ''.join(random.choice(string.ascii_letters) for _x in range(15)) - comment = Comment.objects.create( - text=text, - email=email, - key=key, - ) - - if files: - for file in files: - new_file = File.objects.create(original=file['original']) - if 'name' in file.keys(): - new_file.name = file['name'] - new_file.save() - - comment.files.add(new_file) - - return comment - - -def get_comment(key): - comment = Comment.objects.get(key=key) - return comment - - -def update_comment(key, **kwargs): - comment = Comment.objects.get(key=key) - comment.__dict__.update(kwargs) - comment.save() - return comment - - -def delete_comment(key): - comment = Comment.objects.get(key=key).delete() - return comment