Skip to content

Commit

Permalink
Add favorite button
Browse files Browse the repository at this point in the history
  • Loading branch information
brianjp93 committed Nov 17, 2024
1 parent d308a09 commit ed02635
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 37 deletions.
18 changes: 17 additions & 1 deletion player/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
from django.urls import path, re_path
from django.urls import path, re_path, register_converter
from django.contrib.auth.views import LoginView, LogoutView
from django.views.generic import TemplateView
from . import views


app_name = 'player'


class BoolConverter:
regex = "[tT]rue|[fF]alse"

def to_python(self, value):
return value.lower() == 'true'

def to_url(self, value):
return 'true' if value else 'false'


register_converter(BoolConverter, "bool")


urlpatterns = [
re_path(r"(?P<region>(?:na|eune|euw|jp|kr|lan|las|br|oce|tr|ru))/(?P<name>[^\-]+)-(?P<tagline>\w+)/$", views.SummonerPage.as_view(), name="summoner-page"),
re_path(r"spectate/(?P<region>(?:na|eune|euw|jp|kr|lan|las|br|oce|tr|ru))/(?P<puuid>[^/]+)/$", views.SpectateView.as_view(), name="spectate"),
Expand All @@ -16,4 +31,5 @@
path("player/signup/", views.SignupView.as_view(), name="signup"),
path("player/activate/", views.EmailActivationView.as_view(), name="activate"),
path("player/account-created/", TemplateView.as_view(template_name="registration/account_created.html"), name="account-created"),
path("player/favorite/", views.FavoriteView.as_view(), name="favorite"),
]
32 changes: 30 additions & 2 deletions player/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import logging

from django.core.signing import BadSignature, SignatureExpired
from django.middleware.csrf import CsrfViewMiddleware
from django.views.decorators.csrf import csrf_protect
import requests

from django.http import Http404
from django.shortcuts import redirect, render
from django.shortcuts import get_object_or_404, redirect, render
from django.contrib.auth import authenticate, login, logout, get_user_model
from django.urls import reverse, reverse_lazy
from django.utils import timezone
Expand All @@ -26,7 +28,7 @@
from match.viewsapi import MatchBySummoner
from match import tasks as mt
from player.filters import SummonerAutocompleteFilter, SummonerMatchFilter
from player.models import EmailVerification, NameChange, Summoner
from player.models import EmailVerification, Favorite, NameChange, Summoner
from player.serializers import RankPositionSerializer
from player.viewsapi import get_by_puuid
from player.forms import SignupForm
Expand Down Expand Up @@ -153,6 +155,9 @@ def get_context_data(self, *args, **kwargs):
context['prev_url'] = prev_url
context["summoner"] = self.summoner
context["filterset"] = self.filterset
if self.request.user.is_authenticated:
context["is_favorite"] = self.request.user.favorite_set.filter(summoner=self.summoner)
self.request.user
context["namechanges"] = NameChange.objects.filter(
summoner=self.summoner,
).order_by("-created_date")
Expand Down Expand Up @@ -343,3 +348,26 @@ def get_context_data(self, **kwargs):
user.is_active = True
user.save()
return context


class FavoriteView(generic.TemplateView, CsrfViewMiddleware):
template_name = "player/_favorite.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
summoner_id = self.request.POST.get("summoner_id")
context["summoner_id"] = summoner_id
if self.request.user.is_authenticated:
context["is_favorite"] = self.request.user.favorite_set.filter(summoner__id=summoner_id).exists()
return context


def post(self, request, *args, **kwargs):
summoner_id = request.POST.get("summoner_id")
is_favorite = request.POST.get("is_favorite")
summoner = get_object_or_404(Summoner, id=summoner_id)
if is_favorite:
Favorite.objects.update_or_create(user=self.request.user, summoner=summoner)
else:
Favorite.objects.filter(user=self.request.user, summoner=summoner).delete()
return render(request, self.get_template_names(), self.get_context_data())
60 changes: 33 additions & 27 deletions templates/cotton/player/card.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,41 @@
{{ summoner.summoner_level }}
</div>
</div>
<div class="flex flex-col gap-y-1 mb-auto">
<c-tooltip>
<div class="mx-auto flex underline">
<div>
{{ summoner.riot_id_name }}
</div>
<div class="text-gray-400">
#{{ summoner.riot_id_tagline }}
<div class="flex flex-col gap-y-1 mb-auto w-full">
<div class="flex w-full">
<c-tooltip class="mx-auto">
<div class="mx-auto flex underline">
<div>
{{ summoner.riot_id_name }}
</div>
<div class="text-gray-400">
#{{ summoner.riot_id_tagline }}
</div>
</div>
<c-slot name="content">
<div class="flex flex-col gap-y-2 mx-2">
<h4 class="underline"
title="Only names found in imported games will be listed here.">
Previous Names
</h4>
{% for name in namechanges %}
<div>
{{ name.old_name }}
</div>
{% empty %}
<div>
No Names Found
</div>
{% endfor %}
</div>
</c-slot>
</c-tooltip>
<div class="ml-auto">
<c-player.favorite :summoner_id="summoner.id" :is_favorite="is_favorite" />
</div>
<c-slot name="content">
<div class="flex flex-col gap-y-2 mx-2">
<h4 class="underline"
title="Only names found in imported games will be listed here.">
Previous Names
</h4>
{% for name in namechanges %}
<div>
{{ name.old_name }}
</div>
{% empty %}
<div>
No Names Found
</div>
{% endfor %}
</div>
</c-slot>
</c-tooltip>
<div class="text-xs">
</div>

<div class="text-xs mx-auto">
<button
hx-get="{% url 'player:spectate' region=summoner.region puuid=summoner.puuid %}"
hx-target=".live-game-content"
Expand Down
41 changes: 41 additions & 0 deletions templates/cotton/player/favorite.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<c-vars class="" />

<div
tabindex="1"
class="
{{ class }} swap-me
"
>
<form
id="favorite-button-{{ summoner_id }}"
hx-post="{% url 'player:favorite' %}"
hx-target="closest .swap-me"
hx-swap="outerHTML"
hx-trigger="click"
class="cursor-pointer"
>
{% csrf_token %}
<input
name="summoner_id"
type="hidden"
value="{{ summoner_id }}">
<input
name="is_favorite"
class="hidden"
type="checkbox"
{% if not is_favorite %}
checked
{% endif %}
>
{% if is_favorite %}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="size-6">
<path d="m11.645 20.91-.007-.003-.022-.012a15.247 15.247 0 0 1-.383-.218 25.18 25.18 0 0 1-4.244-3.17C4.688 15.36 2.25 12.174 2.25 8.25 2.25 5.322 4.714 3 7.688 3A5.5 5.5 0 0 1 12 5.052 5.5 5.5 0 0 1 16.313 3c2.973 0 5.437 2.322 5.437 5.25 0 3.925-2.438 7.111-4.739 9.256a25.175 25.175 0 0 1-4.244 3.17 15.247 15.247 0 0 1-.383.219l-.022.012-.007.004-.003.001a.752.752 0 0 1-.704 0l-.003-.001Z" />
</svg>
{% else %}
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z" />
</svg>
{% endif %}
</form>
</div>

1 change: 1 addition & 0 deletions templates/player/_favorite.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<c-player.favorite :is_favorite="is_favorite" :summoner_id="summoner_id" />
20 changes: 14 additions & 6 deletions templates/registration/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

{% block content %}
<div class="container mx-auto">
<h1 class="mx-auto">Login to the Hardstuck Club</h1>
<form class="forms" action="" method="POST">
{% csrf_token %}
{{ form }}
<button class="btn btn-success w-full" type="submit">Log In</button>
</form>
<h1 class="mx-auto">
Login to the Hardstuck Club
</h1>
<form class="forms" action="" method="POST">
{% csrf_token %}
{{ form }}
<button class="btn btn-success w-full" type="submit">
Log In
</button>
</form>
<div>
Don't have an account?
<a href="{% url 'player:signup' %}">Sign Up</a>
</div>
</div>
{% endblock content %}
2 changes: 1 addition & 1 deletion templates/registration/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1 class="mx-auto">Sign Up</h1>
{% csrf_token %}
{{ form }}
<button
class="g-recaptcha btn btn-success w-full"
class="g-recaptcha btn btn-success w-full mt-4"
data-sitekey="{{ GOOGLE_RECAPTCHA_KEY }}"
data-callback='onSubmit'
data-action='submit'
Expand Down

0 comments on commit ed02635

Please sign in to comment.