def build_categories(file_directory=''): """ Builds the Categories with tree from the xml file available in: http://simap.europa.eu/news/new-cpv/cpv_2008_xml.zip """ file_directory += 'cpv_2008.xml' tree = xml.etree.ElementTree.parse(file_directory) root = tree.getroot() data = {} for child in root: data['code'] = child.attrib['CODE'] data['description_en'] = child.findall("TEXT")[5].text data['description_pt'] = child.findall("TEXT")[18].text pure_code = data['code'][:-2] # which depth on the tree? depth = 1 while depth != 7: if pure_code[depth + 1] == '0': break depth += 1 if depth == 1: try: category = Category.objects.get(code=data['code']) assert depth == category.depth, 'category %s with different depth' % data['code'] except Category.DoesNotExist: Category.add_root(**data) logger.info('category %s added', data['code']) else: # we need to build the parent code: # we will use __startwith, dropping the "-#". s = list(pure_code) # turn string into a mutable object s[depth] = '0' # pick the parent code parent_pure_code = "".join(s) try: category = Category.objects.get(code=data['code']) assert depth == category.depth, 'category %s with different depth' % data['code'] assert category.get_parent() == Category.objects.get(code__startswith=parent_pure_code),\ 'category %s with different parent' % data['code'] except Category.DoesNotExist: try: parent_category = Category.objects.get(code__startswith=parent_pure_code) parent_category.add_child(**data) logger.info('category %s added', data['code']) except Category.DoesNotExist: logger.warning('category %s not added because lacks parent', data['code']) continue
def test_cache(self): cat = Category.add_root(code='45233141-9') e1 = Entity.objects.create(nif='506780902', base_id=5826, name='bla') e2 = Entity.objects.create(nif='506572218', base_id=101, name='bla1') e3 = Entity.objects.create(nif='dasda', base_id=21, name='bla2') e4 = Entity.objects.create(nif='dasda', base_id=22, name='bla2') c1 = Contract.objects.create(base_id=1, contract_description='da', price=200, added_date=datetime(year=2003, month=1, day=1), category=cat) c2 = Contract.objects.create(base_id=2, contract_description='da', price=100, added_date=datetime(year=2003, month=1, day=1), category=cat) c1.contractors.add(e1) c1.contracted.add(e3) c2.contractors.add(e2) c2.contracted.add(e4) call_command('cache_contracts', all=True) self.assertTrue(e1.data.is_updated)
def test_main(self): cat = Category.add_root(code='45233141-9') act_type = ActType.objects.create(base_id=1, name='bla') model_type = ModelType.objects.create(base_id=1, name='bla') t = Tender.objects.create(base_id=1, publication_date=datetime(year=2003, month=1, day=1), deadline_date=datetime(year=2003, month=1, day=5), price=100, act_type=act_type, model_type=model_type, category=cat) c = Contract.objects.create(base_id=1, contract_description='da', price=100, added_date=datetime(year=2003, month=1, day=1), category=cat) e1 = Entity.objects.create(name='test1', base_id=20, nif='nif') e2 = Entity.objects.create(name='test2', base_id=21, nif='nif') c.contractors.add(e1) c.contracted.add(e2) # main view response = self.client.get(reverse(category_main_view, args=(cat.id, ))) self.assertEqual(200, response.status_code) # contracts response = self.client.get(reverse(category_contracts, args=(cat.id, ))) self.assertEqual(c.base_id, response.context['contracts'][0].base_id) # contractors response = self.client.get( reverse(category_contractors, args=(cat.id, ))) self.assertEqual(e1.base_id, response.context['entities'][0].base_id) # contracted response = self.client.get( reverse(category_contracted, args=(cat.id, ))) self.assertEqual(e2.base_id, response.context['entities'][0].base_id) # tenders response = self.client.get(reverse(category_tenders, args=(cat.id, ))) self.assertEqual(t.base_id, response.context['tenders'][0].base_id)
def add_category(data): parent = _get_parent(data['code']) try: category = Category.objects.get(code=data['code']) except Category.DoesNotExist: if parent is None: category = Category.add_root(**data) else: category = parent.add_child(**data) return category
def setUp(self): self.cat = Category.add_root(code='45233141-9', description_pt='cat1') c = Contract.objects.create( base_id=1, contract_description='da', price=100, added_date=datetime(year=2004, month=1, day=1), signing_date=datetime(year=2004, month=1, day=1), category=self.cat) e1 = Entity.objects.create(name='test1', base_id=20, nif='nif') e2 = Entity.objects.create(name='test2', base_id=21, nif='nif') c.contractors.add(e1) c.contracted.add(e2)
def setUp(self): self.cat = Category.add_root(code='45233141-9', description_pt='cat1') act_type = ActType.objects.create(base_id=1, name='bla') model_type = ModelType.objects.create(base_id=1, name='bla') t = Tender.objects.create( base_id=1, publication_date=datetime(year=2003, month=1, day=1), deadline_date=datetime(year=2003, month=1, day=5), price=100, act_type=act_type, model_type=model_type, description='test', dre_url='http://www.example.com', category=self.cat ) e1 = Entity.objects.create(name='test1', base_id=20, nif='nif') t.contractors.add(e1)
def test_main(self): cat = Category.add_root(code='45233141-9') act_type = ActType.objects.create(base_id=1, name='bla') model_type = ModelType.objects.create(base_id=1, name='bla') t = Tender.objects.create( base_id=1, publication_date=datetime(year=2003, month=1, day=1), deadline_date=datetime(year=2003, month=1, day=5), price=100, act_type=act_type, model_type=model_type, category=cat ) c = Contract.objects.create(base_id=1, contract_description='da', price=100, added_date=datetime(year=2003, month=1, day=1), category=cat) e1 = Entity.objects.create(name='test1', base_id=20, nif='nif') e2 = Entity.objects.create(name='test2', base_id=21, nif='nif') c.contractors.add(e1) c.contracted.add(e2) # main view response = self.client.get(reverse(category_main_view, args=(cat.id,))) self.assertEqual(200, response.status_code) # contracts response = self.client.get(reverse(category_contracts, args=(cat.id,))) self.assertEqual(c.base_id, response.context['contracts'][0].base_id) # contractors response = self.client.get(reverse(category_contractors, args=(cat.id,))) self.assertEqual(e1.base_id, response.context['entities'][0].base_id) # contracted response = self.client.get(reverse(category_contracted, args=(cat.id,))) self.assertEqual(e2.base_id, response.context['entities'][0].base_id) # tenders response = self.client.get(reverse(category_tenders, args=(cat.id,))) self.assertEqual(t.base_id, response.context['tenders'][0].base_id)