Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChris Komaroff
Courses Plus Student 14,198 PointsHere is code for ChangeStatus view - copied from video, seems to work for me.
class ChangeStatus(
LoginRequiredMixin,
PermissionRequiredMixin,
generic.RedirectView
):
permission_required = "communities.ban_member"
# View requires "communites.ban_member" except for admins
def has_permission(self):
return any([
super().has_permission(),
self.request.user.id in self.get_object().admins
])
# ChangeStatus view based on Community model instance for slug
def get_object(self):
return get_object_or_404(
models.Community,
slug = self.kwargs.get("slug")
)
def get_redirect_url(self, *args, **kwargs):
return self.get_object().get_absolute_url()
# HTTP get()
def get(self, request, *args, **kwargs):
role = int(self.kwargs.get("status"))
membership = get_object_or_404(
models.CommunityMember,
community__slug = self.kwargs.get("slug"),
user__id = self.kwargs.get("user_id")
)
membership.role = role
membership.save()
try:
moderators = Group.objects.get(name__iexact="moderators")
except Group.DoesNotExist:
moderators = Group.objects.create(name="Moderators")
moderators.permissions.add(
Permission.objects.get(codename="ban_member")
)
if role in [2, 3]:
membership.user.groups.add(moderators)
else:
membership.user.groups.remove(moderators)
messages.success(request, "@{} is now {}".format(
membership.user.username,
membership.get_role_display()
))
return super().get(request, *args, **kwargs)
Tanja Riet
10,803 PointsTanja Riet
10,803 PointsThanks.