You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

53 lines
1.2 KiB

from storage.models import Comment, File
def upload_file(original=None, name=None, base64=None, **_kwargs) -> File:
if original:
new_file = File.objects.create(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, out_key: str, files=None) -> Comment:
"""
:param text: sting
:param out_key: string
:param files: {name?: string, original?: File, base64?: string}[] одно из двух последних свойств должно быть указано
:return: Comment
"""
files = [] if files is None else files
comment = Comment.objects.create(
text=text,
user_key=out_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(token=key)
return comment
def update_comment(key, **kwargs):
comment = Comment.objects.get(token=key)
comment.__dict__.update(kwargs)
comment.save()
return comment
def delete_comment(key):
comment = Comment.objects.get(token=key).delete()
return comment