Lookaway App Profile Views

Written by Kyle Bruder, published on April 5, 2021, 1:40 a.m., last updated on April 6, 2021, 10:31 p.m.


logo



  1. App Profile View Imports

    Make sure to import the mixins along with the models and forms.

    foo/views.py
    ...
    from django.views.generic.base import TemplateView
    from django.views.generic.edit import CreateView, FormView, UpdateView, DeleteView
    from django.views.generic.detail import DetailView
    from django.views.generic.list import ListView
    from lookaway.mixins import AppPageMixin
    from members.models import Member
    from members.mixins import MemberCreateMixin, MemberUpdateMixin, MemberDeleteMixin
    from objects.utils import Text
    from .forms import FooAppProfileForm, FooPageSectionForm, FooForm, BarForm
    from .models import FooAppProfile, FooPageSection, Foo, Bar
    ...

    Source: kbruder Tech


  2. App Profile Update View

    This is a special update view because the section queryset needs to be included in the context data.

    foo/views.py
    class FooAppProfileUpdateView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
    
        permission_required = 'foo.change_fooappprofile'
        model = FooAppProfile
        form_class = FooAppProfileForm
    
        def get_form_kwargs(self):
            kwargs = super(FooAppProfileUpdateView, self).get_form_kwargs()
            kwargs['user'] = self.request.user
            return kwargs
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            # App profile
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            # SEO stuff
            context['meta_title'] = profile.title
            context['meta_desc'] = "Update \"{}\" profile settings".format(profile.title)
            context['sections'] = FooPageSection.objects.all().order_by(
                'order',
            )
            return context
    
        def get_success_url(self):
            next_url = self.request.GET.get('next')
            if next_url:
                return next_url
            else:
                return reverse('foo:foo_page')

    Source: kbruder Tech


  3. App Profile Landing Page View

    foo/views.py
    class FooPageView(TemplateView, AppPageMixin):
    
        template_name = 'foo/foo_page.html'
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            # SEO stuff
            context['meta_title'] = profile.title
            context['meta_desc'] = profile.meta_description
            # Sections
            context['sections'] = FooPageSection.objects.filter(
                is_enabled = True,
            ).order_by(
                'order',
            )
            # New and top Foo model instances
            context['new_foos'], context['top_foos'] = self.get_sets(
                Foo,
                profile.n_foos,
                show_new=profile.show_new_foos,
                show_top=profile.show_top_foos,
            )
            # New and top Bar model instances
            context['new_bars'], context['top_bars'] = self.get_sets(
                Bar,
                profile.n_bars,
                show_new=profile.show_new_bars,
                show_top=profile.show_top_bars,
            )
            # Create Foo button
            if self.request.user.has_perm('foo.add_foo'):
                context['show_create_foo_button'] = True
                context['create_foo_url'] = reverse(
                    'foo:foo_create',
                )
            # Create Bar button
            if self.request.user.has_perm('foo.add_bar'):
                context['show_create_bar_button'] = True
                context['create_bar_url'] = reverse(
                    'foo:bar_create',
                )
            # Update AppProfile button
            if self.request.user.has_perm('foo.change_fooappprofile'):
                context['show_edit_profile_button'] = True
                context['edit_profile_url'] = reverse(
                    'foo:foo_app_profile_update',
                    kwargs={'pk': 1},
                )
            return context

    Source: kbruder Tech


  4. App Page Section Create View

    foo/views.py
    class FooPageSectionCreateView(LoginRequiredMixin, PermissionRequiredMixin, MemberCreateMixin, CreateView):
    
        permission_required = 'foo.add_foopagesection'
        model = FooPageSection
        form_class = FooPageSectionForm
    
        def get_form_kwargs(self):
            kwargs = super(FooPageSectionCreateView, self).get_form_kwargs()
            kwargs['user'] = self.request.user
            return kwargs
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            # App profile
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            context['meta_title'] = "New Page Section"
            context['meta_desc'] = "Add a section to the {} landing page.".format(profile.title)
            context['profile'] = profile
            return context
    
        def form_valid(self, form):
            member = Member.objects.get(pk=self.request.user.pk)
            form.instance.creation_date = timezone.now()
            form.instance.owner = member
            return super().form_valid(form)
    
        def get_success_url(self):
            next_url = self.request.GET.get('next')
            if next_url:
                return next_url
            else:
                return reverse(
                    'foo:foo_page_section_detail',
                    kwargs={'pk': self.object.pk},
                )

    Source: kbruder Tech


  5. App Page Section Detail View

    foo/views.py
    class FooPageSectionDetailView(LoginRequiredMixin, DetailView):
    
        model = FooPageSection
        context_object_name = 'section'
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            # App profile
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            context['meta_title'] = profile.title
            return context

    Source: kbruder Tech


  6. App Page Section Update View

    foo/views.py
    class FooPageSectionUpdateView(LoginRequiredMixin, PermissionRequiredMixin, MemberUpdateMixin, UpdateView):
    
        permission_required = 'foo.change_foopagesection'
        model = FooPageSection
        form_class = FooPageSectionForm
    
        def get_form_kwargs(self):
            kwargs = super(FooPageSectionUpdateView, self).get_form_kwargs()
            kwargs['user'] = self.request.user
            return kwargs
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            # App profile
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            context['meta_title'] = "Update \"{}\"".format(self.object.title)
            context['meta_desc'] = "Make changes to this landing page section.".format(self.object.title)
            return context
    
        def form_valid(self, form):
            # Update last modified date for the Section
            form.instance.last_modified = timezone.now()
            return super().form_valid(form)
    
        def get_success_url(self):
            next_url = self.request.GET.get('next')
            if next_url:
                return next_url
            else:
                return reverse(
                    'foo:foo_page_section_detail',
                    kwargs={'pk': self.object.pk},
                )

    Source: kbruder Tech


  7. App Page Section Delete View

    foo/views.py
    class FooPageSectionDeleteView(LoginRequiredMixin, PermissionRequiredMixin, DeleteView):
    
        permission_required = 'foo.delete_foopagesection'
        model = FooPageSection
        context_object_name = "section"
    
        def get_success_url(self):
            return reverse(
                'foo:foo_page',
            )

    Source: kbruder Tech


  8. Generic Create View Example

    foo/views.py
    class FooCreateView(LoginRequiredMixin, PermissionRequiredMixin, MemberCreateMixin, CreateView):
    
        permission_required = 'foo.add_bar'
        model = Foo
        form_class = FooForm
    
        def get_form_kwargs(self):
            kwargs = super(FooCreateView, self).get_form_kwargs()
            kwargs['user'] = self.request.user
            return kwargs
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            # App profile
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            context['meta_title'] = "New Foo"
            context['meta_desc'] = "Enter the form instructions here"
            return context
    
        def form_valid(self, form):
            member = Member.objects.get(pk=self.request.user.pk)
            form.instance.creation_date = timezone.now()
            form.instance.owner = member
            form.instance.slug = Text.slugify_unique(
                    self.model,
                    form.instance.title,
            )
            return super().form_valid(form)
    
        def get_success_url(self):
            next_url = self.request.GET.get('next')
            if next_url:
                return next_url
            else:
                return reverse(
                    'foo:foo_detail',
                    kwargs={'slug': self.object.slug},
                )

    Source: kbruder Tech


  9. Generic List View Example

    foo/views.py
    class FooListView(ListView):
    
        model = Foo
        paginate_by = FooAppProfile.objects.get_or_create(pk=1)[0].list_pagination
        context_object_name = 'foos'
    
        def get_queryset(self, *args, **kwargs):
            if self.request.user.is_authenticated:
                return Foo.objects.filter(
                    Q(owner=self.request.user) | Q(is_public=True),
                ).order_by(
                    'is_public',
                    '-publication_date',
                )
            else:
                return Foo.objects.filter(
                    is_public=True,
                ).order_by(
                    '-publication_date',
                )
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            # App profile
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            context['app_list_context'] = "New Foos"
            # SEO stuff
            context['meta_title'] = "{} | {}".format(
                context['app_list_context'],
                profile.title,
            )
            context['meta_desc'] = "Recently published Foos by {} contributors.".format(
                profile.title,
            )
            # Create button
            if self.request.user.has_perm('foo.add_foo'):
                context['show_create_button'] = True
                context['create_button_url'] = reverse(
                    'foo:foo_create',
                )
            return context

    Source: kbruder Tech


  10. Model by Member List View

    foo/views.py
    class MemberBarView(ListView):
    
        model = Bar
        paginate_by = FooAppProfile.objects.get_or_create(pk=1)[0].bar_list_pagination
        context_object_name = 'bars'
    
        def get_queryset(self, *args, **kwargs):
            member = Member.objects.get(username=self.kwargs['member'])
            if self.request.user.pk == member.pk:
                return Bar.objects.filter(
                    owner=member
                ).order_by(
                    '-last_modified',
                )
            else:
                return Bar.objects.filter(
                    owner=member,
                    is_public=True,
                ).order_by(
                    '-publication_date',
                )
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            member = Member.objects.get(username=self.kwargs['member'])
            # App profile
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            context['app_list_context'] = "Bars"
            context['meta_title'] = "Bars by {} | {}".format(
                member,
                profile.title,
                )
            context['meta_desc'] = "Bars curated by {} for {}.".format(
                member,
                profile.title,
            )
            # Create button
            if self.request.user.has_perm('foo.add_bar'):
                context['show_create_button'] = True
                context['create_button_url'] = reverse(
                    'foo:bar_create',
                )
            context['member'] = member
            return context

    Source: kbruder Tech


  11. Generic Detail View Example

    foo/views.py
    class FooDetailView(DetailView):
    
        model = Foo
        context_object_name = 'foo'
        
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            if self.request.user.is_authenticated:
                member = Member.objects.get(pk=self.request.user.pk)
                if member.check_can_allocate() and not member.check_is_new():
                    context['can_add_marshmallow'] = True
                else:
                    context['can_add_marshmallow'] = False
            return context

    Source: kbruder Tech


  12. Generic Update View Example

    foo/views.py
    class FooUpdateView(LoginRequiredMixin, PermissionRequiredMixin, MemberUpdateMixin, UpdateView):
    
        permission_required = 'foo.change_foo'
        model = Foo
        form_class = FooForm
    
        def get_form_kwargs(self):
            kwargs = super(FooUpdateView, self).get_form_kwargs()
            kwargs['user'] = self.request.user
            return kwargs
    
        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            # App profile
            profile, created = FooAppProfile.objects.get_or_create(pk=1)
            context['profile'] = profile
            context['meta_title'] = "Update \"{}\"".format(self.object.title)
            context['meta_desc'] = "Make changes to this Foo."
            return context
    
        def form_valid(self, form):
            form.instance.last_modified = timezone.now()
            return super().form_valid(form)
    
        def get_success_url(self):
            next_url = self.request.GET.get('next')
            if next_url:
                return next_url
            else:
                return reverse(
                    'foo:foo_detail',
                    kwargs={'slug': self.object.slug},
                )

    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:40 a.m.
Last updated on April 6, 2021, 10:31 p.m.