Refactor HTML and Python code for improved readability and consistency
This commit is contained in:
parent
250aaac2a0
commit
b8a1cc4ff4
3 changed files with 83 additions and 114 deletions
|
|
@ -50,16 +50,16 @@
|
||||||
max-width: 120px;
|
max-width: 120px;
|
||||||
max-height: 120px;
|
max-height: 120px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hover-effect {
|
.hover-effect {
|
||||||
transition: all 0.3s ease;
|
transition: all 0.3s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hover-effect:hover {
|
.hover-effect:hover {
|
||||||
transform: translateY(-5px);
|
transform: translateY(-5px);
|
||||||
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1) !important;
|
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1) !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
.card-header h3 {
|
.card-header h3 {
|
||||||
font-size: 1.5rem;
|
font-size: 1.5rem;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,50 +11,50 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if games_by_org %}
|
{% if games_by_org %}
|
||||||
{% for organization, games in games_by_org.items %}
|
{% for organization, games in games_by_org.items %}
|
||||||
<div class="card mb-4 shadow-sm">
|
<div class="card mb-4 shadow-sm">
|
||||||
<div class="card-header bg-dark text-white">
|
<div class="card-header bg-dark text-white">
|
||||||
<h3 class="mb-0">
|
<h3 class="mb-0">
|
||||||
<i class="fas fa-building me-2"></i>{{ organization.name }}
|
<i class="fas fa-building me-2"></i>{{ organization.name }}
|
||||||
</h3>
|
</h3>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<div class="row row-cols-1 row-cols-md-3 g-4">
|
<div class="row row-cols-1 row-cols-md-3 g-4">
|
||||||
{% for item in games %}
|
{% for item in games %}
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<div class="card h-100 border-0 shadow-sm hover-effect">
|
<div class="card h-100 border-0 shadow-sm hover-effect">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h5 class="card-title">
|
<h5 class="card-title">
|
||||||
<a href="{% url 'twitch:game_detail' item.game.id %}" class="text-decoration-none">
|
<a href="{% url 'twitch:game_detail' item.game.id %}" class="text-decoration-none">
|
||||||
{{ item.game.display_name }}
|
{{ item.game.display_name }}
|
||||||
</a>
|
</a>
|
||||||
</h5>
|
</h5>
|
||||||
<div class="d-flex justify-content-between mt-3">
|
<div class="d-flex justify-content-between mt-3">
|
||||||
<span class="badge bg-secondary">
|
<span class="badge bg-secondary">
|
||||||
<i class="fas fa-gift me-1"></i> {{ item.campaign_count }} Campaigns
|
<i class="fas fa-gift me-1"></i> {{ item.campaign_count }} Campaigns
|
||||||
</span>
|
</span>
|
||||||
{% if item.active_count > 0 %}
|
{% if item.active_count > 0 %}
|
||||||
<span class="badge bg-success">
|
<span class="badge bg-success">
|
||||||
<i class="fas fa-circle-play me-1"></i> {{ item.active_count }} Active
|
<i class="fas fa-circle-play me-1"></i> {{ item.active_count }} Active
|
||||||
</span>
|
</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="card-footer bg-transparent border-0">
|
|
||||||
<a href="{% url 'twitch:game_detail' item.game.id %}" class="btn btn-sm btn-primary">
|
|
||||||
<i class="fas fa-eye me-1"></i> View Campaigns
|
|
||||||
</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
<div class="card-footer bg-transparent border-0">
|
||||||
|
<a href="{% url 'twitch:game_detail' item.game.id %}" class="btn btn-sm btn-primary">
|
||||||
|
<i class="fas fa-eye me-1"></i> View Campaigns
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
|
||||||
{% else %}
|
|
||||||
<div class="alert alert-info">
|
|
||||||
<i class="fas fa-info-circle me-2"></i> No games found.
|
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
{% else %}
|
||||||
|
<div class="alert alert-info">
|
||||||
|
<i class="fas fa-info-circle me-2"></i> No games found.
|
||||||
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
113
twitch/views.py
113
twitch/views.py
|
|
@ -53,7 +53,7 @@ class DropCampaignListView(ListView):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
|
|
||||||
# Load all games in a single query instead of multiple queries per game
|
# Load all games in a single query instead of multiple queries per game
|
||||||
context["games"] = Game.objects.all().order_by('display_name')
|
context["games"] = Game.objects.all().order_by("display_name")
|
||||||
|
|
||||||
# Add status options for filtering
|
# Add status options for filtering
|
||||||
context["status_options"] = [status[0] for status in DropCampaign.STATUS_CHOICES]
|
context["status_options"] = [status[0] for status in DropCampaign.STATUS_CHOICES]
|
||||||
|
|
@ -74,25 +74,25 @@ class DropCampaignDetailView(DetailView):
|
||||||
model = DropCampaign
|
model = DropCampaign
|
||||||
template_name = "twitch/campaign_detail.html"
|
template_name = "twitch/campaign_detail.html"
|
||||||
context_object_name = "campaign"
|
context_object_name = "campaign"
|
||||||
|
|
||||||
def get_object(self, queryset=None):
|
def get_object(self, queryset=None):
|
||||||
"""Get the campaign object with related data prefetched.
|
"""Get the campaign object with related data prefetched.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
queryset: Optional queryset to use.
|
queryset: Optional queryset to use.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
DropCampaign: The campaign object with prefetched relations.
|
DropCampaign: The campaign object with prefetched relations.
|
||||||
"""
|
"""
|
||||||
# Prefetch all needed related objects in a single query
|
# Prefetch all needed related objects in a single query
|
||||||
if queryset is None:
|
if queryset is None:
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
|
|
||||||
queryset = queryset.select_related("game", "owner")
|
queryset = queryset.select_related("game", "owner")
|
||||||
|
|
||||||
# We don't need to prefetch time_based_drops here since we're fetching them separately in get_context_data
|
# We don't need to prefetch time_based_drops here since we're fetching them separately in get_context_data
|
||||||
# with proper ordering and prefetching of benefits
|
# with proper ordering and prefetching of benefits
|
||||||
|
|
||||||
return super().get_object(queryset=queryset)
|
return super().get_object(queryset=queryset)
|
||||||
|
|
||||||
def get_context_data(self, **kwargs) -> dict[str, Any]:
|
def get_context_data(self, **kwargs) -> dict[str, Any]:
|
||||||
|
|
@ -151,86 +151,70 @@ class GameListView(ListView):
|
||||||
|
|
||||||
def get_context_data(self, **kwargs) -> dict[str, Any]:
|
def get_context_data(self, **kwargs) -> dict[str, Any]:
|
||||||
"""Add additional context data with games grouped by organization.
|
"""Add additional context data with games grouped by organization.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
**kwargs: Additional keyword arguments.
|
**kwargs: Additional keyword arguments.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: Context data with games grouped by organization.
|
dict: Context data with games grouped by organization.
|
||||||
"""
|
"""
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
|
|
||||||
# Create a dictionary to hold games by organization
|
# Create a dictionary to hold games by organization
|
||||||
games_by_org = {}
|
games_by_org = {}
|
||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
|
|
||||||
# Step 1: Get all organizations with games in a single query
|
# Step 1: Get all organizations with games in a single query
|
||||||
# We'll prefetch the games related to each organization through drop_campaigns
|
# We'll prefetch the games related to each organization through drop_campaigns
|
||||||
organizations_with_games = Organization.objects.filter(
|
organizations_with_games = Organization.objects.filter(drop_campaigns__isnull=False).distinct().order_by("name")
|
||||||
drop_campaigns__isnull=False
|
|
||||||
).distinct().order_by('name')
|
|
||||||
|
|
||||||
# Step 2: Get all game-organization relationships in a single efficient query
|
# Step 2: Get all game-organization relationships in a single efficient query
|
||||||
# This query gets all games with their campaign counts and organization info
|
# This query gets all games with their campaign counts and organization info
|
||||||
game_org_relations = DropCampaign.objects.values(
|
game_org_relations = DropCampaign.objects.values("game_id", "owner_id", "owner__name").annotate(
|
||||||
'game_id', 'owner_id', 'owner__name'
|
campaign_count=Count("id", distinct=True),
|
||||||
).annotate(
|
active_count=Count("id", filter=Q(start_at__lte=now, end_at__gte=now, status="ACTIVE"), distinct=True),
|
||||||
campaign_count=Count('id', distinct=True),
|
|
||||||
active_count=Count(
|
|
||||||
'id',
|
|
||||||
filter=Q(
|
|
||||||
start_at__lte=now,
|
|
||||||
end_at__gte=now,
|
|
||||||
status="ACTIVE"
|
|
||||||
),
|
|
||||||
distinct=True
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Step 3: Get all games in a single query with their display names
|
# Step 3: Get all games in a single query with their display names
|
||||||
all_games = {
|
all_games = {game.id: game for game in Game.objects.all()}
|
||||||
game.id: game for game in Game.objects.all()
|
|
||||||
}
|
|
||||||
|
|
||||||
# Step 4: Create a mapping of organization_id to organization_name
|
# Step 4: Create a mapping of organization_id to organization_name
|
||||||
org_names = {
|
org_names = {org.id: org.name for org in organizations_with_games}
|
||||||
org.id: org.name for org in organizations_with_games
|
|
||||||
}
|
|
||||||
|
|
||||||
# Step 5: Group games by organization
|
# Step 5: Group games by organization
|
||||||
game_org_map = {}
|
game_org_map = {}
|
||||||
for relation in game_org_relations:
|
for relation in game_org_relations:
|
||||||
org_id = relation['owner_id']
|
org_id = relation["owner_id"]
|
||||||
game_id = relation['game_id']
|
game_id = relation["game_id"]
|
||||||
|
|
||||||
if org_id not in game_org_map:
|
if org_id not in game_org_map:
|
||||||
game_org_map[org_id] = {}
|
game_org_map[org_id] = {}
|
||||||
|
|
||||||
if game_id not in game_org_map[org_id]:
|
if game_id not in game_org_map[org_id]:
|
||||||
game = all_games.get(game_id)
|
game = all_games.get(game_id)
|
||||||
if game:
|
if game:
|
||||||
game_org_map[org_id][game_id] = {
|
game_org_map[org_id][game_id] = {
|
||||||
'game': game,
|
"game": game,
|
||||||
'campaign_count': relation['campaign_count'],
|
"campaign_count": relation["campaign_count"],
|
||||||
'active_count': relation['active_count']
|
"active_count": relation["active_count"],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Step 6: Convert the nested dictionary to the format expected by the template
|
# Step 6: Convert the nested dictionary to the format expected by the template
|
||||||
for org_id, games in game_org_map.items():
|
for org_id, games in game_org_map.items():
|
||||||
if org_id in org_names:
|
if org_id in org_names:
|
||||||
# Create an Organization-like object with id and name
|
# Create an Organization-like object with id and name
|
||||||
org_obj = type('Organization', (), {'id': org_id, 'name': org_names[org_id]})
|
org_obj = type("Organization", (), {"id": org_id, "name": org_names[org_id]})
|
||||||
games_by_org[org_obj] = list(games.values())
|
games_by_org[org_obj] = list(games.values())
|
||||||
|
|
||||||
# Create the flattened games_with_counts for backward compatibility
|
# Create the flattened games_with_counts for backward compatibility
|
||||||
games_with_counts = []
|
games_with_counts = []
|
||||||
for org_games in games_by_org.values():
|
for org_games in games_by_org.values():
|
||||||
games_with_counts.extend(org_games)
|
games_with_counts.extend(org_games)
|
||||||
|
|
||||||
# Add to context
|
# Add to context
|
||||||
context["games_with_counts"] = games_with_counts # Keep the original list for backward compatibility
|
context["games_with_counts"] = games_with_counts # Keep the original list for backward compatibility
|
||||||
context["games_by_org"] = games_by_org # Add the new organized structure
|
context["games_by_org"] = games_by_org # Add the new organized structure
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -255,25 +239,19 @@ class GameDetailView(DetailView):
|
||||||
|
|
||||||
# Get all campaigns for this game in a single query with prefetching
|
# Get all campaigns for this game in a single query with prefetching
|
||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
all_campaigns = DropCampaign.objects.filter(
|
all_campaigns = DropCampaign.objects.filter(game=game).select_related("owner").order_by("-end_at")
|
||||||
game=game
|
|
||||||
).select_related("owner").order_by('-end_at')
|
|
||||||
|
|
||||||
# Filter the campaigns in Python instead of making multiple queries
|
# Filter the campaigns in Python instead of making multiple queries
|
||||||
active_campaigns = [
|
active_campaigns = [
|
||||||
campaign for campaign in all_campaigns
|
campaign for campaign in all_campaigns if campaign.start_at <= now and campaign.end_at >= now and campaign.status == "ACTIVE"
|
||||||
if campaign.start_at <= now and campaign.end_at >= now and campaign.status == "ACTIVE"
|
|
||||||
]
|
]
|
||||||
active_campaigns.sort(key=lambda c: c.end_at) # Sort by end_at ascending
|
active_campaigns.sort(key=lambda c: c.end_at) # Sort by end_at ascending
|
||||||
|
|
||||||
upcoming_campaigns = [
|
upcoming_campaigns = [campaign for campaign in all_campaigns if campaign.start_at > now and campaign.status == "UPCOMING"]
|
||||||
campaign for campaign in all_campaigns
|
|
||||||
if campaign.start_at > now and campaign.status == "UPCOMING"
|
|
||||||
]
|
|
||||||
upcoming_campaigns.sort(key=lambda c: c.start_at) # Sort by start_at ascending
|
upcoming_campaigns.sort(key=lambda c: c.start_at) # Sort by start_at ascending
|
||||||
|
|
||||||
# No need to fetch expired_campaigns separately as we already have all_campaigns
|
# No need to fetch expired_campaigns separately as we already have all_campaigns
|
||||||
|
|
||||||
context.update({
|
context.update({
|
||||||
"active_campaigns": active_campaigns,
|
"active_campaigns": active_campaigns,
|
||||||
"upcoming_campaigns": upcoming_campaigns,
|
"upcoming_campaigns": upcoming_campaigns,
|
||||||
|
|
@ -296,19 +274,10 @@ def dashboard(request: HttpRequest) -> HttpResponse:
|
||||||
# Get active campaigns with prefetching to reduce queries
|
# Get active campaigns with prefetching to reduce queries
|
||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
active_campaigns = (
|
active_campaigns = (
|
||||||
DropCampaign.objects.filter(
|
DropCampaign.objects.filter(start_at__lte=now, end_at__gte=now, status="ACTIVE")
|
||||||
start_at__lte=now,
|
|
||||||
end_at__gte=now,
|
|
||||||
status="ACTIVE"
|
|
||||||
)
|
|
||||||
.select_related("game", "owner")
|
.select_related("game", "owner")
|
||||||
# Prefetch the time-based drops with their benefits to avoid N+1 queries
|
# Prefetch the time-based drops with their benefits to avoid N+1 queries
|
||||||
.prefetch_related(
|
.prefetch_related(Prefetch("time_based_drops", queryset=TimeBasedDrop.objects.prefetch_related("benefits")))
|
||||||
Prefetch(
|
|
||||||
'time_based_drops',
|
|
||||||
queryset=TimeBasedDrop.objects.prefetch_related('benefits')
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return render(
|
return render(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue