Commit 4d7756f4 authored by Jo Sama's avatar Jo Sama 😱
Browse files

Add support for departments and budget holders

parent cedca197
......@@ -5,7 +5,8 @@ import uuid
from typing import Optional, List, Iterator
from urllib.parse import urljoin, urlparse
from .models import Branch, BranchReference, Operator, Person
from .models import (Branch, BranchReference, Operator, Person,
Department, DepartmentRef, BudgetHolder, BudgetHolderRef)
logger = logging.getLogger(__name__)
......@@ -23,6 +24,12 @@ class Endpoints:
def get_branch(self, branch_id):
return urljoin(self._prepend_base_url('branches/id/'), branch_id)
def get_departments(self):
return self._prepend_base_url('departments')
def get_budget_holder(self):
return self._prepend_base_url('budgetholders')
def get_operator(self, identity):
return urljoin(self._prepend_base_url('operators/id/'), identity)
......@@ -41,7 +48,7 @@ class TopDeskClient:
url,
username,
password,
headers=None,
headers={},
rewrite_url=None):
self.urls = Endpoints(url)
self.rewrite_url = rewrite_url
......@@ -100,6 +107,58 @@ class TopDeskClient:
return
params['$fields'] = ','.join(fields)
def create_department(self, department):
url = self.urls.get_departments()
r = self.post(url,
return_response=True,
headers={'Content-Type': 'application/json'},
data=department.json())
if r.status_code == 201:
return DepartmentRef.from_json(r.content)
else:
r.raise_for_status()
return None
def get_department(self, department):
url = self.urls.get_departments()
r = self.get(url, return_response=True)
if r.status_code == 200:
for x in r.json():
if x.get('name') == department:
return DepartmentRef.from_dict(x)
return None
elif r.status_code in (404, 500):
return None
else:
r.raise_for_status()
return None
def create_budget_holder(self, budget_holder):
url = self.urls.get_budget_holder()
r = self.post(url,
return_response=True,
headers={'Content-Type': 'application/json'},
data=budget_holder.json())
if r.status_code == 201:
return BudgetHolderRef.from_json(r.content)
else:
r.raise_for_status()
return None
def get_budget_holder(self, budget_holder):
url = self.urls.get_budget_holder()
r = self.get(url, return_response=True)
if r.status_code == 200:
for x in r.json():
if x.get('name') == budget_holder:
return BudgetHolderRef.from_dict(x)
return None
elif r.status_code == 404:
return None
else:
r.raise_for_status()
return None
def get_operator(self, identity):
def _get_operator_by_id(ident):
url = self.urls.get_operator(ident)
......
......@@ -3,7 +3,7 @@ import datetime
import json
import pydantic
from typing import Optional
from typing import Optional, List
class BaseModel(pydantic.BaseModel):
......@@ -190,15 +190,50 @@ class Operator(BaseModel):
return super(Operator, self).dict(*args, **kwargs)
class ExternalLink(BaseModel):
id: str
type: str
date: str = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
class Department(BaseModel):
id: Optional[str]
name: str
externalLinks: List[ExternalLink] = []
def dict(self, *args, **kwargs):
kwargs['exclude'] = {'id'}
return super().dict(*args, **kwargs)
class DepartmentRef(BaseModel):
id: str
name: str
def dict(self, *args, **kwargs):
kwargs['include'] = {'id'}
return super().dict(*args, **kwargs)
class BudgetHolder(BaseModel):
id: Optional[str]
name: str
externalLink: List[ExternalLink] = []
def dict(self, *args, **kwargs):
kwargs['exclude'] = {'id'}
return super().dict(*args, **kwargs)
class BudgetHolderRef(BaseModel):
id: str
name: str
def dict(self, *args, **kwargs):
kwargs['include'] = {'id'}
return super().dict(*args, **kwargs)
class Person(BaseModel):
id: Optional[str]
first_name: str
......@@ -212,8 +247,8 @@ class Person(BaseModel):
# TODO: Remove the password field when proper auth is in place
password: Optional[str]
department: Optional[Department]
budgetHolder: Optional[BudgetHolder]
department: Optional[DepartmentRef]
budgetHolder: Optional[BudgetHolderRef]
jobTitle: Optional[str]
class Config:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment