-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #924 from ae-utbm/unique-student-card
Make student card unique per user
- Loading branch information
Showing
16 changed files
with
353 additions
and
434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Generated by Django 4.2.17 on 2024-12-08 13:30 | ||
from operator import attrgetter | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
from django.db.migrations.state import StateApps | ||
from django.db.models import Count | ||
|
||
|
||
def delete_duplicates(apps: StateApps, schema_editor): | ||
"""Delete cards of users with more than one student cards. | ||
For all users who have more than one registered student card, all | ||
the cards except the last one are deleted. | ||
""" | ||
Customer = apps.get_model("counter", "Customer") | ||
StudentCard = apps.get_model("counter", "StudentCard") | ||
customers = ( | ||
Customer.objects.annotate(nb_cards=Count("student_cards")) | ||
.filter(nb_cards__gt=1) | ||
.prefetch_related("student_cards") | ||
) | ||
to_delete = [ | ||
card.id | ||
for customer in customers | ||
for card in sorted(customer.student_cards.all(), key=attrgetter("id"))[:-1] | ||
] | ||
StudentCard.objects.filter(id__in=to_delete).delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [("counter", "0025_remove_product_parent_product_and_more")] | ||
|
||
operations = [ | ||
migrations.RunPython(delete_duplicates, migrations.RunPython.noop), | ||
migrations.AlterField( | ||
model_name="studentcard", | ||
name="customer", | ||
field=models.OneToOneField( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="student_card", | ||
to="counter.customer", | ||
verbose_name="student card", | ||
), | ||
), | ||
migrations.AlterModelOptions( | ||
name="studentcard", | ||
options={ | ||
"verbose_name": "student card", | ||
"verbose_name_plural": "student cards", | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 25 additions & 25 deletions
50
counter/templates/counter/fragments/create_student_card.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
<div id="student_card_form"> | ||
<h3>{% trans %}Add a student card{% endtrans %}</h3> | ||
<form | ||
hx-post="{{ action }}" | ||
hx-swap="outerHTML" | ||
hx-target="#student_card_form" | ||
> | ||
{% csrf_token %} | ||
{{ form.as_p() }} | ||
<input type="submit" value="{% trans %}Go{% endtrans %}"/> | ||
|
||
</form> | ||
<h6>{% trans %}Registered cards{% endtrans %}</h6> | ||
{% if student_cards %} | ||
|
||
<ul> | ||
{% for card in student_cards %} | ||
<li> | ||
{{ card.uid }} | ||
<a href="{{ url('counter:delete_student_card', customer_id=customer.pk, card_id=card.id) }}"> | ||
{% trans %}Delete{% endtrans %} | ||
</a> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
{% else %} | ||
{% if not customer.student_card %} | ||
<form | ||
hx-post="{{ action }}" | ||
hx-swap="outerHTML" | ||
hx-target="#student_card_form" | ||
> | ||
{% csrf_token %} | ||
{{ form.as_p() }} | ||
<input type="submit" value="{% trans %}Go{% endtrans %}"/> | ||
</form> | ||
<em class="no-cards">{% trans %}No student card registered.{% endtrans %}</em> | ||
{% else %} | ||
<p> | ||
<span tooltip="{% trans uid=customer.student_card.uid %}uid: {{ uid }} {% endtrans %}"> | ||
{% trans %}Card registered{% endtrans %} | ||
<i class="fa fa-check" style="color: green"></i> | ||
</span> | ||
- | ||
<button | ||
hx-get="{{ url('counter:delete_student_card', customer_id=customer.pk) }}" | ||
hx-swap="outerHTML" | ||
hx-target="#student_card_form" | ||
> | ||
{% trans %}Delete{% endtrans %} | ||
</button> | ||
</p> | ||
{% endif %} | ||
</div> |
15 changes: 15 additions & 0 deletions
15
counter/templates/counter/fragments/delete_student_card.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div id="student_card_form"> | ||
<form hx-post="{{ action }}" hx-swap="outerHTML" hx-target="#student_card_form"> | ||
{% csrf_token %} | ||
<p>{% trans obj=object %}Are you sure you want to delete "{{ obj }}"?{% endtrans %}</p> | ||
<input type="submit" value="{% trans %}Confirm{% endtrans %}" /> | ||
<input | ||
hx-get="{{ action_cancel }}" | ||
hx-swap="outerHTML" | ||
hx-target="#student_card_form" | ||
type="submit" | ||
name="cancel" | ||
value="{% trans %}Cancel{% endtrans %}" | ||
/> | ||
</form> | ||
</div> |
Oops, something went wrong.