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