2012-10-20 19:05:29 +02:00
# -*- coding: utf-8 -*-
2012-07-22 10:47:24 +02:00
import os
2012-07-08 16:23:39 +02:00
from datetime import datetime , date , time
from django . shortcuts import render
from django . contrib . auth import authenticate , login , logout
2012-09-26 21:13:11 +02:00
from django . http import HttpResponseRedirect , HttpResponse , Http404
2012-07-08 16:23:39 +02:00
from django . contrib . auth . decorators import login_required
2012-10-04 21:49:33 +02:00
from django . views . decorators . csrf import csrf_exempt
2012-07-08 16:23:39 +02:00
from django . forms . models import inlineformset_factory
2012-10-20 19:05:29 +02:00
from django . core import mail
from django . core . mail import EmailMultiAlternatives
2012-07-08 16:23:39 +02:00
from dynastie . models import *
from dynastie . forms import *
2012-10-04 21:49:33 +02:00
from django . template . defaultfilters import register
from django . template import Variable , VariableDoesNotExist
@register.filter
def hash ( object , attr ) :
2012-10-11 22:26:47 +02:00
2012-10-04 21:49:33 +02:00
pseudo_context = { ' object ' : object }
try :
value = Variable ( ' object. %s ' % attr ) . resolve ( pseudo_context )
except VariableDoesNotExist :
value = None
return value
2012-10-09 20:47:12 +02:00
def have_I_right ( request , blog_id = None , post_id = None , must_be_superuser = False ) :
2012-10-04 21:49:33 +02:00
b = None
p = None
2012-10-09 20:47:12 +02:00
if must_be_superuser and not request . user . is_superuser :
raise Http404
2012-10-04 21:49:33 +02:00
if not post_id is None :
2012-10-11 22:26:47 +02:00
p = Post . objects . get ( pk = post_id )
2012-10-04 21:49:33 +02:00
if p is None :
raise Http404
blog_id = p . blog . id
if not blog_id is None :
if not request . user . is_superuser :
2012-10-09 20:47:12 +02:00
b = Blog . objects . filter ( pk = blog_id ) . filter ( writers = request . user . id )
if len ( b ) == 0 :
raise Http404
b = b [ 0 ]
2012-10-04 21:49:33 +02:00
else :
b = Blog . objects . get ( pk = post . blog . id )
if b is None :
raise Http404
return ( b , p )
2012-07-08 16:23:39 +02:00
def index ( request ) :
if request . user . is_authenticated ( ) :
return HttpResponseRedirect ( ' /blog ' )
login_failed = False
if ' login ' in request . POST :
user = authenticate ( username = request . POST [ ' login ' ] , password = request . POST [ ' password ' ] )
if user is None :
login_failed = True
else :
login ( request , user )
2012-10-04 21:49:33 +02:00
if ' next ' in request . GET :
return HttpResponseRedirect ( request . GET [ ' next ' ] )
else :
return HttpResponseRedirect ( ' /blog ' )
2012-07-08 16:23:39 +02:00
c = { ' auth_key ' : ' AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ' , \
' login_failed ' : login_failed }
2012-07-22 10:47:24 +02:00
return render ( request , ' templates/login.html ' , c )
2012-07-08 16:23:39 +02:00
def disconnect ( request ) :
logout ( request )
c = { ' auth_key ' : ' AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ' , \
' login_failed ' : False }
2012-09-25 21:49:56 +02:00
return HttpResponseRedirect ( ' / ' )
2012-07-08 16:23:39 +02:00
@login_required
def user ( request ) :
users = User . objects . all ( )
c = { ' users ' : users }
2012-07-22 10:47:24 +02:00
return render ( request , ' templates/user.html ' , c )
2012-07-08 16:23:39 +02:00
@login_required
def add_user ( request ) :
if not request . user . is_superuser :
return HttpResponseRedirect ( ' /user ' )
if request . method == ' POST ' : # If the form has been submitted...
if ' add ' in request . POST :
form = UserForm ( request . POST ) # A form bound to the POST data
if form . is_valid ( ) : # All validation rules pass
form = form . save ( )
user = User . objects . get ( pk = form . id )
user . set_password ( request . POST [ ' password ' ] )
user . save ( )
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect ( ' /user ' ) # Redirect after POST
else :
return HttpResponseRedirect ( ' /user ' ) # Redirect after POST
else :
form = UserForm ( ) # An unbound form
return render ( request , ' add_user.html ' , {
' form ' : form ,
} )
@login_required
def edit_user ( request , user_id ) :
user = User . objects . get ( pk = user_id )
if user is None :
raise Http404
2012-07-22 10:47:24 +02:00
edited = False
2012-07-08 16:23:39 +02:00
if request . method == ' POST ' : # If the form has been submitted...
2012-07-22 10:47:24 +02:00
if int ( user_id ) != int ( request . user . id ) and ( not request . user . is_superuser ) :
return HttpResponseRedirect ( ' /user/ ' + str ( user_id ) )
2012-07-08 16:23:39 +02:00
if ' edit ' in request . POST :
2012-07-22 10:47:24 +02:00
form = UserForm ( request . POST , instance = user , initial = { ' password ' : ' ' } ) # A form bound to the POST data
2012-07-08 16:23:39 +02:00
if form . is_valid ( ) : # All validation rules pass
form . save ( )
2012-07-22 10:47:24 +02:00
if request . POST [ ' password ' ] != ' ' :
user . set_password ( request . POST [ ' password ' ] )
2012-07-08 16:23:39 +02:00
user . save ( )
2012-07-22 10:47:24 +02:00
edited = True
2012-07-08 16:23:39 +02:00
else :
if ' delete ' in request . POST and request . user . is_superuser :
User . objects . get ( pk = user_id ) . delete ( )
return HttpResponseRedirect ( ' /user ' )
if ' cancel ' in request . POST :
return HttpResponseRedirect ( ' /user ' )
else :
2012-07-22 10:47:24 +02:00
form = UserForm ( instance = user , initial = { ' password ' : ' ' } ) # An unbound form
c = { ' user_to_edit ' : user , ' form ' : form , ' edited ' : edited }
2012-07-08 16:23:39 +02:00
2012-07-22 10:47:24 +02:00
return render ( request , ' templates/edit_user.html ' , c )
2012-07-08 16:23:39 +02:00
@login_required
2012-10-09 20:47:12 +02:00
def category ( request , blog_id ) :
b = have_I_right ( request , blog_id )
categories = Category . objects . filter ( blog_id = blog_id )
2012-07-08 16:23:39 +02:00
c = { ' categories ' : categories }
2012-07-22 10:47:24 +02:00
return render ( request , ' templates/category.html ' , c )
2012-07-08 16:23:39 +02:00
@login_required
2012-10-09 20:47:12 +02:00
def add_category ( request , blog_id ) :
b = have_I_right ( request , blog_id ) [ 0 ]
2012-07-08 16:23:39 +02:00
if request . method == ' POST ' : # If the form has been submitted...
if ' add ' in request . POST :
form = CategoryForm ( request . POST ) # A form bound to the POST data
2012-10-09 20:47:12 +02:00
form . blog = b
2012-07-08 16:23:39 +02:00
if form . is_valid ( ) : # All validation rules pass
form = form . save ( )
# Process the data in form.cleaned_data
# ...
2012-10-09 20:47:12 +02:00
return HttpResponseRedirect ( ' /category/ ' + str ( b . id ) )
2012-07-08 16:23:39 +02:00
else :
2012-10-09 20:47:12 +02:00
return HttpResponseRedirect ( ' /category/ ' + str ( b . id ) )
2012-07-08 16:23:39 +02:00
else :
form = CategoryForm ( ) # An unbound form
return render ( request , ' add_category.html ' , {
' form ' : form ,
} )
@login_required
def edit_category ( request , category_id ) :
category = Category . objects . get ( pk = category_id )
if category is None :
raise Http404
2012-10-09 20:47:12 +02:00
b = have_I_right ( request , category . blog . id ) [ 0 ]
2012-07-08 16:23:39 +02:00
if request . method == ' POST ' : # If the form has been submitted...
if ' cancel ' in request . POST :
2012-10-09 20:47:12 +02:00
return HttpResponseRedirect ( ' /category ' + str ( b . id ) )
if ' edit ' in request . POST :
2012-08-04 21:21:04 +02:00
name = category . name
2012-07-08 16:23:39 +02:00
form = CategoryForm ( request . POST , instance = category ) # A form bound to the POST data
if form . is_valid ( ) : # All validation rules pass
2012-09-08 12:35:52 +02:00
if request . POST [ ' name ' ] != name :
2012-08-04 21:21:04 +02:00
category . remove ( )
2012-07-08 16:23:39 +02:00
form . save ( )
2012-10-09 20:47:12 +02:00
return HttpResponseRedirect ( ' /category/ ' + str ( b . id ) )
2012-07-08 16:23:39 +02:00
else :
form = CategoryForm ( instance = category ) # An unbound form
c = { ' category ' : category , ' form ' : form }
2012-07-22 10:47:24 +02:00
return render ( request , ' templates/edit_category.html ' , c )
2012-07-08 16:23:39 +02:00
@login_required
def delete_category ( request , category_id ) :
2012-07-15 18:21:26 +02:00
category = Category . objects . get ( pk = category_id )
if category is None :
raise Http404
2012-10-09 20:47:12 +02:00
b = have_I_right ( request , category . blog . id ) [ 0 ]
2012-07-22 20:49:11 +02:00
category . remove ( )
2012-07-15 18:21:26 +02:00
category . delete ( )
2012-10-09 20:47:12 +02:00
return HttpResponseRedirect ( ' /category/ ' + str ( b . id ) )
2012-07-08 16:23:39 +02:00
@login_required
def blog ( request ) :
if request . user . is_superuser :
b = Blog . objects . all ( )
else :
b = Blog . objects . filter ( writers = request . user . id )
c = { ' blogs ' : b }
2012-07-22 10:47:24 +02:00
return render ( request , ' templates/blog.html ' , c )
2012-07-08 16:23:39 +02:00
@login_required
def add_blog ( request ) :
if not request . user . is_superuser :
return HttpResponseRedirect ( ' /blog ' )
if request . method == ' POST ' : # If the form has been submitted...
if ' add ' in request . POST :
form = BlogForm ( request . POST ) # A form bound to the POST data
if form . is_valid ( ) : # All validation rules pass
form = form . save ( )
2012-07-15 18:21:26 +02:00
form . create ( )
2012-07-08 20:41:16 +02:00
return HttpResponseRedirect ( ' /blog ' ) # Redirect after POST
2012-07-08 16:23:39 +02:00
else :
return HttpResponseRedirect ( ' /blog ' ) # Redirect after POST
else :
form = BlogForm ( ) # An unbound form
return render ( request , ' add_blog.html ' , {
' form ' : form ,
} )
@login_required
def view_blog ( request , blog_id ) :
if not request . user . is_superuser :
b = Blog . objects . filter ( id = blog_id ) . filter ( writers = request . user . id )
else :
b = Blog . objects . get ( pk = blog_id )
if b is None :
raise Http404
2012-08-28 09:09:14 +02:00
posts = Post . objects . filter ( blog = b ) . order_by ( ' -creation_date ' )
2012-07-08 20:41:16 +02:00
b = Blog . objects . get ( pk = blog_id )
2012-07-08 16:23:39 +02:00
form = BlogForm ( instance = b )
2012-07-08 20:41:16 +02:00
2012-10-04 21:49:33 +02:00
comments = Comment . objects . all ( )
dict_comments = { }
for comment in comments :
2012-10-11 22:26:47 +02:00
key = comment . post . id
if not key in dict_comments :
dict_comments [ key ] = 1
2012-10-04 21:49:33 +02:00
else :
2012-10-11 22:26:47 +02:00
dict_comments [ key ] = dict_comments [ key ] + 1
2012-10-04 21:49:33 +02:00
c = { ' blog ' : b , ' posts ' : posts , ' form ' : form , ' comments ' : dict_comments }
2012-07-08 16:23:39 +02:00
2012-07-22 10:47:24 +02:00
return render ( request , ' templates/view_blog.html ' , c )
2012-07-08 16:23:39 +02:00
@login_required
def edit_blog ( request , blog_id ) :
if not request . user . is_superuser :
return HttpResponseRedirect ( ' /blog/ ' + str ( blog_id ) )
b = Blog . objects . get ( pk = blog_id )
if b is None :
raise Http404
if request . method == ' POST ' : # If the form has been submitted...
if ' edit ' in request . POST :
form = BlogForm ( request . POST , instance = b ) # A form bound to the POST data
if form . is_valid ( ) : # All validation rules pass
form . save ( )
else :
if ' delete ' in request . POST :
2012-07-08 20:41:16 +02:00
b = Blog . objects . get ( pk = blog_id )
b . delete ( )
2012-07-08 16:23:39 +02:00
return HttpResponseRedirect ( ' /blog ' )
else :
form = BlogForm ( instance = b ) # An unbound form
2012-08-28 09:09:14 +02:00
posts = Post . objects . filter ( blog = b ) . order_by ( ' -creation_date ' )
2012-07-08 16:23:39 +02:00
2012-08-28 09:09:14 +02:00
c = { ' blog ' : b , ' posts ' : posts , ' form ' : form }
2012-07-08 16:23:39 +02:00
2012-07-22 10:47:24 +02:00
return render ( request , ' templates/view_blog.html ' , c )
2012-07-08 16:23:39 +02:00
@login_required
2012-08-28 09:09:14 +02:00
def add_post ( request , blog_id ) :
2012-10-09 20:47:12 +02:00
( b , ) = have_I_right ( request , blog_id )
2012-07-08 16:23:39 +02:00
if request . method == ' POST ' : # If the form has been submitted...
if ' add ' in request . POST :
2012-09-08 12:35:52 +02:00
post = Post ( blog = Blog . objects . get ( pk = blog_id ) , author = User . objects . get ( pk = request . user . id ) , creation_date = datetime . now ( ) , modification_date = datetime . now ( ) )
2012-07-22 10:47:24 +02:00
content = request . POST [ ' content ' ]
# del request.POST['content']
2012-08-28 09:09:14 +02:00
form = PostForm ( request . POST , instance = post ) # A form bound to the POST data
2012-07-08 16:23:39 +02:00
if form . is_valid ( ) : # All validation rules pass
2012-07-22 10:47:24 +02:00
form = form . save ( )
2012-08-28 09:09:14 +02:00
form . createPost ( content )
2012-07-08 16:23:39 +02:00
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect ( ' /blog/ ' + blog_id ) # Redirect after POST
else :
return HttpResponseRedirect ( ' /blog/ ' + blog_id ) # Redirect after POST
else :
2012-08-28 09:09:14 +02:00
form = PostForm ( ) # An unbound form
2012-07-08 16:23:39 +02:00
2012-08-28 09:09:14 +02:00
return render ( request , ' add_post.html ' , {
2012-07-08 16:23:39 +02:00
' form ' : form , ' blog_id ' : blog_id
} )
@login_required
2012-08-28 09:09:14 +02:00
def edit_post ( request , post_id ) :
2012-10-04 21:49:33 +02:00
( b , post ) = have_I_right ( request , None , post_id )
2012-08-28 09:09:14 +02:00
post = Post . objects . get ( pk = post_id )
2012-07-08 16:23:39 +02:00
2012-08-28 09:09:14 +02:00
title = post . title
2012-10-04 21:49:33 +02:00
blog_id = b . id
2012-07-08 16:23:39 +02:00
if request . method == ' POST ' : # If the form has been submitted...
if ' edit ' in request . POST :
2012-09-08 12:35:52 +02:00
post . modification_date = datetime . now ( )
2012-08-28 09:09:14 +02:00
form = PostForm ( request . POST , instance = post ) # A form bound to the POST data
2012-07-08 16:23:39 +02:00
if form . is_valid ( ) : # All validation rules pass
2012-09-08 12:35:52 +02:00
if title != request . POST [ ' title ' ] :
2012-08-28 09:09:14 +02:00
post . remove ( )
2012-07-08 16:23:39 +02:00
form . save ( )
2012-10-11 22:26:47 +02:00
post . createPost ( request . POST [ ' content ' ] )
2012-07-08 16:23:39 +02:00
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect ( ' /blog/ ' + str ( blog_id ) ) # Redirect after POST
else :
if ' cancel ' in request . POST :
return HttpResponseRedirect ( ' /blog/ ' + str ( blog_id ) ) # Redirect after POST
else :
2012-08-28 09:09:14 +02:00
form = PostForm ( instance = post ) # An unbound form
2012-07-08 16:23:39 +02:00
2012-07-22 10:47:24 +02:00
b . create_paths ( )
2012-08-28 09:09:14 +02:00
filename = b . src_path + ' /_post/ ' + str ( post . pk )
2012-07-22 10:47:24 +02:00
if os . path . exists ( filename ) :
f = open ( filename , ' rb ' )
content = f . read ( )
f . close ( )
else :
2012-08-28 09:09:14 +02:00
content = ' Empty post '
2012-07-22 10:47:24 +02:00
2012-10-04 21:49:33 +02:00
comments = Comment . objects . filter ( post = post ) . order_by ( ' date ' )
2012-10-20 19:05:29 +02:00
comment_list = [ ]
2012-10-04 21:49:33 +02:00
for comment in comments :
2012-10-20 19:05:29 +02:00
comment_list . append ( comment )
2012-10-04 21:49:33 +02:00
2012-08-28 09:09:14 +02:00
return render ( request , ' edit_post.html ' , {
2012-09-11 22:25:58 +02:00
' form ' : form , ' post_id ' : post_id , ' content ' : content ,
2012-10-20 19:05:29 +02:00
' blog_id ' : blog_id , ' comments ' : comment_list
2012-07-08 16:23:39 +02:00
} )
2012-07-15 18:21:26 +02:00
@login_required
2012-08-28 09:09:14 +02:00
def delete_post ( request , post_id ) :
2012-10-04 21:49:33 +02:00
( b , post ) = have_I_right ( request , None , post_id )
2012-07-15 18:21:26 +02:00
2012-10-04 21:49:33 +02:00
blog_id = b . id
2012-07-22 10:47:24 +02:00
2012-08-28 09:09:14 +02:00
post . delete ( )
2012-07-15 18:21:26 +02:00
2012-07-22 10:47:24 +02:00
return HttpResponseRedirect ( ' /blog/ ' + str ( blog_id ) )
2012-07-15 18:21:26 +02:00
@login_required
def generate ( request , blog_id ) :
2012-10-04 21:49:33 +02:00
( b , post ) = have_I_right ( request , blog_id )
2012-07-15 18:21:26 +02:00
b . create_paths ( )
2012-07-18 11:30:54 +02:00
report = b . generate ( )
2012-07-20 21:54:43 +02:00
2012-08-28 09:09:14 +02:00
posts = Post . objects . filter ( blog = b ) . order_by ( ' -creation_date ' )
2012-07-20 21:54:43 +02:00
b = Blog . objects . get ( pk = blog_id )
form = BlogForm ( instance = b )
2012-10-11 22:26:47 +02:00
comments = Comment . objects . all ( )
dict_comments = { }
2012-10-09 20:47:12 +02:00
for comment in comments :
2012-10-11 22:26:47 +02:00
key = comment . post . id
if not key in dict_comments :
dict_comments [ key ] = 1
else :
dict_comments [ key ] = dict_comments [ key ] + 1
2012-10-09 20:47:12 +02:00
2012-10-11 22:26:47 +02:00
c = { ' blog ' : b , ' posts ' : posts , ' form ' : form , ' report ' : report , ' comments ' : dict_comments }
2012-07-18 11:30:54 +02:00
2012-09-11 22:25:58 +02:00
return render ( request , ' templates/generate.html ' , c )
@login_required
def preview ( request , blog_id ) :
from dynastie . generators import post
values = { ' title ' : request . POST [ ' title ' ] , \
' author ' : request . user . first_name + ' ' + request . user . last_name , \
' content ' : request . POST [ ' content ' ]
}
2012-10-04 21:49:33 +02:00
( b , ) = have_I_right ( request , blog_id )
2012-09-11 22:25:58 +02:00
b . create_paths ( )
engine = globals ( ) [ ' post ' ]
for name , obj in inspect . getmembers ( engine ) :
2012-10-20 19:05:29 +02:00
if inspect . isclass ( obj ) and obj . __module__ . startswith ( " dynastie.generators " ) \
2012-09-11 22:25:58 +02:00
and obj . __module__ . endswith ( " post " ) :
e = obj ( )
content = e . preview ( b . src_path , values )
break
output = b . output_path
path = output + ' /preview.html '
if os . path . exists ( path ) :
os . unlink ( path )
f = open ( path , ' wb ' )
f . write ( content )
f . close ( )
c = { ' content ' : content }
2012-09-11 22:27:33 +02:00
return HttpResponseRedirect ( ' http:// ' + b . name + ' /preview.html ' )
# return HttpResponseRedirect('http://' + 'localhost:8080' + '/preview.html')
2012-09-26 21:13:11 +02:00
def _tinymcelist ( request , b , year , month ) :
ret = ' var tinyMCEImageList = new Array( '
b . create_paths ( )
if month < 10 :
suffix = ' /images/ ' + str ( year ) + ' /0 ' + str ( month )
else :
suffix = ' /images/ ' + str ( year ) + ' / ' + str ( month )
path = b . src_path + ' / ' + suffix
# url = 'http://' + 'localhost:8080' + '/' + suffix + '/'
url = ' http:// ' + b . name + ' / ' + suffix + ' / '
if os . path . exists ( path ) :
files = ' '
for p in os . listdir ( path ) :
files + = ' [ " ' + p + ' " , " ' + url + p + ' " ], '
# Remove last comma
if len ( files ) != 0 :
ret + = files [ : - 1 ]
ret + = ' ); '
return ret
@login_required
def tinymcelist_add ( request , blog_id ) :
now = datetime . now ( )
year = now . year
month = now . month
2012-10-04 21:49:33 +02:00
try :
( b , ) = have_I_right ( request , blog_id )
except Http404 :
2012-09-26 21:13:11 +02:00
return HttpResponse ( ' ' , content_type = ' application/x-javascript ' )
ret = _tinymcelist ( request , b , year , month )
return HttpResponse ( ret , content_type = ' application/x-javascript ' )
@login_required
def tinymcelist_edit ( request , post_id ) :
2012-10-04 21:49:33 +02:00
try :
( b , post ) = have_I_right ( request , None , post_id )
except Http404 :
2012-09-26 21:13:11 +02:00
return HttpResponse ( ' ' , content_type = ' application/x-javascript ' )
2012-10-04 21:49:33 +02:00
2012-09-26 21:13:11 +02:00
year = post . creation_date . year ( )
month = post . creation_date . month ( )
ret = _tinymcelist ( request , b , year , month )
return HttpResponse ( ret , content_type = ' application/x-javascript ' )
2012-10-04 21:49:33 +02:00
@csrf_exempt
def add_comment ( request , post_id , parent_id ) :
from dynastie . generators import post
ref = request . META [ ' HTTP_REFERER ' ]
2012-10-11 22:26:47 +02:00
post = Post . objects . get ( pk = post_id )
2012-10-04 21:49:33 +02:00
if post is None :
2012-10-11 22:26:47 +02:00
print ' no post '
2012-10-04 21:49:33 +02:00
return HttpResponseRedirect ( ref )
2012-10-11 22:26:47 +02:00
blog = Blog . objects . get ( pk = post . blog_id )
2012-10-04 21:49:33 +02:00
2012-10-11 22:26:47 +02:00
if blog is None :
print ' no blog ' + str ( post . blog . id )
return HttpResponseRedirect ( ref )
parent_id = int ( parent_id )
2012-10-04 21:49:33 +02:00
if parent_id != 0 :
2012-10-11 22:26:47 +02:00
parentComment = Comment . objects . get ( pk = parent_id )
2012-10-04 21:49:33 +02:00
if parentComment is None :
return HttpResponseRedirect ( ref )
else :
parentComment = None
if request . POST [ ' author ' ] == ' ' or request . POST [ ' the_comment ' ] == ' ' :
print ' Error on author or the_comment '
return HttpResponseRedirect ( ref )
2012-10-20 20:55:55 +02:00
# Behind a proxy
if ' X-Real-IP ' in request . META :
ip = request . META [ ' X-Real-IP ' ]
else :
ip = request . META [ ' REMOTE_ADDR ' ]
2012-10-04 21:49:33 +02:00
comment = Comment ( post = post , parent = parentComment , date = datetime . now ( ) , author = request . POST [ ' author ' ] , \
2012-10-20 20:55:55 +02:00
email = request . POST [ ' email ' ] , the_comment = request . POST [ ' the_comment ' ] , ip = ip )
2012-10-04 21:49:33 +02:00
comment . save ( )
engine = globals ( ) [ ' post ' ]
blog . create_paths ( )
2012-10-11 22:26:47 +02:00
post_list = [ post ]
2012-10-04 21:49:33 +02:00
for name , obj in inspect . getmembers ( engine ) :
2012-10-20 19:05:29 +02:00
if inspect . isclass ( obj ) and obj . __module__ . startswith ( " dynastie.generators " ) \
2012-10-04 21:49:33 +02:00
and obj . __module__ . endswith ( " post " ) :
e = obj ( )
2012-10-11 22:26:47 +02:00
content = e . _generate ( blog , blog . src_path , blog . output_path , post_list )
2012-10-04 21:49:33 +02:00
break
# Send emails
2012-10-20 19:05:29 +02:00
emails = { }
comments = Comment . objects . filter ( post = post ) . order_by ( ' date ' )
comment_index = str ( len ( comments ) )
for comment in comments :
email = comment . email
2012-10-20 20:19:01 +02:00
if email != ' ' and email != request . POST [ ' email ' ] and not email in emails :
2012-10-20 19:05:29 +02:00
emails [ email ] = comment . author
2012-10-20 20:55:55 +02:00
if post . author . email not in email :
emails [ post . author . email ] = post . author . first_name
2012-10-20 19:05:29 +02:00
if len ( emails ) > 0 :
connection = mail . get_connection ( fail_silently = True )
connection . open ( )
messages = [ ]
subject = ' [ %s ] Nouveau commentaire pour l \' article " %s " ' % ( blog . name , post . title )
for email , author in emails . items ( ) :
text_body = u ' Bonjour %s , \n \n Un nouveau commentaire a été posté pour l \' article " %s " . \n \n ' % ( author , post . title )
text_body + = u ' Pour le consulter, rendez vous sur http:// %s %s /#comment_ %s \n \n ---------------- \n \n ' % ( blog . name , post . getPath ( ) , comment_index )
text_body + = comment . the_comment
text_body + = ' \n '
html_body = u ' <html><head><meta http-equiv= " content-type " content= " text/html; charset=utf-8 " /></head><body> '
html_body + = u ' Bonjour %s ,<br/><br/>Un nouveau commentaire a été posté pour l \' article " %s " .<br/><br/> ' % ( author , post . title )
2012-10-20 20:19:01 +02:00
html_body = html_body + u ' Pour le consulter, rendez vous sur <a href= " http:// %s %s #comment_ %s " >http:// %s %s #comment_ %s </a><br/><br/>----------------<br/><pre> ' % ( blog . name , post . getPath ( ) , comment_index , blog . name , post . getPath ( ) , comment_index )
2012-10-20 19:05:29 +02:00
c = comment . the_comment
# Avoid script injection
c = c . replace ( ' <pre> ' , ' <pre> ' )
c = c . replace ( ' </pre> ' , ' </pre> ' )
html_body + = c + ' </pre> '
html_body + = ' </body></html> '
msg = EmailMultiAlternatives ( subject , text_body , ' no-reply@ %s ' % blog . name , [ email ] )
msg . attach_alternative ( html_body , " text/html " )
messages . append ( msg )
connection . send_messages ( messages )
connection . close ( )
response = HttpResponseRedirect ( ref )
2012-10-20 21:00:51 +02:00
response [ ' Cache-Control ' ] = ' no-cache '
2012-10-20 19:05:29 +02:00
response . set_cookie ( ' author ' , request . POST [ ' author ' ] , domain = blog . name , secure = True , httponly = False ) ;
if request . POST [ ' email ' ] != ' ' :
response . set_cookie ( ' email ' , request . POST [ ' email ' ] , domain = blog . name , secure = True , httponly = False ) ;
2012-10-04 21:49:33 +02:00
2012-10-20 19:05:29 +02:00
return response
2012-10-04 21:49:33 +02:00
@login_required
def edit_comment ( request , comment_id ) :
2012-10-11 22:26:47 +02:00
comment = Comment . objects . get ( pk = comment_id )
2012-10-04 21:49:33 +02:00
if comment is None :
return Http404
2012-10-11 22:26:47 +02:00
( b , post ) = have_I_right ( request , None , comment . post_id )
post_id = comment . post_id
2012-10-04 21:49:33 +02:00
if request . method == ' POST ' : # If the form has been submitted...
if ' edit ' in request . POST :
2012-10-11 22:26:47 +02:00
form = CommentForm ( request . POST , instance = comment ) # A form bound to the POST data
2012-10-04 21:49:33 +02:00
if form . is_valid ( ) : # All validation rules pass
form = form . save ( )
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect ( ' /post/edit/ ' + str ( post_id ) )
else :
return HttpResponseRedirect ( ' /post/edit/ ' + str ( post_id ) )
else :
2012-10-11 22:26:47 +02:00
form = CommentForm ( instance = comment ) # An unbound form
2012-10-04 21:49:33 +02:00
return render ( request , ' edit_comment.html ' , {
' form ' : form , ' comment ' : comment
} )
@login_required
def delete_comment ( request , comment_id ) :
2012-10-11 22:26:47 +02:00
comment = Comment . objects . get ( pk = comment_id )
2012-10-04 21:49:33 +02:00
if comment is None :
return Http404
2012-10-11 22:26:47 +02:00
( b , post ) = have_I_right ( request , None , comment . post_id )
post_id = comment . post_id
2012-10-16 22:06:33 +02:00
childs = Comment . objects . filter ( parent = comment . id )
try :
parent = comment . parent
except :
parent = None
2012-10-04 21:49:33 +02:00
for child in childs :
2012-10-16 22:06:33 +02:00
child . parent = parent
2012-10-04 21:49:33 +02:00
child . save ( )
comment . delete ( )
return HttpResponseRedirect ( ' /post/edit/ ' + str ( post_id ) )