wagtailwagtail-admin

Wagtail admin page for different clients


We are in the process of making a UberEat like app and will be using Django for the backend. I discovered wagtail not so long ago and was wondering if it would be possible to use it for our vendors, with each vendor having the possibility to log into Wagtail admin and having access to his/her own products, category, prices, etc. Rephrased, is there a possibility to have different admins in Wagtail that would have access to their own model instances ? Thanks to you all


Solution

  • With Wagtail’s page permissions you could solve that via a "parent" page in combination with a custom "group" for each vendor. A clumsy workaround to be honest, it would require 3 steps, which could be automated. But this way you could use the full-featured Wagtail CMS in a way, in which each vendor could only see their "own" pages.

    You could manage images and documents in the same way by assigning collections to vendors as well. But let’s start with the pages here.

    The slug would look something like ubereatlike.app/vendorxyz/... with vendorxyz/ representing this "parent" page.

    Groups can be added via the SETTINGS in the sidebar. Groups are a native part of the Django framework, see documentation. To add a new vendor, you would have to do three steps. (1) Create a parent page for this vendor. (2) Create a group for this vendor via SETTINGS --> GROUPS. Here you should check "Can access admin" and create a "native" new page permisson for the vendor’s page with all necessary permissions, like "Add", "Edit" and "Publish", for example. (3) Lastly you would have to add a new User for the vendor and choose its group as role.

    To automate this I would create a custom user model, see documentation. With a post_signal for this custom user model, I would create both the parent page and the group and its permissions. This way you would add the User only and the page and the group would be generated programmatically.

    # startapp vendors with models.py
    
    from django.contrib.auth.models import AbstractUser
    from django.contrib.auth.models import Group, Permission
    from django.dispatch import receiver
    from wagtail.models import Page
    from home.models import HomePage
    
    
    class VendorPage(Page):
        parent_page_types = ['home.HomePage']
        ...
    
    class Vendor(AbstractUser): 
        company_name = models.CharField(max_length=255)
        # ...other fields
    
    @receiver(models.signals.post_save, sender=Vendor)
    def vendor_created(sender, instance, created, **kwargs):
        if created:
            # create vendor page as child of home page
            home_page = HomePage.objects.first()
            vendor_page = VendorPage(title=instance.company_name)
            home_page.add_child(instance=vendor_page)
            vendor_page.save_revision().publish()  # recommended
            # create group with permissions
            vendor_group = Group.objects.create(...)
            permission = Permisson.objects.get(...)  # please look into Wagtail’s page permissons, this is just a rough draft
            vendor_group.permissions.add(permission)