Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow BuildableTemplateView to utilise reverse_lazy (prevent duplicatation of URLs, allow directory paths) #120

Closed
mattaustin opened this issue Jan 20, 2018 · 2 comments

Comments

@mattaustin
Copy link
Contributor

I've recently been using django-bakery on a project for the first time. There are a couple of non-model urls which should live in a directory path (e.g. '/terms-of-use/'), and need to be build in a similar way to how the BuildableDetailView does things.

I was also thinking that defining the url in urls.py, as well as on the view class seemed to duplicate things a little (I got caught out, where urls.py was updated, but not the build_path on the view).

I've been using the following mixin in my project. This is likely a little rough around the edges, and breaks when explicitly specifying a build_path such as index.html - but I thought it might prompt some discussion.

If this approach is considered a good idea, then I could continue with it (time permitting), and get it ready as a proper patch. Just wanted some feedback before I continued with it.

class ReversableBuildableTemplateMixin(object):

    def build(self):
        logger.debug('Building {}'.format(self.template_name))
        self.request = self.create_request(self.build_path)
        self.build_file(self.get_build_path(), self.get_content())

    def get_build_path(self):
        # Adapted from BuildableDetailView, so we can use a reversed (lazy)
        # url, rather than defining the url multiple times.
        target_path = path.join(settings.BUILD_DIR,
                                str(self.build_path).lstrip('/'))
        if not self.fs.exists(target_path):
            logger.debug('Creating {}'.format(target_path))
            self.fs.makedirs(target_path)
        return path.join(target_path, 'index.html')
class ExampleView(ReversableBuildableTemplateMixin, BuildableTemplateView):

    build_path = reverse_lazy('example_url_name')
urlpatterns = [
    path('example-path/', views.ExampleView.as_view(), name='example_url_name'),
]
@palewire
Copy link
Owner

palewire commented Jan 22, 2018

I like the idea of being able to provide a URL name to link things up. I've used something similar in some of my projects.

Could we do this without breaking any existing tests and use cases? If we can establish a unittest alongside this new feature, I'm all for including it.

Is it something you'd want to submit a pull request on?

@mattaustin mattaustin changed the title Allow BuildableTemplateView to utilise reverse_lazy (prevent duplicatation of URLs, allow directory paths) Allow BuildableTemplateView to utilise reverse_lazy (prevent duplicatation of URLs, allow directory paths) Mar 19, 2018
@mattaustin mattaustin changed the title Allow BuildableTemplateView to utilise reverse_lazy (prevent duplicatation of URLs, allow directory paths) Allow BuildableTemplateView to utilise reverse_lazy (prevent duplicatation of URLs, allow directory paths) Mar 19, 2018
@palewire
Copy link
Owner

palewire commented Dec 5, 2018

I think we have at least partial support for the feature proposed here. @mattaustin, if you want to take it further, let me know, but I'm closing the ticket for now. Thanks again for your patch.

@palewire palewire closed this as completed Dec 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants