本文整理汇总了Python中papers.models.Paper.get_file_dir方法的典型用法代码示例。如果您正苦于以下问题:Python Paper.get_file_dir方法的具体用法?Python Paper.get_file_dir怎么用?Python Paper.get_file_dir使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类papers.models.Paper
的用法示例。
在下文中一共展示了Paper.get_file_dir方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CrocodocTests
# 需要导入模块: from papers.models import Paper [as 别名]
# 或者: from papers.models.Paper import get_file_dir [as 别名]
class CrocodocTests(TestCase):
'''
NOTE: At the same time, we end up testing some of the Crocodoc model methods.
It may be a good idea to just test the task here without going through the
Crocodoc model, but there is just so much overlap.
'''
def setUp(self):
#Bypass the celery daemon and directly test synchronously.
settings.CELERY_ALWAYS_EAGER = True
#Set Crocodoc upload method to POST
settings.CROCODOC_UPLOAD_METHOD = 'post'
#We want to mock the crocodoc API library so that our tests don't have
#to actually issue HTTP requests.
self.patcher = mock.patch('crocodoc.Crocodoc')
Mock = self.patcher.start()
self.crocodoc_instance = Mock.return_value
self.crocodoc_instance.upload.return_value = {'shortId': 'yQZpPm',
'uuid': '8e5b0721-26c4-11df-b354-002170de47d3'}
self.crocodoc_instance.get_session.return_value = {'sessionId':
'fgH9qWEwnsJUeB0'}
self.crocodoc_instance.delete.return_value = True
#Create and login test user.
self.user = User.objects.create_user('test', '[email protected]', 'test')
self.client.login(username = 'test', password = 'test')
#Create a new paper entry with associated file.
upload_filename = 'blank.pdf'
self.data = {'user': self.user, 'title': 'Test Title', 'url':
'http://example.com', 'journal': 'Journal of Test', 'year':
'2011', 'volume': '1', 'authors':
"Author One\nAuthor Two\nAuthor Three", 'issue': '2', 'pages':
'3-4', 'file': upload_filename }
self.p = Paper(**self.data) #unpack dictionary to arguments
self.p.save()
#uploading to crocodoc will fail here since the file does not exist (we
#add it below).
#Now let's copy the dummy upload file from the test files location path
#to the uploaded file location
to_path = self.p.get_file_dir()
from_path = os.path.join(settings.SITE_ROOT, 'papers', 'tests',
'files')
os.makedirs(to_path, mode = 0755)
shutil.copy2(os.path.join(from_path, upload_filename), to_path)
self.uploaded_file = os.path.join(to_path, upload_filename)
#We should re-save the Paper here to automatically generate thumbnail
#and upload to crocodoc. But we don't since we are testing the
#uploading to crocodoc.
def tearDown(self):
#Delete the crocodoc uploaded paper
try:
self.p.crocodoc.delete()
except AssertionError:
#If we try to delete an already deleted object (like when we test
#delete), we need to catch this error so that the test doesn't
#fail.
pass
#Remove the user's upload directory recursively. This also gets rid of
#any test uploads.
path = os.path.join(settings.UPLOAD_ROOT, self.user.username)
shutil.rmtree(path)
#Stop patching process
self.patcher.stop()
def test_crocodoc_upload_paper_post(self):
'''
Call the upload task on the new paper. The task should return
True and short_id and uuid of the paper should be set.
NOTE: We are using the POST method of uploading papers because there
is not an easy way of testing the url method of uploading.
NOTE: This test may not be very necessary since the act of saving
the file above automatically uploads the paper to crocodoc.
'''
r = self.p.crocodoc.upload()
self.assertTrue(r.get()) #wait until task is done and get result
#Need to refresh our object because of the upload.
self.p = Paper.objects.get(pk = self.p.pk)
self.assertTrue(len(self.p.crocodoc.short_id) > 4)
self.assertTrue(len(self.p.crocodoc.uuid) == 36)
#And that we also have a session_id
self.assertTrue(len(self.p.crocodoc.session_id) == 15)
def test_crocodoc_get_session_id(self):
'''
Call the get_session_id task on the new paper. The task should return
True and the session_id should be different.
'''
#.........这里部分代码省略.........
示例2: GenerateThumbnailTest
# 需要导入模块: from papers.models import Paper [as 别名]
# 或者: from papers.models.Paper import get_file_dir [as 别名]
class GenerateThumbnailTest(TestCase):
def setUp(self):
#Bypass the celery daemon and directly test synchronously.
settings.CELERY_ALWAYS_EAGER = True
#Set Crocodoc upload method to POST
settings.CROCODOC_UPLOAD_METHOD = 'post'
#We want to mock the crocodoc API library so that our tests don't have
#to actually issue HTTP requests.
self.patcher = mock.patch('crocodoc.Crocodoc')
Mock = self.patcher.start()
self.crocodoc_instance = Mock.return_value
self.crocodoc_instance.upload.return_value = {'shortId': 'yQZpPm',
'uuid': '8e5b0721-26c4-11df-b354-002170de47d3'}
self.crocodoc_instance.get_session.return_value = {'sessionId':
'fgH9qWEwnsJUeB0'}
self.crocodoc_instance.delete.return_value = True
#Create and login test user.
self.user = User.objects.create_user('test', '[email protected]', 'test')
self.client.login(username = 'test', password = 'test')
#Create a new paper entry with associated file.
upload_filename = 'blank.pdf'
self.data = {'user': self.user, 'title': 'Test Title', 'url':
'http://example.com', 'journal': 'Journal of Test', 'year':
'2011', 'volume': '1', 'authors':
"Author One\nAuthor Two\nAuthor Three", 'issue': '2', 'pages':
'3-4', 'file': upload_filename }
self.p = Paper(**self.data) #unpack dictionary to arguments
self.p.save()
#Now let's copy the dummy upload file from the test files location path
#to the uploaded file location
to_path = self.p.get_file_dir()
from_path = os.path.join(settings.SITE_ROOT, 'papers', 'tests',
'files')
os.makedirs(to_path, mode = 0755)
shutil.copy2(os.path.join(from_path, upload_filename), to_path)
self.uploaded_file = os.path.join(to_path, upload_filename)
def tearDown(self):
#Remove the user's upload directory recursively. This also gets rid of
#any test uploads.
path = os.path.join(settings.UPLOAD_ROOT, self.user.username)
shutil.rmtree(path)
#Stop patching process
self.patcher.stop()
def test_generate_thumbnail(self):
'''
Call the generate thumbnail task on the new paper. The task should return
True and the thumbnail should be generated.
'''
r = tasks.generate_paper_thumbnail.delay(self.p)
self.assertTrue(r.get()) #wait until task is done and get result
#Refresh paper object
self.p = Paper.objects.get(pk = self.p.pk)
self.assertTrue(self.p.has_thumbnail)
paper_dir = self.p.get_file_dir()
thumbnail_file = os.path.join(paper_dir, settings.THUMBNAIL_FILENAME % self.p.hash)
self.assertTrue(os.path.exists(thumbnail_file))