1212from django .utils import timezone
1313from guardian .shortcuts import assign_perm
1414from rest_framework import status
15+ from rest_framework .authtoken .models import Token
1516from rest_framework .response import Response
1617
17- from api .scripts .utilities import DbUtils
18- from api .scripts .utilities import UserUtils
18+ from api .scripts .utilities .DbUtils import DbUtils
19+ from api .scripts .utilities .UserUtils import UserUtils
20+
21+
22+ usr_utils = UserUtils ()
23+ db_utils = DbUtils ()
1924
2025class GroupInfo (models .Model ):
2126 """Some additional information for Group.
@@ -33,84 +38,40 @@ class GroupInfo(models.Model):
3338 max_n_members = models .IntegerField (blank = True , null = True )
3439 owner_user = models .ForeignKey (User , on_delete = models .CASCADE , to_field = 'username' )
3540
36- # --- Group --- #
37- @receiver (pre_delete , sender = Group )
38- def delete_group_perms (sender , instance = None , ** kwargs ):
39- """
40- Link group deletion to permissions deletion.
41- pre_delete and NOT post_delete because we need
42- to get the Group's information before deleting it.
43- """
44- for perm in ['add_members_' + instance .name , 'delete_members_' + instance .name ]:
45- Permission .objects .filter (codename = perm ).delete ()
46-
47-
48- @receiver (post_save , sender = GroupInfo )
49- def create_group_perms (sender , instance = None , created = False , ** kwargs ):
50- """Group info
51- Link group creation to permission creation.
52- Check to see whether or not the permissions
53- have already been created for this prefix.
54- Create the permissions, then use GroupInfo to give the group admin
55- the admin permissions.
56- Create the administrative permissions for the group.
57- Give the administrative permissions to the user
58- creating this group.
41+ def post_api_groups_info (token ):
42+ """Retrieve Group information by user
43+
5944 """
60- if created :
61- try :
62- for perm in ['add_members_' + instance .group_id ,
63- 'delete_members_' + instance .group_id ]:
64- Permission .objects .create (
65- name = 'Can ' + perm ,
66- content_type = ContentType .objects .get (app_label = 'auth' , model = 'group' ),
67- codename = perm )
45+ import pdb ; pdb .set_trace ()
46+ group_list = list (Group .objects .all ())#.values_list('name', flat=True))
47+ user = Token .objects .get (key = token ).user
48+ username = user .username
49+ user_groups = {}
50+ # for group in
51+ print (usr_utils .get_user_groups_by_username (un = username ))
52+
53+ return Response (status = status .HTTP_200_OK )
6854
69- User .objects .get (id = instance .id ).user_permissions .add (Permission .objects .get (codename = perm ))
55+ # user.get_all_permissions()
56+ # user.get_group_permissions()
7057
71- except PermErrors .IntegrityError :
72- # The permissions already exist.
73- pass
7458
7559def post_api_groups_create (request ):
7660 """
7761 Instantiate any necessary imports.
7862 """
79- db = DbUtils .DbUtils ()
80- uu = UserUtils .UserUtils ()
8163
82- # Define the bulk request.
8364 bulk_request = request .data ['POST_api_groups_create' ]
84-
85- # Establish who is the group administrator.
86- group_admin = uu .user_from_request (request = request )
87-
88- # Get all group names.
89-
90- # This is a better solution than querying for
91- # each individual group name.
92- groups = list (
93- Group .objects .all ().values_list (
94- 'name' ,
95- flat = True
96- )
97- )
98-
99- # Construct an array to return information about processing
100- # the request.
65+ group_admin = usr_utils .user_from_request (request = request )
66+ groups = list (Group .objects .all ().values_list ('name' , flat = True ))
10167 returning = []
102-
103- # Since bulk_request is an array, go over each
104- # item in the array.
10568 for creation_object in bulk_request :
106- # Standardize the group name.
10769 standardized = creation_object ['name' ].lower ()
10870 if standardized not in groups :
10971 # Not guaranteed which of username and group
11072 # will be provided.
11173 if 'usernames' not in creation_object :
11274 creation_object ['usernames' ] = []
113-
11475 # Create the optional keys if they haven't
11576 # been provided.
11677 if 'delete_members_on_group_deletion' not in creation_object :
@@ -141,6 +102,7 @@ def post_api_groups_create(request):
141102
142103
143104 if 'expiration' not in creation_object :
105+ import pdb ; pdb .set_trace ()
144106 GroupInfo .objects .create (
145107 delete_members_on_group_deletion = bool (creation_object ['delete_members_on_group_deletion' ]),
146108 description = creation_object ['description' ],
@@ -164,24 +126,23 @@ def post_api_groups_create(request):
164126 # those that don't.
165127 for usrnm in creation_object ['usernames' ]:
166128
167- if uu .check_user_exists (un = usrnm ):
129+ if usr_utils .check_user_exists (un = usrnm ):
168130
169131 # Add the user to the group.
170- User .objects .get (
171- username = usrnm
132+ User .objects .get (username = usrnm
172133 ).groups .add (
173134 Group .objects .get (
174135 name = creation_object ['name' ]
175136 )
176137 )
177- returning .append (db .messages (parameters = {'group' : standardized })['201_group_create' ])
138+ returning .append (db_utils .messages (parameters = {'group' : standardized })['201_group_create' ])
178139 else :
179140 # Bad request. Username doesn't exist
180141 # TODO: Update this to be more informative
181- returning .append (db .messages (parameters = {})['400_bad_request' ])
142+ returning .append (db_utils .messages (parameters = {})['400_bad_request' ])
182143 else :
183144 # Update the request status.
184- returning .append (db .messages (parameters = {'group' : standardized })['409_group_conflict' ])
145+ returning .append (db_utils .messages (parameters = {'group' : standardized })['409_group_conflict' ])
185146
186147 # As this view is for a bulk operation, status 200
187148 # means that the request was successfully processed,
@@ -191,37 +152,29 @@ def post_api_groups_create(request):
191152def post_api_groups_delete (request ):
192153 "Instantiate any necessary imports."
193154
194- db = DbUtils .DbUtils ()
195- uu = UserUtils .UserUtils ()
196-
197155 # Define the bulk request.
198156 bulk_request = request .data ['POST_api_groups_delete' ]['names' ]
199157
200158 # Establish who has made the request.
201- requestor_info = uu .user_from_request (request = request )
159+ requestor_info = usr_utils .user_from_request (request = request )
202160
203161 # Get all group names.
204162
205163 # This is a better solution than querying for
206164 # each individual group name.
207- groups = list (
208- Group .objects .all ().values_list (
209- 'name' ,
210- flat = True
211- )
212- )
165+ groups = list (Group .objects .all ().values_list ('name' , flat = True ))
213166
214167 # Construct an array to return information about processing
215168 # the request.
216169 returning = []
217170 any_failed = False
218-
171+
219172 # Since bulk_request is an array, go over each
220173 # item in the array.
221174 for deletion_object in bulk_request :
222175 # Standardize the group name.
223176 standardized = deletion_object .lower ()
224-
177+ deleted_count = 0
225178 if standardized in groups :
226179 # Get the group and its information.
227180 grouped = Group .objects .get (name = standardized )
@@ -236,28 +189,29 @@ def post_api_groups_delete(request):
236189 User .objects .filter (groups__name = grouped .name ).delete ()
237190 # Delete the group itself.
238191 deleted_count , deleted_info = grouped .delete ()
239- if deleted_count < 1 :
192+ if deleted_count < 3 :
240193 # Too few deleted, error with this delete
241- returning .append (db .messages (parameters = {
194+ returning .append (db_utils .messages (parameters = {
242195 'group' : grouped .name })['404_missing_bulk_parameters' ])
243196 any_failed = True
244197 continue
245- elif deleted_count > 1 :
198+
199+ elif deleted_count > 3 :
246200 print (deleted_count , 'deleted_count' )
247201 # We don't expect there to be duplicates, so while this was successful it should throw a warning
248- returning .append (db .messages (parameters = {
202+ returning .append (db_utils .messages (parameters = {
249203 'group' : grouped .name })['418_too_many_deleted' ])
250204 any_failed = True
251205 continue
252206 # Everything looks OK
253- returning .append (db .messages (parameters = {'group' : grouped .name })['200_OK_group_delete' ])
207+ returning .append (db_utils .messages (parameters = {'group' : grouped .name })['200_OK_group_delete' ])
254208 else :
255209 # Requestor is not the admin.
256- returning .append (db .messages (parameters = {})['403_insufficient_permissions' ])
210+ returning .append (db_utils .messages (parameters = {})['403_insufficient_permissions' ])
257211 any_failed = True
258212 else :
259213 # Update the request status.
260- returning .append (db .messages (parameters = {})['400_bad_request' ])
214+ returning .append (db_utils .messages (parameters = {})['400_bad_request' ])
261215 any_failed = True
262216
263217 # As this view is for a bulk operation, status 200
@@ -270,14 +224,12 @@ def post_api_groups_delete(request):
270224
271225def post_api_groups_modify (request ):
272226 """Instantiate any necessary imports."""
273- db = DbUtils .DbUtils ()
274- uu = UserUtils .UserUtils ()
275227
276228 # Define the bulk request.
277229 bulk_request = request .data ['POST_api_groups_modify' ]
278230
279231 # Establish who has made the request.
280- requestor_info = uu .user_from_request (request = request )
232+ requestor_info = usr_utils .user_from_request (request = request )
281233
282234 # Get all group names.
283235
@@ -339,7 +291,7 @@ def post_api_groups_modify(request):
339291 # WARNING: This could cause an error if this is sent in!
340292 if 'owner_group' in action_set :
341293 # Make sure the provided owner group exists.
342- if uu .check_group_exists (n = action_set ['owner_group' ]):
294+ if usr_utils .check_group_exists (n = action_set ['owner_group' ]):
343295 group_information .owner_group = Group .objects .get (
344296 name = action_set ['owner_group' ]
345297 )
@@ -350,7 +302,7 @@ def post_api_groups_modify(request):
350302
351303 if 'owner_user' in action_set :
352304 # Make sure the provided owner user exists.
353- if uu .check_user_exists (un = action_set ['owner_user' ]):
305+ if usr_utils .check_user_exists (un = action_set ['owner_user' ]):
354306 group_information .owner_user = User .objects .get (
355307 username = action_set ['owner_user' ]
356308 )
@@ -407,13 +359,13 @@ def post_api_groups_modify(request):
407359 all_users .update (a_group_users )
408360 else :
409361 pass
410- returning .append (db .messages (parameters = {'group' : grouped .name })['200_OK_group_modify' ])
362+ returning .append (db_utils .messages (parameters = {'group' : grouped .name })['200_OK_group_modify' ])
411363 else :
412364 # Requestor is not the admin.
413- returning .append (db .messages (parameters = {})['403_insufficient_permissions' ])
365+ returning .append (db_utils .messages (parameters = {})['403_insufficient_permissions' ])
414366 else :
415367 # Update the request status.
416- returning .append (db .messages (parameters = {})['400_bad_request' ])
368+ returning .append (db_utils .messages (parameters = {})['400_bad_request' ])
417369
418370 # As this view is for a bulk operation, status 200
419371 # means that the request was successfully processed,
0 commit comments