Exemplo n.º 1
0
 def test_add_tag(self):
     file_ = self.root.append_file('That\'s How Strong My Love Is.mp3')
     tag = Tag(_id='Redding', name='Redding')
     tag.save()
     file_.tags.add(tag)
     file_.save()
     find = query_tag_file('Redding')['results']
     assert_equal(len(find), 1)
Exemplo n.º 2
0
 def test_add_tag(self):
     file_ = self.root.append_file('That\'s How Strong My Love Is.mp3')
     tag = Tag(_id='Redding')
     tag.save()
     file_.tags.append(tag)
     file_.save()
     find = query_tag_file('Redding')['results']
     assert_equal(len(find), 1)
Exemplo n.º 3
0
 def test_file_remove_tag(self):
     file = self.node_settings.get_root().append_file('Champion.mp3')
     tag = Tag(name='Graduation')
     tag.save()
     file.tags.add(tag)
     file.save()
     assert_in('Graduation', file.tags.values_list('name', flat=True))
     url = self.project.api_url_for('osfstorage_remove_tag', fid=file._id)
     self.app.delete_json(url, {'tag': 'Graduation'}, auth=self.user.auth)
     file.reload()
     assert_not_in('Graduation', file.tags.values_list('name', flat=True))
Exemplo n.º 4
0
    def test_file_add_tag_fail_doesnt_create_log(self, mock_log):
        file = self.node_settings.get_root().append_file('UltraLightBeam.mp3')
        tag = Tag(_id='The Life of Pablo')
        tag.save()
        file.tags.append(tag)
        file.save()
        url = self.project.api_url_for('osfstorage_add_tag', fid=file._id)
        res = self.app.post_json(url, {'tag': 'The Life of Pablo'}, auth=self.user.auth, expect_errors=True)

        assert_equal(res.status_code, 400)
        mock_log.assert_not_called()
Exemplo n.º 5
0
 def test_tag_the_same_tag(self):
     file = self.node_settings.get_root().append_file('Lie,Cheat,Steal.mp3')
     tag = Tag(_id='Run_the_Jewels')
     tag.save()
     file.tags.append(tag)
     file.save()
     assert_in('Run_the_Jewels', file.tags)
     url = self.project.api_url_for('osfstorage_add_tag', fid=file._id)
     res = self.app.post_json(url, {'tag': 'Run_the_Jewels'}, auth=self.user.auth, expect_errors=True)
     assert_equal(res.status_code, 400)
     assert_equal(res.json['status'], 'failure')
Exemplo n.º 6
0
 def test_file_remove_tag(self):
     file = self.node_settings.get_root().append_file('Champion.mp3')
     tag = Tag(_id='Graduation')
     tag.save()
     file.tags.append(tag)
     file.save()
     assert_in('Graduation', file.tags)
     url = self.project.api_url_for('osfstorage_remove_tag', fid=file._id)
     self.app.delete_json(url, {'tag': 'Graduation'}, auth=self.user.auth)
     file.reload()
     assert_not_in('Graduation', file.tags)
Exemplo n.º 7
0
    def test_file_add_tag_fail_doesnt_create_log(self, mock_log):
        file = self.node_settings.get_root().append_file('UltraLightBeam.mp3')
        tag = Tag(_id='The Life of Pablo')
        tag.save()
        file.tags.append(tag)
        file.save()
        url = self.project.api_url_for('osfstorage_add_tag', fid=file._id)
        res = self.app.post_json(url, {'tag': 'The Life of Pablo'}, auth=self.user.auth, expect_errors=True)

        assert_equal(res.status_code, 400)
        mock_log.assert_not_called()
Exemplo n.º 8
0
 def test_tag_the_same_tag(self):
     file = self.node_settings.get_root().append_file('Lie,Cheat,Steal.mp3')
     tag = Tag(_id='Run_the_Jewels')
     tag.save()
     file.tags.append(tag)
     file.save()
     assert_in('Run_the_Jewels', file.tags)
     url = self.project.api_url_for('osfstorage_add_tag', fid=file._id)
     res = self.app.post_json(url, {'tag': 'Run_the_Jewels'}, auth=self.user.auth, expect_errors=True)
     assert_equal(res.status_code, 400)
     assert_equal(res.json['status'], 'failure')
Exemplo n.º 9
0
 def test_remove_tag(self):
     file_ = self.root.append_file('I\'ve Been Loving You Too Long.mp3')
     tag = Tag(_id='Blue', name='Blue')
     tag.save()
     file_.tags.add(tag)
     file_.save()
     find = query_tag_file('Blue')['results']
     assert_equal(len(find), 1)
     file_.tags.remove(tag)
     file_.save()
     find = query_tag_file('Blue')['results']
     assert_equal(len(find), 0)
Exemplo n.º 10
0
    def test_file_remove_tag_creates_log(self):
        file = self.node_settings.get_root().append_file('Formation.flac')
        tag = Tag(_id='You that when you cause all this conversation')
        tag.save()
        file.tags.append(tag)
        file.save()
        url = self.project.api_url_for('osfstorage_remove_tag', fid=file._id)
        res = self.app.delete_json(url, {'tag': 'You that when you cause all this conversation'}, auth=self.user.auth)

        assert_equal(res.status_code, 200)
        self.node.reload()
        assert_equal(self.node.logs[-1].action, 'file_tag_removed')
Exemplo n.º 11
0
 def test_remove_tag(self):
     file_ = self.root.append_file('I\'ve Been Loving You Too Long.mp3')
     tag = Tag(_id='Blue')
     tag.save()
     file_.tags.append(tag)
     file_.save()
     find = query_tag_file('Blue')['results']
     assert_equal(len(find), 1)
     file_.tags.remove('Blue')
     file_.save()
     find = query_tag_file('Blue')['results']
     assert_equal(len(find), 0)
Exemplo n.º 12
0
    def test_file_remove_tag_creates_log(self):
        file = self.node_settings.get_root().append_file('Formation.flac')
        tag = Tag(_id='You that when you cause all this conversation')
        tag.save()
        file.tags.append(tag)
        file.save()
        url = self.project.api_url_for('osfstorage_remove_tag', fid=file._id)
        res = self.app.delete_json(url, {'tag': 'You that when you cause all this conversation'}, auth=self.user.auth)

        assert_equal(res.status_code, 200)
        self.node.reload()
        assert_equal(self.node.logs[-1].action, 'file_tag_removed')
Exemplo n.º 13
0
 def add_tag(self, tag, auth, save=True, log=True):
     from website.models import Tag, NodeLog  # Prevent import error
     if tag not in self.tags and not self.node.is_registration:
         new_tag = Tag.load(tag)
         if not new_tag:
             new_tag = Tag(_id=tag)
         new_tag.save()
         self.tags.append(new_tag)
         if log:
             self.add_tag_log(NodeLog.FILE_TAG_ADDED, tag, auth)
         if save:
             self.save()
         return True
     return False
Exemplo n.º 14
0
 def add_tag(self, tag, auth, save=True, log=True):
     from website.models import Tag, NodeLog  # Prevent import error
     if tag not in self.tags and not self.node.is_registration:
         new_tag = Tag.load(tag)
         if not new_tag:
             new_tag = Tag(_id=tag)
         new_tag.save()
         self.tags.append(new_tag)
         if log:
             self.add_tag_log(NodeLog.FILE_TAG_ADDED, tag, auth)
         if save:
             self.save()
         return True
     return False
Exemplo n.º 15
0
    def parse(self):
        raw_post = random.choice(Post.objects.filter(is_raw = True))
        page = self.get_html(raw_post.original_url)
        soup = BeautifulSoup(page,"html.parser")
        content =str(soup.find("div", class_="entry-content"))
        if content == "None" or not content:
            raw_post.delete()
            return
        raw_post.html = content
        results = soup.findAll("a", {"rel" : "category tag"})

        tags = [ ( r['href'].split('/')[-2:-1][0], r.contents[0] ) for r in results]
        # for r in results:
        #     tags.append( (r['href'].split('/')[-2:-1][0], r.contents[0]) )

        images = soup.findAll("img")
        raw_post.first_image = images[1]['src']
        raw_post.first_text = str(soup.findAll("p")[0].contents[0])

        raw_post.title = self.reg_find(r"<h1 class=\"entry-title\">(.+?)</h1>", page)[0]
        
        same_post = Post.objects.filter(title = raw_post.title, is_raw = False).first()
        if same_post:
            same_post.delete()

        post_date = self.reg_find(r">.*(\d\d\.\d\d\.\d\d\d\d)</time>", page)[0]
        post_date = post_date.replace('.','')
        #post_date = datetime.datetime.strptime(post_date, "%dd%mm%YYYY").date()
        
        format_str = '%d%m%Y' # The format
        raw_post.post_date = datetime.datetime.strptime(post_date, format_str)

        author = self.reg_find(r"class=\"url fn n\">(.+?)</a>", page)[0]
        a = Author.objects.filter(name = author)
        if a:
            a = a[0]
        else:
            a = Author(name = author)
            a.save()
        raw_post.author = a            

        existed_tags = Tag.objects.filter(name__in = [t[0] for t in tags])
        for t in tags:
            if not t[0] in [et.name for et in  existed_tags]:
                new_tag = Tag(name =t[0], value = t[1])
                new_tag.save()

        post_tags = Tag.objects.filter(name__in = [t[0] for t in tags])
        
        # post = Post(
        #     title = title, 
        #     html = content,  
        #     author =a, 
        #     original_url = url, 
        #     post_date = post_date, 
        #     first_image = first_image, 
        #     first_text=first_p)

        # post.save()
        # post.tags.set(post_tags)
        raw_post.is_raw = False
        raw_post.save()
        raw_post.tags.set(post_tags)

        print("post %s saved" % str(raw_post))