Merge pull request #9 from liangliangyy/dev

完善测试,为非本站链接自动添加nofollow
master
车亮亮 9 years ago committed by GitHub
commit 3b9ba0e43d

@ -0,0 +1,10 @@
[run]
source = .
include = *.py
omit =
*migrations*
*tests*
*.html
*whoosh_cn_backend*
*apps*
*commands*

@ -23,6 +23,8 @@ install:
- pip install coverage
before_script:
- mysql -e 'CREATE DATABASE `djangoblog` /*!40100 DEFAULT CHARACTER SET utf8 */;'
- python manage.py makemigrations
- python manage.py makemigrations sites
- python manage.py makemigrations accounts
- python manage.py makemigrations blog
- python manage.py makemigrations comments

@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import sys
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@ -24,6 +24,7 @@ SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True
DEBUG = False
TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
# ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['www.lylinux.net', '127.0.0.1', 'example.com']

@ -13,8 +13,10 @@
@time: 2017/1/19 上午2:30
"""
from django.core.cache import cache
from django.contrib.sites.models import Site
from hashlib import md5
import mistune
from mistune import escape, escape_link
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import html
@ -110,6 +112,28 @@ class BlogMarkDownRenderer(mistune.Renderer):
linenos = self.options.get('linenos')
return block_code(text, lang, inlinestyles, linenos)
def autolink(self, link, is_email=False):
text = link = escape(link)
if is_email:
link = 'mailto:%s' % link
if not link:
link = "#"
site = Site.objects.get_current()
nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'"
return '<a href="%s" %s>%s</a>' % (link, nofollow, text)
def link(self, link, title, text):
link = escape_link(link, quote=True)
site = Site.objects.get_current()
nofollow = "" if link.find(site.domain) > 0 else "rel='nofollow'"
if not link:
link = "#"
if not title:
return '<a href="%s" %s>%s</a>' % (link, nofollow, text)
title = escape(title, quote=True)
return '<a href="%s" title="%s" %s>%s</a>' % (link, title, nofollow, text)
class CommonMarkdown():
@staticmethod

@ -1,3 +1,41 @@
from django.test import TestCase
from django.test import Client, RequestFactory, TestCase
from blog.models import Article, Category, Tag
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
import datetime
from accounts.models import BlogUser
# Create your tests here.
class AccountTest(TestCase):
def setUp(self):
self.client = Client()
self.factory = RequestFactory()
def test_validate_account(self):
site = Site.objects.get_current().domain
user = BlogUser.objects.create_superuser(email="liangliangyy1@gmail.com",
username="liangliangyy1", password="liangliangyy1")
self.client.login(username='liangliangyy1', password='liangliangyy1')
response = self.client.get('/admin/')
self.assertEqual(response.status_code, 200)
category = Category()
category.name = "categoryaaa"
category.created_time = datetime.datetime.now()
category.last_mod_time = datetime.datetime.now()
category.save()
article = Article()
article.title = "nicetitleaaa"
article.body = "nicecontentaaa"
article.author = user
article.category = category
article.type = 'a'
article.status = 'p'
article.save()
response = self.client.get(article.get_admin_url())
self.assertEqual(response.status_code, 200)

@ -21,8 +21,9 @@ class BaseModel(models.Model):
if 'update_fields' in kwargs and len(kwargs['update_fields']) == 1 and kwargs['update_fields'][0] == 'views':
return
try:
notify_url = self.get_full_url()
SpiderNotify.baidu_notify([notify_url])
if not settings.TESTING:
notify_url = self.get_full_url()
SpiderNotify.baidu_notify([notify_url])
except Exception as ex:
logger.error("notify sipder", ex)
print(ex)

@ -3,6 +3,7 @@ from blog.models import Article, Category, Tag
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
import datetime
from accounts.models import BlogUser
# Create your tests here.
@ -13,12 +14,8 @@ class ArticleTest(TestCase):
self.factory = RequestFactory()
def test_validate_article(self):
from accounts.models import BlogUser
site = Site.objects.get_current().domain
user = BlogUser()
user.email = "liangliangyy@gmail.com"
user.username = "liangliangyy"
user.password = "liangliangyy"
user = BlogUser.objects.get_or_create(email="liangliangyy@gmail.com", username="liangliangyy")[0]
user.set_password("liangliangyy")
user.save()
response = self.client.get(user.get_absolute_url())
@ -32,6 +29,9 @@ class ArticleTest(TestCase):
response = self.client.get(category.get_absolute_url())
self.assertEqual(response.status_code, 200)
tag = Tag()
tag.name = "nicetag"
tag.save()
article = Article()
article.title = "nicetitle"
@ -40,6 +40,26 @@ class ArticleTest(TestCase):
article.category = category
article.type = 'a'
article.status = 'p'
article.save()
self.assertEqual(0, article.tags.count())
article.tags.add(tag)
article.save()
self.assertEqual(1, article.tags.count())
response = self.client.get(article.get_absolute_url())
self.assertEqual(response.status_code, 200)
response = self.client.get(tag.get_absolute_url())
self.assertEqual(response.status_code, 200)
def test_validate_feed(self):
user = BlogUser.objects.get_or_create(email="liangliangyy12@gmail.com", username="liangliangyy")[0]
user.set_password("liangliangyy")
user.save()
response = self.client.get('/feed/')
self.assertEqual(response.status_code, 200)
response = self.client.get('/sitemap.xml')
self.assertEqual(response.status_code, 200)

@ -1,3 +1,61 @@
from django.test import TestCase
from django.test import Client, RequestFactory, TestCase
from blog.models import Article, Category, Tag
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
import datetime
from accounts.models import BlogUser
# Create your tests here.
class CommentsTest(TestCase):
def setUp(self):
self.client = Client()
self.factory = RequestFactory()
def test_validate_comment(self):
site = Site.objects.get_current().domain
user = BlogUser.objects.create_superuser(email="liangliangyy1@gmail.com",
username="liangliangyy1", password="liangliangyy1")
self.client.login(username='liangliangyy1', password='liangliangyy1')
category = Category()
category.name = "categoryccc"
category.created_time = datetime.datetime.now()
category.last_mod_time = datetime.datetime.now()
category.save()
article = Article()
article.title = "nicetitleccc"
article.body = "nicecontentccc"
article.author = user
article.category = category
article.type = 'a'
article.status = 'p'
article.save()
commenturl = reverse('comments:postcomment', kwargs={'article_id': article.id})
response = self.client.post(commenturl,
{
'body': '123ffffffffff'
})
self.assertEqual(response.status_code, 200)
article = Article.objects.get(pk=article.pk)
self.assertEqual(len(article.comment_list()), 0)
response = self.client.post(commenturl,
{
'body': '123ffffffffff',
'email': user.email,
'name': user.username
})
self.assertEqual(response.status_code, 302)
article = Article.objects.get(pk=article.pk)
self.assertEqual(len(article.comment_list()), 1)

@ -68,7 +68,13 @@ class CommentPostView(FormView):
site = Site.objects.get_current().domain
if site.find(':') > 0:
site = site[0:site.find(':')]
expire_view_cache(path, servername=site, serverport=self.request.get_port(), key_prefix='blogdetail')
port = 80
try:
# django1.8 没有这个方法...
port = self.request.get_port()
except:
pass
expire_view_cache(path, servername=site, serverport=port, key_prefix='blogdetail')
if cache.get('seo_processor'):
cache.delete('seo_processor')
comment_cache_key = 'article_comments_{id}'.format(id=article_id)

@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import sys
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@ -25,8 +25,11 @@ SECRET_KEY = '&3g0bdza#c%dm1lf%5gi&0-*53p3t0m*hmcvo29cn^$ji7je(c'
DEBUG = True
# DEBUG = False
TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
# ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['www.lylinux.net', '127.0.0.1']
ALLOWED_HOSTS = ['www.lylinux.net', '127.0.0.1', 'example.com']
# Application definition
INSTALLED_APPS = [
@ -51,7 +54,9 @@ MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.gzip.GZipMiddleware',
# 'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.cache.FetchFromCacheMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
@ -192,8 +197,8 @@ CACHE_MIDDLEWARE_SECONDS = 60 * 60 * 10
CACHE_MIDDLEWARE_KEY_PREFIX = "djangoblog"
CACHE_MIDDLEWARE_ALIAS = 'default'
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = 'default'
# SESSION_ENGINE = "django.contrib.sessions.backends.cache"
# SESSION_CACHE_ALIAS = 'default'
OAHUTH = {
'sina': {

Loading…
Cancel
Save