def _boa_note_to_compatible_json(note, note_read): return { **note_to_compatible_json( note=note.__dict__, note_read=note_read, attachments=[a.to_api_json() for a in note.attachments if not a.deleted_at], topics=[t.topic for t in note.topics if not t.deleted_at], ), **{ 'message': note.body, 'type': 'note', }, }
def _boa_note_to_compatible_json(note, note_read): note_json = note.to_api_json() return { **note_to_compatible_json( note=note_json, note_read=note_read, attachments=note_json.get('attachments'), topics=note_json.get('topics'), ), **{ 'message': note.body, 'type': 'note', }, }
def remove_attachment(note_id, attachment_id): existing_note = Note.find_by_id(note_id=note_id) if not existing_note: raise BadRequestError('Note id not found.') if existing_note.author_uid != current_user.get_uid() and not current_user.is_admin: raise ForbiddenRequestError('You are not authorized to remove attachments from this note.') note = Note.delete_attachment( note_id=note_id, attachment_id=int(attachment_id), ) note_json = note.to_api_json() return tolerant_jsonify( note_to_compatible_json( note=note_json, note_read=NoteRead.find_or_create(current_user.get_id(), note_id), attachments=note_json.get('attachments'), topics=note_json.get('topics'), ), )
def add_attachment(note_id): if Note.find_by_id(note_id=note_id).author_uid != current_user.get_uid(): raise ForbiddenRequestError('Sorry, you are not the author of this note.') attachments = _get_attachments(request.files) if len(attachments) != 1: raise BadRequestError('A single attachment file must be supplied.') note = Note.add_attachment( note_id=note_id, attachment=attachments[0], ) note_json = note.to_api_json() return tolerant_jsonify( note_to_compatible_json( note=note_json, note_read=NoteRead.find_or_create(current_user.get_id(), note_id), attachments=note_json.get('attachments'), topics=note_json.get('topics'), ), )