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=None) -> Comment: """ :param text: sting :param email: string :param files: {name?: string, original?: File, base64?: string}[] одно из двух последних свойств должно быть указано :return: Comment """ files = [] if files is None else files 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