def test_add_tag(self): self._cleanup() url = reverse('api_tags_create') data = {'name': 'test'} response = self.c.post(url, json.dumps(data), content_type='application/json') response = json.loads(response.content.decode('utf-8')) assert response['tag']['name'] == 'test' self._cleanup() group_id = tag_groups_model.get_or_create_by_name(name='testgroup') group = tag_groups_model.get_by_id(group_id) group_dict = dict_from_cursor(group, keys=['_id', 'name']) data = {'name': 'test', 'group': group_dict} response = self.c.post(url, json.dumps(data), content_type='application/json') response = json.loads(response.content.decode('utf-8')) assert response['tag']['name'] == 'test' assert response['tag']['group']['id'] == str(group['_id'])
def get(self, request): tags = tags_model.get_all() filtered_tags = [] for tag in tags: data = dict_from_cursor(data=tag, keys=['name', '_id']) group = tag.get('group') if group: data['group'] = dict_from_cursor(data=group, keys=['name', '_id']) filtered_tags.append(data) filtered_tags.sort(key=lambda e: e.get('group', {}).get('name', '')) status = settings.API_RESULTS['ok'] return Response({'status': status, 'tags': filtered_tags})
def post(self, request): data = request.data name = data.get('name') group_id = data.get('group', {}).get('id') tag_id = tags_model.get_or_create_by_name(name=name) if group_id: tags_model.update({'group_id': group_id}, tag_id) new_tag = tags_model.get_by_id(tag_id) filtered_tag = dict_from_cursor(data=new_tag, keys=['name', '_id']) group = new_tag.get('group') if group != None: filtered_tag['group'] = dict_from_cursor(data=group, keys=['name', '_id']) status = settings.API_RESULTS['ok'] return Response({'status': status, 'tag': filtered_tag})
def get(self, request): groups = tag_groups_model.get_all() groups_list = [] for g in groups: group = dict_from_cursor(data=g, keys=['name', '_id']) groups_list.append(group) status = settings.API_RESULTS['ok'] return Response({'status': status, 'groups': groups_list})
def post(self, request): data = request.data name = data.get('name') _id = tag_groups_model.get_or_create_by_name(name=name) new_group = tag_groups_model.get_by_id(_id) new_group_dict = dict_from_cursor(data=new_group, keys=['name', '_id']) status = settings.API_RESULTS['ok'] return Response({'status': status, 'group': new_group_dict})