Skip to content

Commit

Permalink
Update account templates and base template
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Nov 4, 2024
1 parent ca8c3ac commit 33e0827
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
43 changes: 24 additions & 19 deletions backend/donations/views/account_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from redirectioneaza.common.messaging import send_email

from ..forms.account import ForgotPasswordForm, LoginForm, RegisterForm, ResetPasswordForm
from .base import BaseAccountView
from .base import BaseTemplateView

logger = logging.getLogger(__name__)

Expand All @@ -25,15 +25,16 @@ def django_login(request, user) -> None:
login(request, user, backend="django.contrib.auth.backends.ModelBackend")


class ForgotPasswordView(BaseAccountView):
class ForgotPasswordView(BaseTemplateView):
template_name = "resetare-parola.html"
title = "Resetare parola"

def get(self, request, *args, **kwargs):
context = {"title": "Resetare parola"}
context = self.get_context_data(**kwargs)
return render(request, self.template_name, context)

def post(self, request, *args, **kwargs):
context = {}
context = self.get_context_data(**kwargs)

form = ForgotPasswordForm(request.POST)
if not form.is_valid():
Expand Down Expand Up @@ -78,8 +79,9 @@ def _send_password_reset_email(self, user: UserModel):
)


class LoginView(BaseAccountView):
class LoginView(BaseTemplateView):
template_name = "account/login.html"
title = "Contul meu"

def get(self, request, *args, **kwargs):
# if the user is logged in, then redirect
Expand All @@ -88,20 +90,19 @@ def get(self, request, *args, **kwargs):
return redirect(reverse("admin-index"))
return redirect(reverse("contul-meu"))

context = self.get_context_data(**kwargs)

signup_text: str = _("sign up")
signup_link: str = request.build_absolute_uri(reverse("signup"))
signup_url: str = f'<a href="{signup_link}">{signup_text}</a>'

# noinspection DjangoSafeString
context = {
"title": "Contul meu",
"signup_url": mark_safe(signup_url),
}
context.update({"signup_url": mark_safe(signup_url)})

return render(request, self.template_name, context)

def post(self, request: HttpRequest):
context = {}
def post(self, request: HttpRequest, **kwargs):
context = self.get_context_data(**kwargs)

form = LoginForm(request.POST)
if not form.is_valid():
Expand Down Expand Up @@ -141,17 +142,17 @@ def post(self, request: HttpRequest):
return render(request, self.template_name, context)


class LogoutView(BaseAccountView):
class LogoutView(BaseTemplateView):
def get(self, request, *args, **kwargs):
logout(request)
return redirect("/")


class SetPasswordView(BaseAccountView):
class SetPasswordView(BaseTemplateView):
template_name = "parola-noua.html"

def post(self, request, *args, **kwargs):
context = {}
context = self.get_context_data(**kwargs)

form = ResetPasswordForm(request.POST)
if not form.is_valid():
Expand All @@ -173,15 +174,17 @@ def post(self, request, *args, **kwargs):
return redirect(reverse("contul-meu"))


class SignupView(BaseAccountView):
class SignupView(BaseTemplateView):
template_name = "cont-nou.html"
title = "Cont nou"

def get(self, request, *args, **kwargs):
context = {"title": "Cont nou"}
context = self.get_context_data(**kwargs)

return render(request, self.template_name, context)

def post(self, request, *args, **kwargs):
context = {}
context = self.get_context_data(**kwargs)

form = RegisterForm(request.POST)
if not form.is_valid():
Expand Down Expand Up @@ -250,7 +253,7 @@ def post(self, request, *args, **kwargs):
return redirect(reverse("contul-meu"))


class VerificationView(BaseAccountView):
class VerificationView(BaseTemplateView):
"""
handler used to:
v - verify new account
Expand All @@ -260,6 +263,8 @@ class VerificationView(BaseAccountView):
template_name = "parola-noua.html"

def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)

verification_type = kwargs["verification_type"]
user_id = kwargs["user_id"]
signup_token = kwargs["signup_token"]
Expand Down Expand Up @@ -289,7 +294,7 @@ def get(self, request, *args, **kwargs):

if verification_type == "p":
# supply user to the page
context = {"token": signup_token, "is_admin": request.user.is_staff}
context.update({"token": signup_token, "is_admin": request.user.is_staff})
return render(request, self.template_name, context)

logger.info("verification type not supported")
Expand Down
11 changes: 6 additions & 5 deletions backend/donations/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
from django.views.generic import TemplateView

from redirectioneaza.common.cache import cache_decorator
from .base import BaseAccountView

from ..models.jobs import Job, JobStatusChoices
from ..models.main import ALL_NGOS_CACHE_KEY, Ngo
from ..pdf import create_pdf
from ..workers.update_organization import update_organization
from .base import BaseTemplateView

logger = logging.getLogger(__name__)


class UpdateFromNgohub(BaseAccountView):
class UpdateFromNgohub(BaseTemplateView):
def post(self, request, *args, **kwargs):
redirect_success = redirect(reverse("organization"))
redirect_error = redirect(reverse("organization"))
Expand All @@ -39,7 +40,7 @@ def post(self, request, *args, **kwargs):
return redirect_success


class CheckNgoUrl(BaseAccountView):
class CheckNgoUrl(BaseTemplateView):
ngo_url_block_list = (
"",
"admin",
Expand Down Expand Up @@ -168,7 +169,7 @@ def get(self, request, ngo_url, *args, **kwargs):
return redirect(ngo.prefilled_form.url)


class GetNgoForms(BaseAccountView):
class GetNgoForms(BaseTemplateView):
def get(self, request, *args, **kwargs):
raise Http404

Expand Down Expand Up @@ -221,7 +222,7 @@ def post(self, request, *args, **kwargs):

@method_decorator(login_required(login_url=reverse_lazy("login")), name="dispatch")
@method_decorator(csrf_exempt, name="dispatch")
class GetUploadUrl(BaseAccountView):
class GetUploadUrl(BaseTemplateView):
def get(self, request, *args, **kwargs):
raise Http404

Expand Down
24 changes: 23 additions & 1 deletion backend/donations/views/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
from typing import Any

from django.conf import settings
from django.contrib.auth import get_user_model
from django.views.generic import TemplateView


class BaseAccountView(TemplateView):
class BaseTemplateView(TemplateView):
user_model = get_user_model()
title: str = ""

def _get_checked_property(self, property_name: str, default_value: str) -> Any:
is_production: bool = settings.ENVIRONMENT == "development"

if not hasattr(self, property_name) and not is_production:
raise AttributeError(f"Property '{property_name}' is missing in '{self.__class__.__name__}'.")

if not (property_value := getattr(self, property_name)) and not is_production:
raise ValueError(f"Property '{property_name}' in '{self.__class__.__name__}' is empty.")

return property_value or default_value

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

context["title"] = self._get_checked_property("title", "redirectioneaza.ro")

return context
10 changes: 5 additions & 5 deletions backend/donations/views/my_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
from ..models.jobs import Job, JobStatusChoices
from ..models.main import Donor, Ngo, ngo_id_number_validator
from .api import CheckNgoUrl
from .base import BaseAccountView
from .base import BaseTemplateView

UserModel = get_user_model()


class MyAccountDetailsView(BaseAccountView):
class MyAccountDetailsView(BaseTemplateView):
template_name = "ngo/my-account-details.html"

@method_decorator(login_required(login_url=reverse_lazy("login")))
Expand All @@ -53,7 +53,7 @@ def post(self, request, *args, **kwargs):
return render(request, self.template_name, context)


class ArchiveDownloadLinkView(BaseAccountView):
class ArchiveDownloadLinkView(BaseTemplateView):
@method_decorator(login_required(login_url=reverse_lazy("login")))
def get(self, request: HttpRequest, job_id, *args, **kwargs):
user: User = request.user
Expand Down Expand Up @@ -87,7 +87,7 @@ def get(self, request: HttpRequest, job_id, *args, **kwargs):
return redirect(job.zip.url)


class MyAccountView(BaseAccountView):
class MyAccountView(BaseTemplateView):
template_name = "ngo/my-account.html"

@staticmethod
Expand Down Expand Up @@ -181,7 +181,7 @@ def delete_prefilled_form(ngo_id):
return Ngo.delete_prefilled_form(ngo_id)


class NgoDetailsView(BaseAccountView):
class NgoDetailsView(BaseTemplateView):
template_name = "ngo/ngo-details.html"

def get_context_data(self, **kwargs):
Expand Down

0 comments on commit 33e0827

Please sign in to comment.