Skip to content

Commit

Permalink
udpated functions all_posts anduser_profile to get datetime into JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
AlixLv committed Dec 12, 2024
1 parent b58d171 commit 3d4f62c
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 67 deletions.
112 changes: 108 additions & 4 deletions microblogging/users_app/fixtures/sample-followers.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"model": "users_app.follower",
"pk": 2,
"fields": {
"followed_id": 20,
"followed_id": 3,
"follower_id":1
}
},
{
"model": "users_app.follower",
"pk": 3,
"fields": {
"followed_id": 6,
"followed_id": 4,
"follower_id":1
}
},
Expand All @@ -35,7 +35,7 @@
"model": "users_app.follower",
"pk": 5,
"fields": {
"followed_id": 23,
"followed_id": 3,
"follower_id":2
}
},
Expand All @@ -51,8 +51,112 @@
"model": "users_app.follower",
"pk": 7,
"fields": {
"followed_id": 18,
"followed_id": 5,
"follower_id":1
}
},
{
"model": "users_app.follower",
"pk": 8,
"fields": {
"followed_id": 4,
"follower_id": 2
}
},
{
"model": "users_app.follower",
"pk": 9,
"fields": {
"followed_id": 1,
"follower_id": 3
}
},
{
"model": "users_app.follower",
"pk": 10,
"fields": {
"followed_id": 2,
"follower_id": 3
}
},
{
"model": "users_app.follower",
"pk": 11,
"fields": {
"followed_id": 4,
"follower_id": 3
}
},
{
"model": "users_app.follower",
"pk": 12,
"fields": {
"followed_id": 5,
"follower_id": 3
}
},
{
"model": "users_app.follower",
"pk": 13,
"fields": {
"followed_id": 1,
"follower_id": 4
}
},
{
"model": "users_app.follower",
"pk": 14,
"fields": {
"followed_id": 2,
"follower_id": 4
}
},
{
"model": "users_app.follower",
"pk": 15,
"fields": {
"followed_id": 3,
"follower_id": 4
}
},
{
"model": "users_app.follower",
"pk": 16,
"fields": {
"followed_id": 5,
"follower_id": 4
}
},
{
"model": "users_app.follower",
"pk": 17,
"fields": {
"followed_id": 1,
"follower_id": 5
}
},
{
"model": "users_app.follower",
"pk": 18,
"fields": {
"followed_id": 2,
"follower_id": 5
}
},
{
"model": "users_app.follower",
"pk": 19,
"fields": {
"followed_id": 3,
"follower_id": 5
}
},
{
"model": "users_app.follower",
"pk": 20,
"fields": {
"followed_id": 4,
"follower_id": 5
}
}
]
116 changes: 59 additions & 57 deletions microblogging/users_app/templates/first_template.html
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>

<head>
<title>My Little Books</title>
</head>
</head>

<body>
<div id="side-bar">
<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li>User: {{ user.get_username }}</li>
<li>
<form id="logout-form" method="post" action="{% url 'logout' %}">
{% csrf_token %}
<button type="submit" class="btn btn-link">Logout</button>
</form>
</li>
<!-- {% else %}
<body>
<div id="side-bar">
<ul class="sidebar-nav">
{% if user.is_authenticated %}
<li>User: {{ user.get_username }}</li>
<li>
<form id="logout-form" method="post" action="{% url 'logout' %}">
{% csrf_token %}
<button type="submit" class="btn btn-link">Logout</button>
</form>
</li>
<!-- {% else %}
<li><a href="{% url 'login' %}?next={{ request.path }}">Login</a></li> -->
{% endif %}
</ul>
</div>
<div id="content">
<!-- {% if user.is_authenticated %} -->
<ul>
{% for post in posts %}
<li>
Username : {{ post.user.username }} <br />
Post : {{ post.content }} <br />

<!-- on récupère tous les tags via la méthode .all()-->
Tags : {% if post.tags.all %}

<!-- on parcourt les tags et on affiche chacun d'entre eux-->
{% for tag in post.tags.all %}

<!-- ajout d'une gestion de virugle si plusieurs tags-->
{{tag.tag}} {% if not forloop.last %}, {% endif %} {% endfor %}
{%else%} Aucun tag {% endif %}

<br />
Date : {{ post.created_at }}

<br />
Commentaires: {% if post.parent_id.all %} {% for comment in
post.parent_id.all %}

<!--afficher contenu correspondant au parent_id-->
{% if comment == post.id.all %} {{post.id.content}} {% if not
forloop.last %}, {% endif %} {% endif %} {% endfor %} {% else %} Aucun
commentaire {% endif %}
<br />
</li>
<br />
{% endfor %}
</ul>
<!-- {% else %}
{% endif %}
</ul>
</div>
<div id="content">
<!-- {% if user.is_authenticated %} -->
<ul>
{% for post in posts %}
<li>
Username : {{ post.user.username }} <br>
Post : {{ post.content }} <br>
<!-- on récupère tous les tags via la méthode .all()-->
Tags : {% if post.tags.all %}
<!-- on parcourt les tags et on affiche chacun d'entre eux-->
{% for tag in post.tags.all %}
<!-- ajout d'une gestion de virugle si plusieurs tags-->
{{tag.tag}} {% if not forloop.last %}, {% endif %}
{% endfor %}
{% else %}
Aucun tag
{% endif %}
<br>
Date : {{ post.created_at }}
<br>
Commentaires: {% if post.parent_id.all %}
{% for comment in post.parent_id.all %}
<!--afficher contenu correspondant au parent_id-->
{% if comment == post.id.all %}
{{post.id.content}} {% if not forloop.last %}, {% endif %}
{% endif %}
{% endfor %}
{% else %}
Aucun commentaire
{% endif %}
<br>
</li> <br>
{% endfor %}
</ul>
<!-- {% else %}
<p>Please login to see this page.</p> -->
<!-- {% endif %} -->
</div>
</body>
</html>
{% endif %}
</div>
</body>

</html>
34 changes: 28 additions & 6 deletions microblogging/users_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,30 @@
from users_app import templates
from users_app.models import Post, Tag, Follower, AuthUser
from django.contrib.auth.models import User
from datetime import datetime


@login_required
def all_posts(request):
posts = Post.objects.select_related('user').all()
print(f"πŸ¦€ {posts}")


list_posts = []

for post in posts:
p = model_to_dict(post)
print(f"πŸͺ² {p}")
print(f"{post.created_at}")
p["tags"] = []
p["user"] = model_to_dict(post.user)["username"]
p["user_id"] = model_to_dict(post.user)["id"]
p["post_id"] = model_to_dict(post)["id"]
publish_date = post.created_at
publish_date_str = publish_date.strftime("%Y-%m-%d %H:%M:%S")
print(f"🐣 {publish_date_str}")
print(type(publish_date_str))
p["created_at"] = publish_date_str
for tag in post.tags.all():
p["tags"].append(tag.tag)
list_posts.append(p)
Expand Down Expand Up @@ -87,6 +98,12 @@ def user_profile(request, id):
posts_list = list(query_user_posts.values('id', 'user_id', 'content', 'parent_id', 'created_at'))
print(f"🐹 {posts_list}")

for post in posts_list:
created_date = post["created_at"]
created_date_str = created_date.strftime("%Y-%m-%d %H:%M:%S")
post["created_at"] = created_date_str


query_user_info = AuthUser.objects.get(id=id)
user_info = model_to_dict(query_user_info)
print(f"🐻 {user_info} ")
Expand All @@ -100,24 +117,29 @@ def user_profile(request, id):
}

print(f"πŸ§˜β€β™‚οΈ {user_info}")
print(type(user_info))


query_following = Follower.objects.filter(follower_id=id)

following_list = list(query_following.values('followed_id', 'follower_id'))
print(f"πŸ“ {following_list}")


#on merge les info des posts et les infos du user dans un dictionnaire:
response_data = {
'user': user_info,
'posts': posts_list,
'following': following_list
}

print(f"πŸ‹ {response_data}")
print(type(response_data))

return JsonResponse(response_data, safe=True)
context = {
'data': response_data,
}

data_json = json.dumps(response_data)
print(f"🐼 {data_json}")

return render(request, 'first_template.html', context)


# @csrf_exempt
Expand Down

0 comments on commit 3d4f62c

Please sign in to comment.