Skip to content

Commit b685969

Browse files
committed
Breaking tag functionality into generic handlers.
1 parent 44b5aef commit b685969

File tree

4 files changed

+121
-58
lines changed

4 files changed

+121
-58
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import inspect
4+
5+
6+
class BaseHandler(type):
7+
8+
def __getattr__(cls, name): # noqa
9+
# ignore underscore attrs
10+
if name[0] == "_":
11+
return
12+
13+
import time
14+
a = time.time()
15+
# get the class heirarchy
16+
klasses = inspect.getmro(cls)
17+
b = time.time()
18+
# find a class that can handle this attr
19+
for klass in klasses:
20+
if hasattr(klass, 'handles_attr') and klass.handles_attr(name):
21+
c = time.time()
22+
d = b - a
23+
e = c - b
24+
print("GETMRO %s HANDLE %s" % (d, e))
25+
return klass._get(cls, name)

intercom/generic_handlers/tag.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from intercom import utils
4+
5+
6+
class TagHandler():
7+
def __init__(self, entity, name, context):
8+
self.entity = entity
9+
self.untag = name == "untag"
10+
self.context = context
11+
12+
def __call__(self, *args, **kwargs):
13+
return self.entity._tag_collection(
14+
self.context, *args, untag=self.untag, **kwargs)
15+
16+
17+
class TagUntag():
18+
19+
@classmethod
20+
def handles_attr(cls, name):
21+
name, context = name.split('_', 1)
22+
if name in ["tag", "untag"]:
23+
return True
24+
25+
@classmethod
26+
def _get(cls, entity, name):
27+
name, context = name.split('_', 1)
28+
return TagHandler(entity, name, context)
29+
30+
@classmethod
31+
def _tag_collection(
32+
cls, collection_name, name, objects, untag=False):
33+
from intercom import Intercom
34+
collection = utils.resource_class_to_collection_name(cls)
35+
object_ids = []
36+
for obj in objects:
37+
if not hasattr(obj, 'keys'):
38+
obj = {'id': obj}
39+
if untag:
40+
obj['untag'] = True
41+
object_ids.append(obj)
42+
43+
params = {
44+
'name': name,
45+
collection_name: object_ids,
46+
}
47+
response = Intercom.post("/%s" % (collection), **params)
48+
return cls(**response)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import re
4+
5+
6+
class FindAllHandler():
7+
def __init__(self, entity, context):
8+
self.entity = entity
9+
self.context = context
10+
11+
def __call__(self, *args, **kwargs):
12+
return self.entity._find_all_for(
13+
self.context, *args, **kwargs)
14+
15+
16+
class TagFindAll():
17+
18+
find_matcher = re.compile(r'find_all_for_(\w+)')
19+
20+
@classmethod
21+
def handles_attr(cls, name):
22+
return cls.find_matcher.search(name) is not None
23+
24+
@classmethod
25+
def _get(cls, entity, name):
26+
match = cls.find_matcher.search(name)
27+
context = match.group(1)
28+
return FindAllHandler(entity, context)
29+
30+
@classmethod
31+
def _find_all_for(cls, taggable_type, **kwargs):
32+
params = {
33+
'taggable_type': taggable_type
34+
}
35+
res_id = kwargs.pop('id', None)
36+
if res_id:
37+
params['taggable_id'] = res_id
38+
params.update(kwargs)
39+
40+
return cls.find_all(**params)

intercom/tag.py

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,18 @@
11
# -*- coding: utf-8 -*-
22

3-
from intercom import utils
3+
import six
4+
45
from intercom.api_operations.all import All
56
from intercom.api_operations.count import Count
67
from intercom.api_operations.find import Find
78
from intercom.api_operations.find_all import FindAll
89
from intercom.api_operations.save import Save
10+
from intercom.generic_handlers.base_handler import BaseHandler
11+
from intercom.generic_handlers.tag import TagUntag
12+
from intercom.generic_handlers.tag_find_all import TagFindAll
913
from intercom.traits.api_resource import Resource
1014

1115

12-
class Tag(Resource, All, Count, Find, FindAll, Save):
13-
14-
@classmethod
15-
def _tag_collection(cls, name, collection_name, objects, untag=False):
16-
from intercom import Intercom
17-
collection = utils.resource_class_to_collection_name(cls)
18-
19-
object_ids = []
20-
for obj in objects:
21-
if not hasattr(obj, 'keys'):
22-
obj = {'id': obj}
23-
if untag:
24-
obj['untag'] = True
25-
object_ids.append(obj)
26-
27-
params = {
28-
'name': name,
29-
collection_name: object_ids,
30-
}
31-
response = Intercom.post("/%s" % (collection), **params)
32-
return cls(**response)
33-
34-
@classmethod
35-
def tag_users(cls, name, users):
36-
return cls._tag_collection(name, 'users', users)
37-
38-
@classmethod
39-
def untag_users(cls, name, users):
40-
return cls._tag_collection(name, 'users', users, untag=True)
41-
42-
@classmethod
43-
def tag_companies(cls, name, companies):
44-
return cls._tag_collection(name, 'companies', companies)
45-
46-
@classmethod
47-
def untag_companies(cls, name, companies):
48-
return cls._tag_collection(name, 'companies', companies, untag=True)
49-
50-
@classmethod
51-
def find_all_for_user(cls, **kwargs):
52-
return cls.find_all_for('user', **kwargs)
53-
54-
@classmethod
55-
def find_all_for_company(cls, **kwargs):
56-
return cls.find_all_for('company', **kwargs)
57-
58-
@classmethod
59-
def find_all_for(cls, taggable_type, **kwargs):
60-
params = {
61-
'taggable_type': taggable_type
62-
}
63-
res_id = kwargs.pop('id', None)
64-
if res_id:
65-
params['taggable_id'] = res_id
66-
params.update(kwargs)
67-
68-
return Tag.find_all(**params)
16+
@six.add_metaclass(BaseHandler)
17+
class Tag(Resource, All, Count, Find, FindAll, Save, TagUntag, TagFindAll):
18+
pass

0 commit comments

Comments
 (0)