Skip to content

Commit

Permalink
wip for super user
Browse files Browse the repository at this point in the history
  • Loading branch information
hemant10yadav committed Dec 2, 2024
1 parent bdc1bbf commit 0bd9905
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
30 changes: 30 additions & 0 deletions commcare_connect/opportunity/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from commcare_connect.opportunity.models import (
CatchmentArea,
CompletedWork,
Opportunity,
OpportunityAccess,
Payment,
PaymentInvoice,
Expand All @@ -16,6 +17,12 @@
VisitValidationStatus,
)

TABLE_TEMPLATE = "django_tables2/bootstrap5.html"
RESPONSIVE_TABLE_AND_LIGHT_HEADER = {
"class": "table border table-responsive",
"thead": {"class": "table-light"},
}


class OrgContextTable(tables.Table):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -470,3 +477,26 @@ def date_with_time_popup(table, date):
date.strftime("%d %b, %Y"),
date.strftime("%d %b %Y, %I:%M%p"),
)


class OpportunityTable(tables.Table):
name = columns.Column()
Organization = columns.Column()
start_date = columns.DateColumn()
end_date = columns.DateColumn()
active = columns.Column()
program = columns.Column()

class Meta:
model = Opportunity
fields = (
"name",
"organization",
"start_date",
"end_date",
"active",
"program",
)
empty_text = "No visits submitted for review."
template_name = TABLE_TEMPLATE
attrs = RESPONSIVE_TABLE_AND_LIGHT_HEADER
2 changes: 2 additions & 0 deletions commcare_connect/opportunity/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from commcare_connect.opportunity import views
from commcare_connect.opportunity.views import (
AllOpportunitiesView,
OpportunityCompletedWorkTable,
OpportunityCreate,
OpportunityDeliverStatusTable,
Expand Down Expand Up @@ -54,6 +55,7 @@
app_name = "opportunity"
urlpatterns = [
path("", view=OpportunityList.as_view(), name="list"),
path("all_opportunities", view=AllOpportunitiesView.as_view(), name="all_opportunities"),
path("create/", view=OpportunityCreate.as_view(), name="create"),
path("init/", view=OpportunityInit.as_view(), name="init"),
path("<int:pk>/finalize/", view=OpportunityFinalize.as_view(), name="finalize"),
Expand Down
28 changes: 28 additions & 0 deletions commcare_connect/opportunity/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
DeliverStatusTable,
LearnStatusTable,
OpportunityPaymentTable,
OpportunityTable,
PaymentInvoiceTable,
PaymentReportTable,
PaymentUnitTable,
Expand Down Expand Up @@ -122,6 +123,11 @@ def test_func(self):
) or self.request.user.is_superuser


class SuperUserMixin(LoginRequiredMixin, UserPassesTestMixin):
def test_func(self):
return self.request.user.is_superuser


def get_opportunity_or_404(pk, org_slug):
opp = get_object_or_404(Opportunity, id=pk)

Expand Down Expand Up @@ -164,6 +170,28 @@ 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
4 changes: 4 additions & 0 deletions commcare_connect/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<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
20 changes: 20 additions & 0 deletions commcare_connect/templates/opportunity/all_opportunities_view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends "base.html" %}
{% load static %}
{% load sort_link %}
{% load i18n %}
{% load django_tables2 %}

{% block title %}{{ request.org }} - Opportunities{% endblock %}

{% block content %}
<div class="container">
<div class="row">
<div class="col">
<h1>All Opportunities</h1>
<div class="table-responsive">
{% render_table table %}
</div>
</div>
</div>
</div>
{% endblock content %}

0 comments on commit 0bd9905

Please sign in to comment.