Exemplo n.º 1
0
    def leave(self, request):
        context: Context = request.context
        args: dict = context.args
        team_id = args.pop('team_id', None)
        user_id = args.pop('user_id', None)
        if is_value(team_id) and is_value(user_id):
            team_info, err = self.team.leave_team(team_id, user_id)
        elif not is_value(team_id):
            err = CustomMassenergizeError("No team_id passed to teams.leave")
        else:
            err = CustomMassenergizeError("No user_id passed to teams.leave")

        if err:
            return MassenergizeResponse(error=str(err), status=err.status)
        return MassenergizeResponse(data=team_info)
Exemplo n.º 2
0
    def create(self, request):
        context: Context = request.context
        args: dict = context.args

        admin_emails = args.pop('admin_emails', '')
        if is_value(admin_emails):
            args["admin_emails"] = parse_str_list(admin_emails)

        parentId = args.pop('parent_id', None)
        if is_value(parentId):
            args["parent_id"] = parentId

        args['is_published'] = parse_bool(args.pop('is_published', None))

        team_info, err = self.team.create_team(context, args)
        if err:
            return MassenergizeResponse(error=str(err), status=err.status)
        return MassenergizeResponse(data=team_info)
Exemplo n.º 3
0
    def update(self, request):
        context: Context = request.context
        args: dict = context.args
        team_id = args.pop('id', None)

        parentId = args.pop('parent_id', None)
        if is_value(parentId):
            args["parent_id"] = parentId

        args['is_published'] = parse_bool(args.pop('is_published', None))

        if is_value(team_id):
            team_info, err = self.team.update_team(team_id, args)
        else:
            err = CustomMassenergizeError("No team_id passed to teams.update")

        if err:
            return MassenergizeResponse(error=str(err), status=err.status)
        return MassenergizeResponse(data=team_info)
Exemplo n.º 4
0
    def delete(self, request):
        context: Context = request.context
        args: dict = context.args
        team_id = args.get("team_id", None)
        if is_value(team_id):
            team_info, err = self.team.delete_team(team_id)
        else:
            err = CustomMassEnergizeError("No team_id passed to teams.delete")

        if err:
            return MassenergizeResponse(error=str(err), status=err.status)
        return MassenergizeResponse(data=team_info)
Exemplo n.º 5
0
    def info(self, request):
        context: Context = request.context
        args: dict = context.args
        team_id = args.pop('team_id', None)

        if is_value(team_id):
            team_info, err = self.team.get_team_info(context, team_id)
        else:
            err = CustomMassEnergizeError("No team_id passed to teams.info")

        if err:
            return MassenergizeResponse(error=str(err), status=err.status)
        return MassenergizeResponse(data=team_info)
Exemplo n.º 6
0
    def list_teams_for_community_admin(
            self, context: Context, args) -> Tuple[list, MassEnergizeAPIError]:
        try:
            if context.user_is_super_admin:
                return self.list_teams_for_super_admin(context)

            community_id = args.pop('community_id', None)
            if not is_value(community_id):
                user = UserProfile.objects.get(pk=context.user_id)
                admin_groups = user.communityadmingroup_set.all()
                comm_ids = [ag.community.id for ag in admin_groups]
                teams = Team.objects.filter(communities__id__in=comm_ids,
                                            is_deleted=False).select_related(
                                                'logo', 'primary_community')
                return teams, None

            teams = Team.objects.filter(communities__id=community_id,
                                        is_deleted=False).select_related(
                                            'logo', 'primary_community')
            return teams, None

        except Exception as e:
            capture_message(str(e), level="error")
            return None, CustomMassenergizeError(e)
Exemplo n.º 7
0
    def verify(self, args, strict=False):
        try:
            #first rename all fields that need renaming
            for (old_name, new_name) in self.rename_fields:
                val = args.pop(old_name, None)
                if val:
                    args[new_name] = val

            # when in strict mode remove all unexpected fields
            if strict:
                tmp_args = args.copy()
                for f in args:
                    if f not in self.fields:
                        del tmp_args[f]
                args = tmp_args

            # cleanup and verify all contents of the args and return it
            for field_name, field_info in self.fields.items():
                field_type = field_info["type"]
                field_is_required = field_info["is_required"]

                if field_is_required and field_name not in args:
                    raise Exception(
                        f"You are Missing a Required Input: {self._common_name(field_name)}"
                    )

                if field_name in args:
                    if field_type == str:
                        val = parse_string(args[field_name])
                        options = field_info.get("options", {})
                        min_length = options.get("min_length", None)
                        max_length = options.get("max_length", None)
                        if min_length and len(val) < min_length:
                            raise Exception(
                                f"{field_name} must have at least {min_length} characters"
                            )
                        if max_length and len(val) > max_length:
                            raise Exception(
                                f"{field_name} must have at most {max_length} characters"
                            )
                        args[field_name] = val

                    elif field_type == int:
                        check = args.pop(field_name)
                        if is_value(check
                                    ):  # protect against "undefined" or "NULL"
                            args[field_name] = parse_int(check)
                    elif field_type == bool:
                        args[field_name] = parse_bool(args[field_name])
                    elif field_type == list:
                        args[field_name] = parse_list(args[field_name])
                    elif field_type == 'str_list':
                        check = args.pop(field_name)
                        if is_value(check
                                    ):  # protect against "undefined" or "NULL"
                            args[field_name] = parse_str_list(check)
                    elif field_type == 'date':
                        args[field_name] = parse_date(args[field_name])
                    elif field_type == 'location':
                        parse_location(args)
                    elif field_type == 'file':
                        args[field_name] = args.get(field_name, None) or None
                else:
                    if field_type == 'location':
                        parse_location(args)
                    elif field_type == 'file':
                        args[field_name] = args.get(field_name, None) or None

            # now clear the dictionary
            self.fields = {}
            self.rename_fields = set()

            return args, None

        except Exception as e:
            # now clear the  dictionary
            self.fields = {}
            self.rename_fields = set()

            capture_message(str(e), level="error")
            return None, CustomMassenergizeError(e)