本文整理汇总了Python中django.core.exceptions.ValidationError.status方法的典型用法代码示例。如果您正苦于以下问题:Python ValidationError.status方法的具体用法?Python ValidationError.status怎么用?Python ValidationError.status使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.exceptions.ValidationError
的用法示例。
在下文中一共展示了ValidationError.status方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _pages_from_json
# 需要导入模块: from django.core.exceptions import ValidationError [as 别名]
# 或者: from django.core.exceptions.ValidationError import status [as 别名]
def _pages_from_json(request, offering, data):
with django.db.transaction.atomic():
try:
data = data.decode('utf-8-sig')
except UnicodeDecodeError:
raise ValidationError("Bad UTF-8 data in file.")
try:
data = json.loads(data)
except ValueError as e:
raise ValidationError('JSON decoding error. Exception was: "' + str(e) + '"')
if not isinstance(data, dict):
raise ValidationError('Outer JSON data structure must be an object.')
if 'userid' not in data or 'token' not in data:
raise ValidationError('Outer JSON data object must contain keys "userid" and "token".')
if 'pages' not in data:
raise ValidationError('Outer JSON data object must contain keys "pages".')
if not isinstance(data['pages'], list):
raise ValidationError('Value for "pages" must be a list.')
try:
user = Person.objects.get(userid=data['userid'])
member = Member.objects.exclude(role='DROP').get(person=user, offering=offering)
except (Person.DoesNotExist, Member.DoesNotExist):
raise ValidationError('Person with that userid does not exist.')
if 'pages-token' not in user.config or user.config['pages-token'] != data['token']:
e = ValidationError('Could not validate authentication token.')
e.status = 403
raise e
# if we get this far, the user is authenticated and we can start processing the pages...
for i, pdata in enumerate(data['pages']):
if not isinstance(pdata, dict):
raise ValidationError('Page #%i entry structure must be an object.' % (i))
if 'label' not in pdata:
raise ValidationError('Page #%i entry does not have a "label".' % (i))
# handle changes to the Page object
pages = Page.objects.filter(offering=offering, label=pdata['label'])
if pages:
page = pages[0]
old_ver = page.current_version()
else:
page = Page(offering=offering, label=pdata['label'])
old_ver = None
# check write permissions
# mock the request object enough to satisfy _check_allowed()
class FakeRequest(object):
is_authenticated = True
fake_request = FakeRequest()
fake_request.user = FakeRequest()
fake_request.user.username = user.userid
if old_ver:
m = _check_allowed(fake_request, offering, page.can_write, page.editdate())
else:
m = _check_allowed(fake_request, offering, offering.page_creators())
if not m:
raise ValidationError('You can\'t edit page #%i.' % (i))
# handle Page attributes
if 'can_read' in pdata:
if type(pdata['can_read']) != str or pdata['can_read'] not in ACL_DESC:
raise ValidationError('Page #%i "can_read" value must be one of %s.'
% (i, ','.join(list(ACL_DESC.keys()))))
page.can_read = pdata['can_read']
if 'can_write' in pdata:
if type(pdata['can_write']) != str or pdata['can_write'] not in WRITE_ACL_DESC:
raise ValidationError('Page #%i "can_write" value must be one of %s.'
% (i, ','.join(list(WRITE_ACL_DESC.keys()))))
if m.role == 'STUD':
raise ValidationError('Page #%i: students can\'t change can_write value.' % (i))
page.can_write = pdata['can_write']
if 'new_label' in pdata:
if type(pdata['new_label']) != str:
raise ValidationError('Page #%i "new_label" value must be a string.' % (i))
if m.role == 'STUD':
raise ValidationError('Page #%i: students can\'t change label value.' % (i))
if Page.objects.filter(offering=offering, label=pdata['new_label']):
raise ValidationError('Page #%i: there is already a page with that "new_label".' % (i))
page.label = pdata['new_label']
page.save()
# handle PageVersion changes
ver = PageVersion(page=page, editor=member)
if 'title' in pdata:
if type(pdata['title']) != str:
raise ValidationError('Page #%i "title" value must be a string.' % (i))
#.........这里部分代码省略.........