示例#1
0
def create_article_evento_signal(sender, instance, raw, using, *args,
                                 **kwargs):
    if not instance.privado and not instance.artigo:
        # Cria o artigo
        try:
            section = Section.objects.get(slug='eventos')
        except Section.DoesNotExist:
            section = Section.objects.create(title='Eventos', slug='eventos')
        author = User.objects.get_or_create(username="******")[0]
        artigo = Article(
            title=u'%s - %s' % (
                instance.circulo.titulo,
                instance.nome,
            ),
            header=instance.local.replace('\n', '<br>'),
            content=instance.local.replace('\n', '<br>'),
            author=author,
            created_at=instance.dt_evento,
            is_active=True,
        )
        artigo.save()
        SectionItem(section=section, article=artigo).save()
        # Vincula o artigo ao CirculoEvento
        instance.artigo = artigo
    if instance.artigo:
        # Desabilita/Havilita a visualização do artigo
        instance.artigo.header = instance.local.replace('\n', '<br>')
        instance.artigo.content = instance.local.replace('\n', '<br>')
        instance.artigo.is_active = not instance.privado
        instance.artigo.save()
# Run the following commands in the Django shell `python3 manage.py shell`

# Trying to create a new article. This will result in an error.
from cms.models import Article

article1 = Article()
article1.title = "New Article"
article1.content = "Test content"
article1.slug = "new-article"
article1.save()

# Creating a new user.
from django.contrib.auth.models import User

author1 = User()
author1.first_name = 'Aruna'
author1.last_name = 'Tank'
author1.email = '*****@*****.**'
author1.set_password('apassword')
author1.save()

# The above article can be saved now tha tis has an author attached.
article1.author = author1
article1.save()