Giter Club home page Giter Club logo

Comments (3)

etianen avatar etianen commented on June 2, 2024

I don't mind tweaking this so long as we don't break existing functionality.

Here's the lifecycle:

  1. User views the preview: GET -> 200
  2. Use performs the rollback POST -> 302

At what point is your 302 redirect given? Is is before the preview, or after the rollback?

from django-reversion.

julianklotz avatar julianklotz commented on June 2, 2024

Sorry, my bad. Fixed it a few days ago. But here’s some context and the solution. No need for changes from your side.

# models.py
class Media:


    class TypeChoices:
        PHOTO = 'PH', 'photo'
        VIDEO = 'VI', 'video'

    proxy_map = {
        TypeChoices.PHOTO: "Photo",
        TypeChoices.VIDEO: "Video",
    }


    type = fields.CharField(choices=TypeChoices.choices, max_length=20, db_index=True)
    file = fields.FileField(...)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._cast_to_proxy()
    def get_edit_url(self):
        # Build a dynamic edit url here
        return "the-url"
 
    def _cast_to_proxy(self):
        if not self.type:
            return

        proxy_model_name = self.proxy_map.get(self.type, None)

        if not proxy_model_name:
            return

        proxy_model = apps.get_model(self._meta.app_label, proxy_model_name)
        self.__class__ = proxy_model

class Photo(Media):
    class Meta:
        proxy = True

class Video(Media):
    class Meta:
        proxy = True

# admin.py

class MediaAdmin:
    def render_change_form(
        self, request, context, add=False, change=False, form_url="", obj=None
    ):
        """
        In some cases, an admin is only registered e.g. to provide auto complete,
        but editing is done through proxy models (and their admin classes).
        """

        # Revert is set when restoring object through the model admin.
        # When the revert flag is set, don't mess around with the redirects.
        revert = context.get("revert", False) # => This is how I fixed it.

        if obj is not None and revert is False:
            # Without the revert check, this would cause an exception, because reversion expects a 20x, not a redirect (30x)
            edit_url = obj.get_edit_url()

            if not request.path.startswith(edit_url):
                return HttpResponseRedirect(
                    f"{obj.get_edit_url()}?{request.GET.urlencode()}"
                )

        return super().render_change_form(request, context, add, change, form_url, obj)

from django-reversion.

etianen avatar etianen commented on June 2, 2024

Thanks for the context! Glad you sorted it yourself!

from django-reversion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.