예제 #1
0
    def get_version_date(self, instance):
        """
        Gets the datetime the instance was last modified.  This default
        implementation either uses the field specified by version_date_field
        or grabs a DateTimeField with auto_now=True.

        Override this method to customize how you want to determine the
        version date.
        """
        if hasattr(self, 'version_date_field'):
            return getattr(instance, self.version_date_field)

        # if using versioning, return most recent version date
        if is_versioned(instance):
            try:
                return get_versions(instance).most_recent().version_info.date
            except:
                return ''

        # no version_date_field specified, let's try to guess it
        date_fields = [field for field in instance._meta.fields
                        if isinstance(field, models.DateTimeField) and
                           field.auto_now]
        if date_fields:
            return getattr(instance, date_fields[0].name) or ''

        raise ValueError('MergeMixin cannot find a version_date_field')
예제 #2
0
    def clean(self):
        """
        Detects when the instance is newer than the one used to generate the
        form.  Calls merge() when there is a version conflict, and if there is
        still an error, updates the version date in the form to give the user
        a chance to save anyway.

        Returns
            cleaned_data.

        Raises:
            ValidationError: When there's a conflict when calling merge().
        """
        self.cleaned_data = super(self.__class__.__base__, self).clean()

        current_version_date = str(self.get_version_date(self.instance))
        form_version_date = self.data['version_date']
        if current_version_date != form_version_date:
            ancestor = None
            if is_versioned(self.instance) and form_version_date:
                ancestor_model = get_versions(self.instance).as_of(
                    form_version_date)
                ancestor = model_to_dict(ancestor_model)
            try:
                self.cleaned_data = self.merge(self.cleaned_data, self.initial,
                                               ancestor)
            except forms.ValidationError as e:
                self.data = self.data.copy()
                self.data['version_date'] = current_version_date
                raise e
            else:
                # Note that we performed a merge, for use elsewhere.
                self.performed_merge = True
        return self.cleaned_data
예제 #3
0
파일: views.py 프로젝트: schlos/localwiki
    def get_context_data(self, **kwargs):
        context = super(CompareView, self).get_context_data(**kwargs)

        if self.kwargs.get('date1'):
            # Using datetimes to display diff.
            date1 = self.kwargs.get('date1')
            date2 = self.kwargs.get('date2')
            # Query parameter list used in history compare view.
            dates = self.request.GET.getlist('date')
            if not dates:
                dates = [v for v in (date1, date2) if v]
            dates = [dateparser(v) for v in dates]
            old = min(dates)
            new = max(dates)
            new_version = get_versions(self.object).as_of(date=new)
            prev_version = new_version.version_info.version_number() - 1
            if len(dates) == 1 and prev_version > 0:
                old_version = get_versions(
                    self.object).as_of(version=prev_version)
            elif prev_version <= 0:
                old_version = None
            else:
                old_version = get_versions(self.object).as_of(date=old)
        else:
            # Using version numbers to display diff.
            version1 = self.kwargs.get('version1')
            version2 = self.kwargs.get('version2')
            # Query parameter list used in history compare view.
            versions = self.request.GET.getlist('version')
            if not versions:
                versions = [v for v in (version1, version2) if v]
            if not versions:
                raise Http404("Versions not specified")
            versions = [int(v) for v in versions]
            old = min(versions)
            new = max(versions)
            if len(versions) == 1:
                old = max(new - 1, 1)
            if old > 0:
                old_version = get_versions(self.object).as_of(version=old)
            else:
                old_version = None
            new_version = get_versions(self.object).as_of(version=new)

        context.update({'old': old_version, 'new': new_version})
        return context
예제 #4
0
    def get_context_data(self, **kwargs):
        context = super(CompareView, self).get_context_data(**kwargs)

        if self.kwargs.get('date1'):
            # Using datetimes to display diff.
            date1 = self.kwargs.get('date1')
            date2 = self.kwargs.get('date2')
            # Query parameter list used in history compare view.
            dates = self.request.GET.getlist('date')
            if not dates:
                dates = [v for v in (date1, date2) if v]
            dates = [dateparser(v) for v in dates]
            old = min(dates)
            new = max(dates)
            new_version = get_versions(self.object).as_of(date=new)
            prev_version = new_version.version_info.version_number() - 1
            if len(dates) == 1 and prev_version > 0:
                old_version = get_versions(self.object).as_of(
                    version=prev_version)
            elif prev_version <= 0:
                old_version = None
            else:
                old_version = get_versions(self.object).as_of(date=old)
        else:
            # Using version numbers to display diff.
            version1 = self.kwargs.get('version1')
            version2 = self.kwargs.get('version2')
            # Query parameter list used in history compare view.
            versions = self.request.GET.getlist('version')
            if not versions:
                versions = [v for v in (version1, version2) if v]
            if not versions:
                raise Http404("Versions not specified")
            versions = [int(v) for v in versions]
            old = min(versions)
            new = max(versions)
            if len(versions) == 1:
                old = max(new - 1, 1)
            if old > 0:
                old_version = get_versions(self.object).as_of(version=old)
            else:
                old_version = None
            new_version = get_versions(self.object).as_of(version=new)

        context.update({'old': old_version, 'new': new_version})
        return context