2012-10-20 19:05:29 +02:00
# -*- coding: utf-8 -*-
2013-02-07 18:50:54 +01:00
"""
Copyright 2012 - 2013 Grégory Soutadé
This file is part of Dynastie .
Dynastie is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
Dynastie is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with Dynastie . If not , see < http : / / www . gnu . org / licenses / > .
"""
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-11-25 20:39:19 +01:00
from dynastie . search import *
2012-07-08 16:23:39 +02:00
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-11-08 21:23:30 +01:00
b = Blog . objects . filter ( pk = blog_id , writers = request . user . id )
2012-10-09 20:47:12 +02:00
if len ( b ) == 0 :
raise Http404
b = b [ 0 ]
2012-10-04 21:49:33 +02:00
else :
2012-11-08 21:23:30 +01:00
b = Blog . objects . get ( pk = blog_id )
2012-10-04 21:49:33 +02:00
if b is None :
raise Http404
return ( b , p )
2012-11-17 09:46:33 +01:00
def createNavigationBar ( blog_id , cur_page , nb_pages ) :
2012-12-24 18:07:44 +01:00
if nb_pages == 0 : return ' '
2012-11-17 09:46:33 +01:00
navigation_bar = ' '
if cur_page == 0 :
navigation_bar + = ' << < '
else :
navigation_bar + = ' <a href= " /blog/ %d ?page=0 " ><<</a> ' % blog_id
navigation_bar + = ' <a href= " /blog/ %d ?page= %d " ><</a> ' % ( blog_id , cur_page - 1 )
for i in range ( nb_pages + 1 ) :
if i == cur_page :
navigation_bar + = ' %d ' % ( i + 1 )
else :
navigation_bar + = ' <a href= " /blog/ %d ?page= %d " > %d </a> ' % ( blog_id , i , i + 1 )
if cur_page == nb_pages :
navigation_bar + = ' > >> '
else :
2012-12-24 10:06:46 +01:00
navigation_bar + = ' <a href= " /blog/ %d ?page= %d " >></a> ' % ( blog_id , cur_page + 1 )
2012-11-17 09:46:33 +01:00
navigation_bar + = ' <a href= " /blog/ %d ?page= %d " >>></a> ' % ( blog_id , nb_pages )
return navigation_bar
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
2012-12-24 15:43:33 +01:00
c = { ' login_failed ' : login_failed }
2012-07-08 16:23:39 +02:00
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 )
2012-12-24 16:34:30 +01:00
c = { ' 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 ' )
2012-12-24 16:34:30 +01:00
if request . method == ' POST ' :
2012-07-08 16:23:39 +02:00
if ' add ' in request . POST :
2012-12-24 16:34:30 +01:00
form = UserForm ( request . POST )
if form . is_valid ( ) :
2012-07-08 16:23:39 +02:00
form = form . save ( )
user = User . objects . get ( pk = form . id )
user . set_password ( request . POST [ ' password ' ] )
user . save ( )
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /user ' )
2012-07-08 16:23:39 +02:00
else :
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /user ' )
2012-07-08 16:23:39 +02:00
else :
2012-12-24 16:34:30 +01:00
form = UserForm ( )
2012-07-08 16:23:39 +02:00
2012-12-24 16:34:30 +01:00
return render ( request , ' add_user.html ' , { ' form ' : form } )
2012-07-08 16:23:39 +02:00
@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-12-24 16:34:30 +01:00
if request . method == ' POST ' :
2012-07-22 10:47:24 +02:00
if int ( user_id ) != int ( request . user . id ) and ( not request . user . is_superuser ) :
2012-12-24 15:40:11 +01:00
return HttpResponseRedirect ( ' /user ' )
2012-07-08 16:23:39 +02:00
if ' edit ' in request . POST :
2012-12-24 16:34:30 +01:00
form = UserForm ( request . POST , instance = user , initial = { ' password ' : ' ' } )
if form . is_valid ( ) :
2012-07-08 16:23:39 +02:00
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-12-24 16:34:30 +01:00
form = UserForm ( instance = user , initial = { ' password ' : ' ' } )
2012-07-22 10:47:24 +02:00
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 ) :
2012-11-17 09:46:33 +01:00
b , _ = have_I_right ( request , blog_id )
2012-10-09 20:47:12 +02:00
categories = Category . objects . filter ( blog_id = blog_id )
2012-07-08 16:23:39 +02:00
2012-11-17 09:46:33 +01:00
c = { ' categories ' : categories , ' blog ' : b }
2012-07-08 16:23:39 +02:00
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 ) :
2012-11-17 09:46:33 +01:00
b , _ = have_I_right ( request , blog_id )
2013-01-29 18:51:23 +01:00
2012-12-24 16:34:30 +01:00
if request . method == ' POST ' :
2012-07-08 16:23:39 +02:00
if ' add ' in request . POST :
2012-12-24 16:34:30 +01:00
form = CategoryForm ( request . POST )
2012-10-09 20:47:12 +02:00
form . blog = b
2012-12-24 16:34:30 +01:00
if form . is_valid ( ) :
2012-07-08 16:23:39 +02:00
form = 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 :
2012-10-09 20:47:12 +02:00
return HttpResponseRedirect ( ' /category/ ' + str ( b . id ) )
2012-07-08 16:23:39 +02:00
else :
2012-12-24 16:34:30 +01:00
form = CategoryForm ( )
2012-07-08 16:23:39 +02:00
2012-12-24 16:34:30 +01:00
return render ( request , ' add_category.html ' , { ' form ' : form } )
2012-07-08 16:23:39 +02:00
@login_required
def edit_category ( request , category_id ) :
category = Category . objects . get ( pk = category_id )
if category is None :
raise Http404
2012-11-17 09:46:33 +01:00
b , _ = have_I_right ( request , category . blog . id )
2012-10-09 20:47:12 +02:00
2012-12-24 16:34:30 +01:00
if request . method == ' POST ' :
2012-07-08 16:23:39 +02:00
if ' cancel ' in request . POST :
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /category/ ' + str ( b . id ) )
2012-10-09 20:47:12 +02:00
if ' edit ' in request . POST :
2012-08-04 21:21:04 +02:00
name = category . name
2012-11-08 21:23:30 +01:00
name = name . strip ( )
2012-12-24 16:34:30 +01:00
form = CategoryForm ( request . POST , instance = category )
if form . is_valid ( ) :
2012-09-08 12:35:52 +02:00
if request . POST [ ' name ' ] != name :
2012-12-31 13:58:47 +01: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 :
2012-12-24 16:34:30 +01:00
form = CategoryForm ( instance = category )
2012-07-08 16:23:39 +02:00
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-11-17 09:46:33 +01:00
b , _ = have_I_right ( request , category . blog . id )
2012-10-09 20:47:12 +02:00
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
2012-11-08 21:23:30 +01:00
@login_required
def tag ( request , blog_id ) :
2012-11-17 09:46:33 +01:00
b , _ = have_I_right ( request , blog_id )
2012-11-08 21:23:30 +01:00
tags = Tag . objects . filter ( blog_id = blog_id )
2012-11-17 09:46:33 +01:00
c = { ' tags ' : tags , ' blog ' : b }
2012-11-08 21:23:30 +01:00
return render ( request , ' templates/tag.html ' , c )
@login_required
def edit_tag ( request , tag_id ) :
tag = Tag . objects . get ( pk = tag_id )
if tag is None :
raise Http404
2012-11-17 09:46:33 +01:00
b , _ = have_I_right ( request , tag . blog . id )
2012-11-08 21:23:30 +01:00
2012-12-24 16:34:30 +01:00
if request . method == ' POST ' :
2012-11-08 21:23:30 +01:00
if ' cancel ' in request . POST :
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /tag/ ' + str ( b . id ) )
2012-11-08 21:23:30 +01:00
if ' edit ' in request . POST :
name = tag . name
2012-12-24 16:34:30 +01:00
form = TagForm ( request . POST , instance = tag )
if form . is_valid ( ) :
2012-11-08 21:23:30 +01:00
if request . POST [ ' name ' ] != name :
2012-12-31 13:58:47 +01:00
tag . remove ( )
2012-11-08 21:23:30 +01:00
form . save ( )
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /tag/ ' + str ( b . id ) )
2012-11-08 21:23:30 +01:00
else :
2012-12-24 16:34:30 +01:00
form = TagForm ( instance = tag )
2012-11-08 21:23:30 +01:00
c = { ' tag ' : tag , ' form ' : form }
return render ( request , ' templates/edit_tag.html ' , c )
@login_required
def delete_tag ( request , tag_id ) :
tag = Tag . objects . get ( pk = tag_id )
if tag is None :
raise Http404
2012-11-17 09:46:33 +01:00
b , _ = have_I_right ( request , tag . blog . id )
2012-11-08 21:23:30 +01:00
tag . delete ( )
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /tag/ ' + str ( b . id ) )
2012-11-08 21:23:30 +01:00
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 ' )
2013-01-29 18:51:23 +01:00
2012-12-24 16:34:30 +01:00
if request . method == ' POST ' :
2012-07-08 16:23:39 +02:00
if ' add ' in request . POST :
2012-12-24 16:34:30 +01:00
form = BlogForm ( request . POST )
if form . is_valid ( ) :
2012-07-08 16:23:39 +02:00
form = form . save ( )
2012-07-15 18:21:26 +02:00
form . create ( )
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /blog ' )
2012-07-08 16:23:39 +02:00
else :
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /blog ' )
2012-07-08 16:23:39 +02:00
else :
2012-12-24 16:34:30 +01:00
form = BlogForm ( )
2012-07-08 16:23:39 +02:00
2012-12-24 16:34:30 +01:00
return render ( request , ' add_blog.html ' , { ' form ' : form } )
2012-07-08 16:23:39 +02:00
@login_required
def view_blog ( request , blog_id ) :
2012-11-17 09:46:33 +01:00
b , _ = have_I_right ( request , blog_id )
2012-12-24 16:34:30 +01:00
posts = Post . objects . filter ( blog = b )
count = posts . count ( )
2012-11-17 09:46:33 +01:00
nb_pages = int ( count / 50 )
if ' page ' in request . GET :
cur_page = int ( request . GET [ ' page ' ] )
2012-07-08 16:23:39 +02:00
else :
2012-11-17 09:46:33 +01:00
if ' cur_page ' in request . session :
cur_page = request . session [ ' cur_page ' ]
else :
cur_page = 0
2012-07-08 16:23:39 +02:00
2012-12-24 16:34:30 +01:00
# Prevent error injection
2012-11-17 09:46:33 +01:00
if cur_page < 0 : cur_page = 0
if cur_page > nb_pages : cur_page = nb_pages - 1
2012-07-08 16:23:39 +02:00
2012-11-17 09:46:33 +01:00
request . session [ ' cur_page ' ] = cur_page
start = cur_page * 50
end = start + 50
2012-12-24 16:34:30 +01:00
posts = posts . order_by ( ' -creation_date ' ) [ start : end ]
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
2012-11-17 09:46:33 +01:00
navigation_bar = createNavigationBar ( b . id , cur_page , nb_pages )
c = { ' blog ' : b , ' posts ' : posts , ' form ' : form , ' comments ' : dict_comments , ' navigation_bar ' : navigation_bar }
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
2012-12-24 16:34:30 +01:00
edited = False
if request . method == ' POST ' :
2012-07-08 16:23:39 +02:00
if ' edit ' in request . POST :
2012-12-24 16:34:30 +01:00
form = BlogForm ( request . POST , instance = b )
if form . is_valid ( ) :
2012-07-08 16:23:39 +02:00
form . save ( )
2012-12-24 16:34:30 +01:00
edited = True
2012-07-08 16:23:39 +02:00
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 :
2012-12-24 16:34:30 +01:00
form = BlogForm ( instance = b )
2012-07-08 16:23:39 +02:00
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-12-24 16:34:30 +01:00
c = { ' blog ' : b , ' posts ' : posts , ' form ' : form , ' edited ' : edited }
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-11-25 20:39:19 +01:00
( b , _ ) = have_I_right ( request , blog_id )
2012-07-08 16:23:39 +02:00
2012-12-24 16:34:30 +01:00
if request . method == ' POST ' :
2012-07-08 16:23:39 +02:00
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-12-24 16:34:30 +01:00
form = PostForm ( request . POST , instance = post )
if form . is_valid ( ) :
2012-07-22 10:47:24 +02:00
form = form . save ( )
2012-11-08 21:23:30 +01:00
form . createPost ( content , request . POST [ ' text_tags ' ] )
2012-11-25 20:39:19 +01:00
s = Search ( )
s . index_post ( b , form . id )
2013-03-02 10:11:44 +01:00
request . session [ ' cur_page ' ] = 0
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /blog/ ' + blog_id )
2012-07-08 16:23:39 +02:00
else :
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /blog/ ' + blog_id )
2012-07-08 16:23:39 +02:00
else :
2012-12-24 16:34:30 +01:00
form = PostForm ( )
2012-07-08 16:23:39 +02:00
2012-12-24 16:34:30 +01:00
return render ( request , ' add_post.html ' , { ' form ' : form , ' blog_id ' : blog_id } )
2012-07-08 16:23:39 +02:00
@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-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
2012-12-24 16:34:30 +01:00
if request . method == ' POST ' :
2012-07-08 16:23:39 +02:00
if ' edit ' in request . POST :
2012-12-24 16:34:30 +01:00
form = PostForm ( request . POST , instance = post )
if form . is_valid ( ) :
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-11-08 21:23:30 +01:00
post . createPost ( request . POST [ ' content ' ] , request . POST [ ' text_tags ' ] )
2012-11-25 20:39:19 +01:00
s = Search ( )
s . edit_post ( b , post_id )
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /blog/ ' + str ( blog_id ) )
2012-07-08 16:23:39 +02:00
else :
if ' cancel ' in request . POST :
2012-12-24 16:34:30 +01:00
return HttpResponseRedirect ( ' /blog/ ' + str ( blog_id ) )
2012-07-08 16:23:39 +02:00
else :
2012-12-24 16:34:30 +01:00
form = PostForm ( instance = post , initial = { ' text_tags ' : ' , ' . join ( ( tag . name ) for tag in post . tags . all ( ) ) } )
2012-07-08 16:23:39 +02:00
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 ' , {
2013-01-29 18:51:23 +01:00
' form ' : form , ' post_id ' : post_id , ' content ' : content ,
' blog_id ' : blog_id , ' comments ' : comment_list
} )
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-11-25 20:39:19 +01:00
s = Search ( )
2012-12-24 16:34:30 +01:00
s . delete_post ( b , post_id )
2012-11-25 20:39:19 +01:00
2012-08-28 09:09:14 +02:00
post . delete ( )
2012-07-15 18:21:26 +02:00
2012-11-17 09:46:33 +01:00
return HttpResponseRedirect ( ' /blog/ ' + str ( b . id ) )
2012-07-15 18:21:26 +02:00
2012-11-25 20:39:19 +01:00
def _generate ( request , blog_id , report ) :
2012-11-17 09:46:33 +01:00
b , _ = have_I_right ( request , blog_id )
2012-07-15 18:21:26 +02:00
2012-12-24 16:34:30 +01:00
posts = Post . objects . filter ( blog = b ) . order_by ( ' -creation_date ' )
nb_pages = int ( posts . count ( ) / 50 )
posts = posts [ 0 : 50 ]
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-11-17 09:46:33 +01:00
navigation_bar = createNavigationBar ( b . id , 0 , nb_pages )
c = { ' blog ' : b , ' posts ' : posts , ' form ' : form , ' report ' : report , ' comments ' : dict_comments , ' navigation_bar ' : navigation_bar }
2012-07-18 11:30:54 +02:00
2012-09-11 22:25:58 +02:00
return render ( request , ' templates/generate.html ' , c )
2012-11-25 20:39:19 +01:00
@login_required
def generate ( request , blog_id ) :
b , _ = have_I_right ( request , blog_id )
report = b . generate ( )
return _generate ( request , blog_id , report )
@login_required
def generate_search ( request , blog_id ) :
b , _ = have_I_right ( request , blog_id )
2013-01-22 20:02:56 +01:00
hash_posts = { }
hash_posts_content = { }
s = Search ( hash_posts , hash_posts_content )
2012-11-25 20:39:19 +01:00
report = s . create_index ( b )
return _generate ( request , blog_id , report )
2012-12-10 19:30:25 +01:00
@csrf_exempt
def search ( request , blog_id ) :
from dynastie . generators import search
ref = request . META [ ' HTTP_REFERER ' ]
b = Blog . objects . filter ( pk = blog_id )
if len ( b ) == 0 :
return HttpResponseRedirect ( ref )
b = b [ 0 ]
if ' text ' in request . POST :
text = request . POST [ ' text ' ]
else :
return HttpResponseRedirect ( ref )
2013-01-29 18:51:23 +01:00
2012-12-10 19:30:25 +01:00
s = Search ( )
post_list = s . search ( b , text )
if post_list is None : post_list = [ ]
2013-01-22 20:02:56 +01:00
hash_posts = { }
hash_posts_content = { }
s = search . Search ( hash_posts , hash_posts_content )
2012-12-10 19:30:25 +01:00
res = s . generate ( b , b . src_path , b . output_path , post_list )
c = { ' result ' : res }
2012-12-24 16:34:30 +01:00
# Simple wrapper to HTML content
2012-12-10 19:30:25 +01:00
return render ( request , ' templates/search.html ' , c )
2012-09-11 22:25:58 +02:00
@login_required
def preview ( request , blog_id ) :
from dynastie . generators import post
2012-12-24 16:34:30 +01:00
( b , p ) = have_I_right ( request , blog_id )
2012-09-11 22:25:58 +02:00
values = { ' title ' : request . POST [ ' title ' ] , \
2013-01-29 18:51:23 +01:00
' author ' : request . user . first_name + ' ' + request . user . last_name , \
' content ' : request . POST [ ' content ' ]
2012-09-11 22:25:58 +02:00
}
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 ' )
2013-01-29 18:51:23 +01:00
# 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( '
if month < 10 :
suffix = ' /images/ ' + str ( year ) + ' /0 ' + str ( month )
else :
suffix = ' /images/ ' + str ( year ) + ' / ' + str ( month )
path = b . src_path + ' / ' + suffix
2013-01-27 09:44:17 +01:00
url = ' http:// ' + b . name + suffix + ' / '
2012-09-26 21:13:11 +02:00
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 :
2012-11-08 21:23:30 +01:00
( b , p ) = have_I_right ( request , blog_id )
2012-10-04 21:49:33 +02:00
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-11-08 21:36:21 +01:00
# Anti robot !!
if request . POST [ ' email ' ] != ' ' :
print ' fucking robot '
return HttpResponseRedirect ( ref )
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 :
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 :
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
else :
parentComment = None
if request . POST [ ' author ' ] == ' ' or request . POST [ ' the_comment ' ] == ' ' :
return HttpResponseRedirect ( ref )
2012-10-20 21:03:11 +02:00
# Behind nginx proxy
if ' HTTP_X_FORWARDED_FOR ' in request . META :
ip = request . META [ ' HTTP_X_FORWARDED_FOR ' ]
2012-10-20 20:55:55 +02:00
else :
ip = request . META [ ' REMOTE_ADDR ' ]
2012-10-23 18:59:40 +02:00
# Avoid script injection
the_comment = request . POST [ ' the_comment ' ]
the_comment = the_comment . replace ( ' < ' , ' < ' )
the_comment = the_comment . replace ( ' > ' , ' > ' )
2013-02-09 09:12:01 +01:00
the_comment = the_comment . replace ( ' \n ' , ' <br /> ' )
2012-10-23 18:59:40 +02:00
2012-12-24 18:07:44 +01:00
if ' mel ' in request . POST :
mel = request . POST [ ' mel ' ] . strip ( )
else :
mel = ' '
2012-11-08 21:23:30 +01:00
comment = Comment ( post = post , parent = parentComment , date = datetime . now ( ) , author = request . POST [ ' author ' ] . strip ( ) , \
2012-12-24 18:07:44 +01:00
email = mel , the_comment = the_comment , ip = ip )
2012-10-04 21:49:33 +02:00
comment . save ( )
engine = globals ( ) [ ' post ' ]
2012-10-11 22:26:47 +02:00
post_list = [ post ]
2013-01-29 18:51:23 +01:00
hash_post = { }
hash_post_content = { }
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 " ) :
2013-01-29 18:51:23 +01:00
e = obj ( hash_post , hash_post_content )
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-11-08 21:36:21 +01:00
if email != ' ' and email != request . POST [ ' mel ' ] 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
2012-10-20 21:06:16 +02:00
if post . author . email not in emails :
2012-10-20 20:55:55 +02:00
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 )
2013-02-09 10:55:47 +01:00
text_body + = u ' Pour le consulter, rendez vous sur http:// %s %s #comment_ %s \n \n ---------------- \n \n ' % ( blog . name , post . getPath ( ) , comment_index )
2012-10-23 18:59:40 +02:00
text_body + = the_comment
2012-10-20 19:05:29 +02:00
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
2012-10-23 18:59:40 +02:00
html_body + = the_comment + ' </pre> '
2012-10-20 19:05:29 +02:00
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-25 18:36:10 +02:00
response [ ' Cache-Control ' ] = ' no-store, no-cache, must-revalidate '
response [ ' Expires ' ] = ' Thu, 01 Jan 1970 00:00:00 GMT '
2012-10-20 19:05:29 +02:00
response . set_cookie ( ' author ' , request . POST [ ' author ' ] , domain = blog . name , secure = True , httponly = False ) ;
2012-12-24 18:07:44 +01:00
if mel != ' ' :
response . set_cookie ( ' email ' , mel , 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
2012-12-24 16:34:30 +01:00
if request . method == ' POST ' :
2012-10-04 21:49:33 +02:00
if ' edit ' in request . POST :
2012-12-24 16:34:30 +01:00
form = CommentForm ( request . POST , instance = comment )
if form . is_valid ( ) :
2012-10-04 21:49:33 +02:00
form = form . save ( )
return HttpResponseRedirect ( ' /post/edit/ ' + str ( post_id ) )
else :
return HttpResponseRedirect ( ' /post/edit/ ' + str ( post_id ) )
else :
2013-02-09 09:48:30 +01:00
comment . _remove_br ( )
2012-12-24 16:34:30 +01:00
form = CommentForm ( instance = comment )
2012-10-04 21:49:33 +02:00
2012-12-24 16:34:30 +01:00
return render ( request , ' edit_comment.html ' , { ' form ' : form , ' comment ' : comment } )
2012-10-04 21:49:33 +02:00
@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 ) )