Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy type errors fix #348

Merged
merged 9 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions backend/api/admin/api_keys.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from django.contrib import messages
from django.http import HttpRequest, HttpResponseBadRequest, HttpResponse
from django.http import HttpResponseBadRequest, HttpResponse
from django.shortcuts import render, redirect

from backend.models import APIKey
from backend.types.htmx import HtmxHttpRequest


# Still working on


def generate_api_key(request: HttpRequest) -> HttpResponse():
def generate_api_key(request: HtmxHttpRequest) -> HttpResponse:
if not request.htmx:
return redirect("user settings")
if not request.user.is_staff or not request.user.is_superuser:
Expand Down
7 changes: 4 additions & 3 deletions backend/api/base/notifications.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.http import HttpRequest, HttpResponse
from django.http import HttpResponse
from django.shortcuts import render

from backend.models import Notification
from backend.types.htmx import HtmxHttpRequest


def get_notification_html(request: HttpRequest):
def get_notification_html(request: HtmxHttpRequest):
user_notifications = Notification.objects.filter(user=request.user).order_by("-date")
above_5 = False

Expand All @@ -19,7 +20,7 @@ def get_notification_html(request: HttpRequest):
)


def delete_notification(request: HttpRequest, id: int):
def delete_notification(request: HtmxHttpRequest, id: int):
notif = Notification.objects.filter(id=id, user=request.user).first()

if notif is None or notif.user != request.user:
Expand Down
6 changes: 3 additions & 3 deletions backend/api/clients/fetch.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.db.models import Q
from django.http import HttpRequest
from django.shortcuts import render, redirect
from django.views.decorators.http import require_http_methods

from backend.models import Client
from backend.types.htmx import HtmxHttpRequest


@require_http_methods(["GET"])
def fetch_all_clients(request: HttpRequest):
def fetch_all_clients(request: HtmxHttpRequest):
if not request.htmx:
return redirect("clients dashboard")

Expand All @@ -25,7 +25,7 @@ def fetch_all_clients(request: HttpRequest):


@require_http_methods(["GET"])
def fetch_clients_dropdown(request: HttpRequest):
def fetch_clients_dropdown(request: HtmxHttpRequest):
if not request.htmx:
return redirect("clients dashboard")

Expand Down
5 changes: 3 additions & 2 deletions backend/api/currency_converter/convert.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import datetime

from django.contrib import messages
from django.http import HttpResponse, HttpRequest
from django.http import HttpResponse
from django.shortcuts import render, redirect
from forex_python.converter import CurrencyRates

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


def convert_currency(init_currency, target_currency, amount, date=None):
Expand Down Expand Up @@ -64,7 +65,7 @@ def convert_currency(init_currency, target_currency, amount, date=None):
raise ValueError(f"Error in currency conversion: {e}")


def currency_conversion(request: HttpRequest):
def currency_conversion(request: HtmxHttpRequest):
context = {}

if not request.htmx:
Expand Down
5 changes: 3 additions & 2 deletions backend/api/emails/fetch.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from django.contrib import messages
from django.core.paginator import Paginator
from django.db.models import Q
from django.http import HttpRequest, HttpResponse
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django_ratelimit.core import is_ratelimited
from django_ratelimit.decorators import ratelimit

from backend.models import EmailSendStatus
from backend.types.htmx import HtmxHttpRequest


def fetch_all_emails(request: HttpRequest):
def fetch_all_emails(request: HtmxHttpRequest):
if is_ratelimited(request, group="fetch_all_emails", key="user", rate="2/4s", increment=True) or is_ratelimited(
request,
group="fetch_all_emails",
Expand Down
7 changes: 4 additions & 3 deletions backend/api/invoices/create/services/add.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from django.http import HttpRequest, JsonResponse
from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.http import require_http_methods

from backend.models import InvoiceProduct
from backend.types.htmx import HtmxHttpRequest


@require_http_methods(["POST"])
def add_service(request: HttpRequest):
context = {}
def add_service(request: HtmxHttpRequest):
context: dict = {}
existing_service = request.POST.get("existing_service")

try:
Expand Down
6 changes: 3 additions & 3 deletions backend/api/invoices/create/set_destination.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from django.contrib import messages
from django.http import HttpRequest
from django.shortcuts import render
from django.views.decorators.http import require_http_methods

from backend.models import Client
from backend.types.htmx import HtmxHttpRequest

to_get = ["name", "address", "city", "country", "company", "is_representative"]


@require_http_methods(["POST"])
def set_destination_to(request: HttpRequest):
def set_destination_to(request: HtmxHttpRequest):
context = {"swapping": True}

context.update({key: request.POST.get(key) for key in to_get})
Expand All @@ -28,7 +28,7 @@ def set_destination_to(request: HttpRequest):


@require_http_methods(["POST"])
def set_destination_from(request: HttpRequest):
def set_destination_from(request: HtmxHttpRequest):
context = {"swapping": True}

context.update({key: request.POST.get(key) for key in to_get})
Expand Down
10 changes: 7 additions & 3 deletions backend/api/invoices/delete.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from django.contrib import messages
from django.http import HttpRequest, JsonResponse, QueryDict, HttpResponse, HttpResponseRedirect
from django.http import JsonResponse, QueryDict, HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import resolve, reverse
from django.urls.exceptions import Resolver404
from django.views.decorators.http import require_http_methods

from backend.models import Invoice, QuotaLimit
from backend.types.htmx import HtmxHttpRequest


@require_http_methods(["DELETE"])
def delete_invoice(request: HttpRequest):
def delete_invoice(request: HtmxHttpRequest):
delete_items = QueryDict(request.body)

invoice = delete_items.get("invoice")
Expand Down Expand Up @@ -39,4 +41,6 @@ def delete_invoice(request: HttpRequest):
except Resolver404:
return HttpResponseRedirect(reverse("dashboard"))

return JsonResponse({"message": "Invoice successfully deleted"}, status=200)
return JsonResponse({"message": "Invoice successfully deleted"}, status=200), QuotaLimit.delete_quota_usage(
Domejko marked this conversation as resolved.
Show resolved Hide resolved
"invoices-count", request.user, invoice.id, invoice.date_created
)
7 changes: 4 additions & 3 deletions backend/api/invoices/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from django.views.decorators.http import require_http_methods, require_POST

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


@require_http_methods(["POST"])
def edit_invoice(request: HttpRequest):
def edit_invoice(request: HtmxHttpRequest):
try:
invoice = Invoice.objects.get(id=request.POST.get("invoice_id"))
except Invoice.DoesNotExist:
Expand Down Expand Up @@ -64,7 +65,7 @@ def edit_invoice(request: HttpRequest):


@require_POST
def change_status(request: HttpRequest, invoice_id: int, status: str) -> HttpResponse:
def change_status(request: HtmxHttpRequest, invoice_id: int, status: str) -> HttpResponse:
status = status.lower() if status else ""

if not request.htmx:
Expand Down Expand Up @@ -101,7 +102,7 @@ def change_status(request: HttpRequest, invoice_id: int, status: str) -> HttpRes


@require_POST
def edit_discount(request: HttpRequest, invoice_id: str):
def edit_discount(request: HtmxHttpRequest, invoice_id: str):
discount_type = "percentage" if request.POST.get("discount_type") == "on" else "amount"
discount_amount_str: str = request.POST.get("discount_amount")
percentage_amount_str: str = request.POST.get("percentage_amount")
Expand Down
4 changes: 2 additions & 2 deletions backend/api/invoices/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
Sum,
Prefetch,
)
from django.http import HttpRequest
from django.shortcuts import render, redirect
from django.utils import timezone
from django.views.decorators.http import require_http_methods

from backend.models import Invoice, InvoiceItem
from backend.types.htmx import HtmxHttpRequest


@require_http_methods(["GET"])
def fetch_all_invoices(request: HttpRequest):
def fetch_all_invoices(request: HtmxHttpRequest):
# Redirect if not an HTMX request
if not request.htmx:
return redirect("invoices:dashboard")
Expand Down
6 changes: 3 additions & 3 deletions backend/api/invoices/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.views.decorators.http import require_http_methods

from backend.models import Invoice
from backend.models import UserSettings
from backend.types.htmx import HtmxHttpRequest


class PreviewContext(TypedDict):
Expand All @@ -33,7 +33,7 @@ class ErrorResponse:


@require_http_methods(["GET"])
def tab_preview_invoice(request: HttpRequest, invoice_id):
def tab_preview_invoice(request: HtmxHttpRequest, invoice_id):
# Redirect if not an HTMX request
if not request.htmx:
return redirect("invoices dashboard") # Maybe should be 404?
Expand All @@ -48,7 +48,7 @@ def tab_preview_invoice(request: HttpRequest, invoice_id):
return render(request, "base/toasts.html")


def preview_invoice(request: HttpRequest, invoice_id) -> SuccessResponse | ErrorResponse:
def preview_invoice(request: HtmxHttpRequest, invoice_id) -> SuccessResponse | ErrorResponse:
context = {"type": "preview"}

try:
Expand Down
4 changes: 2 additions & 2 deletions backend/api/products/fetch.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.db.models import Q
from django.http import HttpRequest
from django.shortcuts import render

from backend.models import InvoiceProduct
from backend.types.htmx import HtmxHttpRequest


def fetch_products(request: HttpRequest):
def fetch_products(request: HtmxHttpRequest):
results = []
search_text = request.GET.get("search_existing_service")
if search_text:
Expand Down
4 changes: 2 additions & 2 deletions backend/api/quotas/fetch.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.db.models import Q
from django.http import HttpRequest
from django.shortcuts import render, redirect

from backend.models import QuotaLimit
from backend.types.htmx import HtmxHttpRequest


def fetch_all_quotas(request: HttpRequest, group: str):
def fetch_all_quotas(request: HtmxHttpRequest, group: str):
context = {}
if not request.htmx:
return redirect("quotas")
Expand Down
10 changes: 7 additions & 3 deletions backend/utils/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
T = TypeVar("T")


def extract_to_dataclass(request, class_type: [T], request_types: list[str], *args, **kwargs) -> [T]:
def extract_to_dataclass(request, class_type: T, request_types: list[str], *args, **kwargs) -> T:
"""

Turn kwargs from Key:Value and get request.POST.get(value) and set class.key = request.POST.get(value)
Expand All @@ -27,7 +27,7 @@ def myview(request):
except pydantic.ValidationError:
pass
"""
data = {}
data: dict = {}
if "get" in request_types:
if args:
data |= {key: request.GET.get(key) for key in args}
Expand All @@ -48,4 +48,8 @@ def myview(request):

if kwargs:
data |= {key: request.headers.get(value) for key, value in kwargs.items()}
return class_type(**data)

if isinstance(class_type, type):
return class_type(**data)
else:
raise TypeError("class_type must be a class")
4 changes: 3 additions & 1 deletion settings/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def send_email(data: SingleEmailInput) -> SingleEmailSuccessResponse | SingleEma
response = EMAIL_CLIENT.send_email(
FromEmailAddress=from_email_address,
Destination={"ToAddresses": data.destination},
Content={"Simple": {"Subject": {"Data": data.subject}, "Body": {"Text": {"Data": data.content}}}},
Content={
"Simple": {"Subject": {"Data": data.subject if data.subject else ""}, "Body": {"Text": {"Data": data.content}}}
},
ConfigurationSetName=data.ConfigurationSetName or "",
)
return SingleEmailSuccessResponse(response)
Expand Down
25 changes: 11 additions & 14 deletions settings/local_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@
if DB_TYPE == "mysql" or DB_TYPE == "postgres":
DATABASES: dict = {
"default": {
"ENGINE": (
"django.db.backends.postgresql_psycopg2"
if DB_TYPE == "mysql"
else "django.db.backends.postgresql"
),
"ENGINE": ("django.db.backends.postgresql_psycopg2" if DB_TYPE == "mysql" else "django.db.backends.postgresql"),
"NAME": os.environ.get("DATABASE_NAME") or "myfinances_development",
"USER": os.environ.get("DATABASE_USER") or "root",
"PASSWORD": os.environ.get("DATABASE_PASS") or "",
"HOST": os.environ.get("DATABASE_HOST") or "localhost",
"PORT": os.environ.get("DATABASE_PORT")
or (3306 if DB_TYPE == "mysql" else 5432),
"OPTIONS": {
"sql_mode": "traditional",
} if DB_TYPE == "mysql" else {},
"PORT": os.environ.get("DATABASE_PORT") or (3306 if DB_TYPE == "mysql" else 5432),
"OPTIONS": (
{
"sql_mode": "traditional",
}
if DB_TYPE == "mysql"
else {}
),
}
}

Expand All @@ -45,8 +44,6 @@
}
print("[BACKEND] Using sqlite3 database", flush=True)

ALLOWED_HOSTS: list[str | None] = ["localhost", "127.0.0.1"]
Domejko marked this conversation as resolved.
Show resolved Hide resolved
ALLOWED_HOSTS: list[str] = ["localhost", "127.0.0.1"]

os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = (
"1" # THIS WILL ALLOW HTTP - NOT RECOMMENDED
)
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" # THIS WILL ALLOW HTTP - NOT RECOMMENDED
2 changes: 1 addition & 1 deletion settings/prod_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
print(f"[BACKEND] Using {DB_TYPE} database: {os.environ.get('DATABASE_NAME')}")


ALLOWED_HOSTS: list[str | None] = [os.environ.get("URL")] + URL_LIST
Domejko marked this conversation as resolved.
Show resolved Hide resolved
ALLOWED_HOSTS: list[str] = [os.environ.get("URL")] + URL_LIST # type: ignore[assignment]

os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "0" # THIS WILL ALLOW HTTP IF IT'S SET TO 1 - NOT RECOMMENDED