Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refs #22 multi-blog support in the models, managers & admin

  • Loading branch information...
commit ea54eb5e729f07be60d768eb094be1e24fbdf3b6 1 parent 601fc25
Thomas Schreiber authored
Showing with 89 additions and 18 deletions.
  1. +13 −2 biblion/admin.py
  2. +7 −1 biblion/managers.py
  3. +69 −15 biblion/models.py
View
15 biblion/admin.py
@@ -1,18 +1,28 @@
from django.contrib import admin
from django.utils.functional import curry
-from biblion.models import Post, Image
+from biblion.models import Blog, Post, Image
from biblion.forms import AdminPostForm
from biblion.utils import can_tweet
+class BlogAdmin(admin.ModelAdmin):
+ list_display = ["title", "active_flag", "site"]
+ prepopulated_fields = {"slug": ("title",)}
+
+ def active_flag(self, obj):
+ return bool(obj.active)
+ active_flag.short_description = "Active"
+ active_flag.boolean = True
+
+
class ImageInline(admin.TabularInline):
model = Image
fields = ["image_path"]
class PostAdmin(admin.ModelAdmin):
- list_display = ["title", "published_flag", "section"]
+ list_display = ["blog", "title", "published_flag", "section"]
list_filter = ["section"]
form = AdminPostForm
fields = [
@@ -56,5 +66,6 @@ def save_form(self, request, form, change):
return form.save()
+admin.site.register(Blog, BlogAdmin)
admin.site.register(Post, PostAdmin)
admin.site.register(Image)
View
8 biblion/managers.py
@@ -5,6 +5,12 @@
from biblion.settings import ALL_SECTION_NAME
+class BlogManager(models.Manager):
+
+ def active(self):
+ return self.exclude(active=None)
+
+
class PostManager(models.Manager):
def published(self):
@@ -26,4 +32,4 @@ def section(self, value, queryset=None):
except KeyError:
raise InvalidSection
all_sections = Q(section=self.model.section_idx(ALL_SECTION_NAME))
- return queryset.filter(all_sections | Q(section=section_idx))
+ return queryset.filter(all_sections | Q(section=section_idx))
View
84 biblion/models.py
@@ -11,6 +11,7 @@
from django.utils import simplejson as json
from django.utils.translation import ugettext as _
+from django.contrib.sites.managers import CurrentSiteManager
from django.contrib.sites.models import Site
try:
@@ -18,11 +19,54 @@
except ImportError:
twitter = None
-from biblion.managers import PostManager
+from biblion.managers import BlogManager, PostManager
from biblion.settings import ALL_SECTION_NAME, SECTIONS
from biblion.utils import can_tweet
+class Blog(models.Model):
+
+ site = models.ForeignKey(Site, default=settings.SITE_ID, verbose_name=_("site"))
+
+ title = models.CharField(_("title"), max_length=90)
+ slug = models.SlugField()
+ maintainers = models.ManyToManyField(User, related_name=_("blogs"), verbose_name=_("maintainers"))
+
+ subtitle = models.CharField(_("subtitle"), max_length=255, blank=True,
+ help_text="Looks best if only a few words, like a tagline.")
+ description = models.TextField(_("description"), max_length=4000, help_text=_("""
+ This is your chance to tell potential subscribers all about your blog.
+ Describe your subject matter, media format, post schedule, and other
+ relevant info so that they know what they'll be getting when they
+ subscribe. In addition, make a list of the most relevant search terms
+ that you want your blog to match, then build them into your
+ description. This field can be up to 4000 characters."""), blank=True)
+
+ feedburner = models.URLField(_("feedburner url"), blank=True,
+ help_text=_("""Fill this out after saving this show and at least one
+ episode. URL should look like "http://feeds.feedburner.com/TitleOfShow".
+ <a href="http://www.feedburner.com/fb/a/ping">Manually ping</a>"""))
+
+ default_author = models.ForeignKey(User, verbose_name=_("default author"), default=None, blank=True, null=True)
+ posts_per_page = models.PositiveIntegerField(_("posts per page"), default=6)
+
+ created = models.DateTimeField(_("created"), default=datetime.now, editable=False)
+ updated = models.DateTimeField(_("updated"), null=True, blank=True, editable=False)
+ active = models.BooleanField(_("active"), default=True)
+
+ objects = BlogManager()
+ on_site = CurrentSiteManager()
+
+ class Meta:
+ ordering = ("site", "active", "title")
+ unique_together = ("title", "site")
+ verbose_name = _("blog")
+ verbose_name_plural = _("blogs")
+
+ def __unicode__(self):
+ return u"%s" % (self.title)
+
+
def ig(L, i):
for x in L:
yield x[i]
@@ -30,13 +74,16 @@ def ig(L, i):
class Post(models.Model):
+ blog = models.ForeignKey(Blog, related_name=_("posts"))
+
SECTION_CHOICES = [(1, ALL_SECTION_NAME)] + zip(range(2, 2 + len(SECTIONS)), ig(SECTIONS, 1))
section = models.IntegerField(_("section"), choices=SECTION_CHOICES)
title = models.CharField(_("title"), max_length=90)
slug = models.SlugField()
- author = models.ForeignKey(User, related_name="posts", verbose_name=_('author'))
+
+ author = models.ForeignKey(User, related_name="posts", verbose_name=_("author"))
teaser_html = models.TextField(_("teaser html"), editable=False)
content_html = models.TextField(_("content html"), editable=False)
@@ -58,7 +105,7 @@ class Meta:
verbose_name_plural = _("posts")
def __unicode__(self):
- return self.title
+ return u"%s" % (self.title)
@staticmethod
def section_idx(slug):
@@ -83,11 +130,15 @@ def rev(self, rev_id):
return self.revisions.get(pk=rev_id)
def current(self):
- "the currently visible (latest published) revision"
+ """
+ the currently visible (latest published) revision
+ """
return self.revisions.exclude(published=None).order_by("-published")[0]
def latest(self):
- "the latest modified (even if not published) revision"
+ """
+ the latest modified (even if not published) revision
+ """
try:
return self.revisions.order_by("-updated")[0]
except IndexError:
@@ -95,7 +146,10 @@ def latest(self):
def as_tweet(self):
if not self.tweet_text:
- current_site = Site.objects.get_current()
+ if self.blog.site:
+ current_site = self.blog.site
+ else:
+ current_site = Site.objects.get_current()
api_url = "http://api.tr.im/api/trim_url.json"
u = urllib2.urlopen("%s?url=http://%s%s" % (
api_url,
@@ -149,26 +203,26 @@ def inc_views(self):
class Revision(models.Model):
- post = models.ForeignKey(Post, related_name="revisions", verbose_name=_('post'))
+ post = models.ForeignKey(Post, related_name="revisions", verbose_name=_("post"))
title = models.CharField(_("title"), max_length=90)
teaser = models.TextField(_("teaser"), )
content = models.TextField(_("content"), )
- author = models.ForeignKey(User, related_name="revisions", verbose_name=_('author'))
+ author = models.ForeignKey(User, related_name="revisions", verbose_name=_("author"))
updated = models.DateTimeField(_("updated"), default=datetime.now)
published = models.DateTimeField(_("published"), null=True, blank=True)
view_count = models.IntegerField(_("view count"), default=0, editable=False)
-
+
class Meta:
verbose_name = _("revision")
verbose_name_plural = _("revisions")
def __unicode__(self):
- return 'Revision %s for %s' % (self.updated.strftime('%Y%m%d-%H%M'), self.post.slug)
+ return u"Revision %s for %s" % (self.updated.strftime("%Y%m%d-%H%M"), self.post.slug)
def inc_views(self):
self.view_count += 1
@@ -177,7 +231,7 @@ def inc_views(self):
class Image(models.Model):
- post = models.ForeignKey(Post, related_name="images", verbose_name=_('post'))
+ post = models.ForeignKey(Post, related_name="images", verbose_name=_("post"))
image_path = models.ImageField(_("image path"), upload_to="images/%Y/%m/%d")
url = models.CharField(_("url"), max_length=150, blank=True)
@@ -187,19 +241,19 @@ class Image(models.Model):
class Meta:
verbose_name = _("image")
verbose_name_plural = _("images")
-
+
def __unicode__(self):
if self.pk is not None:
- return "{{ %d }}" % self.pk
+ return u"{{ %d }}" % self.pk
else:
- return "deleted image"
+ return u"deleted image"
class FeedHit(models.Model):
request_data = models.TextField(_("request data"))
created = models.DateTimeField(_("created"), default=datetime.now)
-
+
class Meta:
verbose_name = _("feed hit")
verbose_name_plural = _("feed hits")
Please sign in to comment.
Something went wrong with that request. Please try again.