Lookaway App Profile Mixins

Documented by kbruder tech✞ᵀᵀᴹ on Apr 05, 2021
Last updated on Jun 05, 2021


logo

Introduction

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

The base class for AppProfile models. Contains only the most basic data needed for a webpage such as title, description and links. You can toggle whether these items are displayed on the page as well.

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

    title = models.CharField(
        max_length=255,
        default="Lookaway Information"
    )
    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

A model mixin for Lookaway CMS App Profiles.
Source: kbruder Tech


AppPageMixin

This mixin is used for provide methods for use with landing page views for individual Django apps. The "get_sets()" method returns a list of newest and highest ranked instances of a given model. It ensures that a model instance will not appear in both lists.

More methods may be added in the future. If you are forking Lookaway CMS and wish to adhere to Lookaway design patterns, methods intended for AppProfile views should live here.

lookaway/mixins.py
class AppPageMixin:
    '''
    A collection of methods for use with Lookaway app landing views.
    '''

    def get_sets(self, model, n, show_new=True, show_top=True, member=None):
        '''
        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.
        Useful for assigning context in the 'get_context_data()' method.
        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.
        try:
            public_instances = model.objects.filter(
                is_public=True,
            ).exclude(members_only=True)
            if member != None:
                public_instances = public_instances.all().filter(owner=member)
        except:
            public_instances = model.objects.filter(is_public=True)
            if member != None:
                public_instances = public_instances.all().filter(owner=member)
        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:
                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',
                )[:n]
            # Or in the top list if we don't want to show new_models
            elif show_top:
                top_instances = public_instances.order_by(
                    '-weight',
                )[:n]
        # Return the querysets
        return new_instances, top_instances

Source: kbruder Tech


Section

The Section mixin was originally developed for the Documentation App. The concept was integrated into the AppProfile design so now any model that uses Sections, including Documentation App models, use this mixin. Sections are ordered model instances that contain M2M fields to include multimedia objects like images and videos.

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

    def next_order(self):
        return self.order + 1

    order = models.DecimalField(
        max_digits=8,
        decimal_places=4,
        default=0,
    )
    members_only = models.BooleanField(default=False)
    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


Conclusion

These mixins come standard with Lookaway CMS. There is no need to add this code to your project if you are forking. This page is here for reference. Please feel free to use this code for your own projects as well. Thanks for checking it out!


Referenced by

App Profile Design Patterns - Lookaway CMS

Lookaway CMS comes with mixins, models, forms, and views that can be used when writing new Django apps.

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.