Skip to content

Commit

Permalink
Add a refresh org button for the user (#317)
Browse files Browse the repository at this point in the history
* add last NGO hub start/end update time
* update translations
* style everything

---------

Co-authored-by: Daniel Ursache Dogariu <[email protected]>
  • Loading branch information
tudoramariei and danniel authored Jul 10, 2024
1 parent 9ccfcc6 commit 85d3b8a
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 222 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.13 on 2024-07-10 11:45

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("hub", "0054_alter_organization_city"),
]

operations = [
migrations.AddField(
model_name="organization",
name="ngohub_last_update_ended",
field=models.DateTimeField(blank=True, editable=False, null=True, verbose_name="Last NGO Hub update"),
),
migrations.AddField(
model_name="organization",
name="ngohub_last_update_started",
field=models.DateTimeField(blank=True, editable=False, null=True, verbose_name="Last NGO Hub update"),
),
]
4 changes: 3 additions & 1 deletion backend/hub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ def save(self, *args, **kwargs):


class Organization(StatusModel, TimeStampedModel):
# TODO: add last ngo hub update
STATUS = Choices(
("draft", _("Draft")),
("pending", _("Pending approval")),
Expand Down Expand Up @@ -335,6 +334,9 @@ class Organization(StatusModel, TimeStampedModel):

filename_cache = models.JSONField(_("Filename cache"), editable=False, default=dict, blank=False, null=False)

ngohub_last_update_started = models.DateTimeField(_("Last NGO Hub update"), null=True, blank=True, editable=False)
ngohub_last_update_ended = models.DateTimeField(_("Last NGO Hub update"), null=True, blank=True, editable=False)

class Meta:
verbose_name_plural = _("Organizations")
verbose_name = _("Organization")
Expand Down
148 changes: 82 additions & 66 deletions backend/hub/templates/ngo/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,83 +11,99 @@

{% block left-side-view %}

{% if organization %}

<div class="tabs is-medium">
<ul>
<li class="is-active">
<a href="{% url 'ngo-update' organization.id %}">Profilul organizației</a>
</li>
<li>
{% if organization.candidate %}
<a href="{% url 'candidate-update' organization.candidate.id %}">Candidatura mea</a>
{% elif CANDIDATE_REGISTRATION_ENABLED %}
<a href="{% url 'candidate-register-request' %}">Adaugă candidatură</a>
{% endif %}
</li>
<li>
<a href="{% url 'votes' %}">Voturile mele</a>
</li>
{% if not user.is_ngohub_user %}
<li {% if active == "settings" %}class="is-active"{% endif %}>
<a href="{% url 'account-password-reset' %}">{% trans 'Settings' %}</a>
</li>
{% endif %}
</ul>
</div>

{% endif %}

<h2 class="title border-b uppercase">Profilul organizației</h2>

{% if organization.status == 'accepted' %}
<a href="{% url 'ngo-detail' organization.id %}">Vezi profilul public al organizației</a>
<br><br>
{% endif %}

<div class="container">
{% if organization %}

<div class="tabs is-medium">
<ul>
<li class="is-active">
<a href="{% url 'ngo-update' organization.id %}">Profilul organizației</a>
</li>
<li>
{% if organization.candidate %}
<a href="{% url 'candidate-update' organization.candidate.id %}">Candidatura mea</a>
{% elif CANDIDATE_REGISTRATION_ENABLED %}
<a href="{% url 'candidate-register-request' %}">Adaugă candidatură</a>
{% endif %}
</li>
<li>
<a href="{% url 'votes' %}">Voturile mele</a>
</li>
{% if not user.is_ngohub_user %}
<li {% if active == "settings" %}class="is-active"{% endif %}>
<a href="{% url 'account-password-reset' %}">{% trans 'Settings' %}</a>
</li>
{% endif %}
</ul>
</div>

{% endif %}

<h2 class="title border-b uppercase">Profilul organizației</h2>

{% if organization.status == 'accepted' %}
<a href="{% url 'ngo-detail' organization.id %}">Vezi profilul public al organizației</a>
<br><br>
{% endif %}

<div class="container">
{% if messages %}
<article class="message is-success">
<article class="message is-success">
<div class="message-body">
{% for message in messages %}
{{ message }}
{% endfor %}
{% for message in messages %}
{{ message }}
{% endfor %}
</div>
</article>
{% else %}
</article>
{% endif %}

{% if not organization.is_fully_editable %}
<div class="message is-warning">
<div class="message-body">
{% if not organization.is_fully_editable %}
<div class="message is-warning">
<div class="message-body flex-align-center">
<form id="update-ngo-form"
method="post"
action="{% url 'ngo-update-post' organization.id %}?return_url={{ request.path }}">
{% csrf_token %}
<button class="button is-warning is-small update-ngo-button"
type="submit"
title="{% trans 'Refresh NGO Information' %}">
<i class="fas fa-sync"></i>
</button>
</form>

<p class="has-text-grey-dark">
Unele date din profilul acestei organizații pot fi actualizate doar prin NGO Hub.
</div>
</p>
</div>
{% endif %}
</div>

<form class="ces-form" method="post" enctype="multipart/form-data" >
{% csrf_token %}
<form class="ces-form" method="post" enctype="multipart/form-data">
{% csrf_token %}

{{ form.non_field_errors }}
{{ form.non_field_errors }}

{% for hidden_field in form.hidden_fields %}
{% if hidden_field.errors %}
<article class="message is-danger"><div class="message-body">Erori "{{ hidden_field.label }}": {{ hidden_field.errors }}</div></article>
{% endif %}
{% endfor %}
{% for hidden_field in form.hidden_fields %}
{% if hidden_field.errors %}
<article class="message is-danger">
<div class="message-body">Erori "{{ hidden_field.label }}": {{ hidden_field.errors }}</div>
</article>
{% endif %}
{% endfor %}

{% for field in form %}
{% if field.errors %}
<article class="message is-danger"><div class="message-body">Erori "{{ field.label }}": {{ field.errors }}</div></article>
{% endif %}
{% endfor %}
{% for field in form %}
{% if field.errors %}
<article class="message is-danger">
<div class="message-body">Erori "{{ field.label }}": {{ field.errors }}</div>
</article>
{% endif %}
{% endfor %}

{{ form|crispy }}
{{ form|crispy }}

<div class="has-text-right">
<input class="button is-success" type="submit" value='{% trans "Update organization" %}'>
</div>

<div class="has-text-right">
<input class="button is-success" type="submit" value='{% trans "Update organization" %}'>
</div>

</form>
</form>
{% endif %}
</div>
</div>
{% endblock %}
2 changes: 2 additions & 0 deletions backend/hub/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
candidate_support,
candidate_vote,
organization_vote,
update_organization_information,
)

urlpatterns = [
Expand Down Expand Up @@ -52,6 +53,7 @@
path(_("ngos/<int:pk>"), OrganizationDetailView.as_view(), name="ngo-detail"),
path(_("ngos/<int:pk>/vote/<str:action>"), organization_vote, name="ngo-vote"),
path(_("ngos/<int:pk>/update"), OrganizationUpdateView.as_view(), name="ngo-update"),
path(_("ngo-update/<int:pk>"), update_organization_information, name="ngo-update-post"),
path("ngos/city-autocomplete/", CityAutocomplete.as_view(), name="city-autocomplete"),
path("blog/", BlogListView.as_view(), name="blog-list"),
path("blog/<slug:slug>", BlogPostView.as_view(), name="blog-post"),
Expand Down
22 changes: 22 additions & 0 deletions backend/hub/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from urllib.parse import unquote

from django.conf import settings
Expand Down Expand Up @@ -42,6 +43,7 @@
STAFF_GROUP,
SUPPORT_GROUP,
)
from hub.workers.update_organization import update_organization


class MenuMixin:
Expand Down Expand Up @@ -545,6 +547,26 @@ def candidate_status_confirm(request, pk):
return redirect("candidate-detail", pk=pk)


@permission_required_or_403("hub.change_organization", (Organization, "pk", "pk"))
def update_organization_information(request, pk):
return_url = request.GET.get("return_url", "")
redirect_path = return_url or reverse("ngo-update", args=(pk,))

organization: Organization = Organization.objects.get(pk=pk)
organization_last_update: datetime = organization.ngohub_last_update_started
update_threshold: datetime = timezone.now() - timezone.timedelta(minutes=5)
if organization_last_update and organization_last_update > update_threshold:
messages.error(request, _("Please wait a few minutes before updating the organization again."))
return redirect(redirect_path)

organization.ngohub_last_update_started = timezone.now()
organization.save()

update_organization(pk)

return redirect(redirect_path)


class CityAutocomplete(View):
def get(self, request):
response = []
Expand Down
5 changes: 5 additions & 0 deletions backend/hub/workers/update_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def update_organization_process(organization_id: int, token: str = ""):

organization: Organization = Organization.objects.get(id=organization_id)

organization.ngohub_last_update_started = timezone.now()
organization.save()

if not organization.filename_cache:
organization.filename_cache = {}

Expand Down Expand Up @@ -146,6 +149,8 @@ def update_organization_process(organization_id: int, token: str = ""):
if organization.status == Organization.STATUS.draft:
organization.status = Organization.STATUS.pending

organization.ngohub_last_update_ended = timezone.now()

organization.save()

task_result: Dict[str, any] = {"organization_id": organization_id}
Expand Down
Loading

0 comments on commit 85d3b8a

Please sign in to comment.