Skip to content

Commit 9ae864b

Browse files
committed
fix comment and bugs
1 parent 64de952 commit 9ae864b

File tree

10 files changed

+68
-35
lines changed

10 files changed

+68
-35
lines changed

Test templates/Javascript/Comments/rendercomments.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ if (comment_container){
2323
<div class="card mb-3">
2424
<div class="card-body">
2525
<h3 class="card-title">${name}</h3>
26-
<time>${time}</time>
2726
<article class="card-text">${comment}</article>
27+
<time>${time}</time>
2828
</div>
2929
</div>
3030

Test templates/Javascript/Comments/renderform.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ if(comment_form_container){
1717
<label for="comment-comment">Comment:* (required)</label>
1818
<textarea class="form-control" id="comment-comment" rows="4" placeholder="Enter your comment" required></textarea>
1919
</div>
20+
21+
<input type="hidden" name="current-time" id="current-time" value="">
2022
<p id="comment-status"></p>
2123
<button type="submit" class="btn btn-primary" id="bloggit-submit-comment">Submit</button>
2224

Test templates/Javascript/home_and_blog.js

+16-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ 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-
98
let cont_rend = bloggit_conf.cont_rend
109
if(bloggit_conf.header){
1110
var header_type = bloggit_conf.header.type
@@ -65,29 +64,23 @@ var SendComment = null;
6564
}
6665
}
6766

68-
69-
7067
function convert_datetime(param){
71-
const dateComponents = param.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/);
72-
73-
const year = parseInt(dateComponents[1]);
74-
const month = parseInt(dateComponents[2]) - 1;
75-
const day = parseInt(dateComponents[3]);
76-
77-
// Create a JavaScript Date object without considering the timezone
78-
const date = new Date(year, month, day);
79-
80-
const time = new Date(param);
81-
82-
// Get the hours, minutes, and seconds
83-
const hours = time.getHours();
84-
const minutes = time.getMinutes();
85-
const seconds = time.getSeconds();
86-
87-
let __time__ = `${hours}:${minutes}:${seconds}`;
88-
89-
// Format the date as "Month Day, Year"
90-
return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }).toString() //+ " ; " + __time__;
68+
const originalDate = new Date(param);
69+
const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
70+
71+
const options = {
72+
timeZone: userTimeZone,
73+
year: 'numeric',
74+
month: 'short',
75+
day: 'numeric',
76+
hour: 'numeric',
77+
minute: 'numeric',
78+
second: 'numeric',
79+
hour12: false
80+
};
81+
82+
const convertedDate = new Intl.DateTimeFormat('en-US', options).format(originalDate);
83+
return convertedDate
9184
}
9285

9386
function strip_tags(param){
@@ -141,7 +134,6 @@ fetch(`http://127.0.0.1:8000/posts/api/${api_key}?url=${current_url}&cont_rend=$
141134
}
142135

143136
// Set the attributes of the link element
144-
145137
bloggit_data = (data["posts"] !== undefined) ? data["posts"] : data["post"];
146138

147139

app/views.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def post(self, request, **kwargs):
2525
title = request.POST["title"]
2626
content = request.POST.get("content", "")
2727
category = request.POST["category"]
28+
time = request.POST["current-time"]
2829
if not content.strip() or content is None or content == None or content.strip() is None or content.strip() == '<p><br></p>':
2930
messages.info(request, 'Summernote editor is empty')
3031
return redirect("/dashboard/admin")
@@ -40,7 +41,8 @@ def post(self, request, **kwargs):
4041
title = title,
4142
body= content,
4243
publish = publish,
43-
categories = category
44+
categories = category,
45+
time = time
4446
)
4547

4648
if request.FILES:

comments/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Comment(models.Model):
1010
name = models.CharField(max_length=100)
1111
email = models.EmailField(blank = True, null = True)
1212
comment = HTMLField()
13-
time = models.DateTimeField(default=datetime.now())
13+
time = models.CharField(default=datetime.now(), max_length = 100)
1414

1515
def __str__(self):
1616
return str(self.post)

comments/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def get(self, request, api_key, id, *args, **kwargs):
2424

2525
post = Post.objects.get(custom_id = id)
2626

27-
comments = Comment.objects.filter(post = post.pk)
27+
comments = Comment.objects.filter(post = post.pk)[::-1]
2828

2929
serializer = self.serializer_class(comments, many = True)
3030

db.sqlite3

0 Bytes
Binary file not shown.

static/Javascript/admin.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,37 @@
11
const delpost = document.querySelectorAll(".delpostnow")
2+
const time_post = document.getElementsByClassName("time-of-post")
3+
4+
5+
function convert_datetime(param){
6+
const originalDate = new Date(param);
7+
const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
8+
9+
const options = {
10+
timeZone: userTimeZone,
11+
year: 'numeric',
12+
month: 'short',
13+
day: 'numeric',
14+
hour: 'numeric',
15+
minute: 'numeric',
16+
second: 'numeric',
17+
hour12: false
18+
};
19+
20+
const convertedDate = new Intl.DateTimeFormat('en-US', options).format(originalDate);
21+
return convertedDate
22+
}
23+
24+
if(time_post){
25+
for(let i = 0; i < time_post.length; i++){
26+
try {
27+
const c_dt = convert_datetime(time_post[i].innerText)
28+
time_post[i].innerText = c_dt
29+
} catch (error) {
30+
31+
}
32+
33+
}
34+
}
235
function toggleSection(sectionId) {
336
const sections = document.querySelectorAll('.form-section');
437
sections.forEach(section => section.classList.remove('active'));
@@ -30,7 +63,7 @@ if (parts.length === 2) return parts.pop().split(';').shift();
3063
}
3164

3265
document.getElementById("make-post").addEventListener("click", function(e){
33-
66+
document.querySelector("#current-time").value = new Date()
3467
// Function to check if Summernote is empty
3568
function isSummernoteEmpty() {
3669
var content = $('#content').summernote('code').trim();

static/Javascript/sendcomment.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@ submit_comment_btn.addEventListener("click", function(e){
1010
let comment = document.getElementById("comment-comment")
1111
let comment_name = document.getElementById("comment-name")
1212
let comment_email = document.getElementById("comment-email")
13+
let comment_time = convert_datetime(new Date().toLocaleString());
1314

1415
if(!comment.value|| !comment_name.value){
1516
document.getElementById("comment-status").innerText = 'Name or Comment fields can\'t be empty!'
1617
setTimeout(() => {
1718
document.getElementById("comment-status").innerText = ''
1819
}, 4000);
19-
20+
submit_comment_btn.disabled = false
21+
submit_comment_btn.style.cursor = "pointer"
2022
return
2123
}
2224
data = {
2325
"comment": comment.value,
2426
"name": comment_name.value,
25-
"email": (!comment_email.value) ? "[email protected]" : comment_email.value
27+
"email": (!comment_email.value) ? "[email protected]" : comment_email.value,
28+
"time": comment_time
2629
}
2730
fetch("http://127.0.0.1:8000/comment/" + api_key + "/" + new URLSearchParams(url_parameters).get("id"), {
2831
body: JSON.stringify(data),
@@ -33,15 +36,15 @@ submit_comment_btn.addEventListener("click", function(e){
3336
})
3437
.then(response => {
3538
if(response.status == '201'){
36-
let time = new Date().toDateString();
39+
3740

3841
if(comment_container){
3942
const c_cont_inner = document.querySelector("#bloggit-comment-container #comments")
4043
c_cont_inner.innerHTML = `
4144
<div class="card mb-3">
4245
<div class="card-body">
4346
<h3 class="card-title">${comment_name.value}</h3>
44-
<time>${time}</time>
47+
<time>${comment_time}</time>
4548
<article class="card-text">${comment.value}</article>
4649
</div>
4750
</div>

templates/admin.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ <h3>Create New Post</h3>
5252
</section>
5353
<section class="form-group">
5454
<label for="category">Category:</label>
55-
<input type="text" class="form-control" id="category" name="category" required >
55+
<input type="text" class="form-control" id="category" name="category" >
5656
</section>
5757
<section class="form-check">
5858
<input type="checkbox" class="form-check-input" id="publish" name="publish">
5959
<label class="form-check-label" for="publish">Publish</label>
6060
</section>
61+
<input type="hidden" name="current-time" id="current-time" value="">
6162

6263
<button type="submit" class="btn btn-primary" name="submitpost" id="make-post">Create Post</button>
6364
</form>
@@ -82,7 +83,7 @@ <h2>Manage Posts</h2>
8283
<tr>
8384
<td class="numbering"></td>
8485
<td style="white-space: nowrap;">{{ post.title|truncatewords:5 }}</td>
85-
<td style="white-space: nowrap;">{{ post.time }}</td>
86+
<td style="white-space: nowrap;" class="time-of-post">{{ post.time }}</td>
8687

8788
<td><a href="edit/{{ post.custom_id }}" class="set-btn" id="dp{{ post.custom_id }}">Edit</a></td>
8889
<form action="" method="post">

0 commit comments

Comments
 (0)