<s> from functools import wraps <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . contrib import messages <EOL> from django . core . urlresolvers import reverse <EOL> from django . http . response import HttpResponseForbidden , HttpResponseRedirect <EOL> from django . shortcuts import get_object_or_404 , redirect , render <EOL> from django . utils . translation import ugettext , ugettext_lazy as _ <EOL> from comments . utils import get_comments_context <EOL> from gallery . utils import get_gallery_context <EOL> from . forms import FruitForm , FruitDeleteForm <EOL> from . models import Fruit , Kind <EOL> def _get_fruit ( pk ) : <EOL> return get_object_or_404 ( Fruit . objects . select_related ( '<STR_LIT:user>' , '<STR_LIT>' ) , pk = pk ) <EOL> def detail ( request , fruit_id ) : <EOL> fruit = _get_fruit ( fruit_id ) <EOL> context = { <EOL> '<STR_LIT>' : Kind . objects . all ( ) , <EOL> '<STR_LIT>' : fruit , <EOL> } <EOL> context . update ( get_comments_context ( <EOL> request , <EOL> container = fruit , <EOL> with_complaints = True , <EOL> complaint_label = _ ( '<STR_LIT>' ) , <EOL> ) ) <EOL> context . update ( get_gallery_context ( <EOL> request , <EOL> container = fruit , <EOL> ) ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> def add ( request ) : <EOL> if not request . user . is_authenticated ( ) : <EOL> messages . warning ( request , ugettext ( '<STR_LIT>' ) ) <EOL> redir = '<STR_LIT>' . format ( reverse ( '<STR_LIT>' ) , reverse ( '<STR_LIT>' ) ) <EOL> return HttpResponseRedirect ( redir ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = FruitForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> data = dict ( user = request . user ) <EOL> data . update ( form . cleaned_data ) <EOL> fruit = Fruit ( ** data ) <EOL> fruit . save ( ) <EOL> messages . success ( request , ugettext ( '<STR_LIT>' ) ) <EOL> return redirect ( fruit ) <EOL> else : <EOL> form = FruitForm ( ) <EOL> context = { <EOL> '<STR_LIT>' : Kind . objects . all ( ) , <EOL> '<STR_LIT>' : form , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> def _editor ( func ) : <EOL> @ wraps ( func ) <EOL> def test ( request , fruit_id ) : <EOL> fruit = _get_fruit ( fruit_id ) <EOL> if not ( fruit . user . id == request . user . id ) : <EOL> return HttpResponseForbidden ( _ ( '<STR_LIT>' ) ) <EOL> if fruit . deleted : <EOL> return HttpResponseForbidden ( _ ( '<STR_LIT>' ) ) <EOL> return func ( request , fruit ) <EOL> return test <EOL> @ login_required <EOL> @ _editor <EOL> def edit ( request , fruit ) : <EOL> form = FruitForm ( request . POST or None , instance = fruit ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> messages . success ( request , ugettext ( '<STR_LIT>' ) ) <EOL> return redirect ( fruit ) <EOL> context = { <EOL> '<STR_LIT>' : Kind . objects . all ( ) , <EOL> '<STR_LIT>' : fruit , <EOL> '<STR_LIT>' : form , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ login_required <EOL> @ _editor <EOL> def delete ( request , fruit ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = FruitDeleteForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> fruit . deleted = True <EOL> fruit . why_deleted = form . cleaned_data [ '<STR_LIT>' ] <EOL> fruit . save ( ) <EOL> messages . success ( request , ugettext ( '<STR_LIT>' ) ) <EOL> return redirect ( fruit ) <EOL> else : <EOL> form = FruitDeleteForm ( ) <EOL> context = { <EOL> '<STR_LIT>' : form , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) </s>
