Exemplo n.º 1
0
  def create(self, request):
    context: Context = request.context
    args: dict = context.args
    ok, err = check_length(args, 'name', min_length=5, max_length=100)
    if not ok:
      return MassenergizeResponse(error=str(err), status=err.status)
    args['tags'] = parse_list(args.get('tags', []))
    args['is_global'] = parse_bool(args.pop('is_global', None))
    args['archive'] = parse_bool(args.pop('archive', None))
    args['is_published'] = parse_bool(args.pop('is_published', None))
    args['have_address'] =  parse_bool(args.pop('have_address', False))
    args = parse_location(args)

    event_info, err = self.service.create_event(context, args)
    if err:
      return MassenergizeResponse(error=str(err), status=err.status)
    return MassenergizeResponse(data=event_info)
Exemplo n.º 2
0
    def create(self, request):
        context: Context = request.context
        args: dict = context.args
        args = rename_field(args, 'community_id', 'community')
        args = rename_field(args, 'action_id', 'action')
        args = rename_field(args, 'vendor_id', 'vendor')
        args['tags'] = parse_list(args.get('tags', []))

        is_approved = args.pop("is_approved", None)
        if is_approved:
            args["is_approved"] = parse_bool(is_approved)
        is_published = args.get("is_published", None)
        if is_published:
            args["is_published"] = parse_bool(is_published)

        graph_info, err = self.service.create_graph(context, args)
        if err:
            return MassenergizeResponse(error=str(err), status=err.status)
        return MassenergizeResponse(data=graph_info)
Exemplo n.º 3
0
 def update_testimonial_view(request) -> None: 
   context: Context = request.context
   args: dict = context.args
   
   is_approved = args.pop("is_approved", None)
   if is_approved:
     args["is_approved"] = parse_bool(is_approved)
   is_published = args.get("is_published", None)
   if is_published:
     args["is_published"] = parse_bool(is_published)
   args = rename_field(args, 'community_id', 'community')
   args = rename_field(args, 'action_id', 'action')
   args = rename_field(args, 'vendor_id', 'vendor')
   args['tags'] = parse_list(args.get('tags', []))
   testimonial_id = args.pop("testimonial_id", None)
   testimonial_info, err = self.service.update_testimonial(context, testimonial_id, args)
   if err:
     return MassenergizeResponse(error=str(err), status=err.status)
   return MassenergizeResponse(data=testimonial_info)
Exemplo n.º 4
0
    def create(self, request):
        context: Context = request.context
        args: dict = context.args
        args = rename_field(args, 'community_id', 'community')
        args = rename_field(args, 'action_id', 'action')
        args = rename_field(args, 'vendor_id', 'vendor')
        args = rename_field(args, 'preferredName', 'preferred_name')
        args['tags'] = parse_list(args.get('tags', []))

        # check validity - these should be IDs
        community = args.get('community', None)
        if community and not isinstance(community, int):
            args["community"] = parse_int(community)

        action = args.get('action', None)
        if action and not isinstance(action, int):
            args["action"] = parse_int(action)

        vendor = args.get('vendor', None)
        if vendor and not isinstance(vendor, int):
            args["vendor"] = parse_int(vendor)

        # To do, if we decide:
        # if user specifies other_vendor and passed to API - should record it as an unapproved vendor

        is_approved = args.pop("is_approved", None)
        if is_approved:
            args["is_approved"] = parse_bool(is_approved)
        is_published = args.get("is_published", None)
        if is_published:
            args["is_published"] = parse_bool(is_published)

        # no anonymous option anymore
        args["anonymous"] = False

        testimonial_info, err = self.service.create_testimonial(context, args)
        if err:
            return MassenergizeResponse(error=str(err), status=err.status)
        return MassenergizeResponse(data=testimonial_info)
Exemplo n.º 5
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)
Exemplo n.º 6
0
    def verify(self, args, strict=False):
        try:
            # when in strict mode remove all unexpected fields
            if strict:
                for f in args:
                    if f not in self.fields:
                        del args[f]

            #first rename all fields that need renaming
            for (old_name, new_name) in self.rename_fields:
                val = self.fields.pop(old_name, None)
                if val:
                    self.fields[new_name] = val

            # 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:
                    return None, CustomMassenergizeError(
                        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:
                            return None, CustomMassenergizeError(
                                f"{field_name} must have at least {min_length} characters"
                            )
                        if max_length and len(val) > max_length:
                            return None, CustomMassenergizeError(
                                f"{field_name} must have at most {max_length} characters"
                            )
                        args[field_name] = val

                    elif field_type == int:
                        args[field_name] = parse_int(args[field_name])
                    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 == '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

            return args, None

        except Exception as e:
            import traceback
            traceback.print_exc()
            return None, CustomMassenergizeError(e)
Exemplo n.º 7
0
        def update_home_page_setting_view(request) -> None:
            context: Context = request.context
            args: dict = context.args

            #featured links
            args['show_featured_links'] = parse_bool(
                args.pop('show_featured_links', True))
            args['featured_links'] = [
                {
                    'title': args.pop('icon_box_1_title', ''),
                    'link': args.pop('icon_box_1_link', ''),
                    'icon': args.pop('icon_box_1_icon', ''),
                    'description': args.pop('icon_box_1_description', '')
                },
                {
                    'title': args.pop('icon_box_2_title', ''),
                    'link': args.pop('icon_box_2_link', ''),
                    'icon': args.pop('icon_box_2_icon', ''),
                    'description': args.pop('icon_box_2_description', '')
                },
                {
                    'title': args.pop('icon_box_3_title', ''),
                    'link': args.pop('icon_box_3_link', ''),
                    'icon': args.pop('icon_box_3_icon', ''),
                    'description': args.pop('icon_box_3_description', '')
                },
                {
                    'title': args.pop('icon_box_4_title', ''),
                    'link': args.pop('icon_box_4_link', ''),
                    'icon': args.pop('icon_box_4_icon', ''),
                    'description': args.pop('icon_box_4_description', '')
                },
            ]
            #checks for length
            for t in args["featured_links"]:
                if len(t["description"]) > 40:
                    return MassenergizeResponse(
                        error=
                        f"Please description text for {t['title']} should be less than 40 characters"
                    )

            # events
            args['show_featured_events'] = parse_bool(
                args.pop('show_featured_events', True))
            args['featured_events'] = parse_list(
                args.pop('featured_events', []))

            #statistics
            args['show_featured_stats'] = parse_bool(
                args.pop('show_featured_stats'))
            args['goal'] = {
                'attained_number_of_actions':
                parse_int(args.pop('attained_number_of_actions', 0)),
                'target_number_of_actions':
                parse_int(args.pop('target_number_of_actions', 0)),
                'attained_number_of_households':
                parse_int(args.pop('attained_number_of_households', 0)),
                'target_number_of_households':
                parse_int(args.pop('target_number_of_households', 0))
            }

            args.pop('organic_attained_number_of_households', None)
            args.pop('organic_attained_number_of_actions', None)

            home_page_setting_info, err = self.service.update_home_page_setting(
                args)

            if err:
                return MassenergizeResponse(error=str(err), status=err.status)
            return MassenergizeResponse(data=home_page_setting_info)