Skip to content

Commit

Permalink
Merge branch 'main' into mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
TreyWW authored Apr 21, 2024
2 parents 17231d8 + d65bafb commit bd94b7c
Show file tree
Hide file tree
Showing 35 changed files with 172 additions and 99 deletions.
9 changes: 6 additions & 3 deletions backend/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def extras(request: HttpRequest):
data["import_method"] = get_var("IMPORT_METHOD", default="webpack")
data["analytics"] = get_var("ANALYTICS_STRING")

if hasattr(request, "htmx") and request.htmx.boosted:
data["base"] = "base/htmx.html"

return data


Expand All @@ -52,7 +55,7 @@ def get_item(name: str, url_name: Optional[str] = None, icon: Optional[str] = No
"icon": icon,
}

def generate_breadcrumbs(*breadcrumb_list: str) -> List[dict[Any, Any] | None]:
def generate_breadcrumbs(*breadcrumb_list: str) -> list[dict[Any, Any] | None]:
"""
Generate a list of breadcrumb items based on the provided list of breadcrumb names.
Expand All @@ -66,7 +69,7 @@ def generate_breadcrumbs(*breadcrumb_list: str) -> List[dict[Any, Any] | None]:

current_url_name: str | Any = request.resolver_match.url_name # type: ignore[union-attr]

all_items: Dict[str, dict] = {
all_items: dict[str, dict] = {
"dashboard": get_item("Dashboard", "dashboard", "house"),
"invoices:dashboard": get_item("Invoices", "invoices:dashboard", "file-invoice"),
"invoices:create": get_item("Create", "invoices:create"),
Expand All @@ -78,7 +81,7 @@ def generate_breadcrumbs(*breadcrumb_list: str) -> List[dict[Any, Any] | None]:
"clients create": get_item("Create", "clients create"),
}

all_breadcrumbs: Dict[str | None, list] = {
all_breadcrumbs: dict[str | None, list] = {
"dashboard": generate_breadcrumbs("dashboard"),
"user settings teams": generate_breadcrumbs("dashboard", "user settings teams"),
"receipts dashboard": generate_breadcrumbs("dashboard", "receipts dashboard"),
Expand Down
17 changes: 17 additions & 0 deletions backend/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ def wrapper_func(request, *args, **kwargs):
return decorator


def hx_boost(view):
"""
Decorator for HTMX requests.
used by wrapping FBV in @hx_boost and adding **kwargs to param
then you can use context = kwargs.get("context", {}) to continue and then it will handle HTMX boosts
"""

@wraps(view)
def wrapper(request, *args, **kwargs):
if request.htmx.boosted:
kwargs["context"] = kwargs.get("context", {}) | {"base": "base/htmx.html"}
return view(request, *args, **kwargs)

return wrapper


def feature_flag_check(flag, status=True, api=False, htmx=False):
def decorator(view_func):
@wraps(view_func)
Expand Down
13 changes: 13 additions & 0 deletions backend/middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db import connection, OperationalError
from django.http import HttpResponse

from backend.types.htmx import HtmxAnyHttpRequest


class HealthCheckMiddleware:
def __init__(self, get_response):
Expand All @@ -19,6 +21,17 @@ def __call__(self, request):
return self.get_response(request)


class HTMXPartialLoadMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request: HtmxAnyHttpRequest):
response = self.get_response(request)
if request.htmx.boosted:
response.headers["HX-Retarget"] = "#main_content"
return response


class LastVisitedMiddleware:
def __init__(self, get_response):
self.get_response = get_response
Expand Down
6 changes: 6 additions & 0 deletions backend/types/htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ class HtmxHttpRequest(HttpRequest):

class UnauthorizedHttpRequest(HttpRequest):
user: AnonymousUser
htmx: HtmxDetails


class HtmxAnyHttpRequest(HttpRequest):
user: User | AnonymousUser
htmx: HtmxDetails
25 changes: 12 additions & 13 deletions backend/views/core/invoices/dashboard.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
from django.contrib import messages
from django.http import HttpRequest
from django.shortcuts import render
from django.shortcuts import render, redirect

from backend.decorators import *
from backend.models import *
from backend.models import Invoice
from backend.types.htmx import HtmxHttpRequest


def invoices_dashboard(request: HtmxHttpRequest):
context = {}

return render(request, "pages/invoices/dashboard/dashboard.html", context)
return render(request, "pages/invoices/dashboard/dashboard.html")


def invoices_dashboard_id(request: HtmxHttpRequest, invoice_id):
context = {}

if invoice_id == "create":
return redirect("invoices:create")
elif type(invoice_id) != "int":
elif not isinstance(invoice_id, int):
messages.error(request, "Invalid invoice ID")
return redirect("invoices:dashboard")
invoices = Invoice.objects.get(id=invoice_id)
if not invoices:

try:
Invoice.objects.get(id=invoice_id)
except Invoice.DoesNotExist:
return redirect("invoices:dashboard")
return render(
request,
"pages/invoices/dashboard/dashboard.html",
)
return render(request, "pages/invoices/dashboard/dashboard.html", context)
10 changes: 6 additions & 4 deletions backend/views/core/invoices/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@


def invoices_dashboard(request: HtmxHttpRequest):
context = {}

return render(request, "pages/invoices/dashboard/dashboard.html", context)
return render(request, "pages/invoices/dashboard/dashboard.html")


def manage_invoice(request: HtmxHttpRequest, invoice_id: str):
context = {}

if not invoice_id.isnumeric():
messages.error(request, "Invalid invoice ID")
return redirect("invoices:dashboard")
Expand All @@ -18,4 +18,6 @@ def manage_invoice(request: HtmxHttpRequest, invoice_id: str):

if not invoice:
return redirect("invoices:dashboard")
return render(request, "pages/invoices/dashboard/manage.html", {"invoice": invoice})

print(context | {"invoice": invoice})
return render(request, "pages/invoices/dashboard/manage.html", context | {"invoice": invoice})
4 changes: 1 addition & 3 deletions backend/views/core/quotas/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
from backend.types.htmx import HtmxHttpRequest


@cache_page(3600)
def quotas_page(request: HtmxHttpRequest) -> HttpResponse:
groups = list(QuotaLimit.objects.values_list("slug", flat=True).distinct())

quotas = set(q.split("-")[0] for q in groups if q.split("-"))
quotas = {q.split("-")[0] for q in groups if q.split("-")}

return render(
request,
Expand All @@ -20,7 +19,6 @@ def quotas_page(request: HtmxHttpRequest) -> HttpResponse:
)


@cache_page(3600)
def quotas_list(request: HtmxHttpRequest, group: str) -> HttpResponse:
return render(request, "pages/quotas/list.html", {"group": group})

Expand Down
12 changes: 7 additions & 5 deletions backend/views/core/settings/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@


def teams_dashboard(request: HtmxHttpRequest):
context = {}

users_team: Optional[Team] = request.user.logged_in_as_team

if not users_team:
user_with_counts = User.objects.prefetch_related("teams_joined", "teams_leader_of").get(pk=request.user.pk)
return render(
request,
"pages/settings/teams/main.html",
{
context
| {
"team": None,
"team_count": user_with_counts.teams_joined.count() + user_with_counts.teams_leader_of.count(),
},
Expand All @@ -40,7 +43,8 @@ def teams_dashboard(request: HtmxHttpRequest):
return render(
request,
"pages/settings/teams/main.html",
{
context
| {
"team": None,
"team_count": user_with_counts.teams_joined.count() + user_with_counts.teams_leader_of.count(),
},
Expand All @@ -49,9 +53,7 @@ def teams_dashboard(request: HtmxHttpRequest):
return render(
request,
"pages/settings/teams/main.html",
{
"team": team,
},
context | {"team": team},
)


Expand Down
7 changes: 5 additions & 2 deletions frontend/templates/base/+left_drawer.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{% load feature_enabled %}
{% feature_enabled "areUserEmailsAllowed" as are_user_emails_allowed %}
<div class="drawer-side">
<div class="drawer-side"
data-layout="left_sliding_sidebar"
{% if swap %}hx-swap-oob='innerHTML:div[data-layout="left_sliding_sidebar"]'{% endif %}>
<label for="service_list_drawer"
aria-label="close sidebar"
class="drawer-overlay"></label>
<ul class="menu p-4 w-80 min-h-full bg-base-200 text-base-content z-10 gap-y-1">
<ul class="menu p-4 w-80 min-h-full bg-base-200 text-base-content z-10 gap-y-1"
hx-boost="true">
<label class="btn btn-square btn-outline btn-block top-5 right-5 mb-3"
for="service_list_drawer">
<i class="fa fa-solid fa-x"></i>
Expand Down
2 changes: 1 addition & 1 deletion frontend/templates/base/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div id="modal_container">{# Leave here #}</div>
<div class="drawer">
<input id="service_list_drawer" type="checkbox" class="drawer-toggle" />
<div class="drawer-content">
<div class="drawer-content" id="main_content">
{% block content %}
{% endblock content %}
</div>
Expand Down
5 changes: 4 additions & 1 deletion frontend/templates/base/breadcrumbs.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{% if breadcrumb %}
<div class="breadcrumbs text-sm mb-4 -mt-4">
<div class="breadcrumbs text-sm mb-4 -mt-4"
data-layout="breadcrumps"
hx-boost="true"
{% if swap %}hx-swap-oob='innerHTML:div[data-layout="breadcrumps"]'{% endif %}>
<ul>
{% for item in breadcrumb %}
<li>
Expand Down
10 changes: 10 additions & 0 deletions frontend/templates/base/htmx.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div hx-swap="innerHTML">
{% block content %}
{% endblock content %}
</div>
{# Profile Picture dropdown item #}
<div hx-swap-oob='outerHTML:div[data-layout="icon_dropdown_items"]'>
{% component "base:topbar:icon_dropdown" %}
</div>
{% include "base/+left_drawer.html" with swap=True %}
{% include "base/breadcrumbs.html" with swap=True %}
28 changes: 28 additions & 0 deletions frontend/templates/base/topbar/+icon_dropdown.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div data-layout="icon_dropdown_items">
<li hx-boost="true">
<a {% if request.resolver_match.url_name == "user settings" %}class="active btn-disabled"{% endif %}
href="{% url 'user settings' %}">
<i class="fa fa-solid fa-cog"></i>
Account Settings
</a>
</li>
<li hx-boost="true">
<a {% if request.resolver_match.url_name == "user settings teams" %}class="active btn-disabled"{% endif %}
href="{% url "user settings teams" %}">
<i class="fa fa-solid fa-users"></i>
Manage Team
<span class="badge">New</span>
</a>
</li>
<li>
<label for="logout_modal">
<i class="fa fa-solid fa-right-from-bracket"></i>
Logout
</label>
</li>
{% if request.user.is_superuser and request.user.is_staff %}
<li>
<a tabindex="-1" class="dropdown-item text text-sm mt-1" href="/admin/">Go to admin dashboard</a>
</li>
{% endif %}
</div>
38 changes: 7 additions & 31 deletions frontend/templates/base/topbar/_topbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
</label>
<!-- Topbar Dropdown (MOBILE ONLY) -->
<ul tabindex="0"
class="menu menu-sm dropdown-content mt-3 z-[1] shadow p-2 bg-base-100 rounded-box w-52">
class="menu menu-sm dropdown-content mt-3 z-[1] shadow p-2 bg-base-100 rounded-box w-52"
hx-boost="true">
<li>
<a href="/dashboard/">My Dashboard</a>
</li>
<li>
<a href="{% url "receipts dashboard" %}">My Receipts</a>
</li>
<li>
<a href="{% url "invoices:dashboard" %}">My Invoices</a>
<a href="{% url "invoices:dashboard" %}" hx-boost="true">My Invoices</a>
</li>
<li>
<a href="{% url "clients dashboard" %}">My Clients</a>
Expand All @@ -45,7 +46,7 @@
</div>
<div class="navbar-center hidden lg:flex z-50">
<!-- Topbar Dropdown (LARGE ONLY) -->
<ul class="menu menu-horizontal px-1">
<ul class="menu menu-horizontal px-1" hx-boost="true">
<li>
<a href="/dashboard/">My Dashboard</a>
</li>
Expand All @@ -66,10 +67,10 @@
<a href="{% url "receipts dashboard" %}">My Receipts</a>
</li>
<li>
<a href="{% url "invoices:dashboard" %}">My Invoices</a>
<a href="{% url "invoices:dashboard" %}" hx-boost="true">My Invoices</a>
</li>
<li>
<a href="{% url "clients dashboard" %}">My Clients</a>
<a href="{% url "clients dashboard" %}" hx-boost="true">My Clients</a>
</li>
</ul>
</div>
Expand Down Expand Up @@ -110,32 +111,7 @@
</ul>
</details>
<div class="divider mt-0 mb-0"></div>
<li>
<a {% if request.resolver_match.url_name == "user settings" %}class="active"{% endif %}
href="{% url 'user settings' %}">
<i class="fa fa-solid fa-cog"></i>
Account Settings
</a>
</li>
<li>
<a class="{% if request.resolver_match.url_name == "user settings teams" %}active{% endif %}"
href="{% url "user settings teams" %}">
<i class="fa fa-solid fa-users"></i>
Manage Team
<span class="badge">New</span>
</a>
</li>
<li>
<label for="logout_modal">
<i class="fa fa-solid fa-right-from-bracket"></i>
Logout
</label>
</li>
{% if request.user.is_superuser and request.user.is_staff %}
<li>
<a tabindex="-1" class="dropdown-item text text-sm mt-1" href="/admin/">Go to admin dashboard</a>
</li>
{% endif %}
{% component "base:topbar:icon_dropdown" %}
</ul>
</div>
{# End of Profile Picture #}
Expand Down
3 changes: 2 additions & 1 deletion frontend/templates/pages/clients/create/create.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'base/base.html' %}
{% extends base|default:"base/base.html" %}
{% load static %}
{% block content %}
<div class="card bg-base-100 p-6">
Expand Down Expand Up @@ -122,6 +122,7 @@
<script>
is_rep_inp = document.querySelector('input[name="is_representative"]');
is_rep_div = document.querySelector('div[data-selection="representative"]');

function check_is_rep() {
if (is_rep_inp.checked) {
is_rep_div.style.display = "block";
Expand Down
6 changes: 4 additions & 2 deletions frontend/templates/pages/clients/dashboard/dashboard.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{% extends 'base/base.html' %}
{% extends base|default:"base/base.html" %}
{% block content %}
<div class="card w-full p-6 bg-base-100 shadow-xl mt-2">
<a class="btn btn-primary mb-4" href="{% url "clients create" %}">
<a class="btn btn-primary mb-4"
href="{% url "clients create" %}"
hx-boost="true">
<i class="fa-solid fa-plus pe-1"></i>
Create Client
</a>
Expand Down
Loading

0 comments on commit bd94b7c

Please sign in to comment.