def obtain_expected_result(self, data_dict, obj, read=False): data = dict(data_dict) # convert id into hyperlink data['url'] = convert_id_to_hyperlink(self.detail_url, obj) if read: data['name'] = obj.name data['origin'] = obj.origin data['description'] = obj.description cats = obj.cats.all() if cats: # Convert related cats and homes id to hyperlinks cat_hyperlinks = [] home_hyperlinks = set() # Use set to avoid duplicate data for cat in cats: cat_hyperlinks.append( convert_id_to_hyperlink(vn.CAT_VIEW_DETAIL, cat)) home_hyperlinks.add( convert_id_to_hyperlink(vn.HOME_VIEW_DETAIL, cat.owner.home)) data['cats'] = cat_hyperlinks data['homes'] = home_hyperlinks data = self.sort_hyperlinks(data) else: # If there are no related cats, # the below fields should remain as empty list data['cats'] = [] data['homes'] = [] return data
def parse_obj(self, *dicts): for d in dicts: # convert FKs to hyperlinks d['breed'] = convert_id_to_hyperlink(vn.BREED_VIEW_DETAIL, d['breed']) d['owner'] = convert_id_to_hyperlink(vn.HUMAN_VIEW_DETAIL, d['owner']) # convert datetime object into str, default format(yyyy-mm-dd) d['date_of_birth'] = str(d['date_of_birth'])
def get_cat_obj_url(self, data: dict): # Add url (hyperlink) from the cat object id to the dict object cat_obj = Cat.objects.latest('pk') data['url'] = convert_id_to_hyperlink(vn.CAT_VIEW_DETAIL, cat_obj) # Convert related Breed, Cat, Home id to hyperlink data['breed'] = convert_id_to_hyperlink(vn.BREED_VIEW_DETAIL, cat_obj.breed) data['owner'] = convert_id_to_hyperlink(vn.HUMAN_VIEW_DETAIL, cat_obj.owner) data['home'] = convert_id_to_hyperlink(vn.HOME_VIEW_DETAIL, cat_obj.owner.home) return data
def get_human_obj_url(self, data: dict): # Add url (hyperlink) from the human object id to the dict object human_obj = Human.objects.latest('pk') data['url'] = convert_id_to_hyperlink(vn.HUMAN_VIEW_DETAIL, human_obj) try: data['cats'] = [] cats = Cat.objects.filter(owner=human_obj) for cat in cats: data['cats'].append( convert_id_to_hyperlink(vn.CAT_VIEW_DETAIL, cat) ) except Cat.DoesNotExist: data['cats'] = [] return data
def parse_cat_dict(self, data: dict): # Parse Human instance dict to API standards data['date_of_birth'] = str(data['date_of_birth']) # Convert related Breed, Human, Home id to hyperlink data['breed'] = convert_id_to_hyperlink( vn.BREED_VIEW_DETAIL, data['breed'] ) data['owner'] = convert_id_to_hyperlink( vn.HUMAN_VIEW_DETAIL, data['owner'] ) if data.get('home', None): data['home'] = convert_id_to_hyperlink( vn.HOME_VIEW_DETAIL, data['home'] ) return data
def parse_obj(self, *dicts): for d in dicts: # convert FK to hyperlink d['home'] = convert_id_to_hyperlink(vn.HOME_VIEW_DETAIL, d['home']) # convert datetime object into str, default format(yyyy-mm-dd) d['date_of_birth'] = str(d['date_of_birth'])
def get_breed_obj_url(self, data: dict): # Add url (hyperlink) from the breed object id to the dict object breed_obj = Breed.objects.latest('pk') data['url'] = convert_id_to_hyperlink(vn.BREED_VIEW_DETAIL, breed_obj) try: data['cats'] = [] data['homes'] = [] cats = Cat.objects.filter(breed=breed_obj) for cat in cats: data['cats'].append( convert_id_to_hyperlink(vn.CAT_VIEW_DETAIL, cat)) data['homes'].append( convert_id_to_hyperlink(vn.HOME_VIEW_DETAIL, cat.owner.home)) except Cat.DoesNotExist: data['cats'] = [] data['homes'] = [] return data
def obtain_expected_result(self, data_dict, obj, read=False): data = dict(data_dict) # convert id into hyperlink data['url'] = convert_id_to_hyperlink(self.detail_url, obj) if read: data['name'] = obj.name data['gender'] = obj.gender data['date_of_birth'] = str(obj.date_of_birth) data['description'] = obj.description # Convert related home id to hyperlink data['home'] = convert_id_to_hyperlink(vn.HOME_VIEW_DETAIL, obj.home) data['cats'] = [] for cat in obj.cats.all(): # Obtain all cats through Related Manager data['cats'].append( convert_id_to_hyperlink(vn.CAT_VIEW_DETAIL, cat)) data = self.sort_hyperlinks(data) return data
def obtain_expected_result(self, data_dict, obj, read=False): data = dict(data_dict) # convert id into hyperlink data['url'] = convert_id_to_hyperlink(self.detail_url, obj) if read: data['name'] = obj.name data['gender'] = obj.gender data['date_of_birth'] = str(obj.date_of_birth) data['description'] = obj.description # Convert related Breed, Human, Home id to hyperlink data['breed'] = convert_id_to_hyperlink( vn.BREED_VIEW_DETAIL, obj.breed ) data['owner'] = convert_id_to_hyperlink( vn.HUMAN_VIEW_DETAIL, obj.owner ) data['home'] = convert_id_to_hyperlink( vn.HOME_VIEW_DETAIL, obj.owner.home ) return data
def obtain_expected_result(self, data, obj, read=False): """ Convert Home object id into hyperlink for hyperlink related field Args: data (dict): data of the Home object. obj (Home): Home object instance. read (bool, optional): IF True copy the obj data to the data dict. Defaults to False. Returns: dict : Home object data with hyperlink related field """ # convert id into hyperlink data['url'] = convert_id_to_hyperlink(self.detail_url, obj) if read: data['name'] = obj.name data['address'] = obj.address data['hometype'] = obj.hometype return data
def parse_human_dict(self, data: dict): # Parse Human instance dict to API standards data['date_of_birth'] = str(data['date_of_birth']) data['home'] = convert_id_to_hyperlink(vn.HOME_VIEW_DETAIL, data['home']) return data
def get_home_obj_url(self, data: dict): # Add url (hyperlink) from the home object id to the dict object home_obj = Home.objects.latest('pk') data['url'] = convert_id_to_hyperlink(vn.HOME_VIEW_DETAIL, home_obj) return data