Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit dd358522ab6423103598bef02e485a339fb66ce7 @peterbe committed Jan 31, 2012
3 README
@@ -0,0 +1,3 @@
+Some rushed work in progress.
+Will write a proper README later
+
No changes.
No changes.
@@ -0,0 +1,23 @@
+from django.core.urlresolvers import reverse
+from django.contrib.staticfiles.storage import staticfiles_storage
+
+
+import jinja2
+
+from jingo import register
+
+
+@register.function
+def thisyear():
+ """The current year."""
+ return jinja2.Markup(datetime.date.today().year)
+
+@register.function
+def url(viewname, *args, **kwargs):
+ """Helper for Django's ``reverse`` in templates."""
+ return reverse(viewname, args=args, kwargs=kwargs)
+
+
+@register.function
+def static(path):
+ return staticfiles_storage.url(path)
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
@@ -0,0 +1,19 @@
+{% extends "base.html" %}
+
+{% block content %}
+
+{% for blogitem in blogitems %}
+ <div class="row">
+ <div class="span16">
+ <div class="page-header">
+ <h1>
+ <a href="{{ url('blog_post', blogitem.oid) }}">{{ blogitem.title }}</a>
+ </h1>
+ </div>
+ {{ blogitem.text_rendered }}
+ </div>
+ </div>
+
+{% endfor %}
+
+{% endblock %}
@@ -0,0 +1,16 @@
+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.assertEqual(1 + 1, 2)
@@ -0,0 +1,7 @@
+from django.conf.urls.defaults import patterns, include, url
+
+
+import views
+urlpatterns = patterns('',
+ url('^$', views.home, name='home'),
+)
@@ -0,0 +1,13 @@
+import datetime
+from django.shortcuts import render
+from apps.plog.models import Category, BlogItem, BlogComment
+
+def home(request):
+ data = {}
+ data['blogitems'] = (
+ BlogItem.objects
+ .filter(pub_date__lt=datetime.datetime.utcnow())
+ .order_by('-pub_date')
+ )[:10]
+
+ return render(request, 'homepage/home.html', data)
No changes.
No changes.
No changes.
@@ -0,0 +1,57 @@
+from pprint import pprint
+from django.conf import settings
+from django.core.management.base import BaseCommand
+from django.db import transaction
+
+from apps.legacy.models import LegacyBlogitem, LegacyBlogcomment
+from apps.plog.models import BlogItem, BlogComment
+
+class Command(BaseCommand):
+
+ #@transaction.commit_manually
+ def handle(self, **options):
+ print "MIGRATING", LegacyBlogcomment.objects.all().count(), "BLOG COMMENTS"
+ BlogComment.objects.all().delete()
+ map = {}
+
+ _blogitems = {}
+ def get_blogitem(oid):
+ if oid not in _blogitems:
+ try:
+ _blogitems[oid] = BlogItem.objects.get(oid=oid)
+ except BlogItem.DoesNotExist:
+ print "OID", repr(oid)
+ raise
+ return _blogitems[oid]
+
+ def get_blogcomment(oid):
+ try:
+ return BlogComment.objects.get(oid=oid)
+ except BlogComment.DoesNotExist:
+ print "WARNING BlogComment with oid=%r doesn't exist" % oid
+
+ root_comments = {}
+ for b in LegacyBlogcomment.objects.all().order_by('add_date'):
+ n = BlogComment.objects.create(
+ oid=b.oid,
+ approved=b.approved,
+ name=b.name,
+ email=b.email,
+ add_date=b.add_date,
+ comment=b.comment,
+ )
+ if b.root:
+ n.blogitem = get_blogitem(b.parent_oid)
+ n.parent = None
+ n.save()
+ root_comments[n.pk] = n.blogitem
+ else:
+ n.parent = get_blogcomment(b.parent_oid)
+ n.save()
+ map[b.oid] = n
+
+# for b in LegacyBlogcomment.objects.filter(root=False):
+# comment = map[b.oid]
+# comment.parent = map[b.parent_oid]
+# comment.blogitem = find_parent_blogitem(comment)
+# comment.save()
@@ -0,0 +1,38 @@
+from pprint import pprint
+from django.conf import settings
+from django.core.management.base import BaseCommand
+from django.db import transaction
+
+from apps.legacy.models import LegacyBlogitem
+from apps.plog.models import BlogItem, Category
+
+class Command(BaseCommand):
+
+ #@transaction.commit_manually
+ def handle(self, **options):
+ print "MIGRATING", LegacyBlogitem.objects.all().count(), "BLOG ITEMS"
+ BlogItem.objects.all().delete()
+ map = {}
+ categories = {}
+
+ for b in LegacyBlogitem.objects.all():
+ n = BlogItem.objects.create(
+ oid=b.oid,
+ title=b.title,
+ alias=b.alias,
+ bookmark=bool(b.bookmark),
+ text=b.text,
+ summary=b.summary,
+ url=b.url,
+ pub_date=b.pub_date,
+ display_format=b.display_format,
+ plogrank=b.plogrank,
+ keywords=[x.strip() for x in b.keywords.split('|') if x.strip()],
+ codesyntax=b.codesyntax_display_format,
+ )
+ #map[b.oid] = n.pk
+ for cat in [x.strip() for x in b.itemcategories.split('|') if x.strip()]:
+ if cat not in categories:
+ categories[cat] = Category.objects.create(name=cat)
+ n.categories.add(categories[cat])
+ n.save()
@@ -0,0 +1,6 @@
+from django.conf import settings
+from django.core.management.base import BaseCommand
+
+class Command(BaseCommand):
+
+ def handle(self, **options):
@@ -0,0 +1,32 @@
+from django.db import models
+
+class LegacyBlogcomment(models.Model):
+ oid = models.CharField(max_length=300)
+ parent_oid = models.CharField(max_length=300)
+ root = models.IntegerField(null=True, blank=True)
+ approved = models.IntegerField(null=True, blank=True)
+ comment = models.TextField()
+ add_date = models.DateTimeField()
+ name = models.CharField(max_length=300)
+ email = models.CharField(max_length=600)
+ class Meta:
+ db_table = u'blogcomments'
+
+class LegacyBlogitem(models.Model):
+ oid = models.CharField(max_length=300)
+ title = models.CharField(max_length=600)
+ alias = models.CharField(max_length=600, blank=True)
+ bookmark = models.IntegerField()
+ text = models.TextField()
+ summary = models.TextField()
+ url = models.CharField(max_length=600, blank=True)
+ pub_date = models.DateTimeField()
+ display_format = models.CharField(max_length=60)
+ itemcategories = models.CharField(max_length=600)
+ keywords = models.CharField(max_length=1500)
+ relatedids = models.CharField(max_length=600)
+ plogrank = models.FloatField(null=True, blank=True)
+ codesyntax_display_format = models.CharField(max_length=300, blank=True)
+
+ class Meta:
+ db_table = u'blogitems'
@@ -0,0 +1,16 @@
+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.assertEqual(1 + 1, 2)
@@ -0,0 +1 @@
+# Create your views here.
No changes.
@@ -0,0 +1,19 @@
+import jinja2
+from jingo import register
+from django.template.loader import render_to_string
+from .models import BlogItem, BlogComment, Category
+
+
+@register.function
+def show_comments(parent):
+ if parent.__class__ == BlogItem:
+ filter_ = {'blogitem': parent}
+ else:
+ filter_ = {'parent': parent}
+ html = []
+ for comment in BlogComment.objects.filter(**filter_).order_by('add_date'):
+ html.append(render_to_string('plog/comment.html', {
+ 'comment': comment,
+ 'preview': False,
+ }))
+ return '\n'.join(html)
@@ -0,0 +1,73 @@
+import uuid
+import datetime
+from django.db import models
+from .utils import render_comment_text
+
+
+class ArrayField(models.CharField):
+
+ __metaclass__ = models.SubfieldBase
+ description = "basic field for storing string arrays"
+
+ def __init__(self, *args, **kwargs):
+ kwargs['max_length'] = kwargs.get('max_length', 200)
+ super(ArrayField, self).__init__(*args, **kwargs)
+
+ def to_python(self, value):
+ if isinstance(value, list):
+ return value
+
+ return value.split('|')
+
+ def get_prep_value(self, value):
+ return '|'.join(value)
+
+
+class Category(models.Model):
+ name = models.CharField(max_length=100)
+
+ def __repr__(self):
+ return '<%s: %r>' % (self.__class__.__name__, self.name)
+
+
+class BlogItem(models.Model):
+ oid = models.CharField(max_length=100, db_index=True)
+ title = models.CharField(max_length=200)
+ alias = models.CharField(max_length=200, null=True)
+ bookmark = models.BooleanField(default=False)
+ text = models.TextField()
+ text_rendered = models.TextField(blank=True)
+ summary = models.TextField()
+ url = models.URLField(null=True)
+ pub_date = models.DateTimeField()
+ display_format = models.CharField(max_length=20)
+ categories = models.ManyToManyField(Category)
+ keywords = ArrayField(max_length=500)
+ plogrank = models.FloatField(null=True)
+ codesyntax = models.CharField(max_length=20, blank=True)
+
+ def __repr__(self):
+ return '<%s: %r>' % (self.__class__.__name__, self.oid)
+
+
+class BlogComment(models.Model):
+ oid = models.CharField(max_length=100, db_index=True)
+ blogitem = models.ForeignKey(BlogItem, null=True)
+ parent = models.ForeignKey('BlogComment', null=True)
+ approved = models.BooleanField(default=False)
+ comment = models.TextField()
+ comment_rendered = models.TextField(blank=True, null=True)
+ add_date = models.DateTimeField(default=datetime.datetime.utcnow)
+ name = models.CharField(max_length=100, blank=True)
+ email = models.CharField(max_length=100, blank=True)
+
+ @property
+ def rendered(self):
+ if not self.comment_rendered:
+ self.comment_rendered = render_comment_text(self.comment)
+ self.save()
+ return self.comment_rendered
+
+ @classmethod
+ def next_oid(cls):
+ return 'c' + uuid.uuid4().hex[:6]
@@ -0,0 +1,17 @@
+<div id="{{ comment.oid }}" class="commentinline comment">
+<span class="sn">{% if comment.name %}{{ comment.name }}{% else %}<em>Anonymous</em>{% endif %}</span> -
+<small>{{ comment.add_date.strftime('%d %B %Y') }}</small>
+
+{% if not preview %}
+<span style="font-size:70%">[<a
+rel="nofollow" class="reply" data-oid="{{ comment.oid }}" href="#{{ comment.oid }}"
+>&#171;&#171; Reply to this</a>]</span>
+{% endif %}
+<br>
+
+<span class="commenttext">{{ comment.rendered|safe }}</span>
+
+{% if not preview %}
+{{ show_comments(comment)|safe }}
+{% endif %}
+</div>
@@ -0,0 +1,37 @@
+{% extends "base.html" %}
+
+{% block content_title %}{{ post.title }}{% endblock %}
+
+{% block extrajs %}
+<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
+<script src="{{ static("js/blogitem.js") }}"></script>
+{% endblock %}
+
+{% block content_body %}
+{{ post.text_rendered|safe }}
+
+<h3>Comments</h3>
+<div id="comments-outer">
+{{ show_comments(post)|safe }}
+</div>
+
+<div id="preview-comment-outer" class="preview" style="display:none"></div>
+<form action="{{ url('submit', post.oid) }}" method="post" id="comment">
+<div class="meta">
+ <input type="hidden" name="csrfmiddlewaretoken" value="">
+ <input type="text" name="parent" value="">
+ <input name="name" placeholder="Your full name" title="Your full name"><br>
+ <input name="email" placeholder="Your email" title="Your email"><br>
+ <span class="note">Your email will never ever be published</span>
+ <br>
+ <br>
+ <button type="submit" class="btn preview primary">Preview first</button>
+ <button type="submit" class="btn post">Post comment</button><br>
+ <br>
+ <p class="cancel" style="display:none"><a href="./" title="Abandon ship!">Cancel</a></p>
+
+
+</div>
+<textarea name="comment"></textarea>
+</form>
+{% endblock %}
Oops, something went wrong.

0 comments on commit dd35852

Please sign in to comment.