Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_event_delete(self):
event = create_and_get_event(user=self.user)
response = self.client.post(reverse("delete_event",
kwargs={'slug': event.slug}), follow=True)
self.assertRedirects(response,
reverse("tavern_group_details", kwargs={'slug': event.group.slug}),
status_code=302)
self.assertEqual(event in Event.objects.all(), False)
from django.contrib import admin
from .models import TavernGroup, Event, Attendee, Membership
class GroupAdmin(admin.ModelAdmin):
exclude = ('slug',)
class EventAdmin(admin.ModelAdmin):
exclude = ('slug',)
admin.site.register(TavernGroup, GroupAdmin)
admin.site.register(Event, EventAdmin)
admin.site.register(Attendee)
admin.site.register(Membership)
user_is_member = True
except Membership.DoesNotExist:
user_is_member = False
context['user_is_member'] = user_is_member
recent_group_members = get_list_or_404(Membership, tavern_group=tavern_group)[:5]
context["recent_group_members"] = recent_group_members
return context
class EventDetail(UpcomingEventsMixin, DetailView):
""" Give details about an event and its attendees"""
template_name = "tavern/event_details.html"
context_object_name = "event"
model = Event
queryset = Event.visible_events.all()
def get_context_data(self, **kwargs):
context = super(EventDetail, self).get_context_data(**kwargs)
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_id=self.request.user.id,
event=event)
message = attendee.get_rsvp()
context['attendee'] = attendee
except Attendee.DoesNotExist:
if timezone.now() <= event.ends_at:
message = "You have not rsvped"
except Membership.DoesNotExist:
user_is_member = False
context['user_is_member'] = user_is_member
recent_group_members = get_list_or_404(Membership, tavern_group=tavern_group)[:5]
context["recent_group_members"] = recent_group_members
return context
class EventDetail(UpcomingEventsMixin, DetailView):
""" Give details about an event and its attendees"""
template_name = "tavern/event_details.html"
context_object_name = "event"
model = Event
queryset = Event.visible_events.all()
def get_context_data(self, **kwargs):
context = super(EventDetail, self).get_context_data(**kwargs)
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_id=self.request.user.id,
event=event)
message = attendee.get_rsvp()
context['attendee'] = attendee
except Attendee.DoesNotExist:
if timezone.now() <= event.ends_at:
message = "You have not rsvped"
else:
def index(request, template='tavern/home.html'):
""" index page """
all_groups = TavernGroup.objects.all()
if request.user.is_authenticated():
joined_groups = request.user.tavern_groups.all()
unjoined_groups = list(set(all_groups) - set(joined_groups))
upcoming_events = Event.visible_events.upcoming()
events_rsvped = request.user.events_attending.all()
context = {'joined_groups': joined_groups,
'unjoined_groups': unjoined_groups,
'upcoming_events': upcoming_events,
'events_rsvped': events_rsvped}
else:
context = {'groups': all_groups}
return render(request, template, context)
user_is_member = True
except Membership.DoesNotExist:
user_is_member = False
context['user_is_member'] = user_is_member
recent_group_members = get_list_or_404(Membership, tavern_group=tavern_group)[:5]
context["recent_group_members"] = recent_group_members
return context
class EventDetail(UpcomingEventsMixin, DetailView):
""" Give details about an event and its attendees"""
template_name = "tavern/event_details.html"
context_object_name = "event"
model = Event
def get_queryset(self, **kwargs):
return self.model.visible_events.all()
def get_context_data(self, **kwargs):
context = super(EventDetail, self).get_context_data(**kwargs)
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)
if attendee.rsvp_status == 'yes':
message = "You are attending this event."
elif attendee.rsvp_status == 'no':
super(Event, self).save(*args, **kwargs)
Attendee.objects.get_or_create(
user=self.creator,
event=self,
defaults={'rsvped_on': timezone.now(),
'rsvp_status': 'yes'})
def __unicode__(self):
return "%s" % self.name
class Attendee(models.Model):
"People who have RSVPed to events"
RSVP_CHOICES = (('yes', 'Yes'), ('no', 'No'), ('maybe', 'May Be'))
user = models.ForeignKey(User)
event = models.ForeignKey(Event)
rsvped_on = models.DateTimeField()
rsvp_status = models.CharField(verbose_name="RSVP Status",
choices=RSVP_CHOICES,
max_length=5,
default="yes")
def get_name(self):
return self.user.get_full_name() or self.user.username
def get_rsvp(self):
if self.rsvp_status == "yes" and timezone.now() <= self.event.ends_at:
return "You are attending this event."
elif self.rsvp_status == "yes" and timezone.now() > self.event.ends_at:
return "You have attended this event."
elif self.rsvp_status == "no" and timezone.now() <= self.event.ends_at:
return "You are not attending this event."
def get_context_data(self, **kwargs):
context = super(UpcomingEventsMixin, self).get_context_data(**kwargs)
context['upcoming_events'] = Event.visible_events.upcoming()
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
return event
def index(request, template='tavern/home.html'):
""" index page """
all_groups = TavernGroup.objects.all()
if request.user.is_authenticated():
joined_groups = request.user.tavern_groups.all()
unjoined_groups = list(set(all_groups) - set(joined_groups))
upcoming_events = Event.visible_events.upcoming()
events = Attendee.objects.filter(user=request.user)
events_rsvped = [event.event for event in events]
context = {'joined_groups': joined_groups,
'unjoined_groups': unjoined_groups,
'upcoming_events': upcoming_events,
'events_rsvped': events_rsvped}
else:
context = {'groups': all_groups}
return render(request, template, context)