Member Mixins - Lookaway Code Integration

by Kyle Bruder

Lookaway Logo

The Members app comes with several mixins that are used to add to your models and views to leverage the Member model layer and the Marshmallow ranking system.


MarshmallowMixin()

The MarshmallowMixin() adds two fields to a database model: "marshmallows" and "weight". The "marshmallows" field is a ManyToMany field that maps to the Marshmallow model. The "weight" field is a FloatField that gets updated when new marshmallows are allocated. This mixin was written for integration with Django database models.

members/mixins.py
...
class MarshmallowMixin(models.Model):

    class Meta:
        abstract = True

    marshmallows = models.ManyToManyField('members.marshmallow', blank=True)
    weight = models.FloatField(default=0)
...


MemberCreateMixin()

The MemberCreateMixin() is a mixin that uses a custom form_valid() method that adds a status message via the Django Messages Framework. This mixin was written for FormViews such as UpdateView().

members/mixins.py
...
class MemberCreateMixin:

    def form_valid(self, form):
        instance = form.save()
        messages.add_message(
            self.request, messages.INFO,
            'The {} "{}" has been successfully created.'.format(
                self.model.__name__,
                instance,
            )
        )
        return super().form_valid(form)
...


MemberUpdateMixin()

The MemberOwnershipView() uses a custom form_valid() method that tests whether the "owner" ForeignKey field of an Object matches the id of the requesting Member before calling the save() method on the instance and adds a status message via the Django Messages Framework. This mixin was written for FormViews such as UpdateView().

members/mixins.py
...
class MemberUpdateMixin:

    def form_valid(self, form):
        if self.request.user.pk == self.object.owner.pk:
            instance = form.save()
            messages.add_message(
                self.request, messages.INFO,
                'The {} "{}" has been successfully updated.'.format(
                    self.model.__name__,
                    instance,
                )
            )
            return HttpResponseRedirect(self.get_success_url())
        else:
            messages.add_message(
                self.request, messages.ERROR,
                'You do not own "{}". Modification Failed. It is not nice to \
                modify other people\'s work!'.format(self.object)
            )
            return HttpResponseRedirect(self.get_success_url())
...


MemberDeleteMixin()

The MemberDeleteView() is a View mixin that uses a custom post() method that tests whether the "owner" ForeignKey field of an Object matches the id of the requesting Member before calling the delete() method on the instance and adds a status message via the Django Messages Framework. This mixin was written for FormViews such as DeleteView().

members/mixins.py
...
class MemberDeleteMixin:

    def post(self, request, *args, **kwargs):
        instance = self.model.objects.get(pk=kwargs['pk'])
        if self.request.POST and self.request.user.pk == instance.owner.pk:
            instance.delete()                
            messages.add_message(
                request, messages.INFO,
                'The {} "{}" has been successfully deleted.'.format(
                    self.model.__name__,
                    instance,
                )
            )
            return HttpResponseRedirect(self.get_success_url())
        else:
            messages.add_message(
                request, messages.ERROR,
                'You do not own "{}". Delete Failed. It is not nice to delete \
                other people\'s work!'.format(instance)
            )
            return HttpResponseRedirect(self.get_success_url())
...


Thank you for using Lookaway CMS. Best of luck building your apps.


Referenced by

Members - Lookaway CMS Feature

The Members app is a feature of Lookaway Content Management System. The Member model inherits the User model from the Django Authentication System.

Links

lookaway/mixins.py at master · kylebruder/lookaway · GitHub
A quick and dirty CMS for guerrilla publishing and content sharing. - kylebruder/lookaway

Lookaway: Development Django Lookaway: CMS Python

Contributed by Kyle Bruder on Dec. 24, 2020, 4:49 p.m.
Last updated on Jan. 19, 2021, 7:49 p.m.