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_tavern_group_save(self):
"""When a TavernGroup is saved, we want to make sure
an instance of Membership which associates the creator
with group is created"""
creator = create_and_get_user()
tavern_group = create_and_get_tavern_group(creator=creator)
self.assertEqual(TavernGroup.objects.count(), 1)
self.assertEqual(Membership.objects.count(), 1)
# Change an attribute of tavern_group
tavern_group.description = 'Changed description'
tavern_group.save()
self.assertEqual(TavernGroup.objects.count(), 1)
self.assertEqual(Membership.objects.count(), 1)
member = Membership.objects.all()[0]
self.assertEqual(member.__unicode__(), u'test - TestGroup')
def test_tavern_group_save(self):
"""When a TavernGroup is saved, we want to make sure
an instance of Membership which associates the creator
with group is created"""
creator = create_and_get_user()
tavern_group = create_and_get_tavern_group(creator=creator)
self.assertEqual(TavernGroup.objects.count(), 1)
self.assertEqual(Membership.objects.count(), 1)
# Change an attribute of tavern_group
tavern_group.description = 'Changed description'
tavern_group.save()
self.assertEqual(TavernGroup.objects.count(), 1)
self.assertEqual(Membership.objects.count(), 1)
member = Membership.objects.all()[0]
self.assertEqual(member.__unicode__(), u'test - TestGroup')
def get_context_data(self, **kwargs):
context = super(GroupDetail, self).get_context_data(**kwargs)
context['past_events'] = Event.visible_events.past()
tavern_group = context['group']
try:
Membership.objects.get(tavern_group=tavern_group,
user=self.request.user.id)
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
def get_context_data(self, **kwargs):
context = super(GroupDetail, self).get_context_data(**kwargs)
context['past_events'] = Event.visible_events.past()
tavern_group = context['group']
try:
Membership.objects.get(tavern_group=tavern_group,
user=self.request.user.id)
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
def tavern_toggle_member(request):
"""
Adds a member to the group if he's not in the group, or
deletes a member if he's already a member
"""
user = get_object_or_404(User, id=request.POST.get('user_id'))
group = get_object_or_404(TavernGroup, slug=request.POST.get('slug'))
try:
member = Membership.objects.get(user=user, tavern_group=group)
response = "Join Group"
member.delete()
except Membership.DoesNotExist:
member = Membership.objects.create(
user=user,
tavern_group=group,
join_date=today_date())
response = "Unjoin Group"
return HttpResponse(response)
def clean(self):
cleaned_data = super(CreateEventForm, self).clean()
group = cleaned_data.get('group')
try:
Membership.objects.get(user=self.user, tavern_group=group)
except Membership.DoesNotExist:
raise forms.ValidationError("You are not a member of this group")
return cleaned_data
def get_context_data(self, **kwargs):
context = super(GroupDetail, self).get_context_data(**kwargs)
context['past_events'] = Event.visible_events.past()
tavern_group = context['group']
try:
Membership.objects.get(tavern_group=tavern_group,
user=self.request.user.id)
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
def save(self, *args, **kwargs):
unique_slugify(self, self.name)
super(TavernGroup, self).save(*args, **kwargs)
Membership.objects.get_or_create(
user=self.creator,
tavern_group=self,
defaults={'join_date': timezone.now()})
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(TavernGroup, self).save(*args, **kwargs)
Membership.objects.get_or_create(
user=self.creator,
tavern_group=self,
defaults={'join_date': timezone.now()})
def tavern_toggle_member(request):
"""
Adds a member to the group if he's not in the group, or
deletes a member if he's already a member
"""
user = get_object_or_404(User, id=request.POST.get('user_id'))
group = get_object_or_404(TavernGroup, slug=request.POST.get('slug'))
try:
member = Membership.objects.get(user=user, tavern_group=group)
response = "Join Group"
member.delete()
except Membership.DoesNotExist:
member = Membership.objects.create(
user=user,
tavern_group=group,
join_date=today_date())
response = "Unjoin Group"
return HttpResponse(response)