Commit 5dfb4923 authored by fred's avatar fred
Browse files

add newsitems (#1083)

parent 088596bd
from django.contrib import admin
from .models import Emission, Episode, Category, Schedule
from .models import Emission, Episode, Category, Schedule, \
NewsItem, NewsCategory
class EmissionAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('title',)}
......@@ -24,3 +25,15 @@ class ScheduleAdmin(admin.ModelAdmin):
pass
admin.site.register(Schedule, ScheduleAdmin)
class NewsCategoryAdmin(admin.ModelAdmin):
pass
admin.site.register(NewsCategory, NewsCategoryAdmin)
class NewsItemAdmin(admin.ModelAdmin):
pass
admin.site.register(NewsItem, NewsItemAdmin)
......@@ -3,12 +3,13 @@ import time
import gzip
import xml.etree.ElementTree as ET
import os
import re
import urllib2
from django.core.files import File
from django.core.management.base import BaseCommand, CommandError
from ...models import Emission, Episode, Diffusion, Category, SoundFile
from ...models import Emission, Episode, Diffusion, Category, SoundFile, NewsItem
from _spip2html import makeHtmlFromSpip
......@@ -27,6 +28,10 @@ class Article(object):
pass
class Breve(object):
pass
class KeywordGroup(object):
def __init__(self):
self.keywords = {}
......@@ -50,6 +55,7 @@ class Command(BaseCommand):
keyword_groups = {}
keywords = {}
documents = {}
breves = {}
with open(filename) as fd:
content = fd.read()
......@@ -121,6 +127,15 @@ class Command(BaseCommand):
rubrics[article.id_rubrique].articles[article.id_article] = article
for breve_xml in root.iter('spip_breves'):
breve = Breve()
for attr in ('id_breve', 'titre', 'texte',
'date_heure', 'statut'):
setattr(breve, attr, breve_xml.find(attr).text)
if breve.statut != 'publie':
continue
breves[breve.id_breve] = breve
for spip_url_xml in root.iter('spip_urls'):
id_objet = spip_url_xml.find('id_objet').text
if spip_url_xml.find('type').text == 'article' and id_objet in articles:
......@@ -129,6 +144,8 @@ class Command(BaseCommand):
rubrics[spip_url_xml.find('id_objet').text].url = spip_url_xml.find('url').text
elif spip_url_xml.find('type').text == 'mot' and id_objet in keywords:
keywords[spip_url_xml.find('id_objet').text].url = spip_url_xml.find('url').text
elif spip_url_xml.find('type').text == 'breve' and id_objet in breves:
breves[spip_url_xml.find('id_objet').text].url = spip_url_xml.find('url').text
for spip_doc_xml in root.iter('spip_documents'):
id_document = spip_doc_xml.find('id_document').text
......@@ -255,3 +272,20 @@ class Command(BaseCommand):
soundfile.title = episode_file.title or '[pas de titre]'
soundfile.file = File(file('media/IMG/' + episode_file.filename))
soundfile.save()
for breve in breves.values():
try:
newsitem = NewsItem.objects.get(slug=breve.url.lower())
except NewsItem.DoesNotExist:
newsitem = NewsItem()
newsitem.title = breve.titre
newsitem.slug = breve.url.lower()
newsitem.text = makeHtmlFromSpip(breve.texte,
documents=documents) or None
newsitem.datetime = datetime.strptime(breve.date_heure, '%Y-%m-%d %H:%M:%S')
image_path = re.findall('src="/(media/IMG.*?)"', newsitem.text, re.DOTALL)
if image_path:
image_path = image_path[0]
newsitem.image = File(file(image_path))
newsitem.save()
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'NewsItem'
db.create_table(u'emissions_newsitem', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('title', self.gf('django.db.models.fields.CharField')(max_length=50)),
('slug', self.gf('django.db.models.fields.SlugField')(max_length=50)),
('text', self.gf('ckeditor.fields.RichTextField')()),
('datetime', self.gf('django.db.models.fields.DateTimeField')()),
('image', self.gf('django.db.models.fields.files.ImageField')(max_length=250, null=True, blank=True)),
('category', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['emissions.NewsCategory'], null=True)),
('emission', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['emissions.Emission'], null=True)),
))
db.send_create_signal(u'emissions', ['NewsItem'])
# Adding model 'NewsCategory'
db.create_table(u'emissions_newscategory', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('title', self.gf('django.db.models.fields.CharField')(max_length=50)),
))
db.send_create_signal(u'emissions', ['NewsCategory'])
def backwards(self, orm):
# Deleting model 'NewsItem'
db.delete_table(u'emissions_newsitem')
# Deleting model 'NewsCategory'
db.delete_table(u'emissions_newscategory')
models = {
u'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
},
u'emissions.category': {
'Meta': {'object_name': 'Category'},
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
u'emissions.diffusion': {
'Meta': {'object_name': 'Diffusion'},
'datetime': ('django.db.models.fields.DateTimeField', [], {}),
'episode': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['emissions.Episode']"}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
},
u'emissions.emission': {
'Meta': {'object_name': 'Emission'},
'archived': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'categories': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['emissions.Category']", 'symmetrical': 'False'}),
'description': ('ckeditor.fields.RichTextField', [], {'null': 'True', 'blank': 'True'}),
'duration': ('django.db.models.fields.IntegerField', [], {'default': '60'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '254', 'null': 'True', 'blank': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'image': ('django.db.models.fields.files.ImageField', [], {'max_length': '250', 'null': 'True', 'blank': 'True'}),
'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50'}),
'text': ('ckeditor.fields.RichTextField', [], {'null': 'True'}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
'website': ('django.db.models.fields.URLField', [], {'max_length': '200', 'null': 'True', 'blank': 'True'})
},
u'emissions.episode': {
'Meta': {'object_name': 'Episode'},
'description': ('ckeditor.fields.RichTextField', [], {'null': 'True', 'blank': 'True'}),
'emission': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['emissions.Emission']"}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'image': ('django.db.models.fields.files.ImageField', [], {'max_length': '250', 'null': 'True', 'blank': 'True'}),
'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50'}),
'text': ('ckeditor.fields.RichTextField', [], {'null': 'True'}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
u'emissions.newscategory': {
'Meta': {'object_name': 'NewsCategory'},
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
u'emissions.newsitem': {
'Meta': {'object_name': 'NewsItem'},
'category': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['emissions.NewsCategory']", 'null': 'True'}),
'datetime': ('django.db.models.fields.DateTimeField', [], {}),
'emission': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['emissions.Emission']", 'null': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'image': ('django.db.models.fields.files.ImageField', [], {'max_length': '250', 'null': 'True', 'blank': 'True'}),
'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50'}),
'text': ('ckeditor.fields.RichTextField', [], {}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
u'emissions.schedule': {
'Meta': {'object_name': 'Schedule'},
'datetime': ('django.db.models.fields.DateTimeField', [], {}),
'emission': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['emissions.Emission']"}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'rerun': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'weeks': ('django.db.models.fields.IntegerField', [], {'default': '15'})
},
u'emissions.soundfile': {
'Meta': {'object_name': 'SoundFile'},
'episode': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['emissions.Episode']"}),
'file': ('django.db.models.fields.files.FileField', [], {'max_length': '250'}),
'fragment': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'podcastable': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'title': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
u'taggit.tag': {
'Meta': {'object_name': 'Tag'},
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'}),
'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '100'})
},
u'taggit.taggeditem': {
'Meta': {'object_name': 'TaggedItem'},
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "u'taggit_taggeditem_tagged_items'", 'to': u"orm['contenttypes.ContentType']"}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'object_id': ('django.db.models.fields.IntegerField', [], {'db_index': 'True'}),
'tag': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "u'taggit_taggeditem_items'", 'to': u"orm['taggit.Tag']"})
}
}
complete_apps = ['emissions']
\ No newline at end of file
......@@ -179,3 +179,31 @@ class SoundFile(models.Model):
podcastable = models.BooleanField(default=False)
fragment = models.BooleanField(default=False)
title = models.CharField(max_length=50)
class NewsCategory(models.Model):
title = models.CharField(max_length=50)
def __unicode__(self):
return self.title
def get_newsitem_image_path(instance, filename):
return os.path.join('images', 'news', instance.slug,
os.path.basename(filename))
class NewsItem(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField()
text = RichTextField()
datetime = models.DateTimeField()
image = models.ImageField(upload_to=get_newsitem_image_path, max_length=250, null=True, blank=True)
tags = TaggableManager(blank=True)
category = models.ForeignKey('NewsCategory', verbose_name=u'Category', null=True)
emission = models.ForeignKey('Emission', null=True)
def __unicode__(self):
return self.title
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