Giter Club home page Giter Club logo

Comments (4)

fabiocaccamo avatar fabiocaccamo commented on September 26, 2024

@kennylajara as explained in #242, it should be relatively easy to achieve this by:

  1. extending ThemeQuerySet
  2. overriding get_active method (here you decide the theme to return)
  3. setting the customized queryset as default objects manager for the Theme model.

If you will come back with a reusable solution I will be glad to look at it!

from django-admin-interface.

kennylajara avatar kennylajara commented on September 26, 2024

I can think on using something like this to determine if the Site framework is being used:

from django.conf import settings

def is_sites_app_installed():
    if 'django.contrib.sites' in settings.INSTALLED_APPS:
        try:
            from django.contrib.sites.models import Site
            return True
        except ImportError:
            return False
    return False

Extend the Theme model to add this property:

    site = models.PositiveSmallIntegerField(  # it can be a FK too
        blank=True,
        default=1,
        verbose_name=_("site"),
    )

And change the QuerySet to this:

class ThemeQuerySet(models.QuerySet):
    def get_active(self):
        using_sites_framework = is_sites_app_installed()

        if using_sites_framework:
            objs_active_qs = self.filter(active=True, site=Site.objects.get_current().id)  # `Site.objects.get_current()` is `site` is a FK
        else:
            objs_active_qs = self.filter(active=True)

        objs_active_ls = list(objs_active_qs)
        objs_active_count = len(objs_active_ls)

        if objs_active_count == 0:
            obj = self.all().first()
            if obj:
                obj.set_active()
            else:
                obj = self.create()

        elif objs_active_count == 1:
            obj = objs_active_ls[0]

        elif objs_active_count > 1:
            obj = objs_active_ls[-1]
            obj.set_active()

        return obj

But I think it will still require a way to handle sites that are added and have not set any Theme yet. It might be a way to mark a theme as default or something else.

from django-admin-interface.

kennylajara avatar kennylajara commented on September 26, 2024

Would that work for you? I really need this feature, so, I'm willing to develop it with you guidance.

from django-admin-interface.

fabiocaccamo avatar fabiocaccamo commented on September 26, 2024

@kennylajara it's hard to give help based on code snippets pasted here, I would suggest you to work on your own solution that works well for your use case and that is enough generic to fit other people needs, then submit a PR.

from django-admin-interface.

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.