Example #1
0
def check_content(s):
    if not is_content(s):
        raise error.ValidationError('content')
Example #2
0
def check_title(s):
    if not is_title(s):
        raise error.ValidationError('title')
Example #3
0
def check_name(s):
    if not is_name(s):
        raise error.ValidationError('name')
Example #4
0
def check_category_name(s):
    if not is_id(s):
        raise error.ValidationError('category_name')
Example #5
0
def check_role(s):
    if not is_role(s):
        raise error.ValidationError('role')
Example #6
0
def check_mail(s):
    if not is_mail(s):
        raise error.ValidationError('mail')
Example #7
0
def check_domain_id(s):
    if not is_domain_id(s):
        raise error.ValidationError('domain_id')
Example #8
0
def check_lang(i):
    if not is_lang(i):
        raise error.ValidationError('lang')
Example #9
0
def check_password(s):
    if not is_password(s):
        raise error.ValidationError('password')
Example #10
0
def check_uid(s):
    if not is_uid(s):
        raise error.ValidationError('uid')
Example #11
0
def check_description(s):
    if not is_description(s):
        raise error.ValidationError('description')
Example #12
0
def check_string_pname(s):
  if not is_string_pname(s):
    raise error.ValidationError('pname')
Example #13
0
def check_domain_invitation_code(s):
  if not is_domain_invitation_code(s):
    raise error.ValidationError('invitation_code')
Example #14
0
def check_bulletin(s):
  if not is_bulletin(s):
    raise error.ValidationError('bulletin')
Example #15
0
def check_intro(s):
  if not is_intro(s):
    raise error.ValidationError('intro')
Example #16
0
async def handle_file_upload(self, form_fields=None, raise_error=True):
  """Handles file upload, fills form fields and returns file_id whose metadata.link is already increased."""
  reader = await self.request.multipart()
  try:
    # Check csrf token.
    if self.csrf_token:
      field = await reader.next()
      check_type_and_name(field, 'csrf_token')
      post_csrf_token = await field.read_chunk(len(self.csrf_token.encode()))
      if self.csrf_token.encode() != post_csrf_token:
        raise error.CsrfTokenError()

    # Read form fields.
    if form_fields:
      for k in form_fields:
        field = await reader.next()
        check_type_and_name(field, k)
        form_fields[k] = (await field.read_chunk(TEXT_FIELD_MAX_LENGTH)).decode()

    # Read file data.
    field = await reader.next()
    check_type_and_name(field, 'file')
    file_type = mimetypes.guess_type(field.filename)[0]
    if not file_type or not any(file_type.startswith(allowed_type)
                                for allowed_type in ALLOWED_MIMETYPE_PREFIX):
      raise error.FileTypeNotAllowedError('file', file_type)
    grid_in = None
    finally_delete = True
    try:
      grid_in = await fs.add(file_type)
      # Copy file data and check file length.
      size = 0
      chunk_size = max(field.chunk_size, 8192)
      chunk = await field.read_chunk(chunk_size)
      while chunk:
        size += len(chunk)
        if size > FILE_MAX_LENGTH:
          raise error.FileTooLongError('file')
        _, chunk = await asyncio.gather(grid_in.write(chunk), field.read_chunk(chunk_size))
      if chunk: # remaining
        await grid_in.write(chunk)
      if not size: # empty file
        raise error.ValidationError('file')
      await grid_in.close()
      # Deduplicate.
      file_id = await fs.link_by_md5(grid_in.md5, except_id=grid_in._id)
      if file_id:
        return file_id
      finally_delete = False
      return grid_in._id
    except:
      raise
    finally:
      if grid_in:
        await grid_in.close()
        if finally_delete:
          await fs.unlink(grid_in._id)
  except Exception:
    if raise_error:
      raise
    else:
      return None