def test_saving_and_retrieving_items(self): list_ = List() list_.save() first_item = Item() first_item.text = 'the first list item' first_item.list = list_ first_item.save() second_item = Item() second_item.text = 'item the second' second_item.list = list_ second_item.save() saved_list = List.objects.all() self.assertEqual(saved_list.first(), list_) saved_items = Item.objects.all() self.assertEqual(saved_items.count(), 2) first_saved_item = saved_items[0] second_saved_item = saved_items[1] self.assertEqual(first_saved_item.list, list_) self.assertEqual(first_saved_item.text, 'the first list item') self.assertEqual(second_saved_item.text, 'item the second') self.assertEqual(second_saved_item.list, list_)
def save_li(self, child, is_upstream, parent_category, ls): # let's find the inside anchor link anchor = child.find('a') ul_childs = child.find_all('ul') if anchor: href = urlparse(anchor.attrs['href']) # to avoid `#` links if href.netloc == 'github.com': print(anchor, href, is_upstream) if is_upstream: ls = List(category=parent_category[0], github=anchor.attrs['href'], name=anchor.get_text(), slug=slugify(anchor.get_text())) ls.save() self.read_repo(str(anchor.attrs['href']) + '.git', ls) else: parsed_url = urlparse(anchor.attrs['href']) if parsed_url.path != '': Item(ls=ls, github=anchor.attrs['href'], name=anchor.get_text(), slug=slugify(parsed_url.path.split('/')[1] + '-' + anchor.get_text())).save() if len(ul_childs) > 0: for ul_child in ul_childs: self.save_ul(ul_child, is_upstream, anchor.get_text(), parent_category, ls)
def create_list(o): # A valid user obj must be passed along in order to create a list, and the user must be a active user. if hasattr(o, "user") and User.objects.filter(is_active=True, id=o.user.id).exists(): # obtain the slug, slugify the list's name if slug is not provided in the obj slug = o.slug if hasattr( o, "slug") and o.slug is not None else slugify(o.name) # check if the slug already exits, append an uuid if exists to make it unique while List.objects.filter(slug=slug).exists(): slug = slug + '-' + uuid.uuid4().hex[:8] listObj = List( name=o.name, slug=slug, curator=o.user, ) if hasattr(o, "description") and o.description is not None: listObj.description = o.description # add list properties to the newly created obj, if provided if hasattr(o, "properties") and o.properties is not None: for p in o.properties.items(): setattr(listObj, p[0], p[1]) if hasattr(o, "topic") and o.topic is not None: try: topicObj = Topic.objects.get(slug=o.topic) listObj.topic = topicObj except Topic.DoesNotExist: pass # save and write the obj to db listObj.save() # create items belonging to the list, if provided if o.items: items = [] for i in o.items: i.list = listObj itemCr = create_item(i) items.append((i.position, itemCr)) firstItem = save_positions(items) listObj.firstItem = firstItem listObj.save() return listObj raise Exception(AttributeError, "Valid user obj must be provided to create list.")
def test_save_and_retrieve_item_and_list(self): list_ = List() list_.save() first_item = Item() first_item.text = 'the first ever list item' first_item.listy = list_ first_item.save() second_item = Item() second_item.text = 'the second item' second_item.listy = list_ second_item.save() saved_list = List.objects.all() self.assertIn(list_, saved_list) self.assertEqual(first_item, Item.objects.all().first()) self.assertEqual(Item.objects.all()[0].listy, list_) self.assertEqual(second_item, Item.objects.all()[1]) self.assertEqual(second_item.listy, list_)