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_)
def test_create_returns_new_list_object(self): returned = List.create_new(first_item_text='new item text') new_list = List.objects.first() self.assertEqual(returned, new_list)
def test_create_new_optionally_saves_owner(self): user = User.objects.create(email="*****@*****.**") List.create_new(first_item_text='new item text', owner=user) new_list = List.objects.first() self.assertEqual(new_list.owner, user)
def test_create_new_creates_list_and_first_item(self): List.create_new(first_item_text='new item text') new_item = Item.objects.first() self.assertEqual(new_item.text, 'new item text') new_list = List.objects.first() self.assertEqual(new_item.list, new_list)
def save(self, owner): if owner.is_authenticated(): return List.create_new(first_item_text=self.cleaned_data['text'], owner=owner) else: return List.create_new(first_item_text=self.cleaned_data['text'])
def test_create_returns_new_list_object(self): returned = List.create_new(first_item_text={'new item text'}) new_list = List.objects.first() self.assertEqual(returned, new_list)
def test_create_new_optionally_saves_owner(self): user = User.objects.create() List.create_new(first_item_text='new item text', owner=user) new_list = List.objects.first() self.assertEqual(new_list.owner, user)