mirror of
https://github.com/scrapy/scrapy.git
synced 2025-02-22 06:33:12 +00:00
Changes:
* Simplify article app, it isn't necessary to save them in db, instead this tool should render static templates directly based in the url. Example: if the url is "/article/today" it will look for the template "articles/today.html" in articles templates directory. This app is configured to handle any url, so it will render an url like "/about" (if there isn't other url defined to handle "about" before article definition), and in this case will try to render the template "article.html" in articles templates dir * Removed models, not necessary now * Removed templatetags, not necessary now * Removed flatpages middleware ?? * Added url to articles app, this will used as a last case to handle undefined urls. --HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40136
This commit is contained in:
parent
28bb53fa22
commit
7b0877c50e
@ -1,79 +0,0 @@
|
||||
from datetime import datetime
|
||||
|
||||
from django.db import models
|
||||
from django.template.defaultfilters import slugify
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
REST_HELP_TEXT = _("""ReST markup language allowed
|
||||
<a href='http://en.wikipedia.org/wiki/ReStructuredText'>
|
||||
Read more</a>""")
|
||||
|
||||
MAIN_HELP_TEXT = _("Useful to filter articles, like those public on homepage")
|
||||
|
||||
|
||||
class Article(models.Model):
|
||||
title = models.CharField(_("title"), max_length=256, core=True,
|
||||
blank=False)
|
||||
slug = models.SlugField(_("slug"), prepopulate_from=("title",),
|
||||
editable=False)
|
||||
text = models.TextField(_("text"), core=True, help_text=REST_HELP_TEXT)
|
||||
main = models.BooleanField(_("main"), core=True, blank=False,
|
||||
default=False, help_text=MAIN_HELP_TEXT)
|
||||
position = models.IntegerField(_("position"), core=True, blank=False,
|
||||
default=0)
|
||||
publish = models.BooleanField(_("publish"), core=True, default=False)
|
||||
|
||||
# automatic dates
|
||||
created = models.DateTimeField(core=True, editable=False)
|
||||
updated = models.DateTimeField(core=True, editable=False)
|
||||
|
||||
def toggle_publish(self):
|
||||
self.publish = not self.publish
|
||||
self.save()
|
||||
|
||||
def position_up(self):
|
||||
self.position += 1
|
||||
self.save()
|
||||
|
||||
def position_down(self):
|
||||
self.position -= 1
|
||||
self.save()
|
||||
|
||||
def save(self):
|
||||
if not self.id:
|
||||
self.created = datetime.now()
|
||||
self.updated = datetime.now()
|
||||
self.slug = slugify(self.title)
|
||||
super(Article, self).save()
|
||||
|
||||
def __unicode__(self):
|
||||
return self.title
|
||||
|
||||
# ugly, but django-admin isn't very versatile right now
|
||||
def position_link(self):
|
||||
return _("%(position)s (<a href='/admin/article/article/%(id)s/position/up/'>Up</a>" \
|
||||
" | <a href='/admin/article/article/%(id)s/position/down/'>Down</a>)") % \
|
||||
{ "position": self.position, "id": self.id }
|
||||
position_link.short_description = u"position"
|
||||
position_link.allow_tags = True
|
||||
|
||||
def publish_link(self):
|
||||
img_url = "/media/img/admin/icon-%s.gif" % \
|
||||
(self.publish and "yes" or "no")
|
||||
html = _('<img alt="%s" src="' + img_url + '"/> ' \
|
||||
'(<a href="%s/publish/toggle/">Toggle</a>)')
|
||||
return html % (_(str(self.publish)), self.id)
|
||||
publish_link.short_description = u"publish"
|
||||
publish_link.allow_tags = True
|
||||
|
||||
class Admin:
|
||||
list_display = ("title", "main", "position_link", "publish_link",
|
||||
"updated")
|
||||
list_filter = ("main", "created", "publish")
|
||||
save_on_top = True
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("article")
|
||||
verbose_name_plural = _("articles")
|
||||
ordering = [ "-position", ]
|
@ -1,47 +0,0 @@
|
||||
from django import template
|
||||
|
||||
from scrapyorg.lib.templatetags import *
|
||||
|
||||
from scrapyorg.article.models import Article
|
||||
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.tag(name="load_main_articles")
|
||||
def do_load_main_articles(parser, token):
|
||||
return do_load(parser, token, True)
|
||||
|
||||
|
||||
@register.tag(name="load_last_articles")
|
||||
def do_load_last_articles(parser, token):
|
||||
return do_load(parser, token)
|
||||
|
||||
|
||||
def do_load(parser, token, only_main=False):
|
||||
syntax_msg = 'Syntax: %s "COUNT" as "VAR_NAME"'
|
||||
|
||||
try:
|
||||
tag, count, _as, var_name = token.split_contents()
|
||||
|
||||
if not is_string(count) or not is_string(var_name):
|
||||
raise_syntax(syntax_msg % tag)
|
||||
count = int(unquoute(count))
|
||||
except:
|
||||
raise_syntax(syntax_msg % token.split_contents()[0])
|
||||
return LoadArticlesNode(count, unquoute(var_name), only_main)
|
||||
|
||||
|
||||
class LoadArticlesNode(template.Node):
|
||||
def __init__(self, count, var_name, only_main=False):
|
||||
self.only_main = only_main
|
||||
self.count = count
|
||||
self.var_name = var_name
|
||||
|
||||
def render(self, context):
|
||||
articles = Article.objects.filter(publish=True)
|
||||
if self.only_main:
|
||||
articles = articles.filter(main=True)
|
||||
|
||||
context[self.var_name] = articles[:self.count]
|
||||
return ''
|
@ -4,7 +4,6 @@ from scrapyorg.article.views import *
|
||||
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r"^(?P<article_id>\d+)/position/up/$", position_up),
|
||||
(r"^(?P<article_id>\d+)/position/down/$", position_down),
|
||||
(r"^(?P<article_id>\d+)/publish/toggle/$", publish_toggle),
|
||||
(r"^$", render_template, { "path": "home" }),
|
||||
(r"(?P<path>.*)/", render_template),
|
||||
)
|
||||
|
@ -1,24 +1,21 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from os.path import join
|
||||
|
||||
from scrapyorg.article.models import Article
|
||||
from django.template import TemplateDoesNotExist
|
||||
from django.template.context import RequestContext
|
||||
from django.shortcuts import render_to_response
|
||||
from django.http import Http404
|
||||
|
||||
|
||||
@staff_member_required
|
||||
def position_up(request, article_id):
|
||||
article = get_object_or_404(Article, pk=article_id)
|
||||
article.position_up()
|
||||
return HttpResponseRedirect("/admin/article/article/")
|
||||
ARTICLES_TEMPLATES_DIR = "articles"
|
||||
|
||||
@staff_member_required
|
||||
def position_down(request, article_id):
|
||||
article = get_object_or_404(Article, pk=article_id)
|
||||
article.position_down()
|
||||
return HttpResponseRedirect("/admin/article/article/")
|
||||
|
||||
@staff_member_required
|
||||
def publish_toggle(request, article_id):
|
||||
article = get_object_or_404(Article, pk=article_id)
|
||||
article.toggle_publish()
|
||||
return HttpResponseRedirect("/admin/article/article/")
|
||||
def render_template(request, path):
|
||||
if not path.endswith(".html"):
|
||||
path = path + ".html"
|
||||
path = join(ARTICLES_TEMPLATES_DIR, path)
|
||||
|
||||
try:
|
||||
c = RequestContext(request)
|
||||
return render_to_response(path, context_instance=c)
|
||||
except TemplateDoesNotExist, e:
|
||||
raise Http404("Article does not exists")
|
||||
|
@ -62,7 +62,6 @@ MIDDLEWARE_CLASSES = (
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.middleware.doc.XViewMiddleware',
|
||||
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
|
||||
)
|
||||
|
||||
ROOT_URLCONF = 'scrapyorg.urls'
|
||||
|
@ -4,12 +4,9 @@ from django.conf import settings
|
||||
|
||||
|
||||
urlpatterns = patterns('',
|
||||
(r"^$", direct_to_template, { "template": "home.html" }),
|
||||
(r"^article/", include("scrapyorg.article.urls")),
|
||||
(r"^weblog/", include("scrapyorg.blog.urls")),
|
||||
|
||||
# admin
|
||||
(r"^admin/article/article/", include("scrapyorg.article.urls")),
|
||||
(r"^admin/download/downloadlink/", include("scrapyorg.download.urls")),
|
||||
(r"^admin/", include("django.contrib.admin.urls")),
|
||||
)
|
||||
@ -19,3 +16,8 @@ if settings.DEBUG: # devel
|
||||
urlpatterns += patterns('',
|
||||
(r'^%s/(?P<path>.*)$' % settings.MEDIA_URL[1:], 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
|
||||
)
|
||||
|
||||
# last resort, it's an article
|
||||
urlpatterns += patterns('',
|
||||
(r"", include("scrapyorg.article.urls")),
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user