Dynastie/views.py

793 lines
23 KiB
Python
Raw Normal View History

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
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-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:
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:
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:
b = Blog.objects.get(pk=blog_id)
2012-10-04 21:49:33 +02:00
if b is None:
raise Http404
return (b, p)
def createNavigationBar(blog_id, cur_page, nb_pages):
2012-12-24 18:07:44 +01:00
if nb_pages == 0: return ''
navigation_bar = ''
if cur_page == 0:
navigation_bar += '<< <'
else:
navigation_bar += '<a href="/blog/%d?page=0">&lt;&lt;</a> ' % blog_id
navigation_bar += '<a href="/blog/%d?page=%d">&lt;</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">&gt;</a>' % (blog_id, cur_page+1)
navigation_bar += ' <a href="/blog/%d?page=%d">&gt;&gt;</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
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)
c = {'login_failed' : False}
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':
2012-07-08 16:23:39 +02:00
if 'add' in request.POST:
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()
return HttpResponseRedirect('/user')
2012-07-08 16:23:39 +02:00
else:
return HttpResponseRedirect('/user')
2012-07-08 16:23:39 +02:00
else:
form = UserForm()
2012-07-08 16:23:39 +02: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
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:
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:
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):
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
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):
b,_ = have_I_right(request, blog_id)
if request.method == 'POST':
2012-07-08 16:23:39 +02:00
if 'add' in request.POST:
form = CategoryForm(request.POST)
2012-10-09 20:47:12 +02:00
form.blog = b
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:
form = CategoryForm()
2012-07-08 16:23:39 +02: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
b,_ = have_I_right(request, category.blog.id)
2012-10-09 20:47:12 +02:00
if request.method == 'POST':
2012-07-08 16:23:39 +02:00
if 'cancel' in request.POST:
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
name = name.strip()
form = CategoryForm(request.POST, instance=category)
if form.is_valid():
2012-09-08 12:35:52 +02:00
if request.POST['name'] != name:
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)
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
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
@login_required
def tag(request, blog_id):
b,_ = have_I_right(request, blog_id)
tags = Tag.objects.filter(blog_id=blog_id)
c = {'tags' : tags, 'blog' : b}
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
b,_ = have_I_right(request, tag.blog.id)
if request.method == 'POST':
if 'cancel' in request.POST:
return HttpResponseRedirect('/tag/' + str(b.id))
if 'edit' in request.POST:
name = tag.name
form = TagForm(request.POST, instance=tag)
if form.is_valid():
if request.POST['name'] != name:
tag.remove()
form.save()
return HttpResponseRedirect('/tag/' + str(b.id))
else:
form = TagForm(instance=tag)
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
b,_ = have_I_right(request, tag.blog.id)
tag.delete()
return HttpResponseRedirect('/tag/' + 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':
2012-07-08 16:23:39 +02:00
if 'add' in request.POST:
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()
return HttpResponseRedirect('/blog')
2012-07-08 16:23:39 +02:00
else:
return HttpResponseRedirect('/blog')
2012-07-08 16:23:39 +02:00
else:
form = BlogForm()
2012-07-08 16:23:39 +02: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):
b,_ = have_I_right(request, blog_id)
posts = Post.objects.filter(blog=b)
count = posts.count()
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:
if 'cur_page' in request.session:
cur_page = request.session['cur_page']
else:
cur_page = 0
2012-07-08 16:23:39 +02:00
# Prevent error injection
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
request.session['cur_page'] = cur_page
start = cur_page * 50
end = start + 50
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:
key = comment.post.id
if not key in dict_comments:
dict_comments[key] = 1
2012-10-04 21:49:33 +02:00
else:
dict_comments[key] = dict_comments[key] + 1
2012-10-04 21:49:33 +02: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
edited = False
if request.method == 'POST':
2012-07-08 16:23:39 +02:00
if 'edit' in request.POST:
form = BlogForm(request.POST, instance=b)
if form.is_valid():
2012-07-08 16:23:39 +02:00
form.save()
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:
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
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
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']
form = PostForm(request.POST, instance=post)
if form.is_valid():
2012-07-22 10:47:24 +02:00
form = form.save()
form.createPost(content, request.POST['text_tags'])
2012-11-25 20:39:19 +01:00
s = Search()
s.index_post(b, form.id)
return HttpResponseRedirect('/blog/' + blog_id)
2012-07-08 16:23:39 +02:00
else:
return HttpResponseRedirect('/blog/' + blog_id)
2012-07-08 16:23:39 +02:00
else:
form = PostForm()
2012-07-08 16:23:39 +02: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
if request.method == 'POST':
2012-07-08 16:23:39 +02:00
if 'edit' in request.POST:
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()
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)
return HttpResponseRedirect('/blog/' + str(blog_id))
2012-07-08 16:23:39 +02:00
else:
if 'cancel' in request.POST:
return HttpResponseRedirect('/blog/' + str(blog_id))
2012-07-08 16:23:39 +02:00
else:
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', {
'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()
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
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):
b,_ = have_I_right(request, blog_id)
2012-07-15 18:21:26 +02: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)
comments = Comment.objects.all()
dict_comments = {}
2012-10-09 20:47:12 +02:00
for comment in comments:
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
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)
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)
2012-12-10 19:30:25 +01:00
s = Search()
post_list = s.search(b, text)
if post_list is None: post_list = []
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}
# 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
(b, p) = have_I_right(request, blog_id)
2012-09-11 22:25:58 +02:00
values = {'title' : request.POST['title'], \
'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}
return HttpResponseRedirect('http://' + b.name + '/preview.html')
# return HttpResponseRedirect('http://' + 'localhost:8080' + '/preview.html')
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
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, p) = have_I_right(request, blog_id)
2012-10-04 21:49:33 +02:00
except Http404:
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:
return HttpResponse('', content_type='application/x-javascript')
2012-10-04 21:49:33 +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)
post = Post.objects.get(pk=post_id)
2012-10-04 21:49:33 +02:00
if post is None:
return HttpResponseRedirect(ref)
blog = Blog.objects.get(pk=post.blog_id)
2012-10-04 21:49:33 +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:
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']
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('<', '&lt;')
the_comment = the_comment.replace('>', '&gt;')
2012-12-24 18:07:44 +01:00
if 'mel' in request.POST:
mel = request.POST['mel'].strip()
else:
mel = ''
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']
post_list = [post]
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"):
e = obj(hash_post, hash_post_content)
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 21:06:16 +02:00
if post.author.email not in emails:
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\nUn 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)
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):
comment = Comment.objects.get(pk=comment_id)
2012-10-04 21:49:33 +02:00
if comment is None:
return Http404
(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':
2012-10-04 21:49:33 +02:00
if 'edit' in request.POST:
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:
form = CommentForm(instance=comment)
2012-10-04 21:49:33 +02: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):
comment = Comment.objects.get(pk=comment_id)
2012-10-04 21:49:33 +02:00
if comment is None:
return Http404
(b, post) = have_I_right(request, None, comment.post_id)
post_id = comment.post_id
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:
child.parent = parent
2012-10-04 21:49:33 +02:00
child.save()
comment.delete()
return HttpResponseRedirect('/post/edit/' + str(post_id))