Skip to content

Commit

Permalink
#67 Allow user to subscribe to a recruitment process
Browse files Browse the repository at this point in the history
  • Loading branch information
rnudb committed Jan 10, 2023
1 parent 348bc3c commit 350e1ee
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 78 deletions.
22 changes: 22 additions & 0 deletions interview/migrations/0025_process_subscribers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.16 on 2022-12-15 16:48

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("interview", "0024_merge_20221212_1657"),
]

operations = [
migrations.AddField(
model_name="process",
name="subscribers",
field=models.ManyToManyField(
blank=True, related_name="subscribed_processes", to=settings.AUTH_USER_MODEL, verbose_name="Subscribers"
),
),
]
6 changes: 6 additions & 0 deletions interview/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ class Process(models.Model):
verbose_name=_("Process creator"),
)

subscribers = models.ManyToManyField(
PyouPyouUser, verbose_name=_("Subscribers"), blank=True, related_name="subscribed_processes"
)

def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
is_new = False if self.id else True
if is_new:
Expand Down Expand Up @@ -436,6 +440,7 @@ def trigger_notification(self, is_new):
recipient_list = []
if self.subsidiary.responsible:
recipient_list.append(self.subsidiary.responsible.user.email)
recipient_list = recipient_list + [user.email for user in self.subscribers.all()]
mail.send_mail(
subject=subject, message=body, from_email=settings.MAIL_FROM, recipient_list=set(recipient_list)
)
Expand Down Expand Up @@ -632,6 +637,7 @@ def trigger_notification(self):
if subject and body_template:
url = os.path.join(settings.SITE_HOST, self.process.get_absolute_url().lstrip("/"))
body = render_to_string(body_template, {"interview": self, "url": url})
recipient_list = recipient_list + [user.email for user in self.process.subscribers.all()]
mail.send_mail(
subject=subject, message=body, from_email=settings.MAIL_FROM, recipient_list=set(recipient_list)
)
Expand Down
46 changes: 45 additions & 1 deletion interview/templates/interview/process_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,55 @@

{% block more_css %}
<link rel="stylesheet" href="{% static 'django_tables2/themes/paleblue/css/screen.css' %}" />
<script>
$(function () {
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});

$("#subscribe-button-id").on('click', (event) => {
$.ajax({
url: "/switch_process_subscription",
type: "POST",
data: {"process_id": '{{ process.id }}'},
success: function (json) {
let btn = $("#subscribe-button-id")
btn.toggleClass("btn-danger")
btn.toggleClass("btn-info")

if (json["state"] === "subscribed") {
btn.text("{% trans "Unsubscribe to this process' notifications" %}")
}
if (json["state"] === "unsubscribed") {
btn.text("{% trans "Subscribe to this process' notifications" %}")
}
},
error: function (json) {
//TODO handle errors
}
});
});
});
</script>
{% endblock %}

{% block content %}
<div class="container">
<h3> {% trans "Candidate:" %} <a href="{% url 'candidate' process.pk %}">{{ process.candidate.display_name }} </a> [{{ process.subsidiary }}]</h3>
<div class="row">
<h3 class="col-md-8"> {% trans "Candidate:" %} <a href="{% url 'candidate' process.pk %}">{{ process.candidate.display_name }} </a> [{{ process.subsidiary }}]</h3>
<h3 class="col-md-4">
{% if user not in process.subscribers.all %}
<button type="button" class="btn btn-info" id="subscribe-button-id" style="display: block; margin-left: auto; margin-right: auto" > {% trans "Subscribe to this process' notifications" %}</button>
{% else %}
<button type="button" class="btn btn-danger" id="subscribe-button-id" style="display: block; margin-left: auto; margin-right: auto" > {% trans "Unsubscribe to this process' notifications" %}</button>
{% endif %}
</h3>
</div>
{% if process.is_open %}
<h5> {% trans "This process is in progress" %} </h5>
{% else %}
Expand Down
24 changes: 24 additions & 0 deletions interview/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,30 @@ def process(request, process_id, slug_info=None):
return render(request, "interview/process_detail.html", context)


@login_required
@require_http_methods(["POST"])
def switch_process_subscription_ajax(request):
process_id = request.POST.get("process_id")
p = None
try:
if process_id is None:
raise Process.DoesNotExist
p = Process.objects.get(id=process_id)

except Process.DoesNotExist:
return JsonResponse({"error": "Not Found"})

state = None
if request.user in p.subscribers.all():
p.subscribers.remove(request.user)
state = "unsubscribed"
else:
p.subscribers.add(request.user)
state = "subscribed"
p.save()
return JsonResponse({"state": state})


@login_required
@require_http_methods(["POST"])
@user_passes_test(lambda u: not u.consultant.is_external)
Expand Down
Loading

0 comments on commit 350e1ee

Please sign in to comment.