Exemplo n.º 1
0
  def message_team_admin(self, context: Context, args) -> Tuple[list, MassEnergizeAPIError]:
    try:
      user_name = args.pop("user_name", None)
      team_id = args.pop("team_id", None)
      title = args.pop("title", None)
      email = args.pop("email", None) or context.user_email
      body = args.pop("message", None) or args.pop("body", None)

      if not team_id:
        return None, InvalidResourceError()

      team = Team.objects.filter(pk=team_id).first()
      if not team:
        return None, InvalidResourceError()

      user, err = get_user(context.user_id)
      if err:
        return None, err

      new_message = Message.objects.create(user_name=user_name, user=user, title=title, body=body, community=team.primary_community, team=team, is_team_admin_message=True)
      new_message.save()

      return new_message, None 

    except Exception as e:
      capture_message(str(e), level="error")
      return None, CustomMassenergizeError(e)
Exemplo n.º 2
0
    def get_vendor_info(self, context, args) -> (dict, MassEnergizeAPIError):
        try:
            vendor_id = args.pop('vendor_id', None) or args.pop('id', None)

            if not vendor_id:
                return None, InvalidResourceError()
            vendor = Vendor.objects.filter(pk=vendor_id).first()

            if not vendor:
                return None, InvalidResourceError()

            return vendor, None
        except Exception as e:
            capture_message(str(e), level="error")
            return None, CustomMassenergizeError(e)
Exemplo n.º 3
0
  def reply_from_team_admin(self, context, args) -> Tuple[dict, MassEnergizeAPIError]:
    try:
      message_id = args.pop('message_id', None) or args.pop('id', None)
      
      if not message_id:
        return None, InvalidResourceError()
      message = Message.objects.filter(pk=message_id).first()

      if not message:
        return None, InvalidResourceError()

      return message, None
    except Exception as e:
      capture_message(str(e), level="error")
      return None, CustomMassenergizeError(e)
Exemplo n.º 4
0
  def get_message_info(self, context, args) -> (dict, MassEnergizeAPIError):
    try:
      message_id = args.pop('message_id', None) or args.pop('id', None)
      
      if not message_id:
        return None, InvalidResourceError()
      message = Message.objects.filter(pk=message_id).first()

      if not message:
        return None, InvalidResourceError()

      return message, None
    except Exception as e:
      print(e)
      return None, CustomMassenergizeError(e)
Exemplo n.º 5
0
 def get_goal_info(self, goal_id) -> (Goal, MassEnergizeAPIError):
     try:
         goal = Goal.objects.get(id=goal_id)
         return goal, None
     except Exception as e:
         capture_message(str(e), level="error")
         return None, InvalidResourceError()
Exemplo n.º 6
0
    def rsvp_update(self, context: Context,
                    args) -> Tuple[dict, MassEnergizeAPIError]:
        try:
            event_id = args.pop("event_id", None)
            status = args.pop("status", "SAVE")
            user = get_user_or_die(context, args)
            event = Event.objects.filter(pk=event_id).first()
            if not event:
                return None, InvalidResourceError()

            event_attendees = EventAttendee.objects.filter(event=event,
                                                           user=user,
                                                           is_deleted=False)
            if event_attendees:
                event_attendee = event_attendees.first()
                if status == "Not Going":
                    event_attendee.delete()
                else:
                    event_attendee.status = status
                    event_attendee.save()
            elif status != "Not Going":
                event_attendee = EventAttendee.objects.create(event=event,
                                                              user=user,
                                                              status=status)
            else:
                return None, None
            return event_attendee, None

        except Exception as e:
            capture_message(str(e), level="error")
            return None, CustomMassenergizeError(e)
Exemplo n.º 7
0
 def get_goal_info(self, goal_id) -> (Goal, MassEnergizeAPIError):
     try:
         goal = Goal.objects.get(id=goal_id)
         return goal, None
     except Exception as e:
         print(e)
         return None, InvalidResourceError()
Exemplo n.º 8
0
    def members_preferred_names(self, context: Context,
                                args) -> Tuple[Team, MassEnergizeAPIError]:
        try:
            team_id = args.get('team_id', None)
            if not team_id:
                return [], CustomMassenergizeError(
                    'Please provide a valid team_id')

            team = Team.objects.filter(id=team_id).first()
            users = get_team_users(team)
            res = []
            for user in users:
                # only list users that have joined the platform
                if user.accepts_terms_and_conditions:
                    member = TeamMember.objects.filter(user=user,
                                                       team=team).first()
                    member_obj = {
                        "id": None,
                        "user_id": str(user.id),
                        "preferred_name": user.preferred_name,
                        "is_admin": False
                    }
                    if member:
                        member_obj['id'] = member.id
                        member_obj['is_admin'] = member.is_admin
                    res.append(member_obj)

            return res, None
        except Exception as e:
            capture_message(str(e), level="error")
            return None, InvalidResourceError()
Exemplo n.º 9
0
    def update_community(self, community_id,
                         args) -> (dict, MassEnergizeAPIError):
        try:
            logo = args.pop('logo', None)
            community = Community.objects.filter(id=community_id)
            if not community:
                return None, InvalidResourceError()

            if not args.get('is_geographically_focused', False):
                args['location'] = None

            community.update(**args)

            new_community = community.first()
            # if logo and new_community.logo:   # can't update the logo if the community doesn't have one already?
            #  # new_community.logo.file = logo
            #  # new_community.logo.save()
            if logo:
                cLogo = Media(file=logo,
                              name=f"{args.get('name', '')} CommunityLogo")
                cLogo.save()
                new_community.logo = cLogo
                new_community.save()

            return new_community, None
        except Exception as e:
            return None, CustomMassenergizeError(e)
Exemplo n.º 10
0
    def copy_event(self, context: Context,
                   event_id) -> (dict, MassEnergizeAPIError):
        try:
            events_selected = Event.objects.select_related(
                'image', 'community').prefetch_related(
                    'tags', 'invited_communities').filter(id=event_id)
            event_to_copy: Event = events_selected.first()
            if not event_to_copy:
                return None, InvalidResourceError()

            old_tags = event_to_copy.tags.all()
            event_to_copy.pk = None
            new_event = event_to_copy
            new_event.name = f"{event_to_copy.name}-Copy-{randint(1, 1000)}"
            new_event.is_published = False
            new_event.start_date_and_time = event_to_copy.start_date_and_time
            new_event.end_date_and_time = event_to_copy.end_date_and_time
            new_event.description = event_to_copy.description
            new_event.featured_summary = event_to_copy.featured_summary
            new_event.location = event_to_copy.location
            new_event.save()

            #copy tags over
            for t in old_tags:
                new_event.tags.add(t)

            return new_event, None
        except Exception as e:
            capture_message(str(e), level="error")
            return None, CustomMassenergizeError(e)
Exemplo n.º 11
0
    def update_tag_collection(self, tag_collection_id,
                              args) -> Tuple[dict, MassEnergizeAPIError]:
        try:
            tag_collection = TagCollection.objects.filter(
                id=tag_collection_id).first()
            if not tag_collection:
                return None, InvalidResourceError()

            name = args.pop('name', None)
            if name:
                tag_collection.name = name

            rank = args.pop('rank', None)
            if rank:
                tag_collection.rank = rank

            tags = Tag.objects.filter(tag_collection=tag_collection)
            for (k, v) in args.items():
                if k.startswith('tag_') and not k.endswith('_rank'):
                    tag_id = int(k.split('_')[1])
                    tag = tags.filter(id=tag_id).first()

                    if tag:
                        if not tag.name.strip():
                            tag.delete()
                            continue
                        tag.name = v.strip()
                        tag.save()
                elif k.startswith('tag_') and k.endswith('_rank'):
                    tag_id = int(k.split('_')[1])
                    tag = tags.filter(id=tag_id).first()
                    if tag:
                        if not tag.name.strip():
                            tag.delete()
                            continue

                        tag.rank = v
                        tag.save()

            tags_to_add = args.pop('tags_to_add', '')
            if tags_to_add:
                tags_to_add = tags_to_add.strip().split(',')
                for i in range(len(tags_to_add)):
                    t = tags_to_add[i]
                    tag = Tag.objects.create(name=t.strip().title(),
                                             tag_collection=tag_collection,
                                             rank=len(tags) + i + 1)
                    tag.save()

            tags_to_delete = args.pop('tags_to_delete', '')
            if tags_to_delete:
                tags_to_delete = [t.strip() for t in tags_to_delete.split(',')]
                ts = tags.filter(name__in=tags_to_delete)
                ts.delete()

            tag_collection.save()
            return tag_collection, None
        except Exception as e:
            capture_message(str(e), level="error")
            return None, CustomMassenergizeError(e)
Exemplo n.º 12
0
    def graph_communities_impact(self, context: Context,
                                 args) -> Tuple[Graph, MassEnergizeAPIError]:
        try:
            subdomain = args.get('subdomain', None)
            community_id = args.get('community_id', None)
            if not community_id and not subdomain:
                return None, CustomMassenergizeError(
                    "Missing community_id or subdomain field")

            community: Community = Community.objects.get(
                Q(pk=community_id) | Q(subdomain=subdomain))
            if not community:
                return None, InvalidResourceError()

            res = [get_households_engaged(community)]
            limit = 10
            for c in Community.objects.filter(is_deleted=False,
                                              is_published=True)[:limit]:

                if c.id != community.id:
                    res.append(get_households_engaged(c))

            return {
                "id": 1,
                "title": "Communities Impact",
                "graph_type": "bar_chart",
                "data": res
            }, None
        except Exception as e:
            capture_message(str(e), level="error")
            return None, CustomMassenergizeError(e)
Exemplo n.º 13
0
  def update_vendor(self, context: Context, args) -> Tuple[dict, MassEnergizeAPIError]:
    
    try:
      vendor_id = args.pop('vendor_id', None)
      communities = args.pop('communities', [])
      onboarding_contact_email = args.pop('onboarding_contact_email', None)
      website = args.pop('website', None)
      key_contact_name = args.pop('key_contact_name', None)
      key_contact_email = args.pop('key_contact_email', None)
      key_contact = {
        "name": key_contact_name,
        "email": key_contact_email
      }
      image = args.pop('image', None)
      tags = args.pop('tags', [])
      have_address = args.pop('have_address', False)
      if not have_address:
        args['location'] = None

      vendor = Vendor.objects.filter(id=vendor_id)   
      if not vendor:
        return None, InvalidResourceError()  
      vendor.update(**args)
      vendor = vendor.first()
      
      if communities:
        vendor.communities.set(communities)

      if onboarding_contact_email:
        vendor.onboarding_contact_email = onboarding_contact_email
        
      if key_contact:
        if vendor.key_contact:
          vendor.key_contact.update(key_contact)
        else:
          vendor.key_contact = key_contact

      if image:
        logo = Media(name=f"Logo-{slugify(vendor.name)}", file=image)
        logo.save()
        vendor.logo = logo
      
      if onboarding_contact_email:
        onboarding_contact = UserProfile.objects.filter(email=onboarding_contact_email).first()
        if onboarding_contact:
          vendor.onboarding_contact = onboarding_contact
    
      if tags:
        vendor.tags.set(tags)

      if website:
        vendor.more_info = {'website': website}
        
      vendor.save()
      return vendor, None

    except Exception as e:
      capture_message(str(e), level="error")
      return None, CustomMassenergizeError(e)
Exemplo n.º 14
0
 def update_contact_us_page_setting(self, contact_us_page_setting_id,
                                    args) -> (dict, MassEnergizeAPIError):
     contact_us_page_setting = ContactUsPageSettings.objects.filter(
         id=contact_us_page_setting_id)
     if not contact_us_page_setting:
         return None, InvalidResourceError()
     contact_us_page_setting.update(**args)
     return contact_us_page_setting, None
Exemplo n.º 15
0
 def get_policy_info(self, policy_id) -> (dict, MassEnergizeAPIError):
   try:
     policy = Policy.objects.get(id=policy_id)
     if not policy:
       return None, InvalidResourceError()
     return policy, None
   except Exception as e:
     return None, CustomMassenergizeError(e)
Exemplo n.º 16
0
 def get_teams_page_setting_info(self, args) -> (dict, MassEnergizeAPIError):
   try:
     page = TeamsPageSettings.objects.filter(**args).first()
     if not page:
       return None, InvalidResourceError()
     return page, None
   except Exception as e:
     return None, CustomMassenergizeError(e)
Exemplo n.º 17
0
 def get_home_page_setting_info(self, args) -> (dict, MassEnergizeAPIError):
     try:
         home_page_setting = HomePageSettings.objects.filter(**args).first()
         if not home_page_setting:
             return None, InvalidResourceError()
         return home_page_setting, None
     except Exception as e:
         return None, CustomMassenergizeError(e)
Exemplo n.º 18
0
    def update_testimonial(self, context: Context, testimonial_id,
                           args) -> (dict, MassEnergizeAPIError):
        try:
            testimonial = Testimonial.objects.filter(id=testimonial_id)
            if not testimonial:
                return None, InvalidResourceError()

            image = args.pop('image', None)
            tags = args.pop('tags', [])
            action = args.pop('action', None)
            vendor = args.pop('vendor', None)
            community = args.pop('community', None)
            rank = args.pop('rank', None)
            new_testimonial = testimonial.first()

            if image:
                media = Media.objects.create(
                    file=image, name=f"ImageFor{args.get('name', '')}Event")
                new_testimonial.image = media
            else:
                new_testimonial.image = None

            if action:
                testimonial_action = Action.objects.filter(id=action).first()
                new_testimonial.action = testimonial_action
            else:
                new_testimonial.action = None

            if vendor:
                testimonial_vendor = Vendor.objects.filter(id=vendor).first()
                new_testimonial.vendor = testimonial_vendor
            else:
                new_testimonial.vendor = None

            if community:
                testimonial_community = Community.objects.filter(
                    id=community).first()
                new_testimonial.community = testimonial_community
            else:
                new_testimonial.community = None

            if rank:
                new_testimonial.rank = rank

            tags_to_set = []
            for t in tags:
                tag = Tag.objects.filter(pk=t).first()
                if tag:
                    tags_to_set.append(tag)
            if tags_to_set:
                new_testimonial.tags.set(tags_to_set)

            new_testimonial.save()
            testimonial.update(**args)
            return new_testimonial, None
        except Exception as e:
            capture_message(str(e), level="error")
            return None, CustomMassenergizeError(e)
Exemplo n.º 19
0
 def get_testimonial_info(self,  context: Context, args) -> Tuple[dict, MassEnergizeAPIError]:
   try:
     testimonial = Testimonial.objects.filter(**args).first()
     if not testimonial:
       return None, InvalidResourceError()
     return testimonial, None
   except Exception as e:
     capture_message(str(e), level="error")
     return None, CustomMassenergizeError(e)
Exemplo n.º 20
0
 def get_subscriber_info(self,
                         subscriber_id) -> (dict, MassEnergizeAPIError):
     try:
         subscriber = Subscriber.objects.get(id=subscriber_id)
         if not subscriber:
             return None, InvalidResourceError()
         return subscriber, None
     except Exception as e:
         return None, CustomMassenergizeError(e)
Exemplo n.º 21
0
 def decrease_value(self, goal_id, field_name):
     try:
         goals_to_decrease = Goal.objects.filter(id=goal_id)
         goals_to_decrease.update(**{field_name: F(field_name) - 1})
         if not goals_to_decrease:
             return None, InvalidResourceError()
         return goals_to_decrease.first(), None
     except Exception as e:
         return None, CustomMassenergizeError(str(e))
Exemplo n.º 22
0
 def delete_tag_collection(self, tag_collection_id) -> (dict, MassEnergizeAPIError):
   try:
     tag_collections = TagCollection.objects.filter(id=tag_collection_id)
     if not tag_collections:
       return None, InvalidResourceError()
     tag_collections.delete()
   except Exception as e:
     capture_message(str(e), level="error")
     return None, CustomMassenergizeError(e)
Exemplo n.º 23
0
 def get_policy_info(self, policy_id) -> Tuple[dict, MassEnergizeAPIError]:
   try:
     policy = Policy.objects.get(id=policy_id)
     if not policy:
       return None, InvalidResourceError()
     return policy, None
   except Exception as e:
     capture_message(str(e), level="error")
     return None, CustomMassenergizeError(e)
Exemplo n.º 24
0
 def get_graph_info(self, context: Context,
                    args) -> (dict, MassEnergizeAPIError):
     try:
         graph = Graph.objects.filter(**args).first()
         if not graph:
             return None, InvalidResourceError()
         return graph, None
     except Exception as e:
         return None, CustomMassenergizeError(e)
Exemplo n.º 25
0
    def delete_carbon_equivalency(self, args):
        id = args.get('id', None)
        carbon_equivalency = CarbonEquivalency.objects.filter(id=id)

        if not carbon_equivalency:
            return None, InvalidResourceError()

        carbon_equivalency.delete()
        return carbon_equivalency, None
Exemplo n.º 26
0
 def delete_tag_collection(self, tag_collection_id) -> (dict, MassEnergizeAPIError):
   try:
     tag_collections = TagCollection.objects.filter(id=tag_collection_id)
     print(tag_collection_id, tag_collections)
     if not tag_collections:
       return None, InvalidResourceError()
     tag_collections.delete()
   except Exception as e:
     return None, CustomMassenergizeError(e)
Exemplo n.º 27
0
    def delete_page_setting(
            self, page_setting_id) -> Tuple[dict, MassEnergizeAPIError]:
        page_settings = self.pageSettingsModel.objects.filter(
            id=page_setting_id)
        if not page_settings:
            return None, InvalidResourceError()

        page_settings.update(**{'is_deleted': True})
        return page_settings.first(), None
Exemplo n.º 28
0
    def graph_actions_completed_by_team(
            self, context: Context,
            args) -> Tuple[Graph, MassEnergizeAPIError]:
        try:

            team_id = args.get('team_id', None)

            if not team_id:
                return None, CustomMassenergizeError("Missing team_id field")

            team: Team = Team.objects.get(id=team_id)

            if not team:
                return None, InvalidResourceError()

            if not context.is_sandbox and not context.user_is_admin():
                if not team.is_published:
                    return None, CustomMassenergizeError(
                        "Content Not Available Yet")

            users = get_team_users(team)

            completed_action_rels = []
            for user in users:
                completed_action_rels.extend(
                    user.useractionrel_set.filter(status="DONE").all())

            categories = TagCollection.objects.get(
                name="Category").tag_set.order_by("name").all()

            prefetch_related_objects(completed_action_rels, "action__tags")
            data = []
            for category in categories:
                data.append({
                    "id":
                    category.id,
                    "name":
                    category.name,
                    "value":
                    len(
                        list(
                            filter(
                                lambda action_rel: category in action_rel.
                                action.tags.all(), completed_action_rels)))
                })

            res = {
                "data": data,
                "title": "Actions Completed By Members Of Team",
                "team": team.info()
            }

            return res, None

        except Exception as e:
            capture_message(str(e), level="error")
            return None, CustomMassenergizeError(e)
Exemplo n.º 29
0
  def reply_from_community_admin(self, context, args) -> (dict, MassEnergizeAPIError):
    try:
      message_id = args.pop('message_id', None) or args.pop('id', None)
      title = args.pop('title', None)
      body = args.pop('body', None)
      # attached_file = args.pop('attached_file', None)

      if not message_id:
        return None, InvalidResourceError()
      message = Message.objects.filter(pk=message_id).first()

      if not message:
        return None, InvalidResourceError()

      return message, None
    except Exception as e:
      capture_message(str(e), level="error")
      return None, CustomMassenergizeError(e)
Exemplo n.º 30
0
 def get_testimonial_info(self, context: Context,
                          args) -> (dict, MassEnergizeAPIError):
     try:
         testimonial = Testimonial.objects.filter(**args).first()
         if not testimonial:
             return None, InvalidResourceError()
         return testimonial, None
     except Exception as e:
         return None, CustomMassenergizeError(e)