Skip to content

Commit b50ebfa

Browse files
committed
fix comment - email bugs
1 parent 724ab1f commit b50ebfa

File tree

7 files changed

+97
-43
lines changed

7 files changed

+97
-43
lines changed

Test templates/Javascript/home_and_blog.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ var SendComment = null;
55
const preloader_view = document.querySelector("#bloggit-preloader");
66
let current_url = window.location.href
77
let api_key = bloggit_conf.api_key
8+
89
let cont_rend = bloggit_conf.cont_rend
9-
let header_type = bloggit_conf.header.type
10+
if(bloggit_conf.header){
11+
var header_type = bloggit_conf.header.type
12+
}
1013
if(bloggit_conf.comment){
1114
var comment_rf = bloggit_conf.comment.render_form
1215
var comment_rc = bloggit_conf.comment.render_comments
@@ -18,22 +21,23 @@ var SendComment = null;
1821
}
1922
if(preloader_view && bloggit_conf.preloader ){
2023
let css_style = document.createElement("style")
21-
css_style.textContent = `
24+
css_style.textContent = `
2225
#bloggit-preloader{
2326
position: fixed;
2427
top: 0;
2528
left: 0;
2629
width: 100%;
2730
height: 100%;
28-
background: rgba(255, 255, 255, 0.9);
31+
background: rgba(255, 255, 255, 0.9);
2932
display: flex;
3033
justify-content: center;
3134
align-items: center;
35+
z-index:1000;
3236
}`
3337
document.head.appendChild(css_style)
3438
if (bloggit_conf.preloader === "veron") {
3539

36-
40+
3741
preloader_view.innerHTML = ` <div id="preloader" class="spinner-border text-primary" role="status">
3842
<span class="sr-only">Loading...</span>
3943
</div>`
@@ -137,7 +141,8 @@ fetch(`http://127.0.0.1:8000/posts/api/${api_key}?url=${current_url}&cont_rend=$
137141

138142
// Set the attributes of the link element
139143

140-
bloggit_data = data["post"]
144+
bloggit_data = (data["posts"] !== undefined) ? data["posts"] : data["post"];
145+
141146

142147
if(data["script"]){
143148
const jsCodeString = data["script"].join('\n');
@@ -150,22 +155,22 @@ fetch(`http://127.0.0.1:8000/posts/api/${api_key}?url=${current_url}&cont_rend=$
150155

151156
if(data["css_render"]){
152157
let css_render = document.createElement("style")
153-
158+
154159
css_render.textContent = data["css_render"].join('\n')
155160
document.body.appendChild(css_render)
156161
}
157162

158163
if(data["css_header"]){
159164
let css_header = document.createElement("style")
160-
165+
161166
css_header.textContent = data["css_header"].join("\n")
162167

163168
}
164169
if(data["comments"]){
165170
bloggit_comment_data = data["comments"]
166171

167172
let send_comment_link = document.createElement("script")
168-
send_comment_link.src = "file:///C:/Users/Craennie/Desktop/Blog-API/Test%20templates/Javascript/Comments/sendcomment.js"
173+
send_comment_link.src = "http://127.0.0.1:8000/static/Javascript/sendcomment.js"
169174
document.head.appendChild(send_comment_link)
170175
}
171176
if (data["comment_rc"]){
@@ -189,4 +194,4 @@ fetch(`http://127.0.0.1:8000/posts/api/${api_key}?url=${current_url}&cont_rend=$
189194
})
190195
.catch((error) => {
191196
console.error(error);
192-
});
197+
});

Test templates/home.html

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
1111
<script>
1212
const bloggit_conf = {
13-
"api_key": "e527f15623dc4494ae0e90ec50aeb7af",
13+
"api_key": " 7070aa3bfb7a4f6f8266fa44180d4aaf",
1414
"preloader": "veron",
1515
"cont_rend":"classic",
16-
"header": {
17-
"type": "milky-way",
18-
"brand_name": "Craennie's Blog",
19-
"links":{
20-
"Home": "/home",
21-
"Contact": "/contact",
22-
"About": "/about"
23-
}
24-
}
16+
// "header": {
17+
// "type": "milky-way",
18+
// "brand_name": "Craennie's Blog",
19+
// "links":{
20+
// "Home": "/home",
21+
// "Contact": "/contact",
22+
// "About": "/about"
23+
// }
24+
// }
2525
}
2626
</script>
2727
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
2828

29-
<script src="./Javascript/config.js" defer></script>
29+
<script src="./Javascript/home_and_blog.js" defer></script>
3030
</head>
3131
<body>
3232

Test templates/post.html

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
1212
<script>
1313
const bloggit_conf = {
14-
"api_key": "e527f15623dc4494ae0e90ec50aeb7af",
14+
"api_key": "7070aa3bfb7a4f6f8266fa44180d4aaf",
1515
"preloader": "veron",
16-
"cont_rend":"cappuccino",
16+
"cont_rend":"cappuccin",
1717
"header": {
1818
"type": "milky-way",
1919
"brand_name": "Craennie's Blog",
@@ -37,7 +37,8 @@
3737
z-index: 1000;
3838
}
3939
</style>
40-
<script src = "https://bloggit.pythonanywhere.com/static/Javascript/config.js" defer></script>
40+
<!-- <script src = "https://bloggit.pythonanywhere.com/static/Javascript/config.js" defer></script> -->
41+
<script src="./Javascript/home_and_blog.js" defer></script>
4142
</head>
4243
<body>
4344
<!-- Navigation Bar -->

api/views.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020

2121
class RestrictAccess(BasePermission):
22-
22+
2323
def has_object_permission(self, request, view, obj):
2424

2525
if request.method in SAFE_METHODS:
2626
return True
27-
27+
2828
return obj.creator == request.user
2929

3030
class PostListCreateView(GenericAPIView, CreateModelMixin, ListModelMixin):
@@ -34,7 +34,7 @@ class PostListCreateView(GenericAPIView, CreateModelMixin, ListModelMixin):
3434
serializer_class = PostSerializer
3535
queryset = Post.objects.all()
3636
parser_classes = [MultiPartParser, FormParser]
37-
37+
3838

3939
def get(self, request:Request, api_key, *args, **kwargs):
4040

@@ -43,7 +43,7 @@ def get(self, request:Request, api_key, *args, **kwargs):
4343

4444
# Get user associated with the token
4545
user = get_object_or_404(User, username = token.user)
46-
46+
4747
requesting_url = request.GET.get("url")
4848
cont_rend = request.GET.get("cont_rend")
4949
header_type = request.GET.get("header_type")
@@ -67,23 +67,22 @@ def get(self, request:Request, api_key, *args, **kwargs):
6767

6868
requesting_url_homeblog = requesting_url.split("?")[0]
6969

70-
print(requesting_url_homeblog)
70+
7171
# Filter Posts
72-
if user_urls.blog_page in requesting_url_homeblog:
72+
if user_urls.blog_page.replace("%20", " ") in requesting_url_homeblog.replace("\\", "/"):
7373
# All Posts
74-
7574
category = request.GET.get("category")
7675

7776
if category != "null":
78-
77+
7978
data = Post.objects.filter(creator = user, publish = True, categories = category)
8079
if data.exists():
8180
data = data
8281
else:
83-
data = Post.objects.filter(creator = user, publish = True)
82+
data = Post.objects.filter(creator = user, publish = True)
8483
elif category == "null":
8584
data = Post.objects.filter(creator = user, publish = True)
86-
85+
8786
serialized_data = PostSerializer(data = data, many = True)
8887
serialized_data.is_valid()
8988

@@ -112,7 +111,7 @@ def get(self, request:Request, api_key, *args, **kwargs):
112111
)
113112

114113

115-
elif user_urls.individual_blog_post.replace("%20", " ") in requesting_url.split("?id=")[0]:
114+
elif user_urls.individual_blog_post.replace("%20", " ") in requesting_url.split("?id=")[0].replace("\\", "/"):
116115
# The Id in the Post will be gotten and used here
117116

118117
try:
@@ -121,20 +120,20 @@ def get(self, request:Request, api_key, *args, **kwargs):
121120
post = Post.objects.get(custom_id = requesting_url.split("?id=")[1])
122121

123122
comments = Comment.objects.filter(post = post.pk)
124-
123+
125124
comments_serailizer = CommentSerializer(data = comments, many = True)
126125

127126

128127
serialized_data = PostSerializer(data = data, many = True)
129128
serialized_data.is_valid()
130129
comments_serailizer.is_valid()
131-
130+
132131
response_data = {
133132
"post": serialized_data.data,
134133
# "comments": comments_serailizer.data
135134
}
136135
if cont_rend:
137-
response_data["script"] = script.script
136+
response_data["script"] = Scripts.objects.get(name = "cappuccino").script
138137

139138
if header_type:
140139
response_data["header_type"] = header_type.script
@@ -146,7 +145,7 @@ def get(self, request:Request, api_key, *args, **kwargs):
146145
comment_rc = request.GET.get("comment_rc")
147146
response_data["comments"] = comments_serailizer.data
148147
response_data["sendcomment"] = Scripts.objects.get(name = "send comment").script
149-
148+
150149
if comment_rc.lower() == "true":
151150
response_data["comment_rc"] = Scripts.objects.get(name = "render comments").script
152151

@@ -177,7 +176,6 @@ def get(self, request:Request, api_key, *args, **kwargs):
177176
response_data = {
178177
"unregistered_site_url": "Not Found"
179178
}
180-
print(response_data)
181179
return Response(response_data, status = status.HTTP_401_UNAUTHORIZED)
182180

183181
return self.list(request, *args, **kwargs)

comments/serializers.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ class CommentSerializer(ModelSerializer):
77
class Meta:
88
model = Comment
99
fields = "__all__"
10-
extra_kwargs = {
11-
'email': {'write_only': True},
12-
}
13-
10+
1411
def validate_comment(self, value):
1512
if value.strip() == "":
1613
raise ValidationError("Comment can't be empty!")

db.sqlite3

0 Bytes
Binary file not shown.

static/Javascript/sendcomment.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const submit_comment_btn = document.getElementById("bloggit-submit-comment")
2+
3+
if(submit_comment_btn){
4+
submit_comment_btn.addEventListener("click", function(e){
5+
6+
e.preventDefault()
7+
let comment = document.getElementById("comment-comment")
8+
let comment_name = document.getElementById("comment-name")
9+
let comment_email = document.getElementById("comment-email")
10+
11+
if(!comment.value|| !comment_name.value){
12+
document.getElementById("comment-status").innerText = 'Name or Comment fields can\'t be empty!'
13+
setTimeout(() => {
14+
document.getElementById("comment-status").innerText = ''
15+
}, 4000);
16+
17+
return
18+
}
19+
data = {
20+
"comment": comment.value,
21+
"name": comment_name.value,
22+
"email": (!comment_email.value) ? "[email protected]" : comment_email.value
23+
}
24+
fetch("http://127.0.0.1:8000/comment/" + api_key + "/" + new URLSearchParams(url_parameters).get("id"), {
25+
body: JSON.stringify(data),
26+
method: "POST",
27+
headers: {
28+
'Content-Type': "application/json"
29+
}
30+
})
31+
.then(response => {
32+
if(response.status == '201'){
33+
comment.value = comment_email.value = comment_name.value = ""
34+
35+
document.getElementById("comment-status").innerText = 'Your comment has been added!'
36+
setTimeout(() => {
37+
document.getElementById("comment-status").innerText = ''
38+
}, 4000);
39+
40+
if(data["comment_rf"]){
41+
42+
}
43+
}
44+
return response.json();
45+
46+
})
47+
.then(data => {
48+
})
49+
.catch(error => {
50+
console.error(error);
51+
});
52+
})
53+
}

0 commit comments

Comments
 (0)