Skip to content

Commit

Permalink
Handle the disabling of the registration process (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
danniel authored Jul 5, 2024
1 parent 4298ed8 commit cd2928c
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 104 deletions.
20 changes: 20 additions & 0 deletions backend/accounts/templates/error_org_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'ngo/base.html' %}
{% load social_share %}
{% load static %}
{% load spurl %}
{% load i18n %}


{% block title %}{% trans 'Sign In Error' %}{% endblock %}

{% block left-side-view %}
<div class="container tight-container content mb-50">
<h2>
{% trans 'Sign In Error' %}
</h2>

{% block error_message %}
<p></p>
{% endblock %}
</div>
{% endblock %}
22 changes: 5 additions & 17 deletions backend/accounts/templates/error_org_duplicate.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
{% extends 'ngo/base.html' %}
{% load social_share %}
{% load static %}
{% load spurl %}
{% extends 'error_org_base.html' %}
{% load i18n %}


{% block title %}Eroare autentificare{% endblock %}

{% block left-side-view %}
<div class="container tight-container content mb-50">
<h2>
Eroare autentificare
</h2>

<p>
Organizația dumneavoastră există deja în platforma VotONG, pentru un alt cont de utilizator.
</p>
</div>
{% block error_message %}
<p>
{% trans 'This NGO Hub organization already exists for another VotONG user.' %}
</p>
{% endblock %}
22 changes: 5 additions & 17 deletions backend/accounts/templates/error_org_missing.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
{% extends 'ngo/base.html' %}
{% load social_share %}
{% load static %}
{% load spurl %}
{% extends 'error_org_base.html' %}
{% load i18n %}


{% block title %}Eroare autentificare{% endblock %}

{% block left-side-view %}
<div class="container tight-container content mb-50">
<h2>
Eroare autentificare
</h2>

<p>
Pentru a vă putea autentifica în platforma VotONG, trebuie să aveți o organizație înscrisă in NGO Hub.
</p>
</div>
{% block error_message %}
<p>
{% trans 'There is no NGO Hub organization for this VotONG user.' %}
</p>
{% endblock %}
8 changes: 8 additions & 0 deletions backend/accounts/templates/error_org_registration_closed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends 'error_org_base.html' %}
{% load i18n %}

{% block error_message %}
<p>
{% trans 'The registration process for new organizations is currently disabled.' %}
</p>
{% endblock %}
8 changes: 8 additions & 0 deletions backend/civil_society_vote/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
ANALYTICS_ENABLED=(bool, False),
GLOBAL_SUPPORT_ENABLED=(bool, False),
NGOHUB_ORG_OVERWRITE=(bool, False),
ENABLE_ORG_REGISTRATION_FORM=(bool, False),
CURRENT_EDITION_YEAR=(int, 2024),
# email settings
EMAIL_SEND_METHOD=(str, "async"),
Expand Down Expand Up @@ -571,7 +572,14 @@ def show_toolbar(request):
NGOHUB_API_BASE = f"https://{env('NGOHUB_API_HOST')}/"
NGOHUB_API_ACCOUNT = env("NGOHUB_API_ACCOUNT")
NGOHUB_API_KEY = env("NGOHUB_API_KEY")

# Allow Organization data overwrite from VotONG forms (should be False)
NGOHUB_ORG_OVERWRITE = env("NGOHUB_ORG_OVERWRITE")

# Enable the organization registration form in order to sidestep NGO Hub (should be False)
ENABLE_ORG_REGISTRATION_FORM = env("ENABLE_ORG_REGISTRATION_FORM")

CURRENT_EDITION_YEAR = env("CURRENT_EDITION_YEAR")

# How many previous year reports to require for candidate proposal
PREV_REPORTS_REQUIRED_FOR_PROPOSAL = 3
5 changes: 5 additions & 0 deletions backend/civil_society_vote/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
path("admin/", admin.site.urls),
path("impersonate/", include("impersonate.urls")),
path("ckeditor/", include("ckeditor_uploader.urls")),
path(
_("accounts/error/registration-closed/"),
StaticPageView.as_view(template_name="error_org_registration_closed.html"),
name="error-org-registration-closed",
),
path(
_("accounts/error/duplicate-organization/"),
StaticPageView.as_view(template_name="error_org_duplicate.html"),
Expand Down
3 changes: 2 additions & 1 deletion backend/hub/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def hub_settings(context):
"DEBUG": settings.DEBUG,
"CURRENT_EDITION_YEAR": settings.CURRENT_EDITION_YEAR,
"ANALYTICS_ENABLED": settings.ANALYTICS_ENABLED,
"GLOBAL_SUPPORT_ENABLED": settings.GLOBAL_SUPPORT_ENABLED,
"ENABLE_ORG_REGISTRATION_FORM": settings.ENABLE_ORG_REGISTRATION_FORM,
"GLOBAL_SUPPORT_ENABLED": flags.get("global_support_enabled", False),
"ORG_REGISTRATION_ENABLED": flags.get("enable_org_registration", False),
"ORG_APPROVAL_ENABLED": flags.get("enable_org_approval", False),
"CANDIDATE_REGISTRATION_ENABLED": flags.get("enable_candidate_registration", False),
Expand Down
18 changes: 18 additions & 0 deletions backend/hub/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
class OrganizationException(Exception):
"""Some kind of problem with an organization"""

pass


class ClosedRegistrationException(OrganizationException):
"""New organizations cannot be registered anymore"""

pass


class DisabledOrganizationException(OrganizationException):
"""The requested organization has been disabled from the platform"""

pass


class DuplicateOrganizationException(OrganizationException):
"""An organization with the same NGO Hub ID already exists"""

pass


class MissingOrganizationException(OrganizationException):
"""The requested organization does not exist"""

pass
16 changes: 12 additions & 4 deletions backend/hub/social_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from django.utils.translation import gettext as _

from accounts.models import User
from hub.exceptions import DuplicateOrganizationException, MissingOrganizationException
from hub.models import Organization, NGO_GROUP, City
from hub.exceptions import DuplicateOrganizationException, MissingOrganizationException, ClosedRegistrationException
from hub.models import Organization, NGO_GROUP, City, FeatureFlag


class UserOrgAdapter(DefaultSocialAccountAdapter):
Expand Down Expand Up @@ -65,6 +65,14 @@ def update_user_org(org: Organization, token: str, *, in_auth_flow: bool = False
redirect the user to a relevant error page.
"""

if not FeatureFlag.objects.filter(flag="enable_org_registration", is_enabled=True).exists():
if in_auth_flow:
raise ImmediateHttpResponse(redirect(reverse("error-org-registration-closed")))
else:
raise ClosedRegistrationException(
_("The registration process for new organizations is currently disabled.")
)

auth_headers = {"Authorization": f"Bearer {token}"}

# ngohub_user = requests.get(settings.NGOHUB_API_BASE + "api/ong-user/", headers=auth_headers).json()
Expand All @@ -78,14 +86,14 @@ def update_user_org(org: Organization, token: str, *, in_auth_flow: bool = False
if in_auth_flow:
raise ImmediateHttpResponse(redirect(reverse("error-org-missing")))
else:
raise MissingOrganizationException(_("There is no NGO Hub organization for this VotONG user"))
raise MissingOrganizationException(_("There is no NGO Hub organization for this VotONG user."))

# Check that the current user has an NGO Hub organization
if Organization.objects.filter(ngohub_org_id=ngohub_id).exclude(pk=org.pk).count():
if in_auth_flow:
raise ImmediateHttpResponse(redirect(reverse("error-org-duplicate")))
else:
raise DuplicateOrganizationException(_("This NGO Hub organization already exists for another VotONG user"))
raise DuplicateOrganizationException(_("This NGO Hub organization already exists for another VotONG user."))

org.ngohub_org_id = ngohub_id
ngohub_general = ngohub_org.get("organizationGeneral", {})
Expand Down
2 changes: 1 addition & 1 deletion backend/hub/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</a>

{% if ORG_REGISTRATION_ENABLED %}
<a href="{% url 'ngos-register-request' %}" class="navbar-item">
<a href="{% if ENABLE_ORG_REGISTRATION_FORM %}{% url 'ngos-register-request' %}{% else %}https://www.ngohub.ro/{% endif %}" class="navbar-item">
{% trans "Register your organization" %}
</a>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion backend/hub/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ <h2 class="has-text-weight-bold">Înscrie-te</h2>
</p>
</div>
<div class="column is-narrow">
<a href="{% url 'ngos-register-request' %}" class="button is-warning">Înscrie-te</a>
<a href="{% if ENABLE_ORG_REGISTRATION_FORM %}{% url 'ngos-register-request' %}{% else %}https://www.ngohub.ro/{% endif %}" class="button is-warning">Înscrie-te</a>
</div>
</div>
</div>
Expand Down
10 changes: 8 additions & 2 deletions backend/hub/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,18 @@ class OrganizationRegisterRequestCreateView(HubCreateView):
)

def get(self, request, *args, **kwargs):
if not FeatureFlag.objects.filter(flag="enable_org_registration", is_enabled=True).exists():
if (
not settings.ENABLE_ORG_REGISTRATION_FORM
or not FeatureFlag.objects.filter(flag="enable_org_registration", is_enabled=True).exists()
):
raise PermissionDenied
return super().get(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
if not FeatureFlag.objects.filter(flag="enable_org_registration", is_enabled=True).exists():
if (
not settings.ENABLE_ORG_REGISTRATION_FORM
or not FeatureFlag.objects.filter(flag="enable_org_registration", is_enabled=True).exists()
):
raise PermissionDenied
return super().post(request, *args, **kwargs)

Expand Down
Loading

0 comments on commit cd2928c

Please sign in to comment.