Lookaway App Profile Mixins

Written by Kyle Bruder, published on April 5, 2021, 1:41 a.m., last updated on April 5, 2021, 1:45 a.m.


logo


Lookaway CMS App Profiles are used to customize the look and feel of a Lookaway CMS powered site. Class mixins are used to save time and reuse code when implementing the system into Lookaway Apps. They simply exist to keep track of and display unique information about a website. While this is a task that all websites should be able to do in a dynamic way, you may need to implement a system that does this in a specific way that you require. Although the mixins are already implemented into the core Lookaway apps, rest assured, they are not required when developing new apps for use with Lookaway CMS or the Django Framework.


AppProfile

lookaway/mixins.py
class AppProfile(models.Model):
    '''
    Modifiable settings for Lookaway apps.
    '''
    class Meta:
        abstract = True

    title = models.CharField(
        max_length=255,
        default="Lookaway CMS"
    )
    show_title = models.BooleanField(default=True)
    meta_description = models.TextField(
        max_length = 155,
        blank=True,
        null=True,
    )
    show_desc = models.BooleanField(default=True)
    text = models.TextField(
        max_length=65535,
        blank=True,
        null=True,
    )
    links = models.ManyToManyField(
        'objects.link',
        blank=True,
    )
    def __str__(self):
        return self.title

Source: kbruder Tech


Section

lookaway/mixins.py
class Section(MetaDataMixin):
    '''
    An ordered page section that may contain multimedia objects.
    You must also add a ForeignKey field that points to a model
    which inherits the Doc() mixin below.
    For use with Django models.
    '''
    class Meta:
        abstract = True

    order = models.DecimalField(
        max_digits=8,
        decimal_places=4,
    )
    hide_title = models.BooleanField(default=False)
    title = models.CharField(
        max_length=255,
    )
    text = models.TextField(
        max_length=65535,
        blank=True,
        null=True,
    )
    images = models.ManyToManyField(
        'objects.image',
        blank=True,
    )
    sounds = models.ManyToManyField(
        'objects.sound',
        blank=True,
    )
    videos = models.ManyToManyField(
        'objects.video',
        blank=True,
    )
    code = models.ManyToManyField(
        'objects.code',
        blank=True,
    )
    links = models.ManyToManyField(
        'objects.link',
        blank=True,
    )

    def __str__(self):
        return self.title

Source: kbruder Tech


AppPageMixin

lookaway/mixins.py
class AppPageMixin:

    def get_sets(self, model, n, show_new=True, show_top=True):
        '''
        A method for fetching two model querysets from a Django model.
        Given a model, it will return a list of the newest n items
        whose 'is_public' field is set to true.
        If the number of public models is sufficent, it will
        also return a queryset of at most, the top n items by 'weight'.
        Items that appear in the 'new' queryset will be excluded from
        the 'top' queryset.

        Args:
        instance -  A Django instance with 'is_public', 'publication_date'
                    and 'weight' fields.
        n -         The number of items in each queryset.
        show_new -  If set to False, the 'new_instances' queryset will be empty.
        show_top -  If set to False, the 'top_instances' queryset will be empty.

        Returns:
        new_instances - A queryset of n new public instances of the given instance.
                     Queryset may be less than n if the number of instances
                     is insufficent.
        top_instances - A queryset of the top n public instances by weight
                     of the given instance excluding instances in new_instances.
                     Queryset may be less than n if the number of instances
                     is insufficent.
        '''
        # Initialize variables.
        public_instances = model.objects.filter(is_public=True)
        new_instances = model.objects.none()
        top_instances = model.objects.none()
        # If there are public instances and we want to show the newest n
        if n > 0 and public_instances.count() >= n and show_new:
            # Get the n newest instances.
            new_instances = public_instances.order_by(
                '-publication_date',
            )[:n]
        # If there are public instances and we want to show the top n
        if n > 0 and public_instances.count() >= n and show_top:
            # Get the date of the nth newest instance
            # if there are n or more instances.
            last_new_instance_date = public_instances.order_by(
                '-publication_date',
            )[n-1].publication_date
            # Exclude any instance that appears in the new instances list
            # from the top instance list.
            if show_new:
                top_instances = public_instances.order_by(
                    '-weight',
                ).exclude(
                    publication_date__gte=last_new_instance_date,
                )[:n]
            # Unless new instances aren't being shown, of course
            else:
                print("yep")
                top_instances = public_instances.order_by('-weight')[:n]
        # In the event there are less than n instances,
        # include them in the new model list.
        elif n > 0:
            if show_new:
                new_instances = public_instances.order_by(
                    '-publication_date',
                )
            # Or in the top list if we don't want to show new_models
            elif show_top:
                top_instances = public_instances.order_by(
                    '-weight',
                )
        # Return the querysets
        print({}, ":",  top_instances)
        return new_instances, top_instances

Source: kbruder Tech



Referenced by

App Profile Design Patterns - Lookaway CMS

Lookaway CMS comes with class mixins that can be used when writing new apps. These powerful customization features can extend your models and views.

bitcoin:3MnhNRKgrpTFQWstYicjF6GebY7u7dap4u
Bitcoin Accepted Here

3MnhNRKgrpTFQWstYicjF6GebY7u7dap4u

Please donate some Bitcoin to our site. We can use it to keep improving the site and open up to more members. Any amount will help. Thank you.


litecoin:MT61gm6pdp9rJoLyMWW5A1hnUpxAERnxqg
Litecoin Accepted Here

MT61gm6pdp9rJoLyMWW5A1hnUpxAERnxqg

Please donate some Lite to our site. We can use it to keep improving the site and open it up to more members. Any amount will help. Thank you.


Lookaway: Development Django Python Lookaway: CMS

Contributed by Kyle Bruder on April 5, 2021, 1:41 a.m.
Last updated on April 5, 2021, 1:45 a.m.