|
| 1 | +# coding=utf-8 |
| 2 | +# |
| 3 | +# Copyright 2013 keyes.ie |
| 4 | +# |
| 5 | +# License: http://jkeyes.mit-license.org/ |
| 6 | +# |
| 7 | +""" Tag module. |
| 8 | +
|
| 9 | +>>> from intercom import Intercom |
| 10 | +>>> Intercom.app_id = 'dummy-app-id' |
| 11 | +>>> Intercom.api_key = 'dummy-api-key' |
| 12 | +>>> from intercom.tag import Tag |
| 13 | +
|
| 14 | +""" |
| 15 | + |
| 16 | +from . import Intercom |
| 17 | + |
| 18 | + |
| 19 | +class Tag(dict): |
| 20 | + """ Represents a tag. """ |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def find(cls, **params): |
| 24 | + """ Find a tag using params. |
| 25 | +
|
| 26 | + >>> tag = Tag.find(name="Free Trial") |
| 27 | + >>> tag.id |
| 28 | + >>> tag.name |
| 29 | + u'Free Trial' |
| 30 | + >>> tag.tagged_user_count |
| 31 | + >>> tag.color |
| 32 | + u'green' |
| 33 | +
|
| 34 | + """ |
| 35 | + resp = Intercom.get_tag(**params) |
| 36 | + return cls(resp) |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def find_by_name(cls, name): |
| 40 | + """ Find a tag by name. |
| 41 | +
|
| 42 | + >>> tag = Tag.find_by_name("Free Trial") |
| 43 | + >>> tag.id |
| 44 | + >>> tag.name |
| 45 | + u'Free Trial' |
| 46 | + >>> tag.tagged_user_count |
| 47 | + >>> tag.color |
| 48 | + u'green' |
| 49 | +
|
| 50 | + """ |
| 51 | + return Tag.find(name=name) |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def create( |
| 55 | + cls, name, tag_or_untag, user_ids=None, emails=None, |
| 56 | + color=None): |
| 57 | + """ Create a new tag an optionally tag user. |
| 58 | +
|
| 59 | + >>> tag = Tag.create(user_ids=["abc123", "def456"], |
| 60 | + ... name="Free Trial", tag_or_untag="tag") |
| 61 | + >>> tag.id |
| 62 | + >>> tag.name |
| 63 | + u'Free Trial' |
| 64 | + >>> tag.tagged_user_count |
| 65 | + >>> tag.color |
| 66 | + u'green' |
| 67 | +
|
| 68 | + """ |
| 69 | + resp = Intercom.create_tag( |
| 70 | + name, tag_or_untag, user_ids=user_ids, emails=emails, |
| 71 | + color=color) |
| 72 | + return cls(resp) |
| 73 | + |
| 74 | + def save(self): |
| 75 | + """ Update a tag: |
| 76 | +
|
| 77 | + >>> tag = Tag() |
| 78 | + >>> tag.user_ids = ["abc123", "def456"] |
| 79 | + >>> tag.name = "Free Trial" |
| 80 | + >>> tag.tag_or_untag = "tag" |
| 81 | + >>> tag.save() |
| 82 | + >>> tag.tagged_user_count |
| 83 | + >>> tag.color |
| 84 | + u'green' |
| 85 | +
|
| 86 | + """ |
| 87 | + resp = Intercom.update_tag( |
| 88 | + self.name, self.get('tag_or_untag', None), |
| 89 | + user_ids=self.get('user_ids', None), |
| 90 | + emails=self.get('emails', None), |
| 91 | + color=self.color) |
| 92 | + self.update(resp) |
| 93 | + |
| 94 | + @property |
| 95 | + def name(self): |
| 96 | + """ Get the name of the tag. """ |
| 97 | + return dict.get(self, 'name', None) |
| 98 | + |
| 99 | + @name.setter |
| 100 | + def name(self, name): |
| 101 | + """ Get the name of the tag. """ |
| 102 | + self['name'] = name |
| 103 | + |
| 104 | + @property |
| 105 | + def color(self): |
| 106 | + """ Get the color of the tag. """ |
| 107 | + return dict.get(self, 'color', None) |
| 108 | + |
| 109 | + @color.setter |
| 110 | + def color(self, color): |
| 111 | + """ Get the color of the tag. """ |
| 112 | + self['color'] = color |
| 113 | + |
| 114 | + @property |
| 115 | + def id(self): |
| 116 | + """ The id of the tag. """ |
| 117 | + return dict.get(self, 'id', None) |
| 118 | + |
| 119 | + @property |
| 120 | + def segment(self): |
| 121 | + """ Get the segment of the tag. """ |
| 122 | + return dict.get(self, 'segment', None) |
| 123 | + |
| 124 | + @property |
| 125 | + def tagged_user_count(self): |
| 126 | + """ Get the tagged_user_count of the tag. """ |
| 127 | + return dict.get(self, 'tagged_user_count', None) |
| 128 | + |
| 129 | + @property |
| 130 | + def tag_or_untag(self): |
| 131 | + """ The tag_or_untag of the tag. """ |
| 132 | + raise AttributeError("tag_or_untag is a write-only property") |
| 133 | + |
| 134 | + @tag_or_untag.setter |
| 135 | + def tag_or_untag(self, tag_or_untag): |
| 136 | + """ The tag_or_untag of the tag. """ |
| 137 | + self['tag_or_untag'] = tag_or_untag |
| 138 | + |
| 139 | + @property |
| 140 | + def user_ids(self): |
| 141 | + """ The user_ids of the tag. """ |
| 142 | + raise AttributeError("user_ids is a write-only property") |
| 143 | + |
| 144 | + @user_ids.setter |
| 145 | + def user_ids(self, user_ids): |
| 146 | + """ The user_ids of the tag. """ |
| 147 | + self['user_ids'] = user_ids |
| 148 | + |
| 149 | + @property |
| 150 | + def emails(self): |
| 151 | + """ The emails of the tag. """ |
| 152 | + raise AttributeError("emails is a write-only property") |
| 153 | + |
| 154 | + @emails.setter |
| 155 | + def emails(self, emails): |
| 156 | + """ The emails of the tag. """ |
| 157 | + self['emails'] = emails |
0 commit comments