Example #1
0
    def form_valid(self, form):
        """Checks for permission issues before validating the form as normal.
        """
        # Add author and founder information to the object.
        entity = form.save(commit=False)
        entity.owner = self.request.user
        entity.founder = self.request.user

        # If the user has permission to submit the entity, save it!
        if not has_submit_perms(self.request.user, entity):
            return self.error_page

        # Create and save an initial version.
        version = utility.create_entity_version(entity, self.request.user, first=True)

        entity.save()
        version.entity = entity
        version.save()

        # Now that the version has an id, re-save the entity.
        entity.active_version = version
        entity.save()

        # Set success_url to be based on the new entity and redirect.
        self.success_url = reverse("build_world:entity", kwargs={"etype": entity.etype, "pk": entity.id})
        return HttpResponseRedirect(self.success_url)
Example #2
0
    def form_valid(self, form):
        """Creates a version from the existing data. Grabs the latest inactive
        version that was created when the user loaded the Edit page (or craete
        one if none exists). Warns the user if other edits have been made since
        they began editing.
        """
        # Save the entity only once we know the editor/edits are legit.
        entity = form.save(commit=False)

        # Find the most recent inactive/open version and use it.
        open_versions = EntityVersion.objects.filter(entity=entity, user=self.request.user, active=False).order_by(
            "-id"
        )
        if open_versions:
            version = open_versions[0]
        else:
            # Otherwise, make a new version and assume that the user is editing
            # the latest version of the entity.
            # @TODO Warn the user somehow against clicking Back to editt or
            # using cached versions of the page.
            version = utility.create_entity_version(entity=entity, user=self.request.user)

        if version.modifies == entity.active_version:
            # @TODO If the entity's current version does not exist, simply save the new version and point the entity to it.
            if version.modifies == None:
                pass
            else:
                pass
            # Find the diff, fill out the version, and modify the entity.
            old_entity = copy.deepcopy(entity)
            old_entity = utility.build_entity_to_version(entity.active_version)
            version = old_entity.make_version_with_diffs(entity, version)
            try:
                version.version_num = entity.active_version.version_num + 1
            except AttributeError:
                version.version_num = 1
            version.active = True
            version.accepted = True
            version.edited = True
            version.save()

            # Only save the entity if there are no conflicts.
            entity.active_version = version
            entity.save()

            # Clean up inactive versions for the user.
            EntityVersion.objects.filter(active=False, user=self.request.user).delete()

            return HttpResponseRedirect(self.success_url)
        else:
            # Then somebody has modified it since you've started editing.
            return HttpResponse("versions not equal - someome tampered. Show conflicts!")
Example #3
0
    def dispatch(self, request, *args, **kwargs):
        # @TODO Find a way to move the success_url line to __init__. The kwargs seem to be available in dispatch, but not __init__.
        self.success_url = reverse("build_world:entity", kwargs={"etype": kwargs["etype"], "pk": kwargs["pk"]})

        entity = Entity.objects.get(id=kwargs["pk"])
        if not has_edit_perms(request.user, entity):
            return self.error_page

        # Create a new blank (ie. active=False) version if editing an entity.
        # @NOTE @TODO This does not work if someone loads a cached version of
        # the page! Eg. if someone hits back, dispatch() is not hit.
        if request.method == "GET":
            version = utility.create_entity_version(entity, request.user)
            version.save()

        return super(EntityUpdateView, self).dispatch(request, *args, **kwargs)