Mercurial > public > madeira
changeset 47:0e51e5be34b9
For issue #4, create an articles application and import old data.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 20 Mar 2012 19:47:20 -0500 |
parents | 358c7640ab60 |
children | b8a71c767dc0 |
files | madeira/articles/__init__.py madeira/articles/admin.py madeira/articles/management/__init__.py madeira/articles/management/commands/__init__.py madeira/articles/management/commands/import_old_articles.py madeira/articles/models.py madeira/articles/urls.py madeira/articles/views.py madeira/band/urls.py madeira/settings/base.py madeira/templates/articles/article_detail.html madeira/templates/articles/article_list.html madeira/templates/news/news_list.html madeira/templates/pagination.html madeira/urls.py static/css/theme.css |
diffstat | 13 files changed, 201 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/articles/admin.py Tue Mar 20 19:47:20 2012 -0500 @@ -0,0 +1,21 @@ +""" +Automatic admin definitions for the articles application. + +""" +from django.contrib import admin +from django.conf import settings + +from articles.models import Article + + +class ArticleAdmin(admin.ModelAdmin): + save_on_top = True + list_filter = ['date'] + list_display = ['title', 'date'] + search_fields = ['text', 'title'] + + class Media: + js = settings.THIRD_PARTY_JS['tiny_mce'] + + +admin.site.register(Article, ArticleAdmin)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/articles/management/commands/import_old_articles.py Tue Mar 20 19:47:20 2012 -0500 @@ -0,0 +1,59 @@ +""" +import_old_articles.py - For importing articles from the older version of this +website. + +""" +import datetime + +from django.conf import settings +from django.core.management.base import LabelCommand +from django.utils import simplejson as json +from django.utils.html import linebreaks +import textile + +from articles.models import Article + + +class Command(LabelCommand): + args = '<filename filename ...>' + help = 'Imports older articles in JSON format' + + def handle_label(self, filename, **options): + """ + Process the file of older articles in JSON. Convert to the new model + scheme. + + """ + with open(filename, 'rb') as f: + items = json.load(f) + + for item in items: + if item['model'] == 'band.article': + self.process_item(item) + + def process_item(self, item): + + fields = item['fields'] + + content = fields['text'].strip() + if fields['markup_enabled']: + text = textile.textile(content, encoding='utf-8', output='utf-8') + else: + text = linebreaks(fields['text']) + + source = linebreaks(fields['source'].strip()) + + pdf = fields['pdf'].strip() + if pdf: + pdf = u"%s%s" % (settings.MEDIA_URL, pdf.replace('\\', '/')) + + article = Article( + id=item['pk'], + title=fields['title'].strip(), + date=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'), + text=text, + source=source, + url=fields['url'].strip(), + pdf=pdf) + + article.save()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/articles/models.py Tue Mar 20 19:47:20 2012 -0500 @@ -0,0 +1,28 @@ +""" +Models for the articles application. + +""" +from django.db import models + + +class Article(models.Model): + title = models.CharField(max_length=64) + date = models.DateTimeField(db_index=True) + text = models.TextField() + source = models.TextField(help_text="Enter the source/author for the " + "article, copyright info, etc; it will appear under the article.") + url = models.URLField(blank=True, verify_exists=False, + help_text = 'Link to original article; optional') + pdf = models.FileField(upload_to = 'pdf/articles/%Y/%m/%d/', blank=True, + help_text="If you want to make the original article available as " + "a PDF download, you may upload it here.") + + def __unicode__(self): + return self.title + + class Meta: + ordering = ['-date'] + + @models.permalink + def get_absolute_url(self): + return ('articles-item', [], {'pk': str(self.id)})
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/articles/urls.py Tue Mar 20 19:47:20 2012 -0500 @@ -0,0 +1,21 @@ +""" +Urls for the articles application. + +""" +from django.conf.urls.defaults import patterns, url +from django.views.generic import DetailView, ListView + +from articles.models import Article + + +urlpatterns = patterns('', + url(r'^$', + ListView.as_view( + model=Article, + paginate_by=10, + context_object_name='article_list'), + name='articles-index'), + url(r'^(?P<pk>\d+)/$', + DetailView.as_view(model=Article, context_object_name='article'), + name='articles-item') +)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/articles/views.py Tue Mar 20 19:47:20 2012 -0500 @@ -0,0 +1,1 @@ +# Create your views here.
--- a/madeira/band/urls.py Thu Mar 15 20:04:42 2012 -0500 +++ b/madeira/band/urls.py Tue Mar 20 19:47:20 2012 -0500 @@ -15,8 +15,8 @@ (r'^news_old/$', 'news'), (r'^photos/$', 'photos_index'), (r'^photos/(\d+)$', 'photo_detail'), - (r'^press/$', 'press_index'), - (r'^press/(\d+)$', 'press_detail'), + (r'^press_old/$', 'press_index'), + (r'^press_old/(\d+)$', 'press_detail'), (r'^songs/$', 'songs'), (r'^videos/$', 'videos_index'), (r'^videos/(\d+)$', 'video_detail'),
--- a/madeira/settings/base.py Thu Mar 15 20:04:42 2012 -0500 +++ b/madeira/settings/base.py Tue Mar 20 19:47:20 2012 -0500 @@ -105,6 +105,7 @@ 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.staticfiles', + 'articles', 'band', 'gigs', 'news',
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/templates/articles/article_detail.html Tue Mar 20 19:47:20 2012 -0500 @@ -0,0 +1,19 @@ +{% extends 'band/base.html' %} +{% block title %}The Madeira | Press | {{ article.title }}{% endblock %} +{% block content %} +<h1>{{ article.title }}</h1> +<div>{{ article.text|safe }}</div> +<div class="article-source">{{ article.source|safe }}</div> +{% if article.url %} +<a href="{{ article.url }}">Original article</a> +{% endif %} +{% if article.pdf and article.url %} +| +{% endif %} +{% if article.pdf %} +<a href="{{ article.pdf.url }}" target="_blank">Original article as PDF</a> +<a href="http://www.adobe.com/products/acrobat/readstep2.html"> + <img src="{{ STATIC_URL }}images/get_adobe_reader.gif" alt="Adobe Reader" title="Get Adobe Reader" border="0" + align="middle" /></a> +{% endif %} +{% endblock %}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/templates/articles/article_list.html Tue Mar 20 19:47:20 2012 -0500 @@ -0,0 +1,29 @@ +{% extends 'band/base.html' %} +{% block title %}The Madeira | Press{% endblock %} +{% block content %} +<h1>Madeira Press, Articles, & Reviews</h1> +{% if article_list %} + {% for article in article_list %} + <h2><a href="{{ article.get_absolute_url }}">{{ article.title }}</a></h2> + <div>{{ article.text|safe }}</div> + <div class="article-source">{{ article.source|safe }}</div> + {% if article.url %} + <a href="{{ article.url }}" target="_blank">Original article</a> + {% endif %} + {% if article.pdf and article.url %} + | + {% endif %} + {% if article.pdf %} + <a href="{{ article.pdf.url }}">Original article as PDF</a> + <a href="http://www.adobe.com/products/acrobat/readstep2.html"> + <img src="{{ STATIC_URL }}images/get_adobe_reader.gif" alt="Adobe Reader" title="Get Adobe Reader" border="0" + align="middle" /></a> + {% endif %} + {% endfor %} + + {% include 'pagination.html' %} + +{% else %} +<p>No articles at this time.</p> +{% endif %} +{% endblock %}
--- a/madeira/templates/news/news_list.html Thu Mar 15 20:04:42 2012 -0500 +++ b/madeira/templates/news/news_list.html Tue Mar 20 19:47:20 2012 -0500 @@ -11,23 +11,11 @@ {% endif %} <div>{{ story.content|safe }}</div> {% endfor %} + + {% include 'pagination.html' %} + {% else %} <p>No news at this time.</p> {% endif %} -<div class="pagination"> - <span class="step-links"> - {% if page_obj.has_previous %} - <a href="?page={{ page_obj.previous_page_number }}">« Previous</a> - {% endif %} - - <span class="current"> - Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }} - </span> - - {% if page_obj.has_next %} - <a href="?page={{ page_obj.next_page_number }}">Next »</a> - {% endif %} - </span> -</div> {% endblock %}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/templates/pagination.html Tue Mar 20 19:47:20 2012 -0500 @@ -0,0 +1,15 @@ +<div class="pagination"> + <span class="step-links"> + {% if page_obj.has_previous %} + <a href="?page={{ page_obj.previous_page_number }}">« Previous</a> + {% endif %} + + <span class="current"> + Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }} + </span> + + {% if page_obj.has_next %} + <a href="?page={{ page_obj.next_page_number }}">Next »</a> + {% endif %} + </span> +</div>
--- a/madeira/urls.py Thu Mar 15 20:04:42 2012 -0500 +++ b/madeira/urls.py Tue Mar 20 19:47:20 2012 -0500 @@ -9,6 +9,7 @@ (r'^', include('band.urls')), (r'^gigs/', include('gigs.urls')), (r'^news/', include('news.urls')), + (r'^press/', include('articles.urls')), (r'^admin/doc/', include('django.contrib.admindocs.urls')), (r'^admin/', include(admin.site.urls)), (r'^photologue/', include('photologue.urls')),