jeudi 18 février 2021

How buid a django architecture for multiple user interfaces working with the same data

I have a model like Company. Two user interfaces: an administrator's cabinet(Cabinet) (not native Django) and an interface for ordinary users (Main).

Let's use the example of a list of companies. On the Cabinet, I want to show the companies that he can edit, on the Main - all public companies. I doubt for how to properly organize this moment on the backend.

What I think? Make the base class CompanyListService in the service layer, write general logic in it, and separately each interface has its own class: CabinetCompanyListService(CompanyListService), MainCompanyListService(CompanyListService)

Pass the model manager to it during initialization(Company is Django Model):

class CompanyListService:
    def __init__(self, manager=Company.main_manager):
        self.manager = manager

then I can get here all the data options needed for the front

class MainCompanyListService(CompanyListService):

    def get_companies(self):
        return self.manager.get_publish_companies()


class CabinetCompanyListService(CompanyListService):

    def get_companies(self):
        return self.manager.get_available_companies_where_user_is_staff()


class MainManager(TreeManager):
    def get_available_companies(self):
        """Includes blocked, rejected etc...
        Companies that actually exist and are available at least to the creator, admin"""
        return self.get_queryset().exclude(status='deleted')

    def get_publish_companies(self):
        return self.get_queryset(status='active')

    def get_available_companies_where_user_is_staff(self, user):
        """Companies where user has CompanyManager role"""
        self.get_available_companies().filter(company_company_managers__user=user)

Not sure if this is good architecture. In general, I don't have enough experience to understand how good or bad it is to do this. How do you do this kind of thing? It is with Django. Thanks!

Aucun commentaire:

Enregistrer un commentaire