Example #1
0
        def create_community_view(request) -> None:
            context: Context = request.context
            args = context.get_request_body()

            args['accepted_terms_and_conditions'] = parse_bool(
                args.pop('accepted_terms_and_conditions', None))
            if not args['accepted_terms_and_conditions']:
                return MassenergizeResponse(
                    error="Please accept the terms and conditions")

            ok, err = check_length(args, 'name', 3, 25)
            if not ok:
                return MassenergizeResponse(error=str(err))

            ok, err = check_length(args, 'subdomain', 4, 20)
            if not ok:
                return MassenergizeResponse(error=str(err))

            args['is_geographically_focused'] = parse_bool(
                args.pop('is_geographically_focused', False))
            args['is_published'] = parse_bool(args.pop('is_published', False))
            args['is_approved'] = parse_bool(args.pop('is_approved', False))

            args = rename_field(args, 'image', 'logo')
            args = parse_location(args)
            if not args['is_geographically_focused']:
                args.pop('location', None)

            community_info, err = self.service.create_community(context, args)
            if err:
                return MassenergizeResponse(error=str(err), status=err.status)
            return MassenergizeResponse(data=community_info)
Example #2
0
  def set_request_body(self, request):
    #get the request args
    self.args = get_request_contents(request)
    self.is_sandbox = parse_bool(self.args.pop('__is_sandbox', False))
    self.community = self.args.pop('__community', None)
    self.is_admin_site = parse_bool(self.args.pop('__is_admin_site', False))

    #set the is_dev field
    self.is_prod = parse_bool(self.args.pop('__is_prod', False))
    self.is_dev = not self.is_prod
Example #3
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)
Example #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['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)
Example #5
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)
Example #6
0
 def update(self, request):
   context: Context = request.context
   args: dict = context.args
   policy_id = args.pop('policy_id', None)
   is_global = args.pop('is_global', None)
   if is_global:
     args["is_global"] = parse_bool(is_global)
   policy_info, err = self.service.update_policy(policy_id, args)
   if err:
     return MassenergizeResponse(error=str(err), status=err.status)
   return MassenergizeResponse(data=policy_info)
Example #7
0
 def create_policy_view(request) -> None:
     context: Context = request.context
     args: dict = context.args
     community_id = args.pop('community_id', None)
     is_global = args.pop('is_global', None)
     if is_global:
         args["is_global"] = parse_bool(is_global)
     policy_info, err = self.service.create_policy(community_id, args)
     if err:
         return MassenergizeResponse(error=str(err), status=err.status)
     return MassenergizeResponse(data=policy_info)
Example #8
0
 def update_subscriber_view(request) -> None: 
   context: Context = request.context
   args: dict = context.args
   subscriber_id = args.pop('subscriber_id', None)
   is_global = args.pop('is_global', None)
   if is_global:
     args["is_global"] = parse_bool(is_global)
   subscriber_info, err = self.service.update_subscriber(subscriber_id, args)
   if err:
     return MassenergizeResponse(error=str(err), status=err.status)
   return MassenergizeResponse(data=subscriber_info)
Example #9
0
    def update(self, request):
        context: Context = request.context
        args: dict = context.args

        args = rename_field(args, 'id', 'community_id')
        community_id = args.get('community_id', None)
        if not community_id:
            return MassenergizeResponse(error='Please provide an ID')

        if (args.get('name', None)):
            ok, err = check_length(args, 'name', 3, 25)
            if not ok:
                return MassenergizeResponse(error=str(err))

        if (args.get('subdomain', None)):
            ok, err = check_length(args, 'subdomain', 4, 20)
            if not ok:
                return MassenergizeResponse(error=str(err))

        if (args.get('owner_name', None)):
            args['owner_name'] = args.get('owner_name', None)
        if (args.get('owner_email', None)):
            args['owner_email'] = args.get('owner_email', None)
        if (args.get('owner_phone_number', None)):
            args['owner_phone_number'] = args.get('owner_phone_number', None)

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

        args = rename_field(args, 'image', 'logo')
        args = parse_location(args)

        community_info, err = self.service.update_community(context, args)
        if err:
            return MassenergizeResponse(error=str(err), status=err.status)
        return MassenergizeResponse(data=community_info)
Example #10
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)
Example #11
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)
Example #12
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)
Example #13
0
    def create_subscriber_view(request) -> None: 
      context: Context = request.context
      args: dict = context.args

      validator: Validator = Validator()
      (validator
        .add("name", str, is_required=True)
        .add("email", str, is_required=True)
      )

      args, err = validator.verify(args)
      if err:
        return err
      
      community_id = args.pop('community_id', None)
      is_global = args.pop('is_global', None)
      if is_global:
        args["is_global"] = parse_bool(is_global)
      subscriber_info, err = self.service.create_subscriber(community_id ,args)
      if err:
        return MassenergizeResponse(error=str(err), status=err.status)
      return MassenergizeResponse(data=subscriber_info)
Example #14
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)
Example #15
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)
Example #16
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)