mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Fixes #2665. Regenerated by tabbott with `lint --fix` after a rebase and change in parameters. Note from tabbott: In a few cases, this converts technical debt in the form of unsorted imports into different technical debt in the form of our largest files having very long, ugly import sequences at the start. I expect this change will increase pressure for us to split those files, which isn't a bad thing. Signed-off-by: Anders Kaseorg <anders@zulip.com>
		
			
				
	
	
		
			31 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			31 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from typing import Any, Dict, List
 | 
						|
 | 
						|
from django.utils.translation import ugettext as _
 | 
						|
 | 
						|
from zerver.lib.request import JsonableError
 | 
						|
from zerver.lib.upload import delete_message_image
 | 
						|
from zerver.models import Attachment, UserProfile
 | 
						|
 | 
						|
 | 
						|
def user_attachments(user_profile: UserProfile) -> List[Dict[str, Any]]:
 | 
						|
    attachments = Attachment.objects.filter(owner=user_profile).prefetch_related('messages')
 | 
						|
    return [a.to_dict() for a in attachments]
 | 
						|
 | 
						|
def access_attachment_by_id(user_profile: UserProfile, attachment_id: int,
 | 
						|
                            needs_owner: bool=False) -> Attachment:
 | 
						|
    query = Attachment.objects.filter(id=attachment_id)
 | 
						|
    if needs_owner:
 | 
						|
        query = query.filter(owner=user_profile)
 | 
						|
 | 
						|
    attachment = query.first()
 | 
						|
    if attachment is None:
 | 
						|
        raise JsonableError(_("Invalid attachment"))
 | 
						|
    return attachment
 | 
						|
 | 
						|
def remove_attachment(user_profile: UserProfile, attachment: Attachment) -> None:
 | 
						|
    try:
 | 
						|
        delete_message_image(attachment.path_id)
 | 
						|
    except Exception:
 | 
						|
        raise JsonableError(_("An error occurred while deleting the attachment. Please try again later."))
 | 
						|
    attachment.delete()
 |