Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made Setting for test,and merged before branch main #7

Merged
merged 18 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.shortcuts import redirect, render
from django.views.generic import CreateView, ListView

from tweets.models import Tweet
from tweets.models import Favorite, Tweet

from .forms import LoginForm, SignupForm
from .models import Connection, User
Expand Down Expand Up @@ -36,9 +36,10 @@ class FollowingListView(ListView):

def get_context_data(self):
context = {
"following_list": Connection.objects.select_related("following").filter(
follower=User.objects.prefetch_related("followee").get(username=self.kwargs["username"])
),
"following_list": Connection.objects.select_related("following")
.order_by("created_at")
.reverse()
.filter(follower=User.objects.prefetch_related("followee").get(username=self.kwargs["username"])),
"username": self.kwargs["username"],
}
return context
Expand All @@ -51,9 +52,10 @@ class FollowerListView(ListView):

def get_context_data(self):
context = {
"follower_list": Connection.objects.select_related("follower").filter(
following=User.objects.prefetch_related("follower").get(username=self.kwargs["username"])
),
"follower_list": Connection.objects.select_related("follower")
.order_by("created_at")
.reverse()
.filter(following=User.objects.prefetch_related("follower").get(username=self.kwargs["username"])),
"username": self.kwargs["username"],
}
return context
Expand All @@ -62,14 +64,18 @@ def get_context_data(self):
@login_required
def userprofile_view(request, username):
user = User.objects.get(username=username)
tweets_list = Tweet.objects.select_related("user").filter(user=user)
tweets_list = Tweet.objects.select_related("user").prefetch_related("tweet_liked").filter(user=user)
tweets_liked = Favorite.objects.filter(user=request.user).values_list("tweet_id", flat=True)
n_follower = Connection.objects.select_related("follower").filter(following=user).all().count()
n_following = Connection.objects.select_related("following").filter(follower=user).all().count()
return render(
request,
"tweets/profile.html",
{"username": username, "tweets_list": tweets_list, "n_follower": n_follower, "n_following": n_following},
)
context = {
"username": username,
"tweets_list": tweets_list,
"tweets_liked": tweets_liked,
"n_follower": n_follower,
"n_following": n_following,
}
return render(request, "tweets/profile.html", context)


@login_required
Expand Down
4 changes: 4 additions & 0 deletions mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
"accounts.apps.AccountsConfig",
"tweets.apps.TweetsConfig",
"welcome.apps.WelcomeConfig",
# "debug_toolbar",
]

MIDDLEWARE = [
# "debug_toolbar.middleware.DebugToolbarMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
Expand Down Expand Up @@ -133,3 +135,5 @@
LOGIN_URL = "accounts:login"

LOGOUT_REDIRECT_URL = "/"

# INTERNAL_IPS = ["127.0.0.1"]
1 change: 1 addition & 0 deletions mysite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
path("accounts/", include("accounts.urls")),
path("tweets/", include("tweets.urls"), name="tweets"),
path("", include("welcome.urls"), name="welcome"),
# path("__debug__/", include("debug_toolbar.urls")),
]
3 changes: 2 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
{% block content %}
{% endblock %}
</main>
<script src="" async defer></script>
{% block extlascripts %}
{% endblock %}
</body>

</html>
2 changes: 1 addition & 1 deletion templates/tweets/delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% endblock %}

{% block content %}
<h1>This is the home!</h1>
<h1>Deleted this tweet</h1>
{% for tweet in tweets %}
<p>
{{ tweet.user }}<br>
Expand Down
48 changes: 47 additions & 1 deletion templates/tweets/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,59 @@
<h1>This is Tweet's Detail!</h1>
{% for tweet in tweets %}
<p>
<li><a href="{% url 'accounts:user_profile' tweet.user.username %}">{{tweet.user}}</a></li>
<a href="{% url 'accounts:user_profile' tweet.user.username %}">{{tweet.user}}</a><br>
{{ tweet.title }}<br>
{{ tweet.content}}<br>
{{ tweet.created_at }}
<a>Like: <span id="ajax-like-count-0">{{tweet.tweet_liked.count }}</span></a>
{% if tweet.id in tweets_liked %}
<button id="ajax-like-0" data-url='/tweets/{{tweet.id}}/unlike/'>UnLike</button>
{% else %}
<button id="ajax-like-0" data-url='/tweets/{{tweet.id}}/like/'>Like</button>
{% endif %}
{% if tweet.user == request.user %}
<a href="/tweets/{{tweet.id}}/delete">delete</a>
{% endif %}
</p>
{% endfor %}
{% endblock %}
{% block extlascripts %}
<script>
{% for tweet in tweets %}
document.getElementById(`ajax-like-0`).addEventListener("click",e=>{
e.preventDefault();
const url = document.getElementById(`ajax-like-0`).dataset.url;
fetch(
url,{
method:"POST",
body:`pk={{tweet.id}}`,
headers:{
'Content-Type':'application/x-www-form-urlencoded; charset=utf-8',
'X-CSRFToken':`{{ csrf_token }}`
}
}
).then(
response=>{
return response.json();
}
).then(
response=>{
console.log(response)
count = document.getElementById(`ajax-like-count-0`);
console.log(count)
count.textContent = response.n_liked;
console.log('not error')
if (response.liked == true){
document.getElementById(`ajax-like-0`).textContent = 'UnLike';
document.getElementById(`ajax-like-0`).dataset.url ='/tweets/{{tweet.id}}/unlike/'
}else{
document.getElementById(`ajax-like-0`).textContent = 'Like';
document.getElementById(`ajax-like-0`).dataset.url ='/tweets/{{tweet.id}}/like/'
}
}
);
});
{% endfor %}

</script>
{% endblock %}
49 changes: 48 additions & 1 deletion templates/tweets/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,60 @@
{% block content %}

<h1>This is the home!</h1>
Tweet_number:{{tweets_count}}
{% for tweet in tweets_list %}
<p>
{{ tweet.user }}<br>
<a href="{% url 'accounts:user_profile' tweet.user.username %}">{{tweet.user}}</a><br>
{{ tweet.title }}<br>
{{ tweet.content}}<br>
{{ tweet.created_at }}
<a>Like: <span id="ajax-like-count-{{tweet.id}}">{{tweet.tweet_liked.count }}</span></a>
{% if tweet.id in tweets_liked %}
<button id="ajax-like-{{tweet.id}}" data-url='/tweets/{{tweet.id}}/unlike/'>UnLike</button>
{% else %}
<button id="ajax-like-{{tweet.id}}" data-url='/tweets/{{tweet.id}}/like/'>Like</button>
{% endif %}
<a href="/tweets/{{tweet.id}}">Detail</a>
</p>
{% endfor %}
{% endblock %}
{% block extlascripts %}
<script>
{% for tweet in tweets_list %}
document.getElementById(`ajax-like-{{tweet.id}}`).addEventListener("click",e=>{
e.preventDefault();
const url = document.getElementById(`ajax-like-{{tweet.id}}`).dataset.url;
fetch(
url,{
method:"POST",
body:`pk={{tweet.id}}`,
headers:{
'Content-Type':'application/x-www-form-urlencoded; charset=utf-8',
'X-CSRFToken':`{{ csrf_token }}`
}
}
).then(
response=>{
return response.json();
}
).then(
response=>{
console.log(response)
count = document.getElementById(`ajax-like-count-{{tweet.id}}`);
console.log(count)
count.textContent = response.n_liked;
console.log('not error')
if (response.liked == true){
document.getElementById(`ajax-like-{{tweet.id}}`).textContent = 'UnLike';
document.getElementById(`ajax-like-{{tweet.id}}`).dataset.url ='/tweets/{{tweet.id}}/unlike/'
}else{
document.getElementById(`ajax-like-{{tweet.id}}`).textContent = 'Like';
document.getElementById(`ajax-like-{{tweet.id}}`).dataset.url ='/tweets/{{tweet.id}}/like/'
}
}
);
});
{% endfor %}

</script>
{% endblock %}
50 changes: 48 additions & 2 deletions templates/tweets/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,62 @@ <h1>This is {{ username }} home!</h1>
<a href="/accounts/{{username}}/following_list">フォロー一覧</a>
<a href="/accounts/{{username}}/follower_list">フォロワ一一覧</a>


{% for tweet in tweets_list %}
<p>
{{ tweet.user }}<br>
{{ tweet.title }}<br>
{{ tweet.content}}<br>
{{ tweet.created_at}}
{{ tweet.created_at }}
<a>Like: <span id="ajax-like-count-{{tweet.id}}">{{tweet.tweet_liked.count }}</span></a>
{% if tweet.id in tweets_liked %}
<button id="ajax-like-{{tweet.id}}" data-url='/tweets/{{tweet.id}}/unlike/'>UnLike</button>
{% else %}
<button id="ajax-like-{{tweet.id}}" data-url='/tweets/{{tweet.id}}/like/'>Like</button>
{% endif %}
<a href="/tweets/{{tweet.id}}">Detail</a>
{% if tweet.user == request.user %}
<a href="/tweets/{{tweet.id}}/delete">delete</a>
{% endif %}
</p>
{% endfor %}
{% endblock %}
{% block extlascripts %}
<script>
{% for tweet in tweets_list %}
document.getElementById(`ajax-like-{{tweet.id}}`).addEventListener("click",e=>{
e.preventDefault();
const url = document.getElementById(`ajax-like-{{tweet.id}}`).dataset.url;
fetch(
url,{
method:"POST",
body:`pk={{tweet.id}}`,
headers:{
'Content-Type':'application/x-www-form-urlencoded; charset=utf-8',
'X-CSRFToken':`{{ csrf_token }}`
}
}
).then(
response=>{
return response.json();
}
).then(
response=>{
console.log(response)
count = document.getElementById(`ajax-like-count-{{tweet.id}}`);
console.log(count)
count.textContent = response.n_liked;
console.log('not error')
if (response.liked == true){
document.getElementById(`ajax-like-{{tweet.id}}`).textContent = 'UnLike';
document.getElementById(`ajax-like-{{tweet.id}}`).dataset.url ='/tweets/{{tweet.id}}/unlike/'
}else{
document.getElementById(`ajax-like-{{tweet.id}}`).textContent = 'Like';
document.getElementById(`ajax-like-{{tweet.id}}`).dataset.url ='/tweets/{{tweet.id}}/like/'
}
}
);
});
{% endfor %}

</script>
{% endblock %}
3 changes: 2 additions & 1 deletion tweets/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# from django.contrib import adm in
from django.contrib import admin

from .models import Tweet
from .models import Favorite, Tweet

admin.site.register(Tweet)
admin.site.register(Favorite)
43 changes: 43 additions & 0 deletions tweets/migrations/0007_favorite_favorite_favorite_unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.2.11 on 2024-07-20 11:31

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("tweets", "0006_alter_tweet_id"),
]

operations = [
migrations.CreateModel(
name="Favorite",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("created_at", models.DateTimeField(default=django.utils.timezone.now)),
(
"tweet",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, related_name="tweet", to="tweets.tweet"
),
),
(
"user",
models.ForeignKey(
default="",
on_delete=django.db.models.deletion.CASCADE,
related_name="user",
to=settings.AUTH_USER_MODEL,
),
),
],
),
migrations.AddConstraint(
model_name="favorite",
constraint=models.UniqueConstraint(fields=("user", "tweet"), name="favorite_unique"),
),
]
21 changes: 21 additions & 0 deletions tweets/migrations/0008_alter_favorite_tweet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.11 on 2024-09-16 12:11

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("tweets", "0007_favorite_favorite_favorite_unique"),
]

operations = [
migrations.AlterField(
model_name="favorite",
name="tweet",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, related_name="tweet_liked", to="tweets.tweet"
),
),
]
26 changes: 26 additions & 0 deletions tweets/migrations/0009_alter_favorite_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.11 on 2024-09-16 12:44

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("tweets", "0008_alter_favorite_tweet"),
]

operations = [
migrations.AlterField(
model_name="favorite",
name="user",
field=models.ForeignKey(
default="",
on_delete=django.db.models.deletion.CASCADE,
related_name="user_liked",
to=settings.AUTH_USER_MODEL,
),
),
]
Loading
Loading