Skip to content

Commit

Permalink
Merge pull request #143 from soehlert/topic/soehlert/i_was_there_arti…
Browse files Browse the repository at this point in the history
…st_redirect

refactor(i was there UX): adds I was there to a few other places
  • Loading branch information
soehlert authored Oct 25, 2023
2 parents c5d3320 + 931c3fb commit 7304bee
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 29 deletions.
5 changes: 5 additions & 0 deletions concert_elephant/templates/concerts/artist_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ <h5 class="card-header card-header-custom display-4 text-center">Recent Concerts
<th scope="col">Artist</th>
<th class="text-center" scope="col">Venue</th>
<th class="text-center" scope="col">Date</th>
<th></th>
</tr>
</thead>
{% for concert in recent_concerts %}
Expand All @@ -42,6 +43,10 @@ <h5 class="card-header card-header-custom display-4 text-center">Recent Concerts
<td>{{ concert.artist }}</td>
<td class="text-center">{{ concert.venue }}</td>
<td class="text-center">{{ concert.date }}</td>
<td>
{% include "concerts/attend_concert.html" %}
{% include "concerts/unattend_concert.html" %}
</td>
</tr>
{% endfor %}
</table>
Expand Down
17 changes: 17 additions & 0 deletions concert_elephant/templates/concerts/attend_concert.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% if user.is_authenticated %}
{% if concert.id not in user_concerts %}
<form class="form-inline"
action="{% url 'concerts:attend-concert' pk=concert.id %}"
method="POST">
{% csrf_token %}
{% if in_artist_detail %}
<input type="hidden" name="next" value="artist-detail">
{% elif in_concert_list %}
<input type="hidden" name="next" value="concert-list">
{% endif %}
<div class="text-center">
<button class="btn btn-info btn-sm text-white" type="submit">I Was There!</button>
</div>
</form>
{% endif %}
{% endif %}
5 changes: 5 additions & 0 deletions concert_elephant/templates/concerts/concert_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ <h5 class="display-4 text-center">Concerts</h5>
</span>
</th>
<th class="text-center">Openers</th>
<th></th>
</tr>
</thead>
{% for concert in page_obj %}
Expand All @@ -75,6 +76,10 @@ <h5 class="display-4 text-center">Concerts</h5>
{% if not forloop.last %},{% endif %}
{% endfor %}
</td>
<td>
{% include "concerts/attend_concert.html" %}
{% include "concerts/unattend_concert.html" %}
</td>
</tr>
{% endfor %}
</table>
Expand Down
31 changes: 14 additions & 17 deletions concert_elephant/templates/concerts/unattend_concert.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
{% if user.is_authenticated and concert.id in user_concerts %}
<form class="form-inline"
{% if in_user_detail %}
action="{% url 'concerts:unattend-concert' pk=concert.id next='user-detail' %}"
{% else %}
action="{% url 'concerts:unattend-concert' pk=concert.id next='concert-list' %}"
{% endif %}
method="POST">
{% csrf_token %}
<div class="text-center">
<form class="form-inline" method="POST" action="{% url 'concerts:unattend-concert' pk=concert.id%}">
{% csrf_token %}
<div class="text-center">
{% if in_user_detail %}
<div>
<button type="submit" data-bs-toggle="tooltip" data-bs-placement="top" title="Remove concert from my list"
class="btn btn-danger btn-delete btn-sm">
<button type="submit" data-bs-toggle="tooltip" data-bs-placement="top" title="Remove concert from my list" class="btn btn-danger btn-delete btn-sm">
<i class="bi bi-trash3-fill trash-icon-red"></i>
</button>
</div>
<input type="hidden" name="next" value="user-detail">
{% else %}
<button type="submit" data-bs-toggle="tooltip" data-bs-placement="top" title="Remove concert from my list"
class="btn btn-danger btn-sm btn-delete text-danger">
<button type="submit" data-bs-toggle="tooltip" data-bs-placement="top" title="Remove concert from my list" class="btn btn-danger btn-sm btn-delete text-danger">
Remove From My Concerts
</button>
{% if in_artist_detail %}
<input type="hidden" name="next" value="artist-detail">
{% else %}
<input type="hidden" name="next" value="concert-list">
{% endif %}
{% endif %}
</div>
</form>
</div>
</form>
{% endif %}


5 changes: 5 additions & 0 deletions concert_elephant/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def get_context_data(self, **kwargs):
context["concerts_with_reviews"] = concerts_with_reviews
context["user_concerts"] = user_concerts
context["review_form"] = ConcertReviewForm()

# Indicate that we're on the user detail page
# used for redirection when unattending/attending a concert
context["in_user_detail"] = True

return context


Expand Down
3 changes: 0 additions & 3 deletions concerts/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class ArtistViewset(viewsets.GenericViewSet, ListModelMixin, RetrieveModelMixin,
serializer_class = ArtistSerializer

def get_permissions(self):
print(self.request.headers)
if self.action == "create":
return [permissions.IsAuthenticated()]
elif self.action == "list":
Expand All @@ -25,7 +24,6 @@ class VenueViewset(viewsets.GenericViewSet, ListModelMixin, RetrieveModelMixin,
serializer_class = VenueSerializer

def get_permissions(self):
print(self.request.headers)
if self.action == "create":
return [permissions.IsAuthenticated()]
elif self.action == "list":
Expand All @@ -38,7 +36,6 @@ class ConcertViewset(viewsets.GenericViewSet, ListModelMixin, RetrieveModelMixin
serializer_class = ConcertSerializer

def get_permissions(self):
print(self.request.headers)
if self.action == "create":
return [permissions.IsAuthenticated()]
elif self.action == "list":
Expand Down
2 changes: 1 addition & 1 deletion concerts/tests/features/steps/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def step_impl(context, url_name):
param_mapping = {
"concerts:concert-detail": [concert.id],
"concerts:attend-concert": [concert.id],
"concerts:unattend-concert": [concert.id, "dummy_next_value"],
"concerts:unattend-concert": [concert.id],
"concerts:add-concert-review": [concert.id],
"concerts:update-concert-review": [review.id],
"concerts:delete-concert-review": [review.id],
Expand Down
3 changes: 1 addition & 2 deletions concerts/tests/features/steps/webviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def step_impl(context):

@when("I unattend the concert")
def step_impl(context):
concert_list_url = reverse("concerts:concert-list").replace("/", "")
url = reverse("concerts:unattend-concert", kwargs={"pk": context.entity.id, "next": concert_list_url})
url = reverse("concerts:unattend-concert", kwargs={"pk": context.entity.id})
context.response = context.client.post(url)


Expand Down
2 changes: 1 addition & 1 deletion concerts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
path("concerts/add/", views.ConcertCreateView.as_view(), name="concert-create"),
path("concerts/<int:pk>/", views.ConcertDetailView.as_view(), name="concert-detail"),
path("concerts/attend/<int:pk>/", view=views.attend_concert, name="attend-concert"),
path("concerts/unattend/<int:pk>/<str:next>/", views.unattend_concert, name="unattend-concert"),
path("concerts/unattend/<int:pk>/", views.unattend_concert, name="unattend-concert"),
# Concert Reviews
path("concert/<int:pk>/add_review/", views.ConcertReviewCreateView.as_view(), name="add-concert-review"),
path("concert/review/<int:review_id>/", views.ConcertReviewDetailView.as_view(), name="get-concert-review"),
Expand Down
46 changes: 41 additions & 5 deletions concerts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,31 @@ def get(self, request, model_name, *args, **kwargs):
def attend_concert(request, pk):
concert = get_object_or_404(Concert, pk=pk)
Concert.attend_concert(request.user, concert)

return redirect("concerts:concert-list")
return handle_redirection(request, concert)


@login_required
def unattend_concert(request, pk, next=None):
def unattend_concert(request, pk):
concert = get_object_or_404(Concert, pk=pk)
concert.attendees.remove(request.user)

messages.info(
request,
"Concert removed from your profile. Your review has been saved and can be accessed if you re-add the concert.",
"Concert removed from your profile. If you had a review, your review "
"has been saved and can be accessed if you re-add the concert.",
)
if next == "user-detail":

return handle_redirection(request, concert)


def handle_redirection(request, concert):
next_page = request.POST.get("next", "") or request.GET.get("next", "")

if next_page == "concert-list":
return redirect("concerts:concert-list")
elif next_page == "artist-detail":
return redirect("concerts:artist-detail", pk=concert.artist.pk)
elif next_page == "user-detail":
return redirect("users:detail", request.user.username)
else:
return redirect("concerts:concert-list")
Expand Down Expand Up @@ -145,6 +156,16 @@ def get_context_data(self, **kwargs):
context["recent_concerts"] = total_concerts
context["total_concerts_count"] = total_concerts_count

# Add the user_concerts to the context
if self.request.user.is_authenticated:
context["user_concerts"] = Concert.objects.filter(attendees=self.request.user).values_list("id", flat=True)
else:
context["user_concerts"] = []

# Indicate that we're on the artist detail page
# used for redirection when unattending/attending a concert
context["in_artist_detail"] = True

return context

def render_to_response(self, context, **response_kwargs):
Expand Down Expand Up @@ -301,6 +322,21 @@ def get_queryset(self):

return queryset

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

# Add the user_concerts to the context
if self.request.user.is_authenticated:
context["user_concerts"] = Concert.objects.filter(attendees=self.request.user).values_list("id", flat=True)
else:
context["user_concerts"] = []

# Indicate that we're on the concert list view
# used for redirection when unattending/attending a concert
context["in_concert_list"] = True

return context


class ConcertDetailView(DetailView):
model = Concert
Expand Down

0 comments on commit 7304bee

Please sign in to comment.