all repos — comments @ 6ba60b2f7d901d697933c706143e130bdf5e3605

django app for embedding comment threads in a static site via iframes

views.py (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Comments/views.py
# (c) 2020 Derek Stevens <drkste@zoho.com>

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from .models import Comment, Thread
from django.template import loader
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.core.validators  import ValidationError
from django.views.decorators.clickjacking import xframe_options_sameorigin

def buildCommentList(cThread):
  cList = None
  if cThread and cThread.root_comment:
    current = cThread.root_comment
    if not current.hidden:
      cList = [ current ]
    while current.next:
      current = current.next
      if not current.hidden:
        if cList:
          cList.append(current)
        else:
          cList = [ current ]
  return cList

@xframe_options_sameorigin
def thread(request, thread_id):
	cThread = get_object_or_404(Thread, pk=thread_id)

	commentList = buildCommentList(cThread)

	template = loader.get_template('comments/thread.html')
	context = { 'thread': cThread, 'comments': commentList }
	return HttpResponse(template.render(context, request))

def checkMailAddr(addr):
	if "@" in addr:
		if addr[0] == "@":
			raise ValidationError("Invalid email address!")

		domain = addr.split("@")[1]
		if "." in domain and len(domain) >= 5:
			for i in domain.split("."):
				if len(i) < 2:
					raise ValidationError("Invalid email address!")
			return 1

		else:
			raise ValidationError("Invalid email address!")
	else:
		raise ValidationError("Invalid email address!")

def checkLength(name, x):
	if len(name) > x:
		return 1
	else:
		raise ValidationError("Not enough characters in field!")

@xframe_options_sameorigin
def post(request, thread_id):
	cThread = get_object_or_404(Thread, pk=thread_id)
	template = loader.get_template('comments/thread.html')

	commentList = buildCommentList(cThread)

	context = {'thread': cThread, 'comments': commentList}
	if request.POST:
		name = request.POST['comment_author']
		mail = request.POST['comment_author_email']
		data = request.POST['comment_data']

		try:
			validationCounter = 0
			validationCounter += checkLength(name, 1)
			validationCounter += checkMailAddr(mail)
			validationCounter += checkLength(data, 8)
		except ValidationError:
			if validationCounter == 0:
				context['error_message'] = "What was your name again?"
			if validationCounter == 1:
				context['error_message'] = "Enter a valid e-mail address, please. It is only recorded for accountability; it is not publicized."
			if validationCounter == 2:
				context['error_message'] = "Say something meaningful! At least 8 characters are required for the comment field."
			return HttpResponse(template.render(context, request))

		newComment = Comment(comment_author=name, comment_author_email=mail, comment_data=data)
		newComment.save()
		if cThread.root_comment:
			c = cThread.root_comment
			while c:
				last = c
				c = c.next
			last.next = newComment
			last.save()
		else:
			cThread.root_comment = newComment

		cThread.save()

		return HttpResponseRedirect(reverse('comments:thread', args=(thread_id,)))