Commit cfaa11ed authored by Mathieu Bridon's avatar Mathieu Bridon

participate: Use a custom model

The way we were keeping the document id completely broke down with a
recent change in Ideascube:

    ideascube!905

In addition, this was wrong anyway, because we were using a custom
form to keep the attribute when creating the message, but not when
editing it. And publishing a message means editing it, so de facto those
attributes were all always lost.

So we need a custom model, to keep track of the linked media. This
should probably be an N-to-N relationship directly in Ideascube, but
this is not the time.

This also makes the cleaning command much nicer!
parent 33b490e0
......@@ -3,41 +3,36 @@ from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from ideascube.blog.forms import ContentForm as IdeascubeContentForm
from ideascube.blog.models import Content
from ideascube.mediacenter import utils
from ideascube.mediacenter.forms import DocumentForm
from ideascube.mediacenter.models import Document
from ideascube.widgets import LangSelect, RichTextEntry
from .models import RadioShow
from .models import ListenerMessage, RadioShow
current_year = datetime.now().year
class RicherTextEntry(RichTextEntry):
def __init__(self, *, with_media=False, attrs=None):
super().__init__(with_media=with_media, attrs=attrs)
# Add this, to keep track of the document added to the blog article
self.authorized_attributes['source'].append('data-irfi-document')
class ContentForm(IdeascubeContentForm):
# We need to redefine all this...
# We need to redefine all this
class ContentForm(forms.ModelForm):
class Meta:
model = Content
model = ListenerMessage
widgets = {
"author": forms.HiddenInput,
"published_at": forms.DateInput(format='%Y-%m-%d'),
"lang": LangSelect,
"summary": RichTextEntry,
# ... just to override this :(
"text": RicherTextEntry(with_media=True)
"text": RichTextEntry(with_media=True)
}
fields = "__all__"
def save(self, commit=True):
content = super().save()
content.save() # Index m2m.
return content
class MessageForm(forms.Form):
# Translators: Do not translate this, the translation is in Ideascube
......
......@@ -4,10 +4,7 @@ from bs4 import BeautifulSoup
from django.core.management.base import BaseCommand
from ideascube.blog.models import Content
from ideascube.mediacenter.models import Document
from irfi.models import RadioShow
from irfi.models import ListenerMessage, RadioShow
class Command(BaseCommand):
......@@ -28,31 +25,10 @@ class Command(BaseCommand):
old_shows = RadioShow.objects.filter(air_date__lt=max_age)
old_shows.delete()
old_articles = Content.objects.filter(published_at__lt=max_age)
for article in old_articles:
if article.author.is_staff:
# This is a regular blog article, not a message from a listener
continue
soup = BeautifulSoup(article.text, 'lxml')
for source in soup.find_all('source'):
try:
document_id = int(source.get('data-irfi-document'))
except:
# There is no document to clean
continue
try:
document = Document.objects.get(id=document_id)
if document.hidden:
# This really is a media uploaded for a message/ad
document.delete()
old_messages = ListenerMessage.objects.filter(published_at__lt=max_age)
except Exception as e:
print('Could not remove media embedded in {}: {}'.format(article, e))
for message in old_messages:
if message.media:
message.media.delete()
article.delete()
message.delete()
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2018-01-26 10:12
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('blog', '0014_auto_20171021_1129'),
('mediacenter', '0016_auto_20170921_1402'),
('irfi', '0002_auto_20180119_1617'),
]
operations = [
migrations.CreateModel(
name='ListenerMessage',
fields=[
('content_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='blog.Content')),
('media', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='mediacenter.Document')),
],
options={
'abstract': False,
},
bases=('blog.content',),
),
]
......@@ -2,9 +2,15 @@ from django.core.urlresolvers import reverse
from django.db import models
from django.utils.translation import ugettext_lazy as _
from ideascube.blog.models import Content, ContentQuerySet
from ideascube.mediacenter.models import Document, DocumentQuerySet
class ListenerMessage(Content):
media = models.ForeignKey(Document, blank=True, null=True)
objects = ContentQuerySet.as_manager()
class RadioShow(Document):
THEMES = (
('healthcare', _('Access to health care.')),
......
......@@ -155,6 +155,7 @@ class MessageCreate(FormView):
'published_at': timezone.now(),
'status': Content.DRAFT,
'lang': get_language(),
'media': document.id if document else None,
'text': self._generate_message_text(
text=form.cleaned_data['text'],
motto=motto,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment