<s> from django . contrib import messages <EOL> from django . contrib . auth . decorators import login_required , user_passes_test <EOL> from django . core . mail import mail_admins <EOL> from django . db import transaction <EOL> from django . db . models import Q <EOL> from django . http import Http404 <EOL> from django . shortcuts import get_object_or_404 , redirect , render <EOL> from django . template . loader import render_to_string <EOL> from django . views . decorators . http import require_POST <EOL> from twobuntu . articles . forms import DeleteArticleForm , EditorForm , ScheduledArticleForm <EOL> from twobuntu . articles . models import Article , ScheduledArticle <EOL> from twobuntu . decorators import canonical <EOL> @ canonical ( Article ) <EOL> def view ( request , article ) : <EOL> """<STR_LIT>""" <EOL> if not article . can_view ( request ) : <EOL> raise Http404 <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : article , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:title>' : article . category , <EOL> '<STR_LIT:url>' : article . category . get_absolute_url ( ) , <EOL> } , <EOL> '<STR_LIT>' : article , <EOL> '<STR_LIT>' : article . render ( ) , <EOL> '<STR_LIT>' : article . can_edit ( request ) , <EOL> '<STR_LIT>' : ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) , <EOL> } ) <EOL> def search ( request ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT:q>' in request . GET : <EOL> raise Http404 <EOL> q = request . GET [ '<STR_LIT:q>' ] <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' % q , <EOL> '<STR_LIT>' : Article . apply_filter ( request , Q ( title__icontains = q ) | Q ( body__icontains = q ) ) . select_related ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } ) <EOL> @ login_required <EOL> def editor ( request , id ) : <EOL> """<STR_LIT>""" <EOL> article = get_object_or_404 ( Article , pk = id ) if id else None <EOL> if article and not article . can_edit ( request ) : <EOL> raise Http404 <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = EditorForm ( instance = article , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> creating = article is None <EOL> article = form . save ( commit = False ) <EOL> if creating : <EOL> article . author = request . user <EOL> article . save ( ) <EOL> messages . info ( request , "<STR_LIT>" if creating else "<STR_LIT>" ) <EOL> if '<STR_LIT:action>' in request . POST and request . POST [ '<STR_LIT:action>' ] == '<STR_LIT>' : <EOL> return redirect ( '<STR_LIT>' , article . id ) <EOL> else : <EOL> return redirect ( article ) <EOL> else : <EOL> form = EditorForm ( instance = article ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' % article if article else '<STR_LIT>' , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:description>' : "<STR_LIT>" % ( '<STR_LIT>' if article else '<STR_LIT>' ) , <EOL> } ) <EOL> def markdown ( request ) : <EOL> """<STR_LIT>""" <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> } ) <EOL> @ require_POST <EOL> @ login_required <EOL> @ transaction . atomic <EOL> def modify ( request , action , id ) : <EOL> """<STR_LIT>""" <EOL> article = get_object_or_404 ( Article , pk = id ) <EOL> if action == '<STR_LIT>' : <EOL> if article . author != request . user or article . status != Article . DRAFT : <EOL> raise Http404 <EOL> article . status = Article . UNAPPROVED <EOL> article . save ( ) <EOL> template = render_to_string ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : request . build_absolute_uri ( article . get_absolute_url ( ) ) , <EOL> } ) <EOL> mail_admins ( '<STR_LIT>' , template ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> elif action == '<STR_LIT>' : <EOL> if not request . user . is_staff : <EOL> raise Http404 <EOL> article . status = Article . PUBLISHED <EOL> article . save ( ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> elif action == '<STR_LIT>' : <EOL> if article . author != request . user or article . cc_license : <EOL> raise Http404 <EOL> article . cc_license = True <EOL> article . save ( ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> return redirect ( article ) <EOL> @ user_passes_test ( lambda u : u . is_staff ) <EOL> def schedule ( request , id ) : <EOL> """<STR_LIT>""" <EOL> article = get_object_or_404 ( Article , ~ Q ( status = Article . PUBLISHED ) , pk = id ) <EOL> try : <EOL> scheduled_article = article . scheduledarticle <EOL> except ScheduledArticle . DoesNotExist : <EOL> scheduled_article = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = ScheduledArticleForm ( instance = scheduled_article , data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> scheduled_article = form . save ( commit = False ) <EOL> scheduled_article . article = article <EOL> scheduled_article . save ( ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> return redirect ( article ) <EOL> else : <EOL> form = ScheduledArticleForm ( instance = scheduled_article ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' % article , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:title>' : article , <EOL> '<STR_LIT:url>' : article . get_absolute_url ( ) , <EOL> } , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> } ) <EOL> @ login_required <EOL> def delete ( request , id ) : <EOL> """<STR_LIT>""" <EOL> article = get_object_or_404 ( Article , pk = id , author = request . user , status = Article . DRAFT ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = DeleteArticleForm ( data = request . POST ) <EOL> if form . is_valid ( ) : <EOL> article . delete ( ) <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> return redirect ( request . user . profile ) <EOL> else : <EOL> form = DeleteArticleForm ( ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT:title>' : '<STR_LIT>' % article , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:title>' : article , <EOL> '<STR_LIT:url>' : article . get_absolute_url ( ) , <EOL> } , <EOL> '<STR_LIT:description>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT:action>' : '<STR_LIT>' , <EOL> } ) </s>
