44import re
55from urllib .parse import urlparse
66from django .conf import settings
7+ from django .contrib .auth .models import Permission
8+ from django .contrib .contenttypes .models import ContentType
79from django .utils import timezone
810from prefix .models import Prefix
911from django .db import transaction
@@ -20,6 +22,7 @@ class PrefixSerializer(serializers.Serializer):
2022 prefix = serializers .CharField (min_length = 3 , max_length = 5 )
2123 description = serializers .CharField ()
2224 authorized_groups = serializers .ListField (child = serializers .CharField (allow_blank = True ), required = False )
25+ public = serializers .BooleanField (required = False )
2326
2427 def validate (self , attrs ):
2528 """Prefix Validator
@@ -41,65 +44,52 @@ def validate(self, attrs):
4144 if "create" in request .path_info :
4245 pass
4346 else :
44- errors ["prefix_name" ] = f"That Prefix, { prefix_name } , was not found."
45-
46-
47-
48- # remove blank 'authorized_groups' relic from legacy conversion
49- if attrs ['authorized_groups' ][0 ] == "" :
50- attrs .pop ("authorized_groups" )
51-
52- #check for groups
53- if 'authorized_groups' in attrs :
54- for group in attrs ['authorized_groups' ]:
55- try :
56- Group .objects .get (name = group )
57- except Group .DoesNotExist as err :
58- errors ['authorized_groups' ] = f"Invalid group: { group } "
59-
60- # If erros exist than raise and exception and return it, otherwise
61- # return validated data
62- if errors :
63- raise serializers .ValidationError (errors )
47+ raise serializers .ValidationError ({"prefix_name" : f"That Prefix, { prefix_name } , was not found." })
6448
6549 return attrs
6650
6751 @transaction .atomic
6852 def create (self , validated_data ):
6953 """Create function for Prefix
7054 """
71- authorized_group_names = validated_data .pop ('authorized_groups' , [])
55+ public = validated_data .pop ('public' , [])
56+ import pdb ; pdb .set_trace ()
7257 prefix_instance = Prefix .objects .create (** validated_data , created = timezone .now ())
73- # Set ManyToMany relations
74- if authorized_group_names :
75- authorized_groups = Group .objects .filter (name__in = authorized_group_names )
76- prefix_instance .authorized_groups .set (authorized_groups )
58+
7759 return prefix_instance
7860
7961 @transaction .atomic
8062 def update (self , validated_data ):
8163 """Update function for Prefix."""
8264 prefix_instance = Prefix .objects .get (prefix = validated_data ['prefix' ])
8365 if prefix_instance .owner != validated_data ['owner' ]:
84- # import pdb; pdb.set_trace()
8566 return "denied"
8667 prefix_instance .description = validated_data .get ('description' , prefix_instance .description )
8768 prefix_instance .save ()
8869
89- if 'authorized_groups' in validated_data :
90- authorized_group_names = validated_data ['authorized_groups' ]
91- # If the list is empty or contains only an empty string, clear the groups
92- if not authorized_group_names or authorized_group_names == ["" ]:
93- prefix_instance .authorized_groups .clear ()
70+ return prefix_instance
9471
95- else :
96- # Filter groups that exist in the database
97- authorized_groups = Group .objects .filter (name__in = authorized_group_names )
98-
99- # Set the new groups, which automatically handles adding, keeping, or removing
100- prefix_instance .authorized_groups .set (authorized_groups )
72+ def create_permissions_for_prefix (instance = None , owner = User ):
73+ """Prefix Permission Creation
10174
102- return prefix_instance
75+ Creates permissions for a Prefix if it is not public. Owner is assigned
76+ all permissions and then can add permissions to other users.
77+
78+ 'view' -> View/download Prefix drafts
79+ 'add' -> create new drafts for Prefix
80+ 'change' -> Change existing drafts for Prefix
81+ 'delete' -> Delete drafts for Prefix
82+ 'publish' -> Publish drafts for Prefix
83+ """
84+ try :
85+ for perm in [ "view" , "add" , "change" , "delete" , "publish" ]:
86+ print (instance )
87+ Permission .objects .create (
88+ name = "Can " + perm + " BCOs with prefix " + instance .prefix ,
89+ content_type = ContentType .objects .get (app_label = "api" , model = "bco" ),
90+ codename = perm + "_" + instance .prefix ,)
91+ except :
92+ return 0
10393
10494def prefix_counter_increment (prefix : Prefix ) -> int :
10595 """Prefix Counter Increment
0 commit comments