PageRenderTime 23ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/diamandas/myghtyboard/views_add_edit.py

http://diamanda.googlecode.com/
Python | 312 lines | 294 code | 8 blank | 10 comment | 8 complexity | 6ee166676f5239de1a4e1b3b43741302 MD5 | raw file
  1. #!/usr/bin/python
  2. # Diamanda Application Set
  3. # myghtyboard forum
  4. # add / edit posts and topics
  5. from datetime import datetime
  6. from django.shortcuts import render_to_response
  7. from django.http import HttpResponseRedirect
  8. from django.conf import settings
  9. from django.contrib.auth.models import User
  10. from django.template import RequestContext
  11. from django.utils.translation import ugettext as _
  12. from django.views.generic.list_detail import object_list
  13. from django.core.mail import mail_admins
  14. from django.contrib.auth.decorators import login_required
  15. from django.core.urlresolvers import reverse
  16. from diamandas.myghtyboard.models import *
  17. from diamandas.myghtyboard.forms import *
  18. from diamandas.myghtyboard import permshelpers
  19. from diamandas.myghtyboard.context import forum as forumContext
  20. from diamandas.myghtyboard.utils import *
  21. def add_topic(request, forum_id):
  22. """
  23. add topic
  24. * forum_id - ID of a Forum entry
  25. """
  26. request.forum_id = forum_id
  27. perm = permshelpers.cant_add_topic(request)
  28. if perm:
  29. return perm
  30. forum = Forum.objects.get(id=forum_id)
  31. pr = False
  32. if forum.use_prefixes:
  33. p = Prefix.objects.filter(forums=forum)
  34. if len(p) > 0:
  35. pr = []
  36. for i in p:
  37. pr.append(i)
  38. if request.POST:
  39. stripper = Stripper()
  40. page_data = request.POST.copy()
  41. text = page_data['text']
  42. # block anonymous messages with multiple links
  43. perms = forumContext(request)
  44. if not perms['perms']['is_authenticated'] and text.count('http') > 1:
  45. return render_to_response('bug.html',
  46. {'bug': _('To many links. Is this spam?.')},
  47. context_instance=RequestContext(request, perms)
  48. )
  49. if 'prefix[]' in page_data:
  50. prefixes = page_data.getlist("prefix[]")
  51. pr = Prefix.objects.filter(id__in=prefixes)
  52. page_data['prefixes'] = ''
  53. for p in pr:
  54. page_data['prefixes'] = '%s[%s] ' % (page_data['prefixes'], p.name)
  55. del page_data['prefix[]']
  56. page_data['name'] = stripper.strip(page_data['name'])
  57. page_data['forum'] = forum_id
  58. page_data['posts'] = 1
  59. if perms['perms']['is_authenticated']:
  60. page_data['lastposter'] = unicode(request.user)
  61. page_data['author'] = unicode(request.user)
  62. author = unicode(request.user)
  63. page_data['author_system'] = request.user.id
  64. else:
  65. if 'nick' in page_data and len(stripper.strip(page_data['nick'])) > 2:
  66. author = stripper.strip(page_data['nick'])[0:14]
  67. page_data['lastposter'] = author
  68. page_data['author'] = author
  69. page_data['author_anonymous'] = 1
  70. else:
  71. page_data['lastposter'] = _('Anonymous')
  72. page_data['author'] = _('Anonymous')
  73. author = _('Anonymous')
  74. page_data['author_anonymous'] = 1
  75. page_data['last_pagination_page'] = 1
  76. page_data['modification_date'] = datetime.now()
  77. if request.user.is_authenticated():
  78. chck = Post.objects.filter(author_system=request.user).count()
  79. else:
  80. chck = 0
  81. if chck < 5 and settings.FORUM_USE_RECAPTCHA:
  82. form = AddTopicWithCaptchaForm(page_data)
  83. else:
  84. form = AddTopicForm(page_data)
  85. if form.is_valid():
  86. new_place = form.save()
  87. if 'prefixes' in page_data:
  88. tp = TopicPrefix(topic=new_place)
  89. tp.save()
  90. tp.prefix=pr
  91. tp.save()
  92. post = Post(topic = new_place, text = text, author = author, ip = request.META['REMOTE_ADDR'])
  93. if 'author_anonymous' in page_data:
  94. post.author_anonymous = True
  95. else:
  96. post.author_system = request.user
  97. post.save()
  98. forum.topics = forum.topics +1
  99. forum.posts = forum.posts +1
  100. forum.lastposter = author
  101. if len(new_place.name) > 25:
  102. tname = new_place.name[0:25] + '...'
  103. else:
  104. tname = new_place.name
  105. forum.lasttopic = '<a href="' + reverse('diamandas.myghtyboard.views.post_list', kwargs={'pagination_id': 1, 'topic_id': new_place.id}) + '/">' + tname + '</a>'
  106. forum.modification_date = datetime.now()
  107. forum.save()
  108. if settings.NOTIFY_ADMINS:
  109. mail_admins(_('Topic Added'), _('Topic added') + settings.SITE_DOMAIN + reverse('diamandas.myghtyboard.views.topic_list', kwargs={'forum_id': forum_id}), fail_silently=True)
  110. return redirect_by_template(request, reverse('diamandas.myghtyboard.views.topic_list', kwargs={'forum_id': forum_id}), _('Topic added succesfuly.'))
  111. else:
  112. return render_to_response(
  113. 'myghtyboard/add_topic.html',
  114. {'form': form, 'forum': forum, 'pr': pr},
  115. context_instance=RequestContext(request, forumContext(request)))
  116. if request.user.is_authenticated():
  117. chck = Post.objects.filter(author_system=request.user).count()
  118. else:
  119. chck = 0
  120. if chck < 5 and settings.FORUM_USE_RECAPTCHA:
  121. form = AddTopicWithCaptchaForm()
  122. else:
  123. form = AddTopicForm()
  124. return render_to_response(
  125. 'myghtyboard/add_topic.html',
  126. {'form': form, 'forum': forum, 'pr': pr},
  127. context_instance=RequestContext(request, forumContext(request)))
  128. def add_post(request, topic_id, post_id = False):
  129. """
  130. add post
  131. * topic_id - id of a Topic entry
  132. * post_id - id of a Post entry to be quoted, optional
  133. """
  134. try:
  135. topic = Topic.objects.get(id=topic_id)
  136. forum = Forum.objects.get(id=topic.forum.id)
  137. except:
  138. return HttpResponseRedirect(reverse('diamandas.myghtyboard.views.category_list', kwargs={}))
  139. request.forum_id = forum.id
  140. perm = permshelpers.cant_add_post(request, topic.is_locked)
  141. if perm:
  142. return perm
  143. try:
  144. # check who made the last post.
  145. lastpost = Post.objects.order_by('-date').filter(topic=topic_id)[:2]
  146. # if the last poster is the current one (login) and he isn't staff then we don't let him post after his post (third post)
  147. if unicode(lastpost[0].author) == unicode(request.user) and unicode(lastpost[1].author) == unicode(request.user) and not is_staff:
  148. return render_to_response('bug.html',
  149. {'bug': _('You can\'t post after your post')},
  150. context_instance=RequestContext(request, forumContext(request))
  151. )
  152. except:
  153. pass
  154. lastpost = Post.objects.filter(topic=topic_id).order_by('-id')[:10]
  155. if request.POST:
  156. stripper = Stripper()
  157. page_data = request.POST.copy()
  158. # block anonymous messages with multiple links
  159. perms = forumContext(request)
  160. if not perms['perms']['is_authenticated'] and page_data['text'].count('http') > 1:
  161. return render_to_response('bug.html',
  162. {'bug': _('To many links. Is this spam?.')},
  163. context_instance=RequestContext(request, perms)
  164. )
  165. if perms['perms']['is_authenticated']:
  166. page_data['author'] = unicode(request.user)
  167. author = unicode(request.user)
  168. page_data['author_system'] = request.user.id
  169. else:
  170. if 'nick' in page_data and unicode(stripper.strip(page_data['nick'])) > 2:
  171. author = stripper.strip(page_data['nick'])[0:14]
  172. page_data['author'] = author
  173. page_data['author_anonymous'] = 1
  174. else:
  175. page_data['author'] = _('Anonymous')
  176. author = _('Anonymous')
  177. page_data['author_anonymous'] = 1
  178. page_data['ip'] = request.META['REMOTE_ADDR']
  179. page_data['topic'] = topic_id
  180. page_data['date'] = datetime.now()
  181. if request.user.is_authenticated():
  182. chck = Post.objects.filter(author_system=request.user).count()
  183. else:
  184. chck = 0
  185. if chck < 5 and settings.FORUM_USE_RECAPTCHA:
  186. form = AddPostWithCaptchaForm(page_data)
  187. else:
  188. form = AddPostForm(page_data)
  189. if form.is_valid():
  190. form.save()
  191. posts = Post.objects.filter(topic=topic_id).count()
  192. pmax = posts/10
  193. pmaxten = posts%10
  194. if pmaxten != 0:
  195. pmax = pmax+1
  196. topic.last_pagination_page = pmax
  197. elif pmax > 0:
  198. topic.last_pagination_page = pmax
  199. else:
  200. pmax = 1
  201. topic.last_pagination_page = 1
  202. topic.posts = posts
  203. topic.lastposter = author
  204. topic.modification_date = datetime.now()
  205. topic.save()
  206. forum.posts = forum.posts +1
  207. forum.lastposter = author
  208. if len(topic.name) > 25:
  209. tname = topic.name[0:25] + '...'
  210. else:
  211. tname = topic.name
  212. forum.lasttopic = '<a href="' + reverse('diamandas.myghtyboard.views.post_list', kwargs={'pagination_id': pmax, 'topic_id': topic.id}) + '">' + tname + '</a>'
  213. forum.modification_date = datetime.now()
  214. forum.save()
  215. if settings.NOTIFY_ADMINS:
  216. mail_admins(
  217. _('Post Added'),
  218. _('Post Added') + settings.SITE_DOMAIN + reverse('diamandas.myghtyboard.views.post_list', kwargs={'pagination_id': pmax, 'topic_id': topic.id}),
  219. fail_silently=True
  220. )
  221. return redirect_by_template(request, reverse('diamandas.myghtyboard.views.post_list', kwargs={'pagination_id': pmax, 'topic_id': topic.id}), _('Post added succesfuly.'))
  222. else:
  223. return render_to_response(
  224. 'myghtyboard/add_post.html',
  225. {'forum': forum, 'topic': topic, 'lastpost': lastpost, 'form':form},
  226. context_instance=RequestContext(request, forumContext(request)))
  227. else:
  228. if post_id:
  229. quote = Post.objects.get(id=post_id)
  230. quote_text = '[quote][b]' + quote.author + _(' wrote') + ':[/b]\n\r' + quote.text + '[/quote]\n\r'
  231. else:
  232. quote_text = ''
  233. if request.user.is_authenticated():
  234. chck = Post.objects.filter(author_system=request.user).count()
  235. else:
  236. chck = 0
  237. if chck < 5 and settings.FORUM_USE_RECAPTCHA:
  238. form = AddPostWithCaptchaForm()
  239. else:
  240. form = AddPostForm()
  241. return render_to_response(
  242. 'myghtyboard/add_post.html',
  243. {'forum': forum, 'topic': topic, 'quote_text': quote_text, 'lastpost': lastpost, 'form':form},
  244. context_instance=RequestContext(request, forumContext(request)))
  245. def edit_post(request, post_id):
  246. """
  247. edit post
  248. * post_id - id of a Post entry
  249. """
  250. post = Post.objects.get(id=post_id)
  251. topic = Topic.objects.get(id=post.topic.id)
  252. forum = Forum.objects.get(id=topic.forum.id)
  253. request.forum_id = forum.id
  254. perm = permshelpers.cant_edit_post(request, topic.is_locked, post.author)
  255. if perm:
  256. return perm
  257. if request.POST and len(request.POST.copy()['text']) > 1:
  258. page_data = request.POST.copy()
  259. post.text = page_data['text']
  260. post.save()
  261. pmax = Post.objects.filter(topic=post.topic).count()/10
  262. pmaxten = Post.objects.filter(topic=post.topic).count()%10
  263. if pmaxten != 0:
  264. pmax = pmax+1
  265. return redirect_by_template(request, reverse('diamandas.myghtyboard.views.post_list', kwargs={'pagination_id': pmax, 'topic_id': post.topic.id}), _('Post edited succesfuly.'))
  266. else:
  267. return render_to_response(
  268. 'myghtyboard/edit_post.html',
  269. {'forum': forum, 'topic': topic, 'text': post.text, 'post_id': post_id},
  270. context_instance=RequestContext(request, forumContext(request)))