Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Groups #112

Merged
merged 7 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified admin_only/db.sqlite3.dev
Binary file not shown.
107 changes: 46 additions & 61 deletions bco_api/api/model/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,40 @@ def post_api_groups_info(request):
"""Retrieve Group information by user

"""

user = usr_utils.user_from_request(request=request)
bulk_request = request.data['POST_api_groups_info']
group_info = []

for index, value in enumerate(bulk_request['names']):
group = Group.objects.get(name=value)
group_info_object = GroupInfo.objects.get(group=value)
gropu_permissions = group.permissions.all()
group_members = group.user_set.all()
group_info.append(
{
'name': group.name,
'permissions': gropu_permissions,
'members': group_members
}
)
print(group_info)
group_list = list(Group.objects.all())#.values_list('name', flat=True))
username = user.username
user_groups = {}

# print(usr_utils.get_user_groups_by_username(un=username))
return Response(status=status.HTTP_200_OK)

try:
admin = GroupInfo.objects.get(group=value).owner_user == user
description = GroupInfo.objects.get(group=value).description
except GroupInfo.DoesNotExist:
admin=False
description = 'N/A'

group_permissions = list(
group.permissions.all().values_list('codename', flat=True)
)
group_members = list(
group.user_set.all().values_list('username', flat=True)
)
group_info.append({
'name': group.name,
'permissions': group_permissions,
'members': group_members,
'admin': admin,
'description': description
})

# print(usr_utils.get_user_groups_by_username(un=username))
# user.get_all_permissions()
# user.get_group_permissions()
print(group_info)
return Response(status=status.HTTP_200_OK, data=group_info)


def post_api_groups_create(request):
Expand Down Expand Up @@ -95,7 +104,7 @@ def post_api_groups_create(request):
bulk_request = request.data['POST_api_groups_create']
group_admin = usr_utils.user_from_request(request=request)
groups = list(Group.objects.all().values_list('name', flat=True))
returning = []
return_data = []
any_failed = False

for creation_object in bulk_request:
Expand Down Expand Up @@ -156,27 +165,27 @@ def post_api_groups_create(request):
users_excluded.append(usrnm)

if len(users_excluded) > 0 :
returning.append(db_utils.messages(parameters={
return_data.append(db_utils.messages(parameters={
'group': standardized,
'users_excluded': users_excluded
})['201_group_users_excluded'])

else:
returning.append(db_utils.messages(
return_data.append(db_utils.messages(
parameters={'group': standardized})['201_group_create']
)

else:
# Update the request status.
returning.append(db_utils.messages(
return_data.append(db_utils.messages(
parameters={'group': standardized})['409_group_conflict']
)
any_failed = True

if any_failed:
return Response(status=status.HTTP_207_MULTI_STATUS, data=returning)
return Response(status=status.HTTP_207_MULTI_STATUS, data=return_data)

return Response(status=status.HTTP_200_OK, data=returning)
return Response(status=status.HTTP_200_OK, data=return_data)


def post_api_groups_delete(request):
Expand All @@ -195,7 +204,7 @@ def post_api_groups_delete(request):

# Construct an array to return information about processing
# the request.
returning = []
return_data = []
any_failed = False

# Since bulk_request is an array, go over each
Expand Down Expand Up @@ -233,54 +242,33 @@ def post_api_groups_delete(request):
any_failed = True
continue
# Everything looks OK
returning.append(db_utils.messages(parameters={'group': grouped.name})['200_OK_group_delete'])
return_data.append(db_utils.messages(parameters={'group': grouped.name})['200_OK_group_delete'])
else:
# Requestor is not the admin.
returning.append(db_utils.messages(parameters={})['403_insufficient_permissions'])
return_data.append(db_utils.messages(parameters={})['403_insufficient_permissions'])
any_failed = True
else:
# Update the request status.
returning.append(db_utils.messages(parameters={})['400_bad_request'])
return_data.append(db_utils.messages(parameters={})['400_bad_request'])
any_failed = True

if any_failed:
return Response(status=status.HTTP_207_MULTI_STATUS, data=returning)
return Response(status=status.HTTP_207_MULTI_STATUS, data=return_data)

return Response(status=status.HTTP_200_OK, data=returning)
return Response(status=status.HTTP_200_OK, data=return_data)


def post_api_groups_modify(request):
"""Instantiate any necessary imports."""

# Define the bulk request.
bulk_request = request.data['POST_api_groups_modify']

# Establish who has made the request.
requestor_info = usr_utils.user_from_request(request=request)

# Get all group names.

# This is a better solution than querying for
# each individual group name.
groups = list(Group.objects.all().values_list('name', flat=True))

# Construct an array to return information about processing
# the request.
returning = []

# Since bulk_request is an array, go over each
# item in the array.
return_data = []
for modification_object in bulk_request:

# Standardize the group name.
standardized = modification_object['name'].lower()

if standardized in groups:

# Get the group and its information.
grouped = Group.objects.get(name=standardized)

# Check that the requestor is the group admin.
if requestor_info.is_superuser == True or grouped in requestor_info.groups.all():
# TODO: We shouldn't use a try/except as an if statement; I think there is actually
# a get_or_create() function:
Expand All @@ -289,20 +277,17 @@ def post_api_groups_modify(request):
try:
group_information = GroupInfo.objects.get(group=grouped)
except:
group_information = GroupInfo.objects.create(group=grouped, owner_user=requestor_info)
# Process the request.
# We only care about the actions at this point.
group_information = GroupInfo.objects.create(
group=grouped, owner_user=requestor_info
)
if 'actions' in modification_object:
# Set the working object to the actions.
action_set = modification_object['actions']

# Invalid inputs don't throw 400, 401, or 403 for the
# request. That is, provided parameters that don't
# exist (for example, an owner_user that does not exist)
# simply get skipped over.

# First do the "easy" tasks - name and description.

# Change name of group if set in actions
if 'rename' in action_set:
# Simply re-name to whatever we've been provided,
Expand Down Expand Up @@ -389,18 +374,18 @@ def post_api_groups_modify(request):
all_users.update(a_group_users)
else:
pass
returning.append(db_utils.messages(parameters={'group': grouped.name})['200_OK_group_modify'])
return_data.append(db_utils.messages(parameters={'group': grouped.name})['200_OK_group_modify'])
else:
# Requestor is not the admin.
returning.append(db_utils.messages(parameters={})['403_insufficient_permissions'])
return_data.append(db_utils.messages(parameters={})['403_insufficient_permissions'])
else:
# Update the request status.
returning.append(db_utils.messages(parameters={})['400_bad_request'])
return_data.append(db_utils.messages(parameters={})['400_bad_request'])

# As this view is for a bulk operation, status 200
# means that the request was successfully processed,
# but NOT necessarily each item in the request.
return Response(status=status.HTTP_200_OK, data=returning)
return Response(status=status.HTTP_200_OK, data=return_data)


@receiver(post_save, sender=User)
Expand Down
77 changes: 57 additions & 20 deletions bco_api/api/model/prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from rest_framework import status
from rest_framework.response import Response

from api.model.groups import GroupInfo
from api.scripts.utilities import DbUtils
from api.scripts.utilities import UserUtils

Expand Down Expand Up @@ -634,39 +635,60 @@ def post_api_prefixes_token_flat(request):
@receiver(post_save, sender=Prefix)
def create_permissions_for_prefix(sender, instance=None, created=False, **kwargs):
"""Link prefix creation to permissions creation.
Check to see whether or not the permissions
have already been created for this prefix.
Create the macro-level, draft, and publish permissions.
Give FULL permissions to the prefix user owner
and their group.
Check to see whether or not the permissions
have already been created for this prefix.
Create the macro-level, draft, and publish permissions.
Give FULL permissions to the prefix user owner
and their group.

No try/except necessary here as the user's existence
has already been verified upstream.
No try/except necessary here as the user's existence
has already been verified upstream.

Source: https://stackoverflow.com/a/20361273
Source: https://stackoverflow.com/a/20361273
"""

# GroupInfo.objects.create(
# delete_members_on_group_deletion=False,
# description='Group administrators',
# group=Group.objects.get(name='group_admins'),
# max_n_members=-1,
# owner_user=User.objects.get(username='wheel')
# )
if created:
user = User.objects.get(username=instance.owner_user)
user_group = Group.objects.get(name=instance.owner_user)
draft = instance.prefix.lower() + '_drafters'
publish = instance.prefix.lower() + '_publishers'
owner_user = User.objects.get(username=instance.owner_user)
owner_group = Group.objects.get(name=instance.owner_group_id)
draft = instance.prefix.lower() + '_drafter'
publish = instance.prefix.lower() + '_publisher'

if len(Group.objects.filter(name=draft)) != 0:
drafters = Group.objects.get(name=draft)
user.groups.add(drafters)
owner_user.groups.add(drafters)
else:
Group.objects.create(name=draft)
drafters = Group.objects.get(name=draft)
user.groups.add(drafters)
owner_user.groups.add(drafters)
GroupInfo.objects.create(
delete_members_on_group_deletion=False,
description=instance.description,
group=drafters,
max_n_members=-1,
owner_user=owner_user
)

if len(Group.objects.filter(name=publish)) != 0:
publishers = Group.objects.get(name=publish)
user.groups.add(publishers)
owner_user.groups.add(publishers)
else:
Group.objects.create(name=publish)
publishers = Group.objects.get(name=publish)
user.groups.add(publishers)
owner_user.groups.add(publishers)
GroupInfo.objects.create(
delete_members_on_group_deletion=False,
description=instance.description,
group=publishers,
max_n_members=-1,
owner_user=owner_user
)

try:
for perm in ['add', 'change', 'delete', 'view', 'draft', 'publish']:
Expand All @@ -680,18 +702,33 @@ def create_permissions_for_prefix(sender, instance=None, created=False, **kwargs
)
new_perm = Permission.objects.get(
codename=perm + '_' + instance.prefix)
user.user_permissions.add(new_perm)
user_group.permissions.add(new_perm)
owner_user.user_permissions.add(new_perm)
owner_group.permissions.add(new_perm)
publishers.permissions.add(new_perm)
if perm == 'publish':
pass
else:
drafters.permissions.add(new_perm)
drafters.permissions.add(new_perm)

except PermErrors.IntegrityError:
# The permissions already exist.
pass

@receiver(post_save, sender=Prefix)
def create_counter_for_prefix(sender, instance=None, created=False, **kwargs):
"""Create prefix counter

Creates a prefix counter for each prefix if it does not exist on save.

Parameters
----------
sender: django.db.models.base.ModelBase
instance: api.model.prefix.Prefix
created: bool
"""

if created:
prefix_table.objects.create(n_objects=1,prefix=instance.prefix)

@receiver(post_delete, sender=Prefix)
def delete_permissions_for_prefix(sender, instance=None, **kwargs):
Expand Down
Loading