Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #336 from ajatprabha/ticket_333
Browse files Browse the repository at this point in the history
add cancel button for non-approved join request
  • Loading branch information
yatna committed Apr 27, 2018
2 parents 4f20a9d + 8fc1dc5 commit a1ce9d9
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 6 deletions.
60 changes: 60 additions & 0 deletions systers_portal/meetup/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,3 +1768,63 @@ def test_post_delete_support_request_comment_view(self):
self.assertEqual(response.status_code, 302)
comments = Comment.objects.all()
self.assertEqual(len(comments), 0)


class CancelMeetupLocationJoinRequestViewTestCase(MeetupLocationViewBaseTestCase, TestCase):
def setUp(self):
super(CancelMeetupLocationJoinRequestViewTestCase, self).setUp()
self.password = 'foobar'
self.meetup_location.members.remove(self.systers_user)

def test_cancel_meetup_location_join_request(self):
"""Test GET request to cancel a join request to a meetup location"""
current_url = reverse("about_meetup_location", kwargs={'slug': 'foo'})
self.data = {
'current_url': current_url,
'username': self.user.username
}
self.url = reverse("cancel_meetup_location_join_request", kwargs={
'slug': self.meetup_location.slug,
'username': self.user.username
})
response = self.client.get(self.url, {'current_url': current_url})
self.assertEqual(response.status_code, 403)

user = User.objects.create_user(username='bar', password=self.password)
self.client.login(username=user.username, password=self.password)
nonexistent_meetup_location_url = reverse("about_meetup_location", kwargs={'slug': 'new'})
response = self.client.get(nonexistent_meetup_location_url, {
'current_url': nonexistent_meetup_location_url,
'username': self.user.username
})
self.assertEqual(response.status_code, 404)

self.check_message_in_response(
'warning',
'There is no pending request to join Foo Systers meetup location.',
200
)

self.meetup_location.join_requests.add(self.systers_user)
self.assertEqual(self.meetup_location.join_requests.all().count(), 1)
self.check_message_in_response(
'success',
'Your request to join Foo Systers meetup location was canceled.',
200
)
self.assertEqual(self.meetup_location.join_requests.all().count(), 0)

self.meetup_location.members.add(self.systers_user)
self.check_message_in_response(
'warning',
'You are already a member of Foo Systers meetup location. '
'There is no pending join request.',
200
)

def check_message_in_response(self, _message_tag=None, _message=None, status_code=None):
response = self.client.get(self.url, self.data, follow=True)
self.assertEqual(response.status_code, status_code)
for message in response.context['messages']:
self.assertEqual(message.tags, _message_tag)
self.assertTrue(_message in message.message)
6 changes: 5 additions & 1 deletion systers_portal/meetup/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
NewMeetupLocationRequestsListView, ViewMeetupLocationRequestView,
RejectMeetupLocationRequestView, ApproveRequestMeetupLocationView,
RequestMeetupView, NewMeetupRequestsListView, ViewMeetupRequestView,
ApproveRequestMeetupView, RejectMeetupRequestView)
ApproveRequestMeetupView, RejectMeetupRequestView,
CancelMeetupLocationJoinRequestView)


urlpatterns = [
Expand Down Expand Up @@ -69,6 +70,9 @@
name='make_organizer_meetup_location'),
url(r'^(?P<slug>[\w-]+)/join/(?P<username>[\w.@+-]+)/$', JoinMeetupLocationView.as_view(),
name='join_meetup_location'),
url(r'^(?P<slug>[\w-]+)/cancel/(?P<username>[\w.@+-]+)/$',
CancelMeetupLocationJoinRequestView.as_view(),
name='cancel_meetup_location_join_request'),
url(r'^(?P<slug>[\w-]+)/join_requests/$', MeetupLocationJoinRequestsView.as_view(),
name='join_requests_meetup_location'),
url(r'^(?P<slug>[\w-]+)/join_requests/approve/(?P<username>[\w.@+-]+)/$',
Expand Down
58 changes: 57 additions & 1 deletion systers_portal/meetup/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404
from django.views.generic import DeleteView, TemplateView, RedirectView
from django.views.generic.detail import DetailView
from django.views.generic.detail import DetailView, SingleObjectMixin
from django.views.generic.edit import CreateView, UpdateView, FormView
from django.views.generic.list import ListView
from braces.views import LoginRequiredMixin, PermissionRequiredMixin, StaffuserRequiredMixin
Expand Down Expand Up @@ -1394,3 +1394,59 @@ def check_permissions(self, request):
self.comment = get_object_or_404(Comment, pk=self.kwargs['comment_pk'])
systersuser = get_object_or_404(SystersUser, user=request.user)
return systersuser == self.comment.author


class CancelMeetupLocationJoinRequestView(LoginRequiredMixin, SingleObjectMixin,
RedirectView):
"""Cancel a join request to a meetup location view"""
model = MeetupLocation
permanent = False
raise_exception = True

# TODO: add `redirect_unauthenticated_users = True` when django-braces will
# reach version 1.5

def get_redirect_url(self, *args, **kwargs):
"""Redirect to the page the user was previously on"""
return self.request.GET.get('current_url')

def get(self, request, *args, **kwargs):
"""Attempt to cancel user join request towards a meetup location
* if a SystersUser is already a member, add a warning message
* if there is no pending request, add a warning message
"""
user = get_object_or_404(User, username=self.kwargs.get('username'))
systersuser = get_object_or_404(SystersUser, user=user)

join_requests = self.get_object().join_requests.all()
members = self.get_object().members.all()

if systersuser not in members and systersuser in join_requests:
self.get_object().join_requests.remove(systersuser)
messages.add_message(
request,
messages.SUCCESS,
"Your request to join {0} meetup location was canceled.".format(self.get_object())
)

elif systersuser in members:
messages.add_message(
request,
messages.WARNING,
"You are already a member of {0} meetup location. "
"There is no pending join request.".format(
self.get_object()
))

elif systersuser not in join_requests:
messages.add_message(
request,
messages.WARNING,
"There is no pending request to join {0} meetup location.".format(self.get_object())
)
else:
pass
# TODO: configure logging and log the unknown status
return super(CancelMeetupLocationJoinRequestView, self).get(request, *args,
**kwargs)
14 changes: 10 additions & 4 deletions systers_portal/templates/meetup/snippets/join_button.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{% if user.is_authenticated and user.is_active %}
{% if user not in meetup_location.members.all or user not in meetup_location.organizers.all %}
<div class="sidebar-module mb40">
<a class="btn btn-success btn-block" href="{% url "join_meetup_location" meetup_location.slug user.username %}" role="button">Join Meetup Location</a>
</div>
{% if user.systersuser not in meetup_location.organizers.all %}
{% if user.systersuser not in meetup_location.members.all and user.systersuser not in meetup_location.join_requests.all %}
<div class="sidebar-module mb40">
<a class="btn btn-success btn-block" href="{% url "join_meetup_location" meetup_location.slug user.username %}"
role="button">Join Meetup Location</a>
</div>
{% elif user.systersuser not in meetup_location.members.all and user.systersuser in meetup_location.join_requests.all %}
<a href="{% url "cancel_meetup_location_join_request" meetup_location.slug user.username %}?current_url={{ request.path }}"
role="button" class="btn btn-warning btn-block">Cancel request</a>
{% endif %}
{% endif %}
{% endif %}

0 comments on commit a1ce9d9

Please sign in to comment.