Commits

Dan Watson  committed e7b6dff

Moved archives to a plugin, new twitter plugin

  • Participants
  • Parent commits 415a238

Comments (0)

Files changed (25)

File leaves/plugins/archive/__init__.py

Empty file added.

File leaves/plugins/archive/models.py

Empty file added.

File leaves/plugins/archive/templates/archive/date_list.html

+<ul class="date-archives">
+{% for d in dates %}
+    <li{% ifequal d current %} class="selected"{% endifequal %}>
+        {% ifequal group "month" %}
+        <a href="{% url view-archive-month year=d.year month=d.month %}">{{ d|date:"F Y" }}</a>
+        {% else %}
+        <a href="{% url view-archive-year year=d.year %}">{{ d|date:"Y" }}</a>
+        {% endifequal %}
+    </li>
+{% endfor %}
+</ul>

File leaves/plugins/archive/templatetags/__init__.py

Empty file added.

File leaves/plugins/archive/templatetags/archive.py

+from django import template
+from django.conf import settings
+from leaves.models import Leaf
+
+register = template.Library()
+
+@register.inclusion_tag('archive/date_list.html')
+def archive_dates(group='month', current=None):
+    dates = [d.date() for d in Leaf.on_site.stream().dates('pub_date', group, order='DESC')]
+    return {
+        'dates': dates,
+        'group': group,
+        'current': current,
+    }

File leaves/plugins/archive/urls.py

+from django.conf.urls.defaults import *
+from django.conf import settings
+
+urlpatterns = patterns('leaves.plugins.archive.views',
+    url(r'^archive/$', 'archive_index', name='archive-index'),
+    url(r'^archive/(?P<year>\d{4})/(?P<month>\d{1,2})/$', 'view_archive', name='view-archive-month'),
+    url(r'^archive/(?P<year>\d{4})/$', 'view_archive', name='view-archive-year'),
+)

File leaves/plugins/archive/views.py

+from django.shortcuts import get_object_or_404, redirect
+from django.template.response import TemplateResponse
+from django.conf import settings
+from django.http import Http404
+from leaves.models import Leaf
+from leaves.utils import get_page
+import datetime
+
+def archive_index(request):
+    archive_type = request.site.preferences.date_archives
+    dates = Leaf.on_site.stream().dates('pub_date', archive_type, order='DESC')
+    archives = []
+    for d in dates:
+        qs = Leaf.on_site.stream().filter(pub_date__year=d.year).order_by('pub_date', 'title')
+        if archive_type == 'month':
+            qs = qs.filter(pub_date__month=d.month)
+        archives.append({
+            'date': d.date(),
+            'leaves': qs,
+        })
+    return TemplateResponse(request, 'archive/index.html', {
+        'archive_type': archive_type,
+        'archives': archives,
+    })
+
+def view_archive(request, year, month=None):
+    try:
+        year = int(year)
+        if month:
+            month = int(month)
+    except:
+        raise Http404
+    leaves = Leaf.on_site.stream().filter(pub_date__year=year)
+    current_date = datetime.date(year, 1, 1)
+    archive_type = 'year'
+    if month:
+        leaves = leaves.filter(pub_date__month=month)
+        current_date = datetime.date(year, month, 1)
+        archive_type = 'month'
+    return TemplateResponse(request, 'archive/stream.html', {
+        'page': get_page(request, leaves),
+        'archive_type': archive_type,
+        'current_date': current_date,
+    })

File leaves/plugins/twitter/__init__.py

Empty file added.

File leaves/plugins/twitter/models.py

Empty file added.

File leaves/plugins/twitter/templates/twitter/feed.html

+<ul class="twitter">
+    {% for tweet in updates %}
+    <li class="tweet"><a href="{{ tweet.link }}">{{ tweet.text }}</a></li>
+    {% endfor %}
+</ul>

File leaves/plugins/twitter/templatetags/__init__.py

Empty file added.

File leaves/plugins/twitter/templatetags/twitter.py

+from django import template
+from django.conf import settings
+from django.core.cache import cache
+import datetime
+import urllib
+import json
+
+register = template.Library()
+
+TIMELINE_ENDPOINT = 'http://api.twitter.com/1/statuses/user_timeline.json'
+
+@register.inclusion_tag('twitter/feed.html')
+def twitter_feed(count=5):
+    user_name = settings.LEAVES_TWITTER_USERNAME
+    cache_key = 'twitter_%s_%s' % (user_name, count)
+    data = cache.get(cache_key)
+    if data:
+        return data
+    url = TIMELINE_ENDPOINT + '?' + urllib.urlencode({'screen_name': user_name, 'count': count})
+    fp = urllib.urlopen(url, proxies=settings.LEAVES_PROXIES)
+    data = json.loads(fp.read())
+    fp.close()
+    updates = []
+    for update in data:
+        d = datetime.datetime.strptime(update['created_at'], '%a %b %d %H:%M:%S +0000 %Y')
+        link = 'http://twitter.com/%s/status/%s' % (user_name, update['id'])
+        updates.append({
+            'date': d,
+            'link': link,
+            'text': update['text'],
+            'image': update['user']['profile_image_url'],
+        })
+    data = {
+        'user_name': user_name,
+        'updates': updates,
+    }
+    cache.set(cache_key, data, 60)
+    return data

File leaves/plugins/twitter/urls.py

+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('leaves.plugins.twitter.views',
+)

File leaves/plugins/twitter/views.py

Empty file added.

File leaves/templates/leaf_dates.html

-<ul class="date-archives">
-{% for d in dates %}
-    <li{% ifequal d current %} class="selected"{% endifequal %}>
-        {% ifequal group "month" %}
-        <a href="{% url view-archive year=d.year month=d.month %}">{{ d|date:"F Y" }}</a>
-        {% else %}
-        <a href="{% url view-archive year=d.year %}">{{ d|date:"Y" }}</a>
-        {% endifequal %}
-    </li>
-{% endfor %}
-</ul>

File leaves/templatetags/leaf.py

 	for plugin in settings.LEAVES_PLUGINS:
 		nav_html += navigation(plugin.split('.')[-1])
 	return nav_html
-
-@register.inclusion_tag('leaf_dates.html')
-def leaf_dates(group='month', current=None):
-	dates = [d.date() for d in Leaf.on_site.stream().dates('pub_date', group, order='DESC')]
-	return {
-		'dates': dates,
-		'group': group,
-		'current': current,
-	}

File leaves/themes/stream/static/stream/css/style.css

 }
 
 h1, h2, h3, h4 { margin-top: 0px; }
+img { border: 0; }
 
-.powered {
-    margin-left: 35px;
-}
+.powered { margin-left: 35px; }
 .powered:hover { border: 0 !important; }
-.powered img {
-    border: 0;
-    width: 100px;
-}
+.powered img { width: 100px; }
 
 .container {
     width: 940px;
        0px  0px 7px rgba(255,255,255,0.5);
 }
 
+#header a:hover {
+    border-bottom: 1px solid transparent;
+}
+
 #header a.title {
     font-size: 3em;
     font-weight: bold;
 
 #header .page-list li {
     float: left;
+    margin-left: 10px;
 }
 
 #header .page-list li a {
     padding: 4px 8px;
     margin: 0px;
     font-size: 1.4em;
-    margin-left: 10px;
     color: #fff;
 }
 
-#header .page-list li.selected a {
+#header .page-list li.selected a,
+.archive .page-list li.archive {
     font-weight: bold;
     background-color: #2A889E;
     border-radius: 5px;
 #nav li {
     font-size: 1.2em;
     line-height: 1.5em;
-    padding: 4px 4px 4px 20px;
+    padding: 4px 20px 4px 20px;
     margin: 0px;
 	background: -webkit-gradient(linear, left top, left bottom, from(#FEFEFE), to(#F2F5F5));
     background: -webkit-linear-gradient(top, #FEFEFE, #F2F5F5);
     background-position: 10px 11px;
 }
 
+#nav li.tweet {
+    font-size: 0.9em;
+}
+
+div.archive {
+    width: 300px;
+    float: left;
+    background: -webkit-gradient(linear, left top, left bottom, from(#FEFEFE), to(#F2F5F5));
+    background: -webkit-linear-gradient(top, #FEFEFE, #F2F5F5);
+    background: -moz-linear-gradient(top, #FEFEFE, #F2F5F5);
+    border: 1px solid #d5d5d5;
+    border-radius: 3px;
+    padding: 10px 15px;
+    margin-right: 15px;
+    margin-bottom: 15px;
+}
+div.archive ul {
+    list-style-type: none;
+    margin: 0px;
+    padding: 0px;
+}
+div.archive span.date {
+    display: inline-block;
+    text-align: right;
+    color: #999;
+    width: 50px;
+    margin-right: 10px;
+    font-size: 0.9em;
+}
+div.archive ul li {
+    margin-bottom: 3px;
+}
+
 #search {
 	margin: 0px;
 	padding: 3px;

File leaves/themes/stream/templates/archive.html

-{% extends "index.html" %}
-{% load leaf %}
-{% block title %}{{ site.name }} - Posted in {% ifequal archive_type "month" %}{{ current_date|date:"F Y" }}{% else %}{{ current_date|date:"Y" }}{% endifequal %}{% endblock %}
-{% block content-header %}<h1 class="tag">Posted in {% ifequal archive_type "month" %}{{ current_date|date:"F Y" }}{% else %}{{ current_date|date:"Y" }}{% endifequal %}</h1>{% endblock %}
-{% block datenav %}
-    <div class="box">
-        <div class="title">Archive</div>
-        {% leaf_dates site.preferences.date_archives current_date %}
-    </div>
-{% endblock %}

File leaves/themes/stream/templates/archive/index.html

+{% extends "base.html" %}
+
+{% block section %}archive{% endblock %}
+
+{% block content %}
+<h1>Browse the Archives</h1>
+<div style="float:left;">
+{% for a in archives %}
+<div class="archive">
+    <h2>
+        {% ifequal archive_type "month" %}
+        <a href="{% url view-archive-month year=a.date.year month=a.date.month %}">{{ a.date|date:"F Y" }}</a>
+        {% else %}
+        <a href="{% url view-archive-year year=a.date.year %}">{{ a.date|date:"Y" }}</a>
+        {% endifequal %}
+    </h2>
+    <ul>
+    {% for leaf in a.leaves %}
+        <li><span class="date">{% ifchanged leaf.pub_date.date %}{{ leaf.pub_date|date:"M j" }}{% endifchanged %}</span><a href="{{ leaf.resolve.get_url }}">{{ leaf.title_trans }}</a></li>
+    {% endfor %}
+    </ul>
+</div>
+{% cycle '' '<div style="clear:both;"></div>' %}
+{% endfor %}
+</div>
+{% endblock %}

File leaves/themes/stream/templates/archive/stream.html

+{% extends "index.html" %}
+{% load archive %}
+{% block title %}{{ site.name }} - Posted in {% ifequal archive_type "month" %}{{ current_date|date:"F Y" }}{% else %}{{ current_date|date:"Y" }}{% endifequal %}{% endblock %}
+{% block section %}archive{% endblock %}
+{% block content-header %}<h1 class="tag">Posted in {% ifequal archive_type "month" %}{{ current_date|date:"F Y" }}{% else %}{{ current_date|date:"Y" }}{% endifequal %}</h1>{% endblock %}
+{% block datenav %}
+    <div class="box">
+        <div class="title">Archive</div>
+        {% archive_dates site.preferences.date_archives current_date %}
+    </div>
+{% endblock %}

File leaves/themes/stream/templates/base.html

     <div class="container">
         <a class="title stroked" href="{% url homepage %}">{{ site.name }}</a>
         {% block topnav %}
-            {% navigation "pages" %}
+        <ul class="page-list">
+            <li class="archive"><a href="{% url archive-index %}">Archive</a></li>
+            {% for p in leaves.pages.navigable_pages %}
+            <li{% ifequal p leaf %} class="selected"{% endifequal %}><a href="{{ p.get_url }}">{{ p }}</a></li>
+            {% endfor %}
+        </ul>
         {% endblock %}
     </div>
 </div>
 			{% navigation "photos" %}
 		</div>
 	{% endblock %}
+	<!--
     {% block datenav %}
        <div class="box">
            <div class="title">Archive</div>
-           {% leaf_dates site.preferences.date_archives %}
+           {% load archive %}
+           {% archive_dates site.preferences.date_archives %}
        </div>
     {% endblock %}
+    -->
+        <div class="box">
+            <div class="title">Twitter</div>
+            {% load twitter %}
+            {% twitter_feed 3 %}
+        </div>
 		<div class="box">
 			<div class="title">Search</div>
 			<div class="padded">

File leaves/themes/stream/templates/pages/page.html

 {% extends "leaf.html" %}
 {% load pages %}
-{% block topnav %}
-{% page_hierarchy None leaf %}
-{% endblock %}

File leaves/urls.py

 	url(r'^author/(?P<username>[\w-]+)/$', 'view_author', name='view-author'),
 	url(r'^leaf/(?P<leaf_id>\d+)/$', 'view_leaf', name='view-leaf'),
 	url(r'^files/(?P<attachment_id>\d+)/(?P<filename>[^/]*)/?$', 'view_attachment', name='view-attachment'),
-	url(r'^archive/(?P<year>\d{4})/(?P<month>\d{1,2})/$', 'view_archive', name='view-archive'),
-	url(r'^archive/(?P<year>\d{4})/$', 'view_archive', name='view-archive'),
 )
 
 urlpatterns += patterns('',

File leaves/views.py

 		'page': get_page(request, Leaf.on_site.stream()),
 	})
 
-def view_archive(request, year, month=None):
-	try:
-		year = int(year)
-		if month:
-			month = int(month)
-	except:
-		raise Http404
-	leaves = Leaf.on_site.stream().filter(pub_date__year=year)
-	current_date = datetime.date(year, 1, 1)
-	archive_type = 'year'
-	if month:
-		leaves = leaves.filter(pub_date__month=month)
-		current_date = datetime.date(year, month, 1)
-		archive_type = 'month'
-	return TemplateResponse(request, 'archive.html', {
-		'page': get_page(request, leaves),
-		'archive_type': archive_type,
-		'current_date': current_date,
-	})
-
 def view_attachment(request, attachment_id, filename):
 	attachment = get_object_or_404(Attachment.objects.filter(leaf__sites=request.site), pk=attachment_id)
 	try:
 	'leaves.plugins.pages',
 	'leaves.plugins.blog',
 	'leaves.plugins.photos',
+	'leaves.plugins.archive',
+	'leaves.plugins.twitter',
 )
 
+LEAVES_PROXIES = None
+LEAVES_TWITTER_USERNAME = None
 LEAVES_ENABLE_TRANSLATION = False
 LEAVES_HOMEPAGE_VIEW = 'leaves.views.leaf_stream'
 LEAVES_DEFAULT_COMMENT_STATUS = 'pending'