From dfa0f01d95176f3347d445083e3d083df4bdf1bd Mon Sep 17 00:00:00 2001 From: Shane Moore Date: Sun, 22 Jul 2018 22:08:25 -0400 Subject: [PATCH] Move the invite codes below the 'generate' button and sort them by newest. Update the response of the invite generation endpoint to return the full list of invite codes. This guarantees consistency and makes it simpler to reason about the Intercooler replacement logic. --- .../templates/intercooler/invite_code.jinja2 | 33 +++++++++------ tildes/tildes/templates/invite.jinja2 | 40 ++++++++++--------- tildes/tildes/views/api/web/user.py | 14 ++++++- tildes/tildes/views/user.py | 3 ++ 4 files changed, 58 insertions(+), 32 deletions(-) diff --git a/tildes/tildes/templates/intercooler/invite_code.jinja2 b/tildes/tildes/templates/intercooler/invite_code.jinja2 index dd81f1f..78ddfef 100644 --- a/tildes/tildes/templates/intercooler/invite_code.jinja2 +++ b/tildes/tildes/templates/intercooler/invite_code.jinja2 @@ -1,12 +1,21 @@ - - -{% if num_remaining > 0 %} - -{% endif %} +
+ {% if num_remaining > 0 %} + + {% else %} +

You don't currently have any invite codes available.

+ {% endif %} + {% if codes %} +

You have the following invite codes active that have not been used yet:

+ {% for code in codes %} + + {% endfor %} + {% endif %} +
\ No newline at end of file diff --git a/tildes/tildes/templates/invite.jinja2 b/tildes/tildes/templates/invite.jinja2 index 7ae3d49..c065b35 100644 --- a/tildes/tildes/templates/invite.jinja2 +++ b/tildes/tildes/templates/invite.jinja2 @@ -17,23 +17,25 @@
-{% if codes %} -

You have the following invite codes active that have not been used yet:

- {% for code in codes %} - - {% endfor %} -{% endif %} - -{% if request.user.invite_codes_remaining > 0 %} - -{% else %} -

You don't currently have any invite codes available.

-{% endif %} +
+ {% if request.user.invite_codes_remaining > 0 %} + + {% else %} +

You don't currently have any invite codes available.

+ {% endif %} + {% if codes %} +

You have the following invite codes active that have not been used yet:

+ {% for code in codes %} + + {% endfor %} + {% endif %} +
{% endblock %} diff --git a/tildes/tildes/views/api/web/user.py b/tildes/tildes/views/api/web/user.py index 49d0f87..651917c 100644 --- a/tildes/tildes/views/api/web/user.py +++ b/tildes/tildes/views/api/web/user.py @@ -171,13 +171,25 @@ def get_invite_code(request: Request) -> dict: except IntegrityError: savepoint.rollback() + codes = ( + request.query(UserInviteCode) + .filter( + UserInviteCode.user_id == request.user.user_id, + UserInviteCode.invitee_id == None, # noqa + ) + .order_by( + UserInviteCode.created_time.desc() + ) + .all() + ) + # doing an atomic decrement on request.user.invite_codes_remaining is going # to make it unusable as an integer in the template, so store the expected # value after the decrement first, to be able to use that instead num_remaining = request.user.invite_codes_remaining - 1 request.user.invite_codes_remaining = User.invite_codes_remaining - 1 - return {'code': code, 'num_remaining': num_remaining} + return {'codes': codes, 'num_remaining': num_remaining} @ic_view_config( diff --git a/tildes/tildes/views/user.py b/tildes/tildes/views/user.py index 7627861..c4f30bd 100644 --- a/tildes/tildes/views/user.py +++ b/tildes/tildes/views/user.py @@ -83,6 +83,9 @@ def get_invite(request: Request) -> dict: UserInviteCode.user_id == request.user.user_id, UserInviteCode.invitee_id == None, # noqa ) + .order_by( + UserInviteCode.created_time.desc() + ) .all() )