예제 #1
0
    def get(self, request, *args, **kwargs):
        # `kwargs` will be used as `nav_param` so extract channel_oid from `kwargs` instead of creating param.
        channel_oid_str = kwargs.get("channel_oid", "")
        channel_oid = safe_cast(channel_oid_str, ObjectId)
        u_profs = ProfileManager.get_user_profiles(channel_oid, get_root_oid(request))

        if u_profs:
            return render_template(
                self.request, _("Channel Management - {}").format(channel_oid), "account/channel/manage.html", {
                    "user_profiles": u_profs,
                    "perm_sum": sorted(ProfileManager.get_permissions(u_profs), key=lambda x: x.code),
                    "channel_oid": channel_oid
                }, nav_param=kwargs)
        else:
            c_prof = ChannelManager.get_channel_oid(channel_oid)

            if c_prof:
                messages.info(
                    request, _("You are redirected to the channel info page "
                               "because you don't have any connections linked to the channel."),
                    extra_tags="info"
                )

                return redirect(reverse("info.channel", kwargs={"channel_oid": channel_oid}))
            else:
                return WebsiteErrorView.website_error(
                    request, WebsiteError.PROFILE_LINK_NOT_FOUND, {"channel_oid": channel_oid_str})
예제 #2
0
def profile_delete(e: TextMessageEventObject, name: str):
    # --- Check profile name
    prof = ProfileManager.get_profile_name(name)
    if not prof:
        return [
            HandledMessageEventText(content=_(
                "Profile with the name `{}` not found.").format(name))
        ]

    # --- 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 delete profile."))
        ]

    deleted = ProfileManager.delete_profile(e.channel_oid, prof.id,
                                            e.user_model.id)
    if deleted:
        return [HandledMessageEventText(content=_("Profile deleted."))]
    else:
        return [
            HandledMessageEventText(content=_("Failed to delete the profile."))
        ]
예제 #3
0
    def get(self, request, *args, **kwargs):
        channel_data = self.get_channel_data(*args, **kwargs)
        chcoll_data: Optional[ChannelCollectionModel] = \
            ChannelCollectionManager.get_chcoll_child_channel(channel_data.model.id)

        root_oid = get_root_oid(request)
        channel_name = channel_data.model.get_channel_name(root_oid)

        msgdata_1d = MessageStatsDataProcessor.get_user_channel_messages(channel_data.model, hours_within=24)
        msgdata_7d = MessageStatsDataProcessor.get_user_channel_messages(channel_data.model, hours_within=168)
        member_info = InfoProcessor.get_member_info(channel_data.model)

        return render_template(
            self.request, _("Channel Info - {}").format(channel_name), "info/channel/main.html",
            {
                "channel_name": channel_name,
                "channel_data": channel_data.model,
                "chcoll_data": chcoll_data,
                "user_message_data1d": msgdata_1d,
                "user_message_data7d": msgdata_7d,
                "member_info": member_info,
                "manageable": bool(ProfileManager.get_user_profiles(channel_data.model.id, root_oid)),
                "bot_usage_7d": BotFeatureUsageDataManager.get_channel_usage(channel_data.model.id, hours_within=168),
                "bot_usage_all": BotFeatureUsageDataManager.get_channel_usage(channel_data.model.id)
            },
            nav_param=kwargs)
예제 #4
0
    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)
예제 #5
0
    def get(self, request, *args, **kwargs):
        channel_data = self.get_channel_data(*args, **kwargs)

        # Check if the user is in the channel
        root_oid = get_root_oid(request)
        profs = ProfileManager.get_user_profiles(channel_data.model.id,
                                                 root_oid)
        if not profs or profs == ChannelProfileConnectionModel.ProfileOids.none_obj(
        ):
            return WebsiteErrorView.website_error(
                request,
                WebsiteError.NOT_IN_THE_CHANNEL,
                {"channel_oid": channel_data.oid_org},
                nav_param=kwargs)

        # Process the necessary data
        channel_name = channel_data.model.get_channel_name(root_oid)

        limit = get_limit(request.GET, Website.RecentActivity.MaxMessageCount)

        ctxt = {
            "channel_name":
            channel_name,
            "channel_data":
            channel_data.model,
            "recent_msg_limit":
            limit or "",
            "recent_msg_limit_max":
            Website.RecentActivity.MaxMessageCount,
            "recent_msg_data":
            MessageStatsDataProcessor.get_recent_messages(
                channel_data.model, limit)
        }

        return render_template(self.request,
                               _("Recent Messages - {}").format(channel_name),
                               "info/recent/message.html",
                               ctxt,
                               nav_param=kwargs)
예제 #6
0
    def process_pass(self):
        self._profiles = ProfileManager.get_user_profiles(
            self._channel_oid, self._root_oid)

        if self._profiles:
            self._result = ProfileManager.get_permissions(self._profiles)
예제 #7
0
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