本文整理汇总了Python中reviewboard.attachments.forms.UploadFileForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python UploadFileForm.is_valid方法的具体用法?Python UploadFileForm.is_valid怎么用?Python UploadFileForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reviewboard.attachments.forms.UploadFileForm
的用法示例。
在下文中一共展示了UploadFileForm.is_valid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_utf16_thumbnail
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def test_utf16_thumbnail(self):
"""Testing file attachment thumbnail generation for UTF-16 files"""
filename = os.path.join(os.path.dirname(__file__),
'testdata', 'utf-16.txt')
with open(filename) as f:
review_request = self.create_review_request(publish=True)
file = SimpleUploadedFile(f.name, f.read(),
content_type='text/plain;charset=utf-16le')
form = UploadFileForm(review_request, files={'path': file})
form.is_valid()
file_attachment = form.create()
self.assertEqual(file_attachment.thumbnail,
'<div class="file-thumbnail-clipped"><pre>'
'UTF-16le encoded sample plain-text file</pre>'
'<pre>\u203e\u203e\u203e\u203e\u203e\u203e'
'\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
'\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
'\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
'\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
'\u203e\u203e\u203e\u203e\u203e</pre><pre>'
'</pre><pre>Markus Kuhn [\u02c8ma\u02b3k\u028as'
' ku\u02d0n] <http://www.cl.cam.ac.uk/~mgk25/'
'> \u2014 2002-07-25</pre><pre></pre><pre>'
'</pre><pre>The ASCII compatible UTF-8 encoding '
'used in this plain-text file</pre><pre>is '
'defined in Unicode, ISO 10646-1, and RFC 2279.'
'</pre></div>')
示例2: test_utf16_thumbnail
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def test_utf16_thumbnail(self):
"""Testing file attachment thumbnail generation for UTF-16 files"""
filename = os.path.join(os.path.dirname(__file__), "testdata", "utf-16.txt")
with open(filename) as f:
review_request = self.create_review_request(publish=True)
file = SimpleUploadedFile(f.name, f.read(), content_type="text/plain;charset=utf-16le")
form = UploadFileForm(review_request, files={"path": file})
form.is_valid()
file_attachment = form.create()
self.assertEqual(
file_attachment.thumbnail,
'<div class="file-thumbnail"> <div class="file-thumbnail-clipp'
'ed"><pre>UTF-16le encoded sample plain-text file</pre><pre>'
"\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e"
"\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e"
"\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e"
"\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e</pre>"
"<pre></pre><pre>Markus Kuhn [\u02c8ma\u02b3k\u028as ku\u02d0"
"n] <http://www.cl.cam.ac.uk/~mgk25/> \u2014 2002-07-25"
"</pre><pre></pre><pre></pre><pre>The ASCII compatible UTF-8 "
"encoding used in this plain-text file</pre><pre>is defined "
"in Unicode, ISO 10646-1, and RFC 2279.</pre><pre></pre><pre>"
"</pre><pre>Using Unicode/UTF-8, you can write in emails and "
"source code things such as</pre><pre></pre><pre>Mathematics "
"and sciences:</pre><pre></pre><pre> \u222e E\u22c5da = Q, "
"n \u2192 \u221e, \u2211 f(i) = \u220f g(i), \u23a7\u23a1"
"\u239b\u250c\u2500\u2500\u2500\u2500\u2500\u2510\u239e\u23a4"
"\u23ab</pre><pre> "
" \u23aa\u23a2\u239c\u2502a\xb2+b\xb3 \u239f\u23a5\u23aa"
"</pre><pre> \u2200x\u2208</pre></div></div>",
)
示例3: test_upload_file_revisions
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def test_upload_file_revisions(self):
"""Testing uploading multiple revisions of a file"""
review_request = self.create_review_request(publish=True)
history = FileAttachmentHistory.objects.create(display_position=0)
review_request.file_attachment_histories.add(history)
file = self.make_uploaded_file()
# Add a file with the given history
form = UploadFileForm(review_request, data={"attachment_history": history.pk}, files={"path": file})
self.assertTrue(form.is_valid())
file_attachment = form.create()
history = FileAttachmentHistory.objects.get(pk=history.pk)
self.assertEqual(file_attachment.attachment_revision, 1)
self.assertEqual(history.latest_revision, 1)
self.assertEqual(history.display_position, 0)
review_request.get_draft().publish()
# Post an update
form = UploadFileForm(review_request, data={"attachment_history": history.pk}, files={"path": file})
self.assertTrue(form.is_valid())
file_attachment = form.create()
history = FileAttachmentHistory.objects.get(pk=history.pk)
self.assertEqual(file_attachment.attachment_revision, 2)
self.assertEqual(history.latest_revision, 2)
self.assertEqual(history.display_position, 0)
review_request.get_draft().publish()
# Post two updates without publishing the draft in between
form = UploadFileForm(review_request, data={"attachment_history": history.pk}, files={"path": file})
self.assertTrue(form.is_valid())
file_attachment = form.create()
history = FileAttachmentHistory.objects.get(pk=history.pk)
self.assertEqual(file_attachment.attachment_revision, 3)
self.assertEqual(history.latest_revision, 3)
self.assertEqual(history.display_position, 0)
form = UploadFileForm(review_request, data={"attachment_history": history.pk}, files={"path": file})
self.assertTrue(form.is_valid())
file_attachment = form.create()
history = FileAttachmentHistory.objects.get(pk=history.pk)
self.assertEqual(file_attachment.attachment_revision, 3)
self.assertEqual(history.latest_revision, 3)
self.assertEqual(history.display_position, 0)
# Add another (unrelated) file to check display position
form = UploadFileForm(review_request, files={"path": file})
self.assertTrue(form.is_valid())
file_attachment = form.create()
self.assertEqual(file_attachment.attachment_revision, 1)
self.assertEqual(file_attachment.attachment_history.latest_revision, 1)
self.assertEqual(file_attachment.attachment_history.display_position, 1)
示例4: setUp
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def setUp(self):
uploaded_file = SimpleUploadedFile("test.txt", b"<p>This is a test</p>", content_type="text/plain")
review_request = self.create_review_request(publish=True)
form = UploadFileForm(review_request, files={"path": uploaded_file})
self.assertTrue(form.is_valid())
示例5: test_upload_file
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def test_upload_file(self):
"""Testing uploading a file attachment."""
filename = os.path.join(settings.STATIC_ROOT, "rb", "images", "trophy.png")
f = open(filename, "r")
file = SimpleUploadedFile(f.name, f.read(), content_type="image/png")
f.close()
form = UploadFileForm(files={"path": file})
form.is_valid()
print form.errors
self.assertTrue(form.is_valid())
review_request = ReviewRequest.objects.get(pk=1)
file_attachment = form.create(file, review_request)
self.assertEqual(os.path.basename(file_attachment.file.name), "trophy.png")
self.assertEqual(file_attachment.mimetype, "image/png")
示例6: test_upload_file
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def test_upload_file(self):
"""Testing uploading a file attachment"""
review_request = self.create_review_request(publish=True)
file = self.make_uploaded_file()
form = UploadFileForm(review_request, files={"path": file})
self.assertTrue(form.is_valid())
file_attachment = form.create()
self.assertTrue(os.path.basename(file_attachment.file.name).endswith("__trophy.png"))
self.assertEqual(file_attachment.mimetype, "image/png")
示例7: update
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def update(self, request, *args, **kwargs):
try:
filediff = self.get_object(request, *args, **kwargs)
review_request_draft = filediff.diffset.review_request_draft.get()
except ObjectDoesNotExist:
return DOES_NOT_EXIST
if not review_request_draft.is_mutable_by(request.user):
return self._no_access_error(request.user)
if 'dest_attachment_file' in request.FILES:
if not filediff.binary:
return INVALID_FORM_DATA, {
'fields': {
'dest_attachment_file': [
'Cannot upload a file attachment to a '
'non-binary file in a diff.',
]
}
}
try:
# Check if there's already an attachment. If so, bail.
attachment = FileAttachment.objects.get_for_filediff(filediff)
return INVALID_FORM_DATA, {
'fields': {
'dest_attachment_file': [
'There is already a file attachment associated '
'with this binary file.',
]
}
}
except ObjectDoesNotExist:
pass
dest_attachment_file = request.FILES.get('dest_attachment_file')
form = UploadFileForm({}, {
'path': dest_attachment_file,
})
if not form.is_valid():
return INVALID_FORM_DATA, {
'fields': self._get_form_errors(form),
}
form.create(dest_attachment_file,
review_request_draft.review_request,
filediff)
return 200, {
self.item_result_key: filediff,
}
示例8: test_upload_file
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def test_upload_file(self):
"""Testing uploading a file attachment."""
filename = os.path.join(settings.HTDOCS_ROOT,
'media', 'rb', 'images', 'trophy.png')
f = open(filename, 'r')
file = SimpleUploadedFile(f.name, f.read(), content_type='image/png')
f.close()
form = UploadFileForm(files={
'path': file,
})
form.is_valid()
print form.errors
self.assertTrue(form.is_valid())
review_request = ReviewRequest.objects.get(pk=1)
file_attachment = form.create(file, review_request)
self.assertEqual(os.path.basename(file_attachment.file.name),
'trophy.png')
self.assertEqual(file_attachment.mimetype, 'image/png')
示例9: test_upload_file_with_history
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def test_upload_file_with_history(self):
"""Testing uploading a file attachment to an existing
FileAttachmentHistory
"""
review_request_1 = self.create_review_request(publish=True)
history = FileAttachmentHistory.objects.create(display_position=0)
review_request_1.file_attachment_histories.add(history)
file = self.make_uploaded_file()
form = UploadFileForm(review_request_1, data={"attachment_history": history.pk}, files={"path": file})
self.assertTrue(form.is_valid())
form.create()
示例10: test_upload_file_with_history_mismatch
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def test_upload_file_with_history_mismatch(self):
"""Testing uploading a file attachment to an existing
FileAttachmentHistory with a mismatched review request
"""
review_request_1 = self.create_review_request(publish=True)
review_request_2 = self.create_review_request(publish=True)
history = FileAttachmentHistory.objects.create(display_position=0)
review_request_1.file_attachment_histories.add(history)
form = UploadFileForm(review_request_2, data={"attachment_history": history.pk}, files={"path": file})
self.assertFalse(form.is_valid())
示例11: test_upload_file
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def test_upload_file(self):
"""Testing uploading a file attachment"""
file = self.make_uploaded_file()
form = UploadFileForm(files={
'path': file,
})
self.assertTrue(form.is_valid())
review_request = ReviewRequest.objects.get(pk=1)
file_attachment = form.create(file, review_request)
self.assertTrue(os.path.basename(file_attachment.file.name).endswith(
'__trophy.png'))
self.assertEqual(file_attachment.mimetype, 'image/png')
示例12: setUp
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def setUp(self):
uploaded_file = SimpleUploadedFile(
'test.txt',
b'<p>This is a test</p>',
content_type='text/plain')
review_request = self.create_review_request(publish=True)
form = UploadFileForm(review_request, files={
'path': uploaded_file,
})
self.assertTrue(form.is_valid())
self.file_attachment = form.create()
示例13: update
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def update(self, request, *args, **kwargs):
try:
filediff = self.get_object(request, *args, **kwargs)
review_request_draft = filediff.diffset.review_request_draft.get()
except ObjectDoesNotExist:
return DOES_NOT_EXIST
if not review_request_draft.is_mutable_by(request.user):
return self._no_access_error(request.user)
if "dest_attachment_file" in request.FILES:
if not filediff.binary:
return (
INVALID_FORM_DATA,
{
"fields": {
"dest_attachment_file": [
"Cannot upload a file attachment to a " "non-binary file in a diff."
]
}
},
)
try:
# Check if there's already an attachment. If so, bail.
attachment = FileAttachment.objects.get_for_filediff(filediff)
return (
INVALID_FORM_DATA,
{
"fields": {
"dest_attachment_file": [
"There is already a file attachment associated " "with this binary file."
]
}
},
)
except ObjectDoesNotExist:
pass
dest_attachment_file = request.FILES.get("dest_attachment_file")
form = UploadFileForm({}, {"path": dest_attachment_file})
if not form.is_valid():
return INVALID_FORM_DATA, {"fields": self._get_form_errors(form)}
form.create(dest_attachment_file, review_request_draft.review_request, filediff)
return 200, {self.item_result_key: filediff}
示例14: create
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def create(self, request, *args, **kwargs):
"""Creates a new file from a file attachment.
This accepts any file type and associates it with a draft of a
review request.
It is expected that the client will send the data as part of a
:mimetype:`multipart/form-data` mimetype. The file's name
and content should be stored in the ``path`` field. A typical request
may look like::
-- SoMe BoUnDaRy
Content-Disposition: form-data; name=path; filename="foo.zip"
<Content here>
-- SoMe BoUnDaRy --
"""
try:
review_request = \
resources.review_request.get_object(request, *args, **kwargs)
except ObjectDoesNotExist:
return DOES_NOT_EXIST
if not review_request.is_mutable_by(request.user):
return self.get_no_access_error(request)
form_data = request.POST.copy()
form = UploadFileForm(review_request, form_data, request.FILES)
if not form.is_valid():
return INVALID_FORM_DATA, {
'fields': self._get_form_errors(form),
}
try:
file = form.create()
except ValueError as e:
return INVALID_FORM_DATA, {
'fields': {
'path': [six.text_type(e)],
},
}
return 201, {
self.item_result_key: self.serialize_object(
file, request=request, *args, **kwargs),
}
示例15: update
# 需要导入模块: from reviewboard.attachments.forms import UploadFileForm [as 别名]
# 或者: from reviewboard.attachments.forms.UploadFileForm import is_valid [as 别名]
def update(self, request, extra_fields={}, *args, **kwargs):
"""Updates a per-file diff.
If this represents a binary file, then the contents of the binary
file can be uploaded before the review request is published.
Extra data can be stored for later lookup by passing
``extra_data.key_name=value``. The ``key_name`` and ``value`` can be
any valid strings. Passing a blank ``value`` will remove the key. The
``extra_data.`` prefix is required.
"""
try:
filediff = self.get_object(request, *args, **kwargs)
review_request_draft = filediff.diffset.review_request_draft.get()
except ObjectDoesNotExist:
return DOES_NOT_EXIST
if not review_request_draft.is_mutable_by(request.user):
return self._no_access_error(request.user)
if 'dest_attachment_file' in request.FILES:
if not filediff.binary:
return INVALID_FORM_DATA, {
'fields': {
'dest_attachment_file': [
'Cannot upload a file attachment to a '
'non-binary file in a diff.',
]
}
}
try:
# Check if there's already an attachment. If so, bail.
FileAttachment.objects.get_for_filediff(filediff)
return INVALID_FORM_DATA, {
'fields': {
'dest_attachment_file': [
'There is already a file attachment associated '
'with this binary file.',
]
}
}
except ObjectDoesNotExist:
pass
dest_attachment_file = request.FILES.get('dest_attachment_file')
form = UploadFileForm(review_request_draft.review_request, {}, {
'path': dest_attachment_file,
})
if not form.is_valid():
return INVALID_FORM_DATA, {
'fields': self._get_form_errors(form),
}
form.create(filediff)
if extra_fields:
self._import_extra_data(filediff.extra_data, extra_fields)
filediff.save(update_fields=['extra_data'])
return 200, {
self.item_result_key: filediff,
}