Skip to main content

A simple Django app to add draft/publish capabilities to your models.

Project description

# Django Publishable 🦄

[![PyPI version](https://badge.fury.io/py/django-publishable.svg)](https://badge.fury.io/py/django-publishable) [![CircleCI](https://circleci.com/gh/Ilyes-Hammadi/django-publishable.svg?style=svg)](https://circleci.com/gh/Ilyes-Hammadi/django-publishable)

## What is Django-Publishable?

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

## Instalation
```shell
$ pip install django-publishable
```

## Quick start


1. Add "publishable" to your INSTALLED_APPS setting like this::
```python

INSTALLED_APPS = [
...
'publishable',
]
```
2. Run `python manage.py migrate` to create the publishable models.
3. Try to replicate this with your models:
```python
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
```python
# 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`.

```python
>>> 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
```


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-publishable-0.1.12.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_publishable-0.1.12-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file django-publishable-0.1.12.tar.gz.

File metadata

  • Download URL: django-publishable-0.1.12.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.4

File hashes

Hashes for django-publishable-0.1.12.tar.gz
Algorithm Hash digest
SHA256 3b5f0e98724925c977f0bd0f5429eca3da7cbf032ab6f244371f14030ece0d15
MD5 b85ef6d2ccf93920e474c18975d57b4c
BLAKE2b-256 339a946125346591052470808ad111250adf57cca76b0948e3d888c6273ee94c

See more details on using hashes here.

File details

Details for the file django_publishable-0.1.12-py3-none-any.whl.

File metadata

  • Download URL: django_publishable-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.4

File hashes

Hashes for django_publishable-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 0904f6c3dff22f0c712e73656f9d561b3f5482dd9c324308c50ead6f05ce0d82
MD5 90ebcd983d0b6bea144fb1dbfda639a7
BLAKE2b-256 91faada7b3c9c0256191428c8ea042940c6335eaabe516f05c286a7a949ea57f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page