<s> import json <EOL> import imghdr <EOL> from django . shortcuts import render , get_object_or_404 <EOL> from django . contrib import messages <EOL> from django . utils . six import text_type <EOL> from django . http import HttpResponse <EOL> from sendfile import sendfile <EOL> from waliki . models import Page <EOL> from waliki . acl import permission_required <EOL> from . models import Attachment <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> def attachments ( request , slug ) : <EOL> last_attached = None <EOL> page = get_object_or_404 ( Page , slug = slug ) <EOL> if request . method == '<STR_LIT:POST>' and '<STR_LIT>' in request . FILES : <EOL> last_attached = request . FILES [ '<STR_LIT>' ] <EOL> Attachment . objects . create ( file = last_attached , page = page ) <EOL> messages . success ( request , '<STR_LIT>' % ( last_attached . name , page . slug ) ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : page } ) <EOL> @ permission_required ( '<STR_LIT>' ) <EOL> def delete_attachment ( request , slug , attachment_id_or_filename ) : <EOL> if attachment_id_or_filename . isnumeric ( ) : <EOL> attachment = get_object_or_404 ( Attachment , id = attachment_id_or_filename , page__slug = slug ) <EOL> else : <EOL> attachment = get_object_or_404 ( Attachment , file__endswith = attachment_id_or_filename , page__slug = slug ) <EOL> name = text_type ( attachment ) <EOL> if request . is_ajax ( ) and request . method in ( '<STR_LIT:POST>' , '<STR_LIT>' ) : <EOL> attachment . delete ( ) <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT>' : name } ) , content_type = "<STR_LIT:application/json>" ) <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT>' : None } ) , content_type = "<STR_LIT:application/json>" ) <EOL> @ permission_required ( '<STR_LIT>' , raise_exception = True ) <EOL> def get_file ( request , slug , attachment_id = None , filename = None ) : <EOL> attachment = get_object_or_404 ( Attachment , file__endswith = filename , page__slug = slug ) <EOL> as_attachment = ( ( not imghdr . what ( attachment . file . path ) and '<STR_LIT>' not in request . GET ) <EOL> or '<STR_LIT>' in request . GET ) <EOL> return sendfile ( request , attachment . file . path , <EOL> attachment = as_attachment , attachment_filename = text_type ( attachment ) ) </s>
