tortuga-developers team mailing list archive
-
tortuga-developers team
-
Mailing list archive
-
Message #00004
[Merge] lp:~gaqzi/ppcommunity/convert_news into lp:ppcommunity
Björn Andersson has proposed merging lp:~gaqzi/ppcommunity/convert_news into lp:ppcommunity.
Requested reviews:
Tortuga Developers (tortuga-developers)
Conversion script for the Drupal news.
* News url changed to /nyheter
* News url format changed from /2010/may/ to /2010/05/
--
https://code.launchpad.net/~gaqzi/ppcommunity/convert_news/+merge/25844
Your team Tortuga Developers is requested to review the proposed merge of lp:~gaqzi/ppcommunity/convert_news into lp:ppcommunity.
=== modified file 'tortuga/apps/news/models.py'
--- tortuga/apps/news/models.py 2010-04-19 18:48:46 +0000
+++ tortuga/apps/news/models.py 2010-05-23 15:23:24 +0000
@@ -1,10 +1,11 @@
-from django.db import models
+from django.db import models
from django.conf import settings
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager
from tagging.fields import TagField
from django.contrib.comments.models import Comment
from django.template.defaultfilters import slugify
+from django.utils.translation import ugettext_lazy as _
class NewsManager(CurrentSiteManager):
def published(self, limit=None):
@@ -16,40 +17,40 @@
class NewsAuthor(models.Model):
objects = models.Manager()
on_site = CurrentSiteManager()
-
+
site = models.ForeignKey(Site, editable=False, default=settings.SITE_ID)
name = models.CharField(max_length=500)
slug = models.SlugField()
-
+
def __unicode__(self):
return self.name
-
+
def save(self,*args,**kwargs):
self.slug = slugify(self.name)
self.slug = self.slug.lower().replace('-','_')
super(NewsAuthor,self).save(*args,**kwargs)
-
+
class NewsCategory(models.Model):
objects = models.Manager()
on_site = CurrentSiteManager()
-
+
site = models.ForeignKey(Site, editable=False, default=settings.SITE_ID)
name = models.CharField(max_length=200)
slug = models.SlugField()
-
+
def __unicode__(self):
return self.name
-
+
def save(self,*args,**kwargs):
self.slug = slugify(self.name)
self.slug = self.slug.lower().replace('-','_')
super(NewsCategory,self).save(*args,**kwargs)
class NewsItem(models.Model):
-
+
objects = models.Manager()
on_site = NewsManager()
-
+
title = models.CharField(max_length=100)
slug = models.SlugField(help_text=u'A slug is used as part of the URL for this article. It is recommended to use the default value if possible.')
date = models.DateField(blank=True, null=True, help_text=u'YYYY-MM-DD -- Leave blank if you don\'t want the article to appear on the site yet.' )
@@ -62,29 +63,29 @@
author = models.ForeignKey(NewsAuthor,null=True,editable=True)
allow_comments = models.BooleanField(default=True,blank=False,editable=True)
category = models.ForeignKey(NewsCategory, null=True, editable=True)
-
+
def approved_comments(self):
return Comment.objects.for_model(self).filter(is_public=True)
-
+
def unapproved_comments(self):
return Comment.objects.for_model(self).filter(is_public=False)
-
+
def total_comments(self):
return Comment.objects.for_model(self)
-
+
def __unicode__(self):
return self.title
-
+
# @models.permalink
def get_absolute_url(self):
- return '/news/%s/%s/%s/%s/' % (self.date.strftime('%Y'), self.date.strftime('%b').lower(), self.date.strftime('%d'), self.slug)
+ return '/nyheter/%s/%s/%s/%s/' % (self.date.strftime('%Y'), self.date.strftime('%m'), self.date.strftime('%d'), self.slug)
# return ('news-item', (), {
# 'year': self.date.strftime('%Y'),
- # 'month': self.date.strftime('%b').lower(),
+ # 'month': self.date.strftime('%m')
# 'day': self.date.strftime('%d'),
# 'slug': self.slug
# })
-
+
def get_previous(self):
try:
# isnull is to check whether it's published or not - drafts don't have dates, apparently
@@ -92,7 +93,7 @@
except IndexError, e:
# print 'Exception: %s' % e.message
return None
-
+
def get_next(self):
try:
# isnull is to check whether it's published or not - drafts don't have dates, apparently
@@ -100,7 +101,7 @@
except IndexError, e:
# print 'Exception: %s' % e.message
return None
-
+
class Meta:
ordering = ['-date']
unique_together = (('slug', 'date', 'site'), )
=== modified file 'tortuga/apps/news/urls.py'
--- tortuga/apps/news/urls.py 2010-02-09 22:49:24 +0000
+++ tortuga/apps/news/urls.py 2010-05-23 15:23:24 +0000
@@ -10,15 +10,15 @@
news_dict = {
'queryset': NewsItem.on_site.published(),
- 'template_object_name': 'item',
+ 'template_object_name': 'item'
}
-news_date_dict = dict(news_dict, date_field='date')
+news_date_dict = dict(news_dict, date_field='date', month_format='%m')
urlpatterns = patterns('django.views.generic.date_based',
- url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', news_date_dict, name="news-item"),
- url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', news_date_dict),
- url(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', news_date_dict),
+ url(r'^(?P<year>\d{4})/(?P<month>\w{2})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', news_date_dict, name="news-item"),
+ url(r'^(?P<year>\d{4})/(?P<month>\w{2})/(?P<day>\w{1,2})/$', 'archive_day', news_date_dict),
+ url(r'^(?P<year>\d{4})/(?P<month>\w{2})/$', 'archive_month', news_date_dict),
url(r'^(?P<year>\d{4})/$', 'archive_year', dict(news_date_dict, make_object_list=True)),
)
@@ -34,3 +34,7 @@
url(r'^authors/$',news_views.author_list,name='news-authors'),
)
+# Catch-all, primarily for the old news
+urlpatterns += patterns('',
+ url(r'^(?P<slug>.+)/?$', news_views.redirect_by_slug),
+)
=== modified file 'tortuga/apps/news/views.py'
--- tortuga/apps/news/views.py 2010-02-09 22:49:24 +0000
+++ tortuga/apps/news/views.py 2010-05-23 15:23:24 +0000
@@ -1,23 +1,28 @@
from news.models import NewsItem, NewsAuthor, NewsCategory
from django.views.generic.list_detail import object_list, object_detail
from django.shortcuts import get_object_or_404
+from django.http import HttpResponsePermanentRedirect
def by_tag(request,tag):
qs = NewsItem.on_site.filter(tags__contains=tag,date__isnull=False)
return object_list(request,qs,template_object_name='item')
-
+
def by_category(request,category_slug):
the_category = get_object_or_404(NewsCategory.on_site, slug=category_slug)
qs = NewsItem.on_site.filter(category=the_category,date__isnull=False)
return object_list(request,qs,template_object_name='item',extra_context={'category':the_category})
-
+
def category_list(request,empty_arg):
return object_list(request,NewsCategory.on_site.all(),template_object_name='item')
-
+
def by_author(request,author_slug):
the_author = get_object_or_404(NewsAuthor.on_site, slug=author_slug)
qs = NewsItem.on_site.filter(author=the_author,date__isnull=False)
return object_list(request,qs,template_object_name='item')
-
+
def author_list(request):
- return object_list(request,NewsAuthor.on_site.all(),template_object_name='item')
\ No newline at end of file
+ return object_list(request,NewsAuthor.on_site.all(),template_object_name='item')
+
+def redirect_by_slug(request, slug):
+ qs = get_object_or_404(NewsItem.on_site, slug=slug)
+ return HttpResponsePermanentRedirect(qs.get_absolute_url())
=== added directory 'tortuga/apps/tortugapp/fixtures'
=== added file 'tortuga/apps/tortugapp/fixtures/convertoldnews.json'
--- tortuga/apps/tortugapp/fixtures/convertoldnews.json 1970-01-01 00:00:00 +0000
+++ tortuga/apps/tortugapp/fixtures/convertoldnews.json 2010-05-23 15:23:24 +0000
@@ -0,0 +1,4 @@
+[{"pk": 1, "model": "auth.group", "fields": {"name": "Anonyma anv\u00e4ndare", "permissions": []}},
+ {"pk": 2, "model": "auth.user", "fields": {"username": "Anonym", "first_name": "", "last_name": "", "is_active": true, "is_superuser": false, "is_staff": false, "last_login": "2010-05-23 14:55:11", "groups": [1], "user_permissions": [], "password": "sha1$32a54$30cb45kdfh9acecc855f92b082d44283e2ba2c11548f8e7", "email": "anonymkommentar@xxxxxxxxxxxxxxx", "date_joined": "2010-05-23 14:55:11"}},
+ {"pk": 3, "model": "auth.user", "fields": {"username": "MigreradKommentar", "first_name": "", "last_name": "", "is_active": false, "is_superuser": false, "is_staff": false, "last_login": "2010-05-23 14:58:54", "groups": [1], "user_permissions": [], "password": "sha1$32a54$30cbacecc855f92b082d44283e2jha2c11548f8e4", "email": "migreradkommentar@xxxxxxxxxxxxxxx", "date_joined": "2010-05-23 14:58:54"}}
+]
\ No newline at end of file
=== added file 'tortuga/apps/tortugapp/management/commands/convertoldnews.py'
--- tortuga/apps/tortugapp/management/commands/convertoldnews.py 1970-01-01 00:00:00 +0000
+++ tortuga/apps/tortugapp/management/commands/convertoldnews.py 2010-05-23 15:23:24 +0000
@@ -0,0 +1,117 @@
+from django.core.management.base import BaseCommand, CommandError
+from django.conf import settings
+from news.models import NewsItem
+from threadedcomments.models import ThreadedComment, FreeThreadedComment, PLAINTEXT
+from datetime import datetime
+from time import mktime, localtime
+from optparse import make_option
+import MySQLdb, MySQLdb.cursors
+from django.contrib.auth.models import User
+
+class Command(BaseCommand):
+ option_list = BaseCommand.option_list + (
+ make_option('--user' , '-u', help='The user for MySQL database'),
+ make_option('--password' , '-p', default='', help='The password for the MySQL user'),
+ make_option('--database' , '-d', help='The MySQL database to convert from'),
+ make_option('--host' , '-H', default='localhost', help='The host of the MySQL database.'),
+ )
+ help = 'Will convert news and comments from the old Drupal installation on Piratpartiet.se to Tortuga'
+
+ def handle(self, **options):
+ print 'This is a command: %s', options
+ c = ConvertOldNews(**options)
+ c._fetch_all()
+ c.close()
+
+class OldNewsItem:
+ def __init__(self, cursor, item):
+ self.c = cursor
+ self.item = NewsItem(title=item['title'], date=datetime.fromtimestamp(item['created']),
+ snippet=item['preface'], body=item['body'])
+
+ print '%s - %s' % (self.item, item['nid'])
+ slug = self._get_slug(item['nid'])
+ if len(slug) == 0:
+ return(None)
+ else:
+ self.item.slug = slug[0]
+
+ print self.item.slug
+ self.item.save()
+
+ self.comment_id = {}
+ user = User.objects.get(username='MigreradKommentar')
+ comments = self._get_comments(item['nid'])
+ for comment in comments:
+ node = self._current_node(comment['thread'])
+ parent = self._get_parent_node(node)
+
+ comment_body = []
+ if comment['subject']:
+ comment_body.append('Titel: %s' % comment['subject'])
+ if comment['name']:
+ comment_body.append('Namn: %s' % comment['name'])
+ if comment['homepage']:
+ comment_body.append('Hemsida: %s' % comment['homepage'])
+
+ comment_body.append('')
+ comment_body.append(comment['comment'])
+
+ c_obj = ThreadedComment.objects.create_for_object(self.item, user=user, ip_address=comment['hostname'], is_approved = True,
+ comment="\n".join(comment_body), parent=parent, markup=PLAINTEXT, date_submitted=datetime.fromtimestamp(comment['timestamp']))
+ c_obj.save
+ print 'comment imported %s' % c_obj.pk
+ self.comment_id[node] = c_obj
+ print '----'
+
+ def _get_slug(self, nid):
+ ''' Fetches the slugs for a given node and strips nyheter/ from it'''
+ self.c.execute('''SELECT REPLACE(dst, 'nyheter/', '') AS slug FROM url_alias
+ WHERE src = concat('node/', %s)
+ AND dst LIKE 'nyheter/%%' ''', nid)
+ return map(lambda x: x['slug'], self.c.fetchall())
+
+ def _get_comments(self, nid):
+ self.c.execute('''SELECT nid, cid, subject, comment, timestamp, thread, score, status, name, mail, homepage, hostname
+ FROM comments
+ WHERE nid = %s
+ AND status = 0
+ ORDER BY cid, thread ASC''', nid)
+ return self.c.fetchall()
+
+ def _current_node(self, node):
+ return node.replace('/', '')
+
+ def _get_parent_node(self, node):
+ if node is None:
+ return None
+
+ node = self._current_node(node).split('.')
+ if len(node) == 1:
+ return None
+
+ node = '.'.join(node[0:-1])
+ return self.comment_id [node]
+
+
+class ConvertOldNews:
+ def __init__(self, host='localhost', user='', password='', database='', **kwargs):
+ self.db = MySQLdb.connect(cursorclass=MySQLdb.cursors.DictCursor, user=user,
+ host=host, passwd=password, db=database, charset='utf8')
+ self.c = self.db.cursor()
+
+ def close(self):
+ self.db.close()
+
+ def _fetch_all(self):
+ self.c.execute('''SELECT node.nid, node.title, node.created, node.changed,
+ fd_preface.textual_data AS preface, fd_body.textual_data AS body
+ FROM node
+ INNER JOIN flexinode_data AS fd_preface ON fd_preface.nid = node.nid AND fd_preface.field_id = 41
+ INNER JOIN flexinode_data AS fd_body ON fd_body.nid = node.nid AND fd_body.field_id = 42
+ WHERE node.type = 'flexinode-7'
+ AND status = 1''')
+ # AND node.nid = 1871
+ # LIMIT 1''')
+ for row in self.c.fetchall():
+ news_item = OldNewsItem(self.c, row)
=== modified file 'tortuga/urls.py'
--- tortuga/urls.py 2010-05-05 18:05:05 +0000
+++ tortuga/urls.py 2010-05-23 15:23:24 +0000
@@ -37,7 +37,7 @@
(r'^facebook/', include('tortugapp.facebook_urls')),
(r'^pirateweb/', include('pirateweb.urls')),
(r'^tinymce/', include('tinymce.urls')),
- (r'^news/', include('news.urls')),
+ (r'^nyheter/', include('news.urls')),
(r'^admin/(.*)', admin.site.root),
)