Skip to content

Commit

Permalink
refactor table
Browse files Browse the repository at this point in the history
  • Loading branch information
hemant10yadav committed Dec 9, 2024
1 parent 0bd9905 commit f32c306
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 116 deletions.
71 changes: 49 additions & 22 deletions commcare_connect/opportunity/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.forms import modelformset_factory
from django.http import FileResponse, Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.html import format_html
from django.utils.safestring import mark_safe
Expand Down Expand Up @@ -170,28 +171,6 @@ def get_context_data(self, **kwargs):
return context


class AllOpportunitiesView(SuperUserMixin, SingleTableView):
model = Opportunity
paginate_by = 15
table_class = OpportunityTable
template_name = "opportunity/all_opportunities_view.html"

def get_queryset(self):
ordering = self.request.GET.get("sort", "name")
if ordering not in [
"name",
"-name",
"start_date",
"-start_date",
"end_date",
"-end_date",
"active",
"-active",
]:
ordering = "name"
return Opportunity.objects.all().order_by(ordering)


class OpportunityCreate(OrganizationUserMemberRoleMixin, CreateView):
template_name = "opportunity/opportunity_create.html"
form_class = OpportunityCreationForm
Expand Down Expand Up @@ -1250,3 +1229,51 @@ def invoice_approve(request, org_slug, pk):
)
payment.save()
return HttpResponse(headers={"HX-Trigger": "newInvoice"})


class AllOpportunitiesView(SuperUserMixin, SingleTableView):
model = Opportunity
paginate_by = 10
template_name = "opportunity/all_opportunities_view.html"
table_class = OpportunityTable

def get_queryset(self):
search_query = self.request.GET.get("search", "")
status_filter = self.request.GET.get("status", "")
ordering = self.request.GET.get("sort", "name")

valid_ordering = [
"name",
"-name",
"start_date",
"-start_date",
"end_date",
"-end_date",
]

if ordering not in valid_ordering:
ordering = "name"

opportunities = Opportunity.objects.all()

if search_query:
opportunities = opportunities.filter(name__icontains=search_query)

if status_filter:
if status_filter == "active":
opportunities = opportunities.filter(active=True, end_date__gte=now().date())
elif status_filter == "inactive":
opportunities = opportunities.filter(end_date__lt=now().date())

return opportunities.order_by(ordering)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["table"] = self.table_class(self.get_queryset())
return context

def render_to_response(self, context, **response_kwargs):
if self.request.htmx:
html = render_to_string("opportunity/partial_opportunity_table.html", context, request=self.request)
return HttpResponse(html)
return super().render_to_response(context, **response_kwargs)
4 changes: 0 additions & 4 deletions commcare_connect/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@
<a class="nav-link {% active_link "list || create || edit || detail" namespace='opportunity' %}"
href="{% url 'opportunity:list' request.org.slug %}">{% translate "Opportunities" %}</a>
</li>
<li class="nav-item">
<a class="nav-link {% active_link "all_opportunities" namespace='opportunity' %}"
href="{% url 'opportunity:all_opportunities' request.org.slug %}">{% translate "All opportunities" %}</a>
</li>
{% endif %}
{% endif %}
</ul>
Expand Down
43 changes: 33 additions & 10 deletions commcare_connect/templates/opportunity/all_opportunities_view.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
{% extends "base.html" %}
{% extends "opportunity/base.html" %}
{% load static %}
{% load sort_link %}
{% load i18n %}
{% load django_tables2 %}
{% block title %}{{ request.org }} - All Opportunities{% endblock %}

{% block title %}{{ request.org }} - Opportunities{% endblock %}
{% block breadcrumbs_inner %}
{{ block.super }}
<li class="breadcrumb-item active" aria-current="page">All-Org View</li>
{% endblock %}

{% block content %}
<div class="container">
<div class="row">
<div class="col">
<div class="container bg-white shadow">
<div class="mt-5 py-3">
<h1>All Opportunities</h1>
<div class="table-responsive">
{% render_table table %}
</div>
<div class="pb-4">
<form method="get"
hx-get="{% url 'opportunity:all_opportunities' org_slug=request.org.slug %}"
hx-target="#opportunity-table"
hx-push-url="true"
class="mb-3">
<div class="row">
<div class="col-md-4">
<input type="text" name="search" value="{{ request.GET.search }}" class="form-control" placeholder="Search by name">
</div>
<div class="col-md-3">
<select name="status" class="form-control">
<option value="">All Statuses</option>
<option value="active" {% if request.GET.status == 'active' %}selected{% endif %}>Active</option>
<option value="inactive" {% if request.GET.status == 'inactive' %}selected{% endif %}>Inactive</option>
</select>
</div>
<div class="col-md-1">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</div>
</form>
<div id="opportunity-table">
{% include 'opportunity/partial_opportunity_table.html' %}
</div>
</div>
</div>
</div>
{% endblock content %}
75 changes: 8 additions & 67 deletions commcare_connect/templates/opportunity/opportunity_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
<div class="mt-5 py-3">
<h1> Opportunities
<span class="float-end">
{% if request.user.is_superuser %}
<a class="btn btn-info"
href="{% url 'opportunity:all_opportunities' request.org.slug %}"
title="View opportunities across organizations">
All-Org View
</a>
{% endif %}
{% if request.org_membership.is_viewer %}
<button class="btn btn-primary" disabled>
<span class="bi bi-plus"></span> Add new
Expand All @@ -34,74 +41,8 @@ <h1> Opportunities
</h1>
</div>
<div class="pb-4">
<table class="table border table-responsive">
<thead class="table-light">
<tr>
<th>{% sort_link 'name' 'Name' %}</th>
<th>{% sort_link 'start_date' 'Start Date' %}</th>
<th>{% sort_link 'end_date' 'End Date' %}</th>
<th>Status</th>
<th>Program</th>
<th>Manage</th>
</tr>
</thead>
<tbody x-ref="tbody">
{% for opportunity in page_obj %}
<tr>
<td>{{ opportunity.name }}</td>
<td>{{ opportunity.start_date|default:"Not Set" }}</td>
<td>{{ opportunity.end_date|default:"Not Set" }}</td>
<td>
{% if opportunity.is_setup_complete %}
{% if opportunity.is_active %}
<span class="badge bg-success rounded-pill">Active</span>
{% else %}
<span class="badge bg-secondary text-white rounded-pill">Inactive</span>
{% endif %}
{% else %}
<span class="badge bg-warning text-white rounded-pill">Pending Setup</span>
{% endif %}
</td>
<td>{% if opportunity.managed %} {{ opportunity.managedopportunity.program.name }} {% else %} - {% endif %}</td>
<td width="300">
<div>
<a class="btn btn-primary btn-sm"
href="{% url 'opportunity:detail' org_slug=request.org.slug pk=opportunity.id %}">
<span class="bi bi-eye"></span><span class="d-none d-md-inline">&nbsp;View</span>
</a>
{% if request.org_membership.is_viewer %}
<button class="btn btn-warning btn-sm" disabled>
<span class="bi bi-pen"></span><span class="d-none d-md-inline">&nbsp;Edit</span>
</button>
{% if not opportunity.managed %}
<button class="btn btn-primary btn-sm" disabled>
<span class="bi bi-plus"></span><span class="d-none d-md-inline">&nbsp;Add Budget</span>
</button>
{% endif %}
{% else %}
<a class="btn btn-warning btn-sm"
href="{% url 'opportunity:edit' org_slug=request.org.slug pk=opportunity.id %}"><span
class="bi bi-pen"></span><span class="d-none d-md-inline">&nbsp;Edit</span></a>
{% if not opportunity.managed %}
<a class="btn btn-primary btn-sm"
href="{% url 'opportunity:add_budget_existing_users' org_slug=request.org.slug pk=opportunity.id %}"><span
class="bi bi-plus"></span><span class="d-none d-md-inline">&nbsp;Add Budget</span>
</a>
{% endif %}
{% endif %}
</div>
</td>

</tr>
{% empty %}
<tr>
<td colspan="3">{% translate "No opportunities yet." %}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% include 'opportunity/partial_opportunity_table.html' %}
</div>
{% include 'pagination.html' %}
</div>

{% if program_invitation_table and program_invitation_table.data %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{% load sort_link %}
{% load django_tables2 %}
{% load i18n %}
<div d="opportunity-table">
<table class="table border table-responsive">
<thead class="table-light">
<tr>
<th>{% sort_link 'name' 'Name' %}</th>
<th>{% sort_link 'start_date' 'Start Date' %}</th>
<th>{% sort_link 'end_date' 'End Date' %}</th>
<th>Status</th>
<th>Program</th>
<th>Manage</th>
</tr>
</thead>
<tbody x-ref="tbody">
{% for opportunity in page_obj %}
<tr>
<td>{{ opportunity.name }}</td>
<td>{{ opportunity.start_date|default:"Not Set" }}</td>
<td>{{ opportunity.end_date|default:"Not Set" }}</td>
<td>
{% if opportunity.is_setup_complete %}
{% if opportunity.is_active %}
<span class="badge bg-success rounded-pill">Active</span>
{% else %}
<span class="badge bg-secondary text-white rounded-pill">Inactive</span>
{% endif %}
{% else %}
<span class="badge bg-warning text-white rounded-pill">Pending Setup</span>
{% endif %}
</td>
<td>{% if opportunity.managed %} {{ opportunity.managedopportunity.program.name }} {% else %} - {% endif %}</td>
<td width="300">
<div>
<a class="btn btn-primary btn-sm"
href="{% url 'opportunity:detail' org_slug=request.org.slug pk=opportunity.id %}">
<span class="bi bi-eye"></span><span class="d-none d-md-inline">&nbsp;View</span>
</a>
{% if request.org_membership.is_viewer %}
<button class="btn btn-warning btn-sm" disabled>
<span class="bi bi-pen"></span><span class="d-none d-md-inline">&nbsp;Edit</span>
</button>
{% if not opportunity.managed %}
<button class="btn btn-primary btn-sm" disabled>
<span class="bi bi-plus"></span><span class="d-none d-md-inline">&nbsp;Add Budget</span>
</button>
{% endif %}
{% else %}
<a class="btn btn-warning btn-sm"
href="{% url 'opportunity:edit' org_slug=request.org.slug pk=opportunity.id %}"><span
class="bi bi-pen"></span><span class="d-none d-md-inline">&nbsp;Edit</span></a>
{% if not opportunity.managed %}
<a class="btn btn-primary btn-sm"
href="{% url 'opportunity:add_budget_existing_users' org_slug=request.org.slug pk=opportunity.id %}"><span
class="bi bi-plus"></span><span class="d-none d-md-inline">&nbsp;Add Budget</span>
</a>
{% endif %}
{% endif %}
</div>
</td>

</tr>
{% empty %}
<tr>
<td colspan="3">{% translate "No opportunities yet." %}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div>
{% include 'pagination.html' %}
</div>
</div>
72 changes: 59 additions & 13 deletions commcare_connect/templates/pagination.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,67 @@
</li>
{% endif %}

{% for page_number in page_obj.paginator.page_range %}
{% if page_obj.number == page_number %}
<li class="page-item active">
<a class="page-link" href="#">
{{ page_number }}
</a>
</li>
{% with total_pages=page_obj.paginator.num_pages current_page=page_obj.number %}
{% if total_pages > 10 %}
{# First page #}
{% if current_page > 4 %}
<li class="page-item">
<a class="page-link" href="?{% update_query_params page=1 %}">1</a>
</li>
<li class="page-item disabled">
<span class="page-link">...</span>
</li>
{% endif %}

{# Intelligent page range #}
{% with start=current_page|add:"-2" end=current_page|add:"2" %}
{% for page_number in page_obj.paginator.page_range %}
{% if page_number >= start and page_number <= end %}
{% if current_page == page_number %}
<li class="page-item active">
<a class="page-link" href="#">{{ page_number }}</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="?{% update_query_params page=page_number %}">
{{ page_number }}
</a>
</li>
{% endif %}
{% endif %}
{% endfor %}
{% endwith %}

{# Last page #}
{% if current_page < total_pages|add:"-3" %}
<li class="page-item disabled">
<span class="page-link">...</span>
</li>
<li class="page-item">
<a class="page-link" href="?{% update_query_params page=total_pages %}">
{{ total_pages }}
</a>
</li>
{% endif %}

{% else %}
<li class="page-item">
<a class="page-link" href="?{% update_query_params page=page_number %}">
{{ page_number }}
</a>
</li>
{# If total pages are 10 or less, show all pages normally #}
{% for page_number in page_obj.paginator.page_range %}
{% if page_obj.number == page_number %}
<li class="page-item active">
<a class="page-link" href="#">{{ page_number }}</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="?{% update_query_params page=page_number %}">
{{ page_number }}
</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endwith %}


{% if page_obj.has_next %}
<li class="page-item">
Expand Down

0 comments on commit f32c306

Please sign in to comment.