From 639148a63850505cf733816c97c2b871baab6aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org> Date: Wed, 9 Mar 2022 07:57:13 +0100 Subject: [PATCH] Re-add nomination events --- scipost_django/colleges/admin.py | 7 ++++ .../0038_fellowshipnominationevent.py | 31 ++++++++++++++++++ .../migrations/0039_nomination_add_events.py | 28 ++++++++++++++++ scipost_django/colleges/models/__init__.py | 1 + scipost_django/colleges/models/nomination.py | 32 +++++++++++++++++++ .../colleges/_hx_nomination_li_contents.html | 27 ++++++++++++++++ .../colleges/_hx_nominations_invitations.html | 1 - .../templates/colleges/nominations.html | 2 +- scipost_django/colleges/views.py | 14 ++++++++ 9 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 scipost_django/colleges/migrations/0038_fellowshipnominationevent.py create mode 100644 scipost_django/colleges/migrations/0039_nomination_add_events.py diff --git a/scipost_django/colleges/admin.py b/scipost_django/colleges/admin.py index fbc45560e..bb36f032b 100644 --- a/scipost_django/colleges/admin.py +++ b/scipost_django/colleges/admin.py @@ -10,6 +10,7 @@ from .models import ( PotentialFellowship, PotentialFellowshipEvent, FellowshipNomination, + FellowshipNominationEvent, FellowshipNominationComment, FellowshipNominationVotingRound, FellowshipNominationVote, @@ -70,6 +71,11 @@ class PotentialFellowshipAdmin(admin.ModelAdmin): admin.site.register(PotentialFellowship, PotentialFellowshipAdmin) +class FellowshipNominationEventInline(admin.TabularInline): + model = FellowshipNominationEvent + extra = 0 + + class FellowshipNominationCommentInline(admin.TabularInline): model = FellowshipNominationComment extra = 0 @@ -92,6 +98,7 @@ class FellowshipInvitationInline(admin.TabularInline): class FellowshipNominationAdmin(admin.ModelAdmin): inlines = [ + FellowshipNominationEventInline, FellowshipNominationCommentInline, FellowshipNominationVotingRoundInline, FellowshipNominationDecisionInline, diff --git a/scipost_django/colleges/migrations/0038_fellowshipnominationevent.py b/scipost_django/colleges/migrations/0038_fellowshipnominationevent.py new file mode 100644 index 000000000..ac834802e --- /dev/null +++ b/scipost_django/colleges/migrations/0038_fellowshipnominationevent.py @@ -0,0 +1,31 @@ +# Generated by Django 3.2.12 on 2022-03-09 06:17 + +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone +import scipost.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('scipost', '0040_auto_20210310_2026'), + ('colleges', '0037_alter_fellowshipinvitation_postpone_start_to'), + ] + + operations = [ + migrations.CreateModel( + name='FellowshipNominationEvent', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('description', models.TextField()), + ('on', models.DateTimeField(default=django.utils.timezone.now)), + ('by', models.ForeignKey(blank=True, null=True, on_delete=models.SET(scipost.models.get_sentinel_user), to='scipost.contributor')), + ('nomination', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='events', to='colleges.fellowshipnomination')), + ], + options={ + 'verbose_name_plural': 'Fellowhip Nomination Events', + 'ordering': ['nomination', '-on'], + }, + ), + ] diff --git a/scipost_django/colleges/migrations/0039_nomination_add_events.py b/scipost_django/colleges/migrations/0039_nomination_add_events.py new file mode 100644 index 000000000..ce7a10acc --- /dev/null +++ b/scipost_django/colleges/migrations/0039_nomination_add_events.py @@ -0,0 +1,28 @@ +# Generated by Django 3.2.12 on 2022-03-09 06:35 + +from django.db import migrations + + +def add_events(apps, schema_editor): + FellowshipNomination = apps.get_model("colleges", "FellowshipNomination") + FellowshipNominationEvent = apps.get_model("colleges", "FellowshipNominationEvent") + + for n in FellowshipNomination.objects.exclude(events__description="Nominated"): + event = FellowshipNominationEvent( + nomination=n, + description="Nominated", + on=n.nominated_on, + by=n.nominated_by, + ) + event.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('colleges', '0038_fellowshipnominationevent'), + ] + + operations = [ + migrations.RunPython(add_events, reverse_code=migrations.RunPython.noop), + ] diff --git a/scipost_django/colleges/models/__init__.py b/scipost_django/colleges/models/__init__.py index d0819d70c..b989a9fa6 100644 --- a/scipost_django/colleges/models/__init__.py +++ b/scipost_django/colleges/models/__init__.py @@ -8,6 +8,7 @@ from .fellowship import Fellowship from .nomination import ( FellowshipNomination, + FellowshipNominationEvent, FellowshipNominationComment, FellowshipNominationVotingRound, FellowshipNominationVote, diff --git a/scipost_django/colleges/models/nomination.py b/scipost_django/colleges/models/nomination.py index 31fc3d5b7..57bbfa8de 100644 --- a/scipost_django/colleges/models/nomination.py +++ b/scipost_django/colleges/models/nomination.py @@ -11,6 +11,8 @@ from ..managers import ( FellowshipNominationVoteQuerySet, ) +from scipost.models import get_sentinel_user + class FellowshipNomination(models.Model): @@ -64,6 +66,14 @@ class FellowshipNomination(models.Model): f'on {self.nominated_on.strftime("%Y-%m-%d")}' ) + def add_event(self, description="", by=None): + event = FellowshipNominationEvent( + nomination=self, + description=description, + by=by, + ) + event.save() + @property def ongoing_voting_round(self): return self.voting_rounds.ongoing().first() @@ -92,6 +102,28 @@ class FellowshipNomination(models.Model): return "No voting round found." +class FellowshipNominationEvent(models.Model): + + nomination = models.ForeignKey( + "colleges.FellowshipNomination", on_delete=models.CASCADE, related_name="events" + ) + description = models.TextField() + on = models.DateTimeField(default=timezone.now) + by = models.ForeignKey( + "scipost.Contributor", + on_delete=models.SET(get_sentinel_user), + blank=True, null=True, + ) + + class Meta: + ordering = ["nomination", "-on"] + verbose_name_plural = "Fellowhip Nomination Events" + + def __str__(self): + return (f"Event for {self.nomination.profile} by {self.by} on {self.on}: " + f"{self.description}") + + class FellowshipNominationComment(models.Model): nomination = models.ForeignKey( diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_li_contents.html b/scipost_django/colleges/templates/colleges/_hx_nomination_li_contents.html index 10a8e5336..fc85ed9af 100644 --- a/scipost_django/colleges/templates/colleges/_hx_nomination_li_contents.html +++ b/scipost_django/colleges/templates/colleges/_hx_nomination_li_contents.html @@ -69,6 +69,32 @@ </tr> </table> + <hr> + + {% if "edadmin" in user_roles or "active_senior_fellow" in user_roles %} + <details class="m-2 mt-4 border border-danger"> + <summary class="p-2 bg-light">Events</summary> + <table class="table m-2"> + <thead> + <tr> + <th>Date and time</th> + <th>Description</th> + <th>By</th> + </tr> + </thead> + <tbody> + {% for event in nomination.events.all %} + <tr> + <td>{{ event.on }}</td> + <td>{{ event.description }}</td> + <td>{{ event.by }}</td> + </tr> + {% endfor %} + </tbody> + </table> + </details> + {% endif %} + <div class="card m-2 mt-4"> <div class="card-header"> Comments @@ -216,4 +242,5 @@ </div> {% endif %} + </div> diff --git a/scipost_django/colleges/templates/colleges/_hx_nominations_invitations.html b/scipost_django/colleges/templates/colleges/_hx_nominations_invitations.html index c8045f771..a0106a787 100644 --- a/scipost_django/colleges/templates/colleges/_hx_nominations_invitations.html +++ b/scipost_django/colleges/templates/colleges/_hx_nominations_invitations.html @@ -1,6 +1,5 @@ {% include 'colleges/_hx_nominations_invitations_tablist.html' with selected=selected %} - {% for invitation in invitations.all %} <div class="card"> <div class="card-header"> diff --git a/scipost_django/colleges/templates/colleges/nominations.html b/scipost_django/colleges/templates/colleges/nominations.html index a1688e295..d64fad839 100644 --- a/scipost_django/colleges/templates/colleges/nominations.html +++ b/scipost_django/colleges/templates/colleges/nominations.html @@ -150,7 +150,7 @@ </summary> <div class="p-2 mt-2"> <div id="invitations_tablist" - hx-get="{% url 'colleges:_hx_nominations_invitations' %}?response='notyetinvited'" + hx-get="{% url 'colleges:_hx_nominations_invitations' %}?response=notyetinvited" hx-trigger="load" hx-target="this" hx-swap="innerHTML" diff --git a/scipost_django/colleges/views.py b/scipost_django/colleges/views.py index ea8dad476..2597dfa95 100644 --- a/scipost_django/colleges/views.py +++ b/scipost_django/colleges/views.py @@ -663,6 +663,13 @@ def _hx_nomination_form(request, profile_id): nomination_form = FellowshipNominationForm(request.POST or None, profile=profile) if nomination_form.is_valid(): nomination = nomination_form.save() + nomination.add_event(description="Nominated", by=request.user.contributor) + event = FellowshipNominationEvent( + nomination=nomination, + description="Nominated", + by=request.user.contributor, + ) + event.save() return HttpResponse( f'<div class="bg-success text-white p-2 ">{nomination.profile} ' f"successfully nominated to {nomination.college}.</div>" @@ -723,6 +730,7 @@ def _hx_nomination_comments(request, nomination_id): form = FellowshipNominationCommentForm(request.POST or None, initial=initial) if form.is_valid(): form.save() + nomination.add_event(description="Comment added", by=request.user.contributor) form = FellowshipNominationCommentForm(initial=initial) context = {"nomination": nomination, "form": form,} return render(request, "colleges/_hx_nomination_comments.html", context) @@ -777,6 +785,10 @@ def _hx_nomination_vote(request, voting_round_id): "on": timezone.now(), } ) + if created: + nomination.add_event(description="Vote received", by=request.user.contributor) + else: + nomination.add_event(description="Vote updated", by=request.user.contributor) context = {"voting_round": voting_round, "vote_object": vote_object,} return render(request, "colleges/_hx_nomination_vote.html", context) @@ -788,12 +800,14 @@ def _hx_nomination_decision(request, nomination_id): decision_form = FellowshipNominationDecisionForm(request.POST or None) if decision_form.is_valid(): decision = decision_form.save() + nomination.add_event(description="Decision fixed", by=request.user.contributor) if decision.outcome == FellowshipNominationDecision.OUTCOME_ELECTED: invitation = FellowshipInvitation( nomination=nomination, response=FellowshipInvitation.RESPONSE_NOT_YET_INVITED, ) invitation.save() + nomination.add_event(description="Invitation created", by=request.user.contributor) else: decision_form.fields["nomination"].initial = nomination context = { -- GitLab