From 0bd9905291a66fbe2fc817bb97566b7c4e25bedc Mon Sep 17 00:00:00 2001 From: hemant10yadav Date: Mon, 2 Dec 2024 15:44:01 +0530 Subject: [PATCH] wip for super user --- commcare_connect/opportunity/tables.py | 30 +++++++++++++++++++ commcare_connect/opportunity/urls.py | 2 ++ commcare_connect/opportunity/views.py | 28 +++++++++++++++++ commcare_connect/templates/base.html | 4 +++ .../opportunity/all_opportunities_view.html | 20 +++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 commcare_connect/templates/opportunity/all_opportunities_view.html diff --git a/commcare_connect/opportunity/tables.py b/commcare_connect/opportunity/tables.py index a856650f..c2ee0e21 100644 --- a/commcare_connect/opportunity/tables.py +++ b/commcare_connect/opportunity/tables.py @@ -6,6 +6,7 @@ from commcare_connect.opportunity.models import ( CatchmentArea, CompletedWork, + Opportunity, OpportunityAccess, Payment, PaymentInvoice, @@ -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): @@ -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 diff --git a/commcare_connect/opportunity/urls.py b/commcare_connect/opportunity/urls.py index 66b7a957..4390151f 100644 --- a/commcare_connect/opportunity/urls.py +++ b/commcare_connect/opportunity/urls.py @@ -2,6 +2,7 @@ from commcare_connect.opportunity import views from commcare_connect.opportunity.views import ( + AllOpportunitiesView, OpportunityCompletedWorkTable, OpportunityCreate, OpportunityDeliverStatusTable, @@ -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("/finalize/", view=OpportunityFinalize.as_view(), name="finalize"), diff --git a/commcare_connect/opportunity/views.py b/commcare_connect/opportunity/views.py index a53c0050..081ac0a0 100644 --- a/commcare_connect/opportunity/views.py +++ b/commcare_connect/opportunity/views.py @@ -73,6 +73,7 @@ DeliverStatusTable, LearnStatusTable, OpportunityPaymentTable, + OpportunityTable, PaymentInvoiceTable, PaymentReportTable, PaymentUnitTable, @@ -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) @@ -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 diff --git a/commcare_connect/templates/base.html b/commcare_connect/templates/base.html index f417ddab..947c260b 100644 --- a/commcare_connect/templates/base.html +++ b/commcare_connect/templates/base.html @@ -65,6 +65,10 @@ {% translate "Opportunities" %} + {% endif %} {% endif %} diff --git a/commcare_connect/templates/opportunity/all_opportunities_view.html b/commcare_connect/templates/opportunity/all_opportunities_view.html new file mode 100644 index 00000000..05f696cf --- /dev/null +++ b/commcare_connect/templates/opportunity/all_opportunities_view.html @@ -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 %} +
+
+
+

All Opportunities

+
+ {% render_table table %} +
+
+
+
+{% endblock content %}