<s> from __future__ import unicode_literals <EOL> from django . apps import apps <EOL> from django . contrib import messages <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import render_to_response , get_object_or_404 <EOL> from django . template . context import RequestContext <EOL> from django . utils . translation import ugettext <EOL> from django . views . decorators . http import require_POST <EOL> from . forms import AttachmentForm <EOL> from . models import Attachment <EOL> def add_url_for_obj ( obj ) : <EOL> return reverse ( '<STR_LIT>' , kwargs = { <EOL> '<STR_LIT>' : obj . _meta . app_label , <EOL> '<STR_LIT>' : obj . _meta . model_name , <EOL> '<STR_LIT>' : obj . pk <EOL> } ) <EOL> @ require_POST <EOL> @ login_required <EOL> def add_attachment ( request , app_label , model_name , pk , <EOL> template_name = '<STR_LIT>' , extra_context = { } ) : <EOL> next = request . POST . get ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> if not request . user . has_perm ( '<STR_LIT>' ) : <EOL> return HttpResponseRedirect ( next ) <EOL> model = apps . get_model ( app_label , model_name ) <EOL> obj = get_object_or_404 ( model , pk = pk ) <EOL> form = AttachmentForm ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> form . save ( request , obj ) <EOL> messages . success ( request , ugettext ( '<STR_LIT>' ) ) <EOL> return HttpResponseRedirect ( next ) <EOL> template_context = { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : add_url_for_obj ( obj ) , <EOL> '<STR_LIT>' : next , <EOL> } <EOL> template_context . update ( extra_context ) <EOL> return render_to_response ( template_name , template_context , <EOL> RequestContext ( request ) ) <EOL> @ login_required <EOL> def delete_attachment ( request , attachment_pk ) : <EOL> g = get_object_or_404 ( Attachment , pk = attachment_pk ) <EOL> if ( <EOL> ( request . user . has_perm ( '<STR_LIT>' ) and <EOL> request . user == g . creator ) <EOL> or <EOL> request . user . has_perm ( '<STR_LIT>' ) <EOL> ) : <EOL> g . delete ( ) <EOL> messages . success ( request , ugettext ( '<STR_LIT>' ) ) <EOL> next = request . GET . get ( '<STR_LIT>' ) or '<STR_LIT:/>' <EOL> return HttpResponseRedirect ( next ) </s>
