def create_from_upload(self, repository, diff_file, parent_diff_file, diffset_history, basedir, request, base_commit_id=None, save=True): """Create a DiffSet from a form upload. The diff_file and parent_diff_file parameters are django forms UploadedFile objects. """ siteconfig = SiteConfiguration.objects.get_current() max_diff_size = siteconfig.get('diffviewer_max_diff_size') if max_diff_size > 0: if diff_file.size > max_diff_size: raise DiffTooBigError(_('The supplied diff file is too large'), max_diff_size=max_diff_size) if parent_diff_file and parent_diff_file.size > max_diff_size: raise DiffTooBigError( _('The supplied parent diff file is too large'), max_diff_size=max_diff_size) if parent_diff_file: parent_diff_file_name = parent_diff_file.name parent_diff_file_contents = parent_diff_file.read() else: parent_diff_file_name = None parent_diff_file_contents = None return self.create_from_data(repository, diff_file.name, diff_file.read(), parent_diff_file_name, parent_diff_file_contents, diffset_history, basedir, request, base_commit_id=base_commit_id, save=save)
def create_from_upload(self, repository, diff_file, parent_diff_file=None, diffset_history=None, basedir=None, request=None, base_commit_id=None, validate_only=False, **kwargs): """Create a DiffSet from a form upload. This parses a diff and optional parent diff covering one or more files, validates, and constructs :py:class:`DiffSets <reviewboard.diffviewer.models.DiffSet>` and :py:class:`FileDiffs <reviewboard.diffviewer.models.FileDiff>` representing the diff. This can optionally validate the diff without saving anything to the database. In this case, no value will be returned. Instead, callers should take any result as success. Args: repository (reviewboard.scmtools.models.Repository): The repository the diff applies to. diff_file (django.core.files.uploadedfile.UploadedFile): The diff file uploaded in the form. parent_diff_file (django.core.files.uploadedfile.UploadedFile, optional): The parent diff file uploaded in the form. diffset_history (reviewboard.diffviewer.models.DiffSetHistory, optional): The history object to associate the DiffSet with. This is not required if using ``validate_only=True``. basedir (unicode, optional): The base directory to prepend to all file paths in the diff. request (django.http.HttpRequest, optional): The current HTTP request, if any. This will result in better logging. base_commit_id (unicode, optional): The ID of the commit that the diff is based upon. This is needed by some SCMs or hosting services to properly look up files, if the diffs represent blob IDs instead of commit IDs and the service doesn't support those lookups. validate_only (bool, optional): Whether to just validate and not save. If ``True``, then this won't populate the database at all and will return ``None`` upon success. This defaults to ``False``. Returns: reviewboard.diffviewer.models.DiffSet: The resulting DiffSet stored in the database, if processing succeeded and ``validate_only=False``. Raises: reviewboard.diffviewer.errors.DiffParserError: There was an error parsing the main diff or parent diff. reviewboard.diffviewer.errors.DiffTooBigError: The diff file was too big to be uploaded, based on the configured maximum diff size in settings. reviewboard.diffviewer.errors.EmptyDiffError: The provided diff file did not contain any file changes. reviewboard.scmtools.core.FileNotFoundError: A file specified in the diff could not be found in the repository. reviewboard.scmtools.core.SCMError: There was an error talking to the repository when validating the existence of a file. reviewboard.scmtools.git.ShortSHA1Error: A SHA1 specified in the diff was in the short form, which could not be used to look up the file. This is applicable only to Git. """ if 'save' in kwargs: warnings.warn( 'The save parameter to ' 'DiffSet.objects.create_from_upload is deprecated. ' 'Please set validate_only instead.', DeprecationWarning) validate_only = not kwargs['save'] siteconfig = SiteConfiguration.objects.get_current() max_diff_size = siteconfig.get('diffviewer_max_diff_size') if max_diff_size > 0: if diff_file.size > max_diff_size: raise DiffTooBigError(_('The supplied diff file is too large'), max_diff_size=max_diff_size) if parent_diff_file and parent_diff_file.size > max_diff_size: raise DiffTooBigError( _('The supplied parent diff file is too large'), max_diff_size=max_diff_size) if parent_diff_file: parent_diff_file_name = parent_diff_file.name parent_diff_file_contents = parent_diff_file.read() else: parent_diff_file_name = None parent_diff_file_contents = None return self.create_from_data( repository=repository, diff_file_name=diff_file.name, diff_file_contents=diff_file.read(), parent_diff_file_name=parent_diff_file_name, parent_diff_file_contents=parent_diff_file_contents, diffset_history=diffset_history, basedir=basedir, request=request, base_commit_id=base_commit_id, validate_only=validate_only)