<s> import datetime <EOL> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , Http404 <EOL> from django . shortcuts import render_to_response , get_object_or_404 <EOL> from django . template import RequestContext <EOL> from django . utils . translation import ugettext , ugettext_lazy as _ <EOL> from django . contrib import messages <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . auth . decorators import login_required <EOL> from blog . models import Post <EOL> from blog . forms import * <EOL> if "<STR_LIT>" in settings . INSTALLED_APPS : <EOL> from notification import models as notification <EOL> else : <EOL> notification = None <EOL> try : <EOL> from friends . models import Friendship <EOL> friends = True <EOL> except ImportError : <EOL> friends = False <EOL> def blogs ( request , username = None , template_name = "<STR_LIT>" ) : <EOL> blogs = Post . objects . filter ( status = <NUM_LIT:2> ) . select_related ( '<STR_LIT>' ) . order_by ( "<STR_LIT>" ) <EOL> if username is not None : <EOL> user = get_object_or_404 ( User , username = username . lower ( ) ) <EOL> blogs = blogs . filter ( author = user ) <EOL> return render_to_response ( template_name , { <EOL> "<STR_LIT>" : blogs , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> def post ( request , username , slug , <EOL> template_name = "<STR_LIT>" ) : <EOL> post = Post . objects . filter ( <EOL> slug = slug , <EOL> author__username = username <EOL> ) <EOL> if not post : <EOL> raise Http404 <EOL> if post [ <NUM_LIT:0> ] . status == <NUM_LIT:1> and post [ <NUM_LIT:0> ] . author != request . user : <EOL> raise Http404 <EOL> return render_to_response ( template_name , { <EOL> "<STR_LIT>" : post [ <NUM_LIT:0> ] , <EOL> "<STR_LIT:url>" : request . build_absolute_uri ( post [ <NUM_LIT:0> ] . get_absolute_url ( ) ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ login_required <EOL> def your_posts ( request , template_name = "<STR_LIT>" ) : <EOL> return render_to_response ( template_name , { <EOL> "<STR_LIT>" : Post . objects . filter ( author = request . user ) , <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ login_required <EOL> def destroy ( request , id ) : <EOL> post = Post . objects . get ( pk = id ) <EOL> user = request . user <EOL> title = post . title <EOL> if post . author != request . user : <EOL> messages . add_message ( request , messages . ERROR , <EOL> ugettext ( "<STR_LIT>" ) <EOL> ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> if request . method == "<STR_LIT:POST>" and request . POST [ "<STR_LIT:action>" ] == "<STR_LIT>" : <EOL> post . delete ( ) <EOL> messages . add_message ( request , messages . SUCCESS , <EOL> ugettext ( "<STR_LIT>" ) % title <EOL> ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> return render_to_response ( context_instance = RequestContext ( request ) ) <EOL> @ login_required <EOL> def new ( request , form_class = BlogForm , template_name = "<STR_LIT>" ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> if request . POST [ "<STR_LIT:action>" ] == "<STR_LIT>" : <EOL> blog_form = form_class ( request . user , request . POST ) <EOL> if blog_form . is_valid ( ) : <EOL> blog = blog_form . save ( commit = False ) <EOL> blog . author = request . user <EOL> if getattr ( settings , '<STR_LIT>' , False ) : <EOL> blog . creator_ip = request . META [ "<STR_LIT>" ] <EOL> else : <EOL> blog . creator_ip = request . META [ '<STR_LIT>' ] <EOL> blog . save ( ) <EOL> messages . add_message ( request , messages . SUCCESS , <EOL> ugettext ( "<STR_LIT>" ) % blog . title <EOL> ) <EOL> if notification : <EOL> if blog . status == <NUM_LIT:2> : <EOL> if friends : <EOL> notification . send ( ( x [ '<STR_LIT>' ] for x in Friendship . objects . friends_for_user ( blog . author ) ) , "<STR_LIT>" , { "<STR_LIT>" : blog } ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> blog_form = form_class ( ) <EOL> else : <EOL> blog_form = form_class ( ) <EOL> return render_to_response ( template_name , { <EOL> "<STR_LIT>" : blog_form <EOL> } , context_instance = RequestContext ( request ) ) <EOL> @ login_required <EOL> def edit ( request , id , form_class = BlogForm , template_name = "<STR_LIT>" ) : <EOL> post = get_object_or_404 ( Post , id = id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> if post . author != request . user : <EOL> messages . add_message ( request , messages . ERROR , <EOL> ugettext ( "<STR_LIT>" ) <EOL> ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> if request . POST [ "<STR_LIT:action>" ] == "<STR_LIT>" : <EOL> blog_form = form_class ( request . user , request . POST , instance = post ) <EOL> if blog_form . is_valid ( ) : <EOL> blog = blog_form . save ( commit = False ) <EOL> blog . save ( ) <EOL> messages . add_message ( request , messages . SUCCESS , <EOL> ugettext ( "<STR_LIT>" ) % blog . title <EOL> ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" ) ) <EOL> else : <EOL> blog_form = form_class ( instance = post ) <EOL> else : <EOL> blog_form = form_class ( instance = post ) <EOL> return render_to_response ( template_name , { <EOL> "<STR_LIT>" : blog_form , <EOL> "<STR_LIT>" : post , <EOL> } , context_instance = RequestContext ( request ) ) </s>
