def get(self, request, *args, **kwargs): root_oid = get_root_oid(request) channel_data = self.get_channel_data(*args, **kwargs) profiles = ProfileManager.get_user_profiles(channel_data.model.id, root_oid) max_perm_lv = ProfileManager.get_highest_permission_level(profiles) return render_template( self.request, _("Create Profile"), "account/channel/prof/create.html", { "channel_oid": channel_data.model.id, "max_perm_lv": max_perm_lv, "perm_cats_controllable": ProfilePermissionDefault.get_overridden_permissions(max_perm_lv), "perm_cats": list(ProfilePermission), "value_color": ColorFactory.DEFAULT.color_hex }, nav_param=kwargs)
def profile_create_internal(e: TextMessageEventObject, name: str, color: Union[str, Color], permission: Union[str, List[ProfilePermission]], perm_lv: Union[str, PermissionLevel]): # --- Check permission profiles = ProfileManager.get_user_profiles(e.channel_model.id, e.user_model.id) user_permissions = ProfileManager.get_permissions(profiles) if ProfilePermission.PRF_CED not in user_permissions: return [ HandledMessageEventText( content=_("Insufficient permission to create profile.")) ] # --- Parse name prof_name = _parse_name_(e.channel_oid, name) if not prof_name: return [ HandledMessageEventText(content=_( "The profile name `{}` is unavailable in this channel. " "Please choose a different one.").format(name)) ] # --- Parse color color = _parse_color_(color) if not color: return [ HandledMessageEventText(content=_( "Failed to parse color. Make sure it is in the format of `81D8D0`." )) ] # --- Parse permission level perm_lv = _parse_permission_level_(perm_lv) if not perm_lv: return [ HandledMessageEventText(content=_( "Failed to parse permission level. Make sure it is a valid level or check the documentation." )) ] # --- Parse permission msg_on_hold = [] if isinstance(permission, str): max_perm_lv = ProfileManager.get_highest_permission_level(profiles) parsed_perm = _parse_permission_(permission, max_perm_lv) if parsed_perm.has_skipped: msg_on_hold.append( HandledMessageEventText(content=_( "Strings below were skipped because it cannot be understood as permission:\n{}" ).format("\n".join(parsed_perm.skipped)))) if parsed_perm.has_invalid: msg_on_hold.append( HandledMessageEventText(content=_( "Permissions below were not be able to set on the profile " "because your permission level is insufficient:\n{}" ).format("\n".join([str(p) for p in parsed_perm.invalid])))) if not parsed_perm.has_valid: return [ HandledMessageEventText( content=_("Profile permission parsing failed.")) ] + msg_on_hold permission = parsed_perm.valid # --- Process permission perm_dict = { perm.code_str: perm in permission for perm in list(ProfilePermission) } # --- Create Model mdl = ChannelProfileModel(ChannelOid=e.channel_model.id, Name=prof_name, Color=color, Permission=perm_dict, PermissionLevel=perm_lv) mdl = ProfileManager.register_new_model(e.user_model.id, mdl) if mdl: return [ HandledMessageEventText(content=_("Profile created and attached.")) ] + msg_on_hold else: return [ HandledMessageEventText(content=_("Profile registration failed.")) ] + msg_on_hold