Skip to content

Commit

Permalink
Fix avatar URL in templates and add default avatar to media directory
Browse files Browse the repository at this point in the history
Co-authored-by: Aktaş <[email protected]>
  • Loading branch information
temasictfic and yeaktas committed Feb 27, 2024
1 parent b830930 commit f551882
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 122 deletions.
2 changes: 1 addition & 1 deletion indianpong/pong/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UserProfileAdmin(admin.ModelAdmin):
list_display = ('username', 'email', 'displayname', 'avatar_thumbnail')
search_fields = ('username', 'email', 'displayname')
fieldsets = (
('User Information', {'fields': ('username', 'password', 'displayname', 'email', 'avatar', 'friends', 'elo_point', 'indian_wallet', 'is_online', 'is_offline', 'is_playing')}),
('User Information', {'fields': ('username', 'password', 'displayname', 'email', 'avatar', 'friends', 'elo_point', 'indian_wallet', 'is_online', 'is_playing')}),
('Dates', {'fields': ('date_joined', 'last_login')}),
('Roles', {'fields': ('is_staff', 'is_active', 'is_superuser', 'is_verified', 'is_42student', 'is_indianai')}),
('Permissions', {'fields': ('groups', 'user_permissions')}),
Expand Down
26 changes: 8 additions & 18 deletions indianpong/pong/apps.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
from django.apps import AppConfig
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.dispatch import receiver
from django.contrib.auth import get_user_model


class PongConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'pong'

def ready(self):
from .models import UserProfile # Buraya taşıdık
User = get_user_model()

@receiver(user_logged_in, sender=User)
def user_logged_in_handler(sender, request, user, **kwargs):
profile = UserProfile.objects.get(displayname=user.displayname)
profile.is_online = True
profile.is_offline = False
profile.save()
@receiver(user_logged_in)
def user_logged_in_handler(sender, request, **kwargs):
request.user.is_online = True
request.user.save()

@receiver(user_logged_out, sender=User)
def user_logged_out_handler(sender, request, user, **kwargs):
try:
profile = UserProfile.objects.get(displayname=user.displayname)
profile.is_online = False
profile.is_offline = True
profile.save()
except UserProfile.DoesNotExist:
pass
@receiver(user_logged_out)
def user_logged_out_handler(sender, request, **kwargs):
request.user.is_online = False
request.user.save()

4 changes: 2 additions & 2 deletions indianpong/pong/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ def userinfo(request):
return {}
"""
profile = get_object_or_404(UserProfile, username=request.user.username)
profile_avatar = profile.avatar.url if profile.avatar else "/static/assets/profile/profilephoto.jpeg"
return {'username': profile.username, 'avatar': profile_avatar}
#profile_avatar = profile.avatar.url if profile.avatar else "/static/assets/profile/profilephoto.jpeg"
return {'username': profile.username, 'avatar': profile.avatar.url}
8 changes: 8 additions & 0 deletions indianpong/pong/management/commands/initdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.files import File
from os import environ
import json
import shutil

class Command(BaseCommand):
def handle(self, *args, **options):
Expand Down Expand Up @@ -38,3 +39,10 @@ def handle(self, *args, **options):
StoreItem.objects.create(**item_data)

self.stdout.write(self.style.SUCCESS('Store data loaded successfully.'))

# Load default avatar to media directory
source_path = 'static/assets/profile/c4.jpg'
destination_path = 'media/c4.jpg'
shutil.copyfile(source_path, destination_path)


6 changes: 4 additions & 2 deletions indianpong/pong/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0 on 2024-02-21 15:52
# Generated by Django 5.0 on 2024-02-27 22:14

import datetime
import django.contrib.auth.models
Expand Down Expand Up @@ -74,13 +74,15 @@ class Migration(migrations.Migration):
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('displayname', models.CharField(blank=True, max_length=100, null=True)),
('email', models.EmailField(max_length=254, unique=True)),
('avatar', models.ImageField(blank=True, null=True, upload_to=pong.utils.get_upload_to)),
('avatar', models.ImageField(blank=True, default='c4.jpg', null=True, upload_to=pong.utils.get_upload_to)),
('is_verified', models.BooleanField(default=False)),
('is_42student', models.BooleanField(default=False)),
('is_indianai', models.BooleanField(default=False)),
('preffered_lang', models.CharField(blank=True, max_length=100, null=True)),
('indian_wallet', models.IntegerField(blank=True, default=0, null=True, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(9999)])),
('elo_point', models.IntegerField(blank=True, default=0, null=True, validators=[django.core.validators.MinValueValidator(0), django.core.validators.MaxValueValidator(99999)])),
('is_online', models.BooleanField(default=False)),
('is_playing', models.BooleanField(default=False)),
('friends', models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
Expand Down
86 changes: 37 additions & 49 deletions indianpong/pong/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class UserProfile(AbstractUser):
#id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
displayname = models.CharField(max_length=100, blank=True, null=True)
email = models.EmailField(unique=True, max_length=254)
avatar = models.ImageField(upload_to=get_upload_to, null=True, blank=True)
avatar = models.ImageField(upload_to=get_upload_to, null=True, blank=True, default="c4.jpg")
friends = models.ManyToManyField('self', symmetrical=False, blank=True)
social = models.OneToOneField('Social', on_delete=models.SET_NULL, null=True, blank=True)
#channel_name = models.CharField(max_length=100, blank=True, null=True)
Expand All @@ -47,66 +47,54 @@ class UserProfile(AbstractUser):
indian_wallet = models.IntegerField(blank=True, null=True, default=0, validators=[MinValueValidator(0), MaxValueValidator(9999)])
elo_point = models.IntegerField(blank=True, null=True, default=0, validators=[MinValueValidator(0), MaxValueValidator(99999)])
is_online = models.BooleanField(default=False)
is_offline = models.BooleanField(default=False)
is_playing = models.BooleanField(default=False)

def __str__(self) -> str:
return f"{self.username}"

@property
def thumbnail(self):
if self.avatar:
return mark_safe('<img src="%s" width="50" height="50" />' % (self.avatar.url))
""" if self.avatar:
return mark_safe('<img src="%s" width="50" height="50" />' % (self.avatar.url))
else:
return mark_safe('<img src="/static/assets/profile/profilephoto.jpeg" width="50" height="50" />')
return mark_safe('<img src="/static/assets/profile/profilephoto.jpeg" width="50" height="50" />') """

def get_rank_image(self):
if 1 <= self.elo_point <= 150:
return "iron.webp"
elif 150 < self.elo_point <= 200:
return "bronze.webp"
elif 200 < self.elo_point <= 250:
return "silver.webp"
elif 250 < self.elo_point <= 310:
return "gold.webp"
elif 310 < self.elo_point <= 360:
return "platinum.webp"
elif 360 < self.elo_point <= 420:
return "emerlad.webp"
elif 420 < self.elo_point <= 500:
return "diamond.webp"
elif 500 < self.elo_point <= 550:
return "master.webp"
elif 550 < self.elo_point <= 600:
return "grandmaster.webp"
elif 600 < self.elo_point:
return "challenger.webp"
else:
return "unranked.webp"

ranks = {
(1, 150): "iron.webp",
(150, 200): "bronze.webp",
(200, 250): "silver.webp",
(250, 310): "gold.webp",
(310, 360): "platinum.webp",
(360, 420): "emerlad.webp",
(420, 500): "diamond.webp",
(500, 550): "master.webp",
(550, 600): "grandmaster.webp",
(600, float('inf')): "challenger.webp"
}
for rank_range, rank_image in ranks.items():
if rank_range[0] <= self.elo_point <= rank_range[1]:
return rank_image
return "unranked.webp"

def get_rank_name(self):
if 1 <= self.elo_point <= 150:
return "Iron"
elif 150 < self.elo_point <= 200:
return "Bronze"
elif 200 < self.elo_point <= 250:
return "Silver"
elif 250 < self.elo_point <= 310:
return "Gold"
elif 310 < self.elo_point <= 360:
return "Platinum"
elif 360 < self.elo_point <= 420:
return "Emerald"
elif 420 < self.elo_point <= 500:
return "Diamond"
elif 500 < self.elo_point <= 550:
return "Master"
elif 550 < self.elo_point <= 600:
return "Grandmaster"
elif 600 < self.elo_point:
return "Challenger"
else:
return "Unranked"
ranks = {
(1, 150): "Iron",
(150, 200): "Bronze",
(200, 250): "Silver",
(250, 310): "Gold",
(310, 360): "Platinum",
(360, 420): "Emerald",
(420, 500): "Diamond",
(500, 550): "Master",
(550, 600): "Grandmaster",
(600, float('inf')): "Challenger"
}
for rank_range, rank_name in ranks.items():
if rank_range[0] <= self.elo_point <= rank_range[1]:
return rank_name
return "Unranked"


class UserItem(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion indianpong/pong/templates/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{% for user in users %}
<li>
<a href="{% url 'start_chat' user %}" data-link>
<img class="content-message-image" src="{% if user.avatar %}{{ user.avatar.url }}{% else %}/static/assets/profile/profilephoto.jpeg{% endif %}" alt="" />
<img class="content-message-image" src="{{ user.avatar.url }}" alt="" />

<span class="content-message-info">
<span class="content-message-name">{{ user }}</span>
Expand Down
12 changes: 3 additions & 9 deletions indianpong/pong/templates/friends.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
<div class="search-card">
<div class="card-container-wrapper">
<!-- Arama sonuçları -->
{% if friends %}
{% for friend in friends %}
<div class="card-containers">
<div class="profile-card">
<div class="image">
{% if friend.avatar %}
<a href="/profile/{{friend.username}}"><img src="{{friend.avatar.url}}" alt="" class="profile-img" {% if friend.is_online and not friend.is_playing %} style="border: 3px solid #4CAF4F" {% elif friend.is_playing %} style="border: 3px solid #ffa600" {% else %} style="border: 3px solid #ff0000" {% endif %} /></a>
{% else %}
<a href="/profile/{{friend.username}}"><img src="/static/assets/profile/profilephoto.jpeg" alt="" class="profile-img" {% if friend.is_online and not friend.is_playing %} style="border: 3px solid #4CAF4F" {% elif friend.is_playing %} style="border: 3px solid #ffa600" {% else %} style="border: 3px solid #ff0000" {% endif %} /></a>
{% endif %}
<a href="/profile/{{friend.username}}"><img src="{{friend.avatar.url}}" alt="" class="profile-img" {% if friend.is_online and not friend.is_playing %} style="border: 3px solid #4CAF4F" {% elif friend.is_playing %} style="border: 3px solid #ffa600" {% else %} style="border: 3px solid #ff0000" {% endif %} /></a>
</div>
<div class="text-data">
<span class="name">{{friend.username}}</span>
Expand Down Expand Up @@ -66,10 +61,9 @@
</div>
</div>
</div>
{% empty %}
<p style="color:orange; font-weight:bold; text-align: center;">No friends found.</p>
{% endfor %}
{% else %}
<p style="color:orange; font-weight:bold; text-align: center;">No friends found.</p>
{% endif %}
</div>
<nav aria-label="Page navigation example " class="mt-4">
<ul class="pagination justify-content-center">
Expand Down
6 changes: 1 addition & 5 deletions indianpong/pong/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
{% endif %}
<div class="card-body text-center">
<!-- Avatar -->
{% if not profile.avatar %}
<img src="/static/assets/profile/profilephoto.jpeg" alt="avatar" class="profile-image-circ" {% if profile.is_online and not profile.is_playing %} style="border: 3px solid #4CAF4F; aspect-ratio: 1/1; object-fit: cover;" {% elif profile.is_playing %} style="border: 3px solid #ffa600; aspect-ratio: 1/1; object-fit: cover;" {% else %} style="border: 3px solid #ff0000; aspect-ratio: 1/1; object-fit: cover;" {% endif %} >
{% else %}
<img src="{{ profile.avatar.url }}" alt="avatar" class="profile-image-circ" {% if profile.is_online and not profile.is_playing %} style="border: 3px solid #4CAF4F; aspect-ratio: 1/1; object-fit: cover;" {% elif profile.is_playing %} style="border: 3px solid #ffa600; aspect-ratio: 1/1; object-fit: cover;" {% else %} style="border: 3px solid #ff0000; aspect-ratio: 1/1; object-fit: cover;" {% endif %}>
{% endif %}
<img src="{{ profile.avatar.url }}" alt="avatar" class="profile-image-circ" {% if profile.is_online and not profile.is_playing %} style="border: 3px solid #4CAF4F; aspect-ratio: 1/1; object-fit: cover;" {% elif profile.is_playing %} style="border: 3px solid #ffa600; aspect-ratio: 1/1; object-fit: cover;" {% else %} style="border: 3px solid #ff0000; aspect-ratio: 1/1; object-fit: cover;" {% endif %}>
<!-- isim -->
<h5 class="my-3" style="margin-bottom: 0 !important;">{{profile.username}}</h5>
<h7 class="my-5" style="margin-top: -3em !important; margin-bottom: 10px !important;">{{profile.displayname}}</h7>
Expand Down
4 changes: 2 additions & 2 deletions indianpong/pong/templates/room.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{% for user in users %}
<li>
<a href="{% url 'start_chat' user %}" data-link>
<img class="content-message-image" src="{% if user.avatar %}{{ user.avatar.url }}{% else %}/static/assets/profile/profilephoto.jpeg{% endif %}" alt="" />
<img class="content-message-image" src="{{ user.avatar.url }}" alt="" />
<span class="content-message-info">
<span class="content-message-name">{{ user }}</span>
</span>
Expand All @@ -73,7 +73,7 @@
<div class="conversation-top">
<button type="button" class="conversation-back"><i class="bi bi-arrow-left-circle-fill"></i></button>
<div class="conversation-user">
<img class="content-message-image" src="{% if room.first_user == request.user %} {% if room.second_user.avatar %} {{room.second_user.avatar.url}} {% else %} /static/assets/profile/profilephoto.jpeg {% endif %} {% else %} {% if room.first_user.avatar %} {{room.first_user.avatar.url}} {% else %} /static/assets/profile/profilephoto.jpeg {% endif %} {% endif %}" alt="" />
<img class="content-message-image" src="{% if room.first_user == request.user %} {{ room.second_user.avatar.url }} {% else %} {{ room.first_user.avatar.url }} {% endif %}" alt="" />
<div id="userNameOnChat" class="conversation-user-name" data-username="{{ room.first_user|default:'' }}" data-seconduser="{{ room.second_user|default:'' }}" >
{% if room.first_user == request.user %}
{{ room.second_user }}
Expand Down
Loading

0 comments on commit f551882

Please sign in to comment.