parent
f41af94e52
commit
c1d79b5bb3
14 changed files with 105 additions and 65 deletions
@ -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,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…
Reference in new issue