def manage_community(request, pk): if request.user.is_authenticated: community = Community.objects.get(pk=pk) uid = request.user.id errormessage = '' membership = None try: membership = CommunityMembership.objects.get( user=uid, community=community.pk) if membership.role.name == 'community_admin': count = CommunityMembership.objects.filter( community=community.pk, role=membership.role).count() members = CommunityMembership.objects.filter( community=community.pk) if request.method == 'POST': try: username = request.POST['username'] rolename = request.POST['role'] user = User.objects.get(username=username) role = Roles.objects.get(name=rolename) status = request.POST['status'] if status == 'add': try: is_member = CommunityMembership.objects.get( user=user, community=community.pk) except CommunityMembership.DoesNotExist: obj = CommunityMembership.objects.create( user=user, community=community, role=role) #if rolename=='publisher': #create_community_feed(user,'New Publisher has been added',community) else: errormessage = 'user exists in community' if status == 'update': if count > 1 or count == 1 and username != request.user.username: try: update_role_feed(user, community, rolename) notify_update_role(request.user, user, community, rolename) is_member = CommunityMembership.objects.get( user=user, community=community.pk) is_member.role = role is_member.save() except CommunityMembership.DoesNotExist: errormessage = 'no such user in the community' else: errormessage = 'cannot update this user' if status == 'remove': if count > 1 or count == 1 and username != request.user.username: try: remove_or_add_user_feed( user, community, 'removed') notify_remove_or_add_user( request.user, user, community, 'removed') obj = CommunityMembership.objects.filter( user=user, community=community).delete() except CommunityMembership.DoesNotExist: errormessage = 'no such user in the community' else: errormessage = 'cannot remove this user' return render( request, 'managecommunity.html', { 'community': community, 'members': members, 'membership': membership, 'errormessage': errormessage }) # return redirect('manage_community',pk=pk) except User.DoesNotExist: errormessage = "no such user in the system" return render( request, 'managecommunity.html', { 'community': community, 'members': members, 'membership': membership, 'errormessage': errormessage }) else: return redirect('community_view', pk=pk) except CommunityMembership.DoesNotExist: return redirect('community_view', pk=pk) else: return redirect('login')
def manage_group(request,pk): if request.user.is_authenticated: group = Group.objects.get(pk=pk) community = CommunityGroups.objects.get(group=pk) uid = request.user.id errormessage = '' membership = None try: membership = GroupMembership.objects.get(user=uid, group=group.pk) if membership.role.name == 'group_admin': count = GroupMembership.objects.filter( group=group.pk, role=membership.role).count() members = GroupMembership.objects.filter(group=group.pk) if request.method == 'POST': try: username = request.POST['username'] rolename = request.POST['role'] user = User.objects.get(username = username) role = Roles.objects.get(name=rolename) status = request.POST['status'] if status == 'add': try: is_community_member = CommunityMembership.objects.get(user=user, community=community.community.pk) try: is_member = GroupMembership.objects.get(user=user, group=group.pk) errormessage = 'user exists in group' except GroupMembership.DoesNotExist: errormessage = inviteUser(request, group, user, role) except CommunityMembership.DoesNotExist: errormessage = 'user is not a part of the community' if status == 'update': if count > 1 or count == 1 and username != request.user.username: try: notify_update_role(request.user, user, group, rolename) update_role_feed(user, group, rolename) is_member = GroupMembership.objects.get(user=user, group=group.pk) is_member.role = role is_member.save() except GroupMembership.DoesNotExist: errormessage = 'no such user in the group' else: errormessage = 'cannot update this user' if status == 'remove': if count > 1 or count == 1 and username != request.user.username: try: notify_remove_or_add_user(request.user, user, group, 'removed') remove_or_add_user_feed(user, group, "removed") obj = GroupMembership.objects.filter(user=user, group=group).delete() except GroupMembership.DoesNotExist: errormessage = 'no such user in the group' else: errormessage = 'cannot remove this user' return render(request, 'managegroup.html', {'community':community, 'group':group, 'members':members, 'membership':membership, 'errormessage':errormessage}) #return redirect('manage_group',pk=pk) except User.DoesNotExist: errormessage = "no such user in the system" return render(request, 'managegroup.html', {'community':community, 'group':group, 'members':members, 'membership':membership, 'errormessage':errormessage}) else: return redirect('group_view',pk=pk) except GroupMembership.DoesNotExist: return redirect('group_view',pk=pk) else: return redirect('login')