Giter VIP home page Giter VIP logo

django-publishable's Introduction

Django Publishable ๐Ÿฆ„

What is Django-Publishable?

Django library that add to your models the draft/publish features.

Instalation

$ pip install django-publishable

Quick start

  1. Add "publishable" to your INSTALLED_APPS setting like this::
        INSTALLED_APPS = [
            ...
            'publishable',
        ]
  2. Run python manage.py migrate to create the publishable models.
  3. Try to replicate this with your models:
    from django.db import models
    from publishable.models import Publishable, Publisher
    
    # A Publisher is responsible for triggering 
    # the publish all drafts that he contains
    class User(Publisher):
        pass
    
    # Overdide the Publishable to implement the logic
    # of broadcast_need_to_published
    class MyPublishable(Publishable):
        class Meta:
            abstract = True
        
        def broadcast_need_to_published(self):
            """
                This function will return a Draft instance
                then it's up to you select your Publisher
                and add it to it's draft
            """
            draft = super(MyPublishable, self).broadcast_need_to_published()
            chatbot, _ = User.objects.get_or_create(pk=1)
            chatbot.add_draft(draft)
    
    # After setting up your Publishable
    # then inherit from it into the model that you need to receie
    # draft/publish features
    class Article(MyPublishable):
        title = models.CharField(max_length=255)
        content = models.TextField()
    
        comments = models.ManyToManyField('Comment', related_name='comments')
    
        highlighted_comment = models.ForeignKey('Comment', related_name='highlighted_comment', null=True)
    
    
    class Comment(MyPublishable):
        content = models.CharField(max_length=255)
  4. By default now all your model will store changes into the draft
    # Add changes and publish one model
    >>> a = Article.objects.create()
    >>> a.title = "foo"
    >>> a.publish()
    >>> a.published.title
    foo
    
    # Let's add changes and publish using a publisher
    >>> a.title = "boo"
    >>> a.save()
    >>> a.published.title
    foo
    >>> user = User.objects.get(pk=1)
    >>> user.publish()
    
    # You can track the status of the publishing process
    >>> user.publishing_status
    PUBLISHED

Using the Context Manager

In order to keep your code clean and avoid replication of code by doing article.published all the time, you can use the PublishedContextManager. Every operation that you do inside the with will target the published version of the model. But be carful do not do any changes to the published, you're only supposed to read data, inserting the data should target the draft.

>>> from publishable.context_managers import PublishedContextManager
>>> a = Article.objects.create(title="foo")
>>> a.publish()
>>> a.title = "boo"
>>> a.save()
>>> with PublishedContextManager():
        print(a.title)
foo
>>> print(a.title)
boo

django-publishable's People

Contributors

ilyeshammadi avatar sourcery-ai-bot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.