forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorganization.py
More file actions
140 lines (120 loc) · 4.69 KB
/
organization.py
File metadata and controls
140 lines (120 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""This module defines the Domain class, which models organizations monitored by Tracker
and offers methods to get data about them"""
import json
from slugify import slugify
import domain as dom
from formatting import format_name_summary
import queries
class Organization:
"""Class that represents an organization in Tracker
Attributes provide access to scalar fields for the organization in the GraphQL schema,
while methods return JSON data for non-scalar fields. Users should not typically
instantiate this class manually, instead use methods provided by
:class:`tracker_client.client.Client` to get Organizations.
The naming irregularity between parameters and attributes is to match
parameter names to the keys contained in the API responses. This allows easy
use of dict unpacking when creating an Organization instance. Attribute names instead
adhere to Python convention.
:param Client client: the :class:`tracker_client.client.Client` that created
this object. Provides a way for Organization methods to execute queries.
:param str name: full name of the organization.
:param str acronym: acronym for the organization.
:param str zone: the zone the organization belongs to.
:param str sector: the sector the organization belongs to.
:param str country: country the organization resides in.
:param str province: province the organization resides in.
:param str city: city the organization resides in.
:param bool verified: if the organization is verified or not.
:param int domainCount: number of domains controlled by the organization.
"""
def __init__(
self,
client,
name,
acronym,
zone,
sector,
country,
province,
city,
verified,
domainCount,
):
self.client = client
self.name = name
self.acronym = acronym
self.zone = zone
self.sector = sector
self.country = country
self.province = province
self.city = city
self.verified = verified
self.domain_count = domainCount
def get_summary(self):
"""Get summary metrics for this Organization
:return: formatted JSON data with summary metrics for an organization
:rtype: str
:Example:
>>> from tracker_client.client import Client
>>> client = Client()
>>> my_orgs = client.get_organizations()
>>> print(my_orgs[0].get_summary())
{
"FOO": {
"domainCount": 10,
"summaries": {
"web": {
"total": 10,
"categories": [
{
"name": "pass",
"count": 1,
"percentage": 10
},
{
"name": "fail",
"count": 9,
"percentage": 90
}
]
},
"mail": {
"total": 10,
"categories": [
{
"name": "pass",
"count": 5,
"percentage": 50
},
{
"name": "fail",
"count": 5,
"percentage": 50
}
]
}
}
}
}
"""
params = {"orgSlug": slugify(self.name)}
result = self.client.execute_query(queries.SUMMARY_BY_SLUG, params)
if "error" not in result:
result = format_name_summary(result)
return json.dumps(result, indent=4)
# Consider changing to generator
def get_domains(self):
"""Get a list of Domains controlled by this Organization
:return: list of :class:`tracker_client.domain.Domain`s controlled by
this Organization
:rtype: list[Domain]
"""
params = {"orgSlug": slugify(self.name)}
result = self.client.execute_query(queries.GET_ORG_DOMAINS, params)
if "error" in result:
print("Server error: ", result)
raise ValueError("Unable to get domains for " + self.name)
domain_list = []
for edge in result["findOrganizationBySlug"]["domains"]["edges"]:
domain_list.append(dom.Domain(self.client, **edge["node"]))
return domain_list