From cde4cb2a50dbe89bdb55d09d94a4993ade9a6f1f Mon Sep 17 00:00:00 2001 From: "Chen, Chien-ting" Date: Mon, 14 Nov 2022 00:06:07 +0800 Subject: [PATCH] solve the provlem of timeline updating --- khaikang/urls.py | 2 + khaikang/views.py | 41 +++++++- templates/index.html | 133 +++++++++++++++++++++---- templates/registration/logged_out.html | 10 ++ 4 files changed, 165 insertions(+), 21 deletions(-) create mode 100644 templates/registration/logged_out.html diff --git a/khaikang/urls.py b/khaikang/urls.py index 7ea2584..816c025 100644 --- a/khaikang/urls.py +++ b/khaikang/urls.py @@ -24,4 +24,6 @@ urlpatterns = [ path('home/', views.home, name='home'), path('signup/', views.signup), path('api/post', views.api_post), + path('api/get_recent_posts_counter', views.api_get_recent_posts_counter), + path('api/get_latest_posts', views.api_get_latest_posts), ] diff --git a/khaikang/views.py b/khaikang/views.py index e56c555..656adef 100644 --- a/khaikang/views.py +++ b/khaikang/views.py @@ -6,6 +6,36 @@ from django.utils import timezone from django.contrib.auth.forms import UserCreationForm from django.shortcuts import redirect from django.contrib.auth.models import AnonymousUser +from datetime import datetime +from django.http import JsonResponse +from django.core import serializers +from django.db.models import Q + +def api_get_latest_posts(request): + if request.method == 'POST': + post_body_orig = request.body.decode('utf-8') + post_body_json = json.loads(post_body_orig) + latest_time = post_body_json["latest_time"] + latest_datetime_object = datetime.strptime(latest_time, '%Y-%m-%d %H:%M:%S.%f%z') + newer_posts = Post.objects.filter(post_time__gte=latest_datetime_object) + newer_posts_list = [{"id" : o.pk, + "post_time" : datetime.strftime(o.post_time, "%Y-%m-%d %H:%M:%S.%f%z"), + "poster_username" : o.poster.username, + "poster_shown_name":o.poster.shown_name , + "text": o.text} + for o in newer_posts] + return JsonResponse({'newer_posts':newer_posts_list}) + +def api_get_recent_posts_counter(request): + if request.method == 'POST': + post_body_orig = request.body.decode('utf-8') + post_body_json = json.loads(post_body_orig) + latest_time = post_body_json["latest_time"] + latest_datetime_object = datetime.strptime(latest_time, '%Y-%m-%d %H:%M:%S.%f%z') + newer_posts_len = Post.objects.filter(post_time__gte=latest_datetime_object).exclude(Q(poster=request.user) & Q(post_time=latest_datetime_object)).__len__() + return JsonResponse({'newer_posts_num':newer_posts_len}) + + def api_post(request): if request.method == 'POST': @@ -58,14 +88,19 @@ def signup(request): def home(request): + if request.user == AnonymousUser(): + return redirect('/account/login') # redirect to main page + public_timeline_list = Post.objects.filter(privilage = 'public').order_by('-id')[:10] - - print(public_timeline_list) - + latest_received_time = timezone.now() + oldest_received_time = public_timeline_list[9].post_time + template = loader.get_template('index.html') context = { + 'latest_received_time' : latest_received_time, + 'oldest_received_time' : oldest_received_time, 'public_timeline_list': public_timeline_list, } return HttpResponse(template.render(context, request)) \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index cfc3e75..60bc6cb 100644 --- a/templates/index.html +++ b/templates/index.html @@ -26,20 +26,28 @@
+
+
{{latest_received_time|date:"Y-m-d H:i:s.u"}}+0000
+ {% for public_post in public_timeline_list %}
{{public_post.poster.shown_name}} - at {{public_post.post_time|date:"Y-m-d H:i:s"}}+0000
- {{public_post.text}}
+ at {{public_post.post_time|date:"Y-m-d H:i:s.u"}}+0000
+ {{public_post.text}}
+ ↩ī¸ + - 🔁 + - ⭐ +
{% endfor %} +
{{oldest_received_time|date:"Y-m-d H:i:s.u"}}+0000
+ {% endblock %} \ No newline at end of file diff --git a/templates/registration/logged_out.html b/templates/registration/logged_out.html new file mode 100644 index 0000000..dd8c1d2 --- /dev/null +++ b/templates/registration/logged_out.html @@ -0,0 +1,10 @@ +{% extends "base_generic.html" %} + + +{% block content %} + +You have logged out. + +go to Main Page. + +{% endblock %} \ No newline at end of file