Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish and "Save Drafts" feature in Django

I am working on a Django Rest Framework B2B application with a React Frontend. The data is fed from a csv and then Analytics dashboards are rendered in React. Each Account ("User") is a company - and within a company, the app is used by the entire marketing team (say). Each company account has data unique to that co. and dashboard preferences unique to that company. An administrator is a human user who is some Manager / employee of a company (let's say, for example, Boeing, Nike etc) who has edit / administrator rights on behalf of the company. That "admin" makes some changes to the dashboard preferences and wants to "Publish" the changes so that rest of the employees (the rest of the Marketing team) of the company Account can view the updated dashboard. But maybe not yet, hence a "Save Drafts" feature.

I'm not sure how to get these two features in the most industry-standard way in Django (DRF) - When I hit "Publish", the entire marketing team should be able to see the changes. (It's a B2B app). But when I save drafts, I should be able to view the changes (as admin) but not the rest of the marketing team. I'd be grateful for any help. Thank you!

like image 464
steelTissue Avatar asked Dec 07 '25 02:12

steelTissue


1 Answers

You can use a choices field to manage the status of a a model (Dashboard in your case)

Example code follow

models.py

class Dasboard(models.Model):
    STATUS_CHOICES = (('draft', 'Save Draft'), ('published', 'Published'))
    status = models.Charfield(max_length=20, choices=STATUS_CHOICES)
    # Others models fields

views.py

def dashboard(request):
    objects = None
    if request.user.is_admin:
        # The admin users can see draft and saved (all Dashboard objects)
        objects = Dashboard.objects.all()
    else:
        # Others users are seeing only published
        objects = Dashboard.objects.filter(status='published')
    return render(request, 'app_name/dashboard.html', {'objects': objects})

NB : Here i used only Django filter features to retrives some data according to the type of users. But you can also use Django permissions too for more advanced handling.

like image 139
Rvector Avatar answered Dec 08 '25 19:12

Rvector