From 49c08afc91eb38adb17b05810e4404d396ab43d1 Mon Sep 17 00:00:00 2001 From: Christian Glatthard Date: Thu, 6 Aug 2015 16:22:52 +0200 Subject: [PATCH] collaborationgroups web tested and running --- ipynbsrv/core/models.py | 5 +- .../web/collaborationgroups/index.html | 10 +- .../web/collaborationgroups/manage.html | 39 +++- .../collaborationgroups/modal_addusers.html | 2 +- ipynbsrv/web/urls.py | 3 + ipynbsrv/web/views/collaborationgroups.py | 175 +++++++++++++++--- 6 files changed, 190 insertions(+), 44 deletions(-) diff --git a/ipynbsrv/core/models.py b/ipynbsrv/core/models.py index 27af804..2efd0b9 100644 --- a/ipynbsrv/core/models.py +++ b/ipynbsrv/core/models.py @@ -388,12 +388,15 @@ def remove_admin(self, user): def remove_member(self, user): """ Remove the member `user` from the group. + If the user appears to be a group admin, he/she will also be removed as admin. :param user: The user to remove (if is member). :return bool `True` if the user has been a member and removed. """ + print("remove %s" % user) if self.user_is_member(user): + self.remove_admin(user) self.user_set.remove(user.django_user) return True return False @@ -412,7 +415,6 @@ def user_is_admin(self, user): :param user: The user to check. """ return user in self.admins.all() - user_is_admin.boolean = True def user_is_member(self, user): """ @@ -421,7 +423,6 @@ def user_is_member(self, user): :param user: The user to check for membership. """ return user in self.get_members() - user_is_member.boolean = True class Container(models.Model): diff --git a/ipynbsrv/web/templates/web/collaborationgroups/index.html b/ipynbsrv/web/templates/web/collaborationgroups/index.html index 254c2ea..537d54b 100644 --- a/ipynbsrv/web/templates/web/collaborationgroups/index.html +++ b/ipynbsrv/web/templates/web/collaborationgroups/index.html @@ -39,9 +39,9 @@

My Groups

{% else %} {% if request.user.backend_user.id in group.member_ids %}Member{% endif %}{% endif %} - {% if request.user.backend_user.id != group.creator and group.creator %} + {% if request.user.backend_user.id != group.creator.id and group.creator %} {% if request.user.backend_user.id in group.member_ids %} -
+ {% csrf_token %} @@ -50,10 +50,10 @@

My Groups

{% else %} -
+ {% csrf_token %} - + @@ -61,7 +61,7 @@

My Groups

{% endif %} {% endif %} - {% if request.user.backend_user.id == group.creator or request.user.id in group.admins %} + {% if request.user.backend_user.id == group.creator.id or request.user.id in group.admins %} {% csrf_token %} diff --git a/ipynbsrv/web/templates/web/collaborationgroups/manage.html b/ipynbsrv/web/templates/web/collaborationgroups/manage.html index 69fa2f7..867d2bb 100644 --- a/ipynbsrv/web/templates/web/collaborationgroups/manage.html +++ b/ipynbsrv/web/templates/web/collaborationgroups/manage.html @@ -18,15 +18,25 @@

Group

{% csrf_token %} -
{% endif %} - {% if request.user.backend_user.id in group.admins and request.user.backend_user.id != group.creator %} -
- +
+ {% endif %} + {% if request.user.backend_user.id in group.member_ids and request.user.backend_user.id != group.creator %} +
+ {% csrf_token %} + + +
{% endif %} @@ -66,8 +76,8 @@

Members

{% if member.id in group.admins %} Admin{% endif %} {% if member.id == group.creator.id %} Creator{% endif %} - {% if request.user.backend_user.id != group.creator and group.creator and request.user.backend_user.id == member.id %} -
+ {% if request.user.backend_user.id != group.creator.id and group.creator and request.user.backend_user.id == member.id %} + {% csrf_token %} @@ -77,7 +87,6 @@

Members

{% endif %} {% if request.user.backend_user.id in group.admins or request.user.backend_user.id == group.creator.id %} - {% if member.id != group.creator.id %}
{% csrf_token %} @@ -98,6 +107,16 @@

Members

{% endif %} + {% if member.id in group.admins and member.id != group.creator.id %} +
+ {% csrf_token %} + + + +
+ {% endif %} {% endif %} @@ -106,7 +125,7 @@

Members

- {% include 'web/collaborationgroups/modal_addusers.html' with users=users origin='manage' group=group member_ids=member_ids csrf_token=csrf_token only %} + {% include 'web/collaborationgroups/modal_addusers.html' with users=users origin='manage' group=group csrf_token=csrf_token only %} {% endblock %} diff --git a/ipynbsrv/web/templates/web/collaborationgroups/modal_addusers.html b/ipynbsrv/web/templates/web/collaborationgroups/modal_addusers.html index 6ac70d4..4827a46 100644 --- a/ipynbsrv/web/templates/web/collaborationgroups/modal_addusers.html +++ b/ipynbsrv/web/templates/web/collaborationgroups/modal_addusers.html @@ -14,7 +14,7 @@
{% for user in users %} - {% if user.backend_user and user.backend_user.id not in member_ids %} + {% if user.backend_user and user.backend_user.id not in group.member_ids %}
diff --git a/ipynbsrv/web/urls.py b/ipynbsrv/web/urls.py index 16c27ba..c89081f 100644 --- a/ipynbsrv/web/urls.py +++ b/ipynbsrv/web/urls.py @@ -19,7 +19,10 @@ url(r'^groups/delete$', 'ipynbsrv.web.views.collaborationgroups.delete', name='group_delete'), url(r'^groups/manage/(\d+)$', 'ipynbsrv.web.views.collaborationgroups.manage', name='group_manage'), url(r'^groups/remove_member$', 'ipynbsrv.web.views.collaborationgroups.remove_member', name='group_remove_member'), + url(r'^groups/leave$', 'ipynbsrv.web.views.collaborationgroups.leave', name='group_leave'), + url(r'^groups/join$', 'ipynbsrv.web.views.collaborationgroups.join', name='group_join'), url(r'^groups/add_admin$', 'ipynbsrv.web.views.collaborationgroups.add_admin', name='group_add_admin'), + url(r'^groups/remove_admin$', 'ipynbsrv.web.views.collaborationgroups.remove_admin', name='group_remove_admin'), url(r'^groups/add_users$', 'ipynbsrv.web.views.collaborationgroups.add_members', name='group_add_members'), # /container(s)/... diff --git a/ipynbsrv/web/views/collaborationgroups.py b/ipynbsrv/web/views/collaborationgroups.py index 72cfbbd..1b45345 100644 --- a/ipynbsrv/web/views/collaborationgroups.py +++ b/ipynbsrv/web/views/collaborationgroups.py @@ -6,6 +6,7 @@ from ipynbsrv.core.auth.checks import login_allowed from ipynbsrv.core.models import BackendGroup, Notification from ipynbsrv.web.api_client_proxy import get_httpclient_instance +from ipynbsrv.web.views._messages import api_error_message @user_passes_test(login_allowed) @@ -35,14 +36,13 @@ def manage(request, group_id): group = client.collaborationgroups(group_id).get() members = group.members users = client.users.get() - member_ids = [member.id for member in members] + group["member_ids"] = [member.id for member in members] return render(request, 'web/collaborationgroups/manage.html', { 'title': "Group", 'group': group, 'members': members, - 'users': users, - 'member_ids': member_ids + 'users': users }) @@ -52,25 +52,26 @@ def create(request): messages.error(request, "Invalid request method.") return redirect('groups') + params = {} if 'name' not in request.POST: messages.error(request, "Invalid POST request.") else: - name = request.POST.get('name') + params["name"] = request.POST.get('name') - public = False if 'public' in request.POST: - public = True + params["is_public"] = True + else: + params["is_public"] = False client = get_httpclient_instance(request) - # stupid create workaround (see containers) - client.collaborationgroups.get() - client.collaborationgroups.post({ - "name": name, - "is_public": public - }) - - messages.success(request, "Group `{}` created sucessfully.".format(name)) + try: + # stupid create workaround (see containers) + client.collaborationgroups.get() + client.collaborationgroups.post(params) + messages.success(request, "Group `{}` created sucessfully.".format(params.get("name"))) + except Exception as e: + messages.error(request, api_error_message(e, params)) return redirect('groups') @@ -89,8 +90,11 @@ def delete(request): client = get_httpclient_instance(request) group = client.collaborationgroups(group_id).get() if group: - client.collaborationgroups(group_id).delete() - messages.success(request, "Group `{}` deleted.".format(group.name)) + try: + client.collaborationgroups(group_id).delete() + messages.success(request, "Group `{}` deleted.".format(group.name)) + except Exception as e: + messages.error(request, api_error_message(e, "")) else: messages.error(request, "Group does not exist.") @@ -112,13 +116,56 @@ def add_admin(request): client = get_httpclient_instance(request) group = client.collaborationgroups(group_id).get() + user = client.users(user_id).get() + + params = {} + params["users"] = [user_id] if group: if request.user.is_superuser or request.user.backend_user.id == group.creator.id or request.user.backend_user.id in group.admins: - client.collaborationgroups(group_id).add_admins.post({ - "users": [user_id] - }) - messages.success(request, "User is now a admin of {}.".format(group.name)) + try: + client.collaborationgroups(group_id).add_admins.post(params) + messages.success(request, "{} is now a admin of {}.".format(user.username, group.name)) + except Exception as e: + messages.error(request, api_error_message(e, params)) + + request.method = "GET" + return redirect('group_manage', group.id) + else: + messages.error(request, "Not enough permissions to do this.") + return redirect('group_manage', group.id) + else: + messages.error(request, "Group does not exist.") + return redirect('group_manage', group.id) + + +@user_passes_test(login_allowed) +def remove_admin(request): + if request.method != "POST": + messages.error(request, "Invalid request method.") + return redirect('shares') + if 'group_id' not in request.POST or 'user_id' not in request.POST: + messages.error(request, "Invalid POST request.") + return redirect('shares') + + group_id = int(request.POST.get('group_id')) + user_id = int(request.POST.get('user_id')) + + client = get_httpclient_instance(request) + group = client.collaborationgroups(group_id).get() + user = client.users(user_id).get() + + params = {} + params["users"] = [user_id] + + if group: + if request.user.is_superuser or request.user.backend_user.id == group.creator.id or request.user.backend_user.id in group.admins: + try: + client.collaborationgroups(group_id).remove_admins.post(params) + messages.success(request, "{} is not a admin of {} anymore.".format(user.username, group.name)) + except Exception as e: + messages.error(request, api_error_message(e, params)) + request.method = "GET" return redirect('group_manage', group.id) else: @@ -138,21 +185,26 @@ def add_members(request): users = request.POST.getlist('users') group_id = request.POST.get('group_id') + print(users) + client = get_httpclient_instance(request) user_list = [] # validate existance of users first for u in users: user = client.users(u).get() - if user: - user_list.append(u) + if user: + user_list.append(u) # then call API to add the users to the group params = {} params["users"] = user_list - client.collaborationgroups(group_id).add_members.post(params) + try: + client.collaborationgroups(group_id).add_members.post(params) + messages.success(request, "Users successfully added to the group.") + except Exception as e: + messages.error(request, api_error_message(e, params)) - messages.success(request, "Users successfully added to the group.") return redirect('group_manage', group_id) @@ -177,8 +229,13 @@ def remove_member(request): if user: params = {} params["users"] = [user_id] - client.collaborationgroups(group_id).remove_members.post(params) - messages.success(request, "Sucessfully removed the user from the group.") + # API call + try: + client.collaborationgroups(group_id).remove_members.post(params) + messages.success(request, "Sucessfully removed {} from the group.".format(user.username)) + except Exception as e: + messages.error(request, api_error_message(e, params)) + request.method = "GET" return redirect('group_manage', group.id) else: @@ -188,3 +245,69 @@ def remove_member(request): messages.error(request, "Group does not exist.") return redirect('group_manage', group_id) + + +@user_passes_test(login_allowed) +def leave(request): + if request.method != "POST": + messages.error(request, "Invalid request method.") + return redirect('groups') + if 'group_id' not in request.POST or 'user_id' not in request.POST: + messages.error(request, "Invalid POST request.") + return redirect('groups') + + group_id = int(request.POST.get('group_id')) + user_id = int(request.POST.get('user_id')) + + client = get_httpclient_instance(request) + + user = client.users(user_id).get() + group = client.collaborationgroups(group_id).get() + + if group: + if user: + params = {} + params["users"] = [user_id] + try: + client.collaborationgroups(group_id).remove_members.post(params) + messages.success(request, "You are no longer a member of group {}.".format(group.name)) + except Exception as e: + messages.error(request, api_error_message(e, params)) + + request.method = "GET" + return redirect('groups') + else: + messages.error(request, "User does not exist.") + return redirect('group_manage', group.id) + else: + messages.error(request, "Group does not exist.") + + return redirect('groups') + + +@user_passes_test(login_allowed) +def join(request): + if request.method != "POST": + messages.error(request, "Invalid request method.") + return redirect('shares') + if 'group_id' not in request.POST or 'user_id' not in request.POST: + messages.error(request, "Invalid POST request.") + return redirect('groups') + + user_id = request.POST.get('user_id') + group_id = request.POST.get('group_id') + + client = get_httpclient_instance(request) + + group = client.collaborationgroups(group_id).get() + user = client.users(user_id).get() + if user: + params = {} + params["users"] = [user_id] + try: + client.collaborationgroups(group_id).add_members.post(params) + messages.success(request, "You are now a member of {}.".format(group.name)) + except Exception as e: + messages.error(request, api_error_message(e, params)) + + return redirect('groups')