Skip to content

Commit

Permalink
Merge 1276b9f into 78c1ea9
Browse files Browse the repository at this point in the history
  • Loading branch information
yashwanthbabu committed Sep 11, 2014
2 parents 78c1ea9 + 1276b9f commit 12813ed
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
19 changes: 13 additions & 6 deletions tavern/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
from django.core.urlresolvers import reverse
from django.template.defaultfilters import slugify

from .slugify import unique_slugify


class NonEmptyGroupManager(models.Manager):

def get_queryset(self):
def get_non_empty_users(self):
"""
Filters out tavern groups which contain no members
"""
Expand Down Expand Up @@ -91,7 +89,7 @@ class Event(models.Model):
location = models.TextField(null=True, blank=True)

attendees = models.ManyToManyField(User, through="Attendee",
related_name="events_attending")
related_name="events_attending")
slug = models.SlugField(max_length=250)

creator = models.ForeignKey(User)
Expand All @@ -104,10 +102,19 @@ class Meta:
ordering = ['starts_at']

def get_absolute_url(self):
return reverse("tavern_event_details", kwargs={"slug": self.slug})
return reverse("tavern_event_details", kwargs={"slug": self.slug,
"group": self.group.slug})

def save(self, *args, **kwargs):
unique_slugify(self, self.name)
event_slug = slugify(self.name)
group = self.group
group_events = group.event_set.filter(slug=event_slug)
if group_events:
count = len(group_events) + 1
uniq_slug = event_slug + "-%s" % count
self.slug = uniq_slug
else:
self.slug = slugify(self.name)
super(Event, self).save(*args, **kwargs)
Attendee.objects.get_or_create(
user=self.creator,
Expand Down
2 changes: 1 addition & 1 deletion tavern/templates/tavern/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h1>Groups</h1>
<h1> Events I rsvped </h1>
<ul class="list-group">
{% for event in events_rsvped %}
<li class="list-group-item"><a href="{% url 'tavern_event_details' event.slug %}">{{ event.name }}</a></li>
<li class="list-group-item"><a href="{% url 'tavern_event_details' event.group.slug event.slug %}">{{ event.name }}</a></li>
{% empty %}
<li class="list-group-item">No upcoming events</li>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion tavern/templates/tavern/upcoming_events.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1> Upcoming Events</h1>
<ul class="list-group">
{% for event in upcoming_events %}
<li class="list-group-item"><a href="{% url 'tavern_event_details' event.slug %}">{{ event.name }}</a></li>
<li class="list-group-item"><a href="{% url 'tavern_event_details' event.group.slug event.slug %}">{{ event.name }}</a></li>
{% empty %}
<li class="list-group-item">No upcoming events</li>
{% endfor %}
Expand Down
15 changes: 10 additions & 5 deletions tavern/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,19 @@ def test_event_details(self):

event = create_and_get_event(self.user)
response = self.client.get(reverse("tavern_event_details",
kwargs={'slug': event.slug}))
kwargs={'slug': event.slug,
'group': event.group.slug}))
self.assertEqual(response.status_code, 200)

response = self.client.get(reverse("tavern_event_details",
kwargs={'slug': 'incorrect_slug'}))
kwargs={'slug': 'incorrect_slug',
'group': event.group.slug}))
self.assertEqual(response.status_code, 404)
event.show = False
event.save()
response = self.client.get(reverse("tavern_event_details",
kwargs={'slug': event.slug}))
kwargs={'slug': 'incorrect_slug',
'group': event.group.slug}))
self.assertEqual(response.status_code, 404)

def test_tavern_toggle_member(self):
Expand Down Expand Up @@ -220,7 +223,8 @@ def test_event_update(self):
'ends_at': '2014-07-04 20:25',
'location': 'Delhi'})
self.assertRedirects(response,
reverse("tavern_event_details", kwargs={'slug': 'new-name'}),
reverse("tavern_event_details", kwargs={'slug': 'new-name',
'group': event.group.slug}),
status_code=302)

def test_event_delete(self):
Expand Down Expand Up @@ -248,7 +252,8 @@ def test_delete_rsvp(self):
response = self.client.post(reverse("delete_rsvp",
kwargs={'pk': attendee.pk}), follow=True)
self.assertRedirects(response,
reverse("tavern_event_details", kwargs={'slug': event.slug}),
reverse("tavern_event_details", kwargs={'slug': event.slug,
'group': event.group.slug}),
status_code=302)

def test_edit_organizers(self):
Expand Down
2 changes: 1 addition & 1 deletion tavern/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
url(r'^groups/(?P<slug>[\w-]+)/edit_organizer',
views.edit_organizers,
name='edit_organizers'),
url(r'^events/(?P<slug>[\w-]+)/$',
url(r'^(?P<group>[\w-]+)/events/(?P<slug>[\w-]+)/$',
views.event_details, name='tavern_event_details'),
url(r'^create_event/',
views.create_event, name='tavern_create_event'),
Expand Down
20 changes: 17 additions & 3 deletions tavern/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponse, HttpResponseRedirect, \
Http404
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404, get_list_or_404
from django.views.generic import DetailView
Expand Down Expand Up @@ -132,7 +133,10 @@ class EventDetail(UpcomingEventsMixin, DetailView):

def get_context_data(self, **kwargs):
context = super(EventDetail, self).get_context_data(**kwargs)
event = context['event']
event_name = self.kwargs['slug']
group_name = self.kwargs['group']
group = TavernGroup.objects.get(slug=group_name)
event = group.event_set.get(slug=event_name)
try:
attendee = Attendee.objects.get(user=self.request.user,
event=event)
Expand All @@ -152,6 +156,15 @@ def get_context_data(self, **kwargs):
context['editable'] = event.starts_at > timezone.now()
return context

def get_object(self, **kwargs):
group_name = self.kwargs['group']
event_name = self.kwargs['slug']
try:
group = TavernGroup.objects.get(slug=group_name)
event = group.event_set.get(slug=event_name)
except Event.DoesNotExist:
raise Http404


class GroupCreate(LoginRequiredMixin, CreateView):
""" Create new group """
Expand Down Expand Up @@ -260,7 +273,8 @@ class RsvpDelete(LoginRequiredMixin, DeleteView):
model = Attendee

def get_success_url(self, **kwargs):
return reverse("tavern_event_details", kwargs={"slug": self.object.event.slug})
return reverse("tavern_event_details", kwargs={"slug": self.object.event.slug,
"group": self.object.event.group.slug})


tavern_group_update = GroupUpdate.as_view()
Expand Down

0 comments on commit 12813ed

Please sign in to comment.