feature/fix_generate_pass
Andrey 8 years ago
parent f41af94e52
commit c1d79b5bb3
  1. 2
      access/migrations/0001_initial.py
  2. 8
      access/migrations/0002_auto_20171213_1230.py
  3. 2
      achievements/migrations/0001_initial.py
  4. 4
      achievements/migrations/0002_auto_20171213_1230.py
  5. 2
      courses/migrations/0001_initial.py
  6. 4
      courses/migrations/0002_auto_20171213_1230.py
  7. 4
      finance/migrations/0001_initial.py
  8. 2
      library/migrations/0001_initial.py
  9. 4
      maps/migrations/0001_initial.py
  10. 55
      storage/api.py
  11. 3
      storage/migrations/0001_initial.py
  12. 20
      storage/models.py
  13. 13
      storage/tests.py
  14. 47
      storage/views.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

@ -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 = [

@ -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

@ -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 = [

@ -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

@ -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),
]

@ -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'),
]

@ -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

@ -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')]),
),
]

@ -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

@ -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='Видимое имя файла')),
],

@ -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

@ -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)

@ -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
Loading…
Cancel
Save