Skip to content

Commit

Permalink
Merge pull request #9 from liangliangyy/dev
Browse files Browse the repository at this point in the history
完善测试,为非本站链接自动添加nofollow
  • Loading branch information
liangliangyy committed Mar 30, 2017
2 parents 30615ed + 5e0d7c4 commit 3b9ba0e
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 15 deletions.
10 changes: 10 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[run]
source = .
include = *.py
omit =
*migrations*
*tests*
*.html
*whoosh_cn_backend*
*apps*
*commands*
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion DjangoBlog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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, ...)
Expand All @@ -24,6 +24,7 @@
# 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']
Expand Down
24 changes: 24 additions & 0 deletions DjangoBlog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -110,6 +112,28 @@ def block_code(self, text, lang):
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
Expand Down
40 changes: 39 additions & 1 deletion accounts/tests.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 3 additions & 2 deletions blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def save(self, *args, **kwargs):
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)
Expand Down
30 changes: 25 additions & 5 deletions blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.
Expand All @@ -13,12 +14,8 @@ def setUp(self):
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())
Expand All @@ -32,6 +29,9 @@ def test_validate_article(self):

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"
Expand All @@ -40,6 +40,26 @@ def test_validate_article(self):
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)
60 changes: 59 additions & 1 deletion comments/tests.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 7 additions & 1 deletion comments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ def form_valid(self, form):
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)
Expand Down
13 changes: 9 additions & 4 deletions test/travis_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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, ...)
Expand All @@ -25,8 +25,11 @@
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 = [
Expand All @@ -51,7 +54,9 @@
'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',
Expand Down Expand Up @@ -192,8 +197,8 @@
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': {
Expand Down

0 comments on commit 3b9ba0e

Please sign in to comment.